eathread_sync_x86.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #if defined(EA_PRAGMA_ONCE_SUPPORTED)
  5. #pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
  6. #endif
  7. /////////////////////////////////////////////////////////////////////////////
  8. // Functionality related to memory and code generation synchronization.
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef EATHREAD_X86_EATHREAD_SYNC_X86_H
  11. #define EATHREAD_X86_EATHREAD_SYNC_X86_H
  12. #ifndef INCLUDED_eabase_H
  13. #include <EABase/eabase.h>
  14. #endif
  15. #if defined(EA_PROCESSOR_X86)
  16. #define EA_THREAD_SYNC_IMPLEMENTED
  17. // By default, we define EA_TARGET_SMP to be true. The reason for this is that most
  18. // applications that users of this code are likely to write are going to be executables
  19. // which run properly on any system, be it multiprocessing or not.
  20. #ifndef EA_TARGET_SMP
  21. #define EA_TARGET_SMP 1
  22. #endif
  23. // EAProcessorPause
  24. // Intel has defined a 'pause' instruction for x86 processors starting with the P4, though this simply
  25. // maps to the otherwise undocumented 'rep nop' instruction. This pause instruction is important for
  26. // high performance spinning, as otherwise a high performance penalty incurs.
  27. #if defined(EA_COMPILER_MSVC) || defined(EA_COMPILER_INTEL) || defined(EA_COMPILER_BORLAND)
  28. // Year 2003+ versions of the Microsoft SDK define 'rep nop' as YieldProcessor and/or __yield or _mm_pause.
  29. #define EAProcessorPause() __asm { rep nop }
  30. #elif defined(EA_COMPILER_GNUC) || defined(EA_COMPILER_CLANG)
  31. #define EAProcessorPause() __asm__ __volatile__ ("rep ; nop")
  32. #else
  33. // In this case we use an Intel-style asm statement. If this doesn't work for your compiler then
  34. // there most likely is some way to make the `rep nop` inline asm statement.
  35. #define EAProcessorPause() __asm { rep nop } // Alternatively: { __asm { _emit 0xf3 }; __asm { _emit 0x90 } }
  36. #endif
  37. // EAReadBarrier / EAWriteBarrier / EAReadWriteBarrier
  38. // The x86 processor memory architecture ensures read and write consistency on both single and
  39. // multi processing systems. This makes programming simpler but limits maximimum system performance.
  40. // We define EAReadBarrier here to be the same as EACompilerMemory barrier in order to limit the
  41. // compiler from making any assumptions at its level about memory usage. Year 2003+ versions of the
  42. // Microsoft SDK define a 'MemoryBarrier' statement which has the same effect as EAReadWriteBarrier.
  43. #if defined(__GNUC__) && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 401) // GCC 4.1 or later, includes clang
  44. #define EAReadBarrier __sync_synchronize
  45. #define EAWriteBarrier __sync_synchronize
  46. #define EAReadWriteBarrier __sync_synchronize
  47. #else
  48. #define EAReadBarrier EACompilerMemoryBarrier
  49. #define EAWriteBarrier EACompilerMemoryBarrier
  50. #define EAReadWriteBarrier EACompilerMemoryBarrier
  51. #endif
  52. // EACompilerMemoryBarrier
  53. #if (defined(EA_COMPILER_MSVC) && (EA_COMPILER_VERSION >= 1300) && defined(EA_PLATFORM_MICROSOFT)) || (defined(EA_COMPILER_INTEL) && (EA_COMPILER_VERSION >= 9999999)) // VC7+ or Intel (unknown version at this time)
  54. extern "C" void _ReadWriteBarrier();
  55. #pragma intrinsic(_ReadWriteBarrier)
  56. #define EACompilerMemoryBarrier() _ReadWriteBarrier()
  57. #elif defined(EA_COMPILER_GNUC)
  58. #define EACompilerMemoryBarrier() __asm__ __volatile__ ("":::"memory")
  59. #else
  60. #define EACompilerMemoryBarrier() // Possibly `EAT_ASSERT(false)` here?
  61. #endif
  62. #endif // EA_PROCESSOR_X86
  63. #endif // EATHREAD_X86_EATHREAD_SYNC_X86_H