eathread_sync_x86-64.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_64_EATHREAD_SYNC_X86_64_H
  11. #define EATHREAD_X86_64_EATHREAD_SYNC_X86_64_H
  12. #ifndef INCLUDED_eabase_H
  13. #include "EABase/eabase.h"
  14. #endif
  15. #if defined(EA_PROCESSOR_X86_64)
  16. #define EA_THREAD_SYNC_IMPLEMENTED
  17. #ifdef EA_COMPILER_MSVC
  18. EA_DISABLE_ALL_VC_WARNINGS()
  19. #include <math.h> // VS2008 has an acknowledged bug that requires math.h (and possibly also string.h) to be #included before intrin.h.
  20. #include <intrin.h>
  21. EA_RESTORE_ALL_VC_WARNINGS()
  22. #endif
  23. // By default, we define EA_TARGET_SMP to be true. The reason for this is that most
  24. // applications that users of this code are likely to write are going to be executables
  25. // which run properly on any system, be it multiprocessing or not.
  26. #ifndef EA_TARGET_SMP
  27. #define EA_TARGET_SMP 1
  28. #endif
  29. // EAProcessorPause
  30. // Intel has defined a 'pause' instruction for x86 processors starting with the P4, though this simply
  31. // maps to the otherwise undocumented 'rep nop' instruction. This pause instruction is important for
  32. // high performance spinning, as otherwise a high performance penalty incurs.
  33. #if defined(EA_COMPILER_MSVC) || defined(EA_COMPILER_INTEL) || defined(EA_COMPILER_BORLAND)
  34. // Year 2003+ versions of the Microsoft SDK define 'rep nop' as YieldProcessor and/or __yield or _mm_pause.
  35. #pragma intrinsic(_mm_pause)
  36. #define EAProcessorPause() _mm_pause() // The __yield() intrinsic currently doesn't work on x86-64.
  37. #elif defined(EA_COMPILER_GNUC) || defined(EA_COMPILER_CLANG)
  38. #define EAProcessorPause() __asm__ __volatile__ ("rep ; nop")
  39. #else
  40. // In this case we use an Intel-style asm statement. If this doesn't work for your compiler then
  41. // there most likely is some way to make the `rep nop` inline asm statement.
  42. #define EAProcessorPause() __asm { rep nop } // Alternatively: { __asm { _emit 0xf3 }; __asm { _emit 0x90 } }
  43. #endif
  44. // EAReadBarrier / EAWriteBarrier / EAReadWriteBarrier
  45. // The x86 processor memory architecture ensures read and write consistency on both single and
  46. // multi processing systems. This makes programming simpler but limits maximimum system performance.
  47. // We define EAReadBarrier here to be the same as EACompilerMemory barrier in order to limit the
  48. // compiler from making any assumptions at its level about memory usage. Year 2003+ versions of the
  49. // Microsoft SDK define a 'MemoryBarrier' statement which has the same effect as EAReadWriteBarrier.
  50. #if defined(EA_COMPILER_MSVC)
  51. #pragma intrinsic(_ReadBarrier)
  52. #pragma intrinsic(_WriteBarrier)
  53. #pragma intrinsic(_ReadWriteBarrier)
  54. #define EAReadBarrier() _ReadBarrier()
  55. #define EAWriteBarrier() _WriteBarrier()
  56. #define EAReadWriteBarrier() _ReadWriteBarrier()
  57. #elif defined(EA_PLATFORM_PS4)
  58. #define EAReadBarrier() __asm__ __volatile__ ("lfence" ::: "memory");
  59. #define EAWriteBarrier() __asm__ __volatile__ ("sfence" ::: "memory");
  60. #define EAReadWriteBarrier() __asm__ __volatile__ ("mfence" ::: "memory");
  61. #elif defined(__GNUC__) && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 401) // GCC 4.1 or later, includes clang
  62. #define EAReadBarrier __sync_synchronize
  63. #define EAWriteBarrier __sync_synchronize
  64. #define EAReadWriteBarrier __sync_synchronize
  65. #else
  66. #define EAReadBarrier EACompilerMemoryBarrier // Need to implement this for non-VC++
  67. #define EAWriteBarrier EACompilerMemoryBarrier // Need to implement this for non-VC++
  68. #define EAReadWriteBarrier EACompilerMemoryBarrier // Need to implement this for non-VC++
  69. #endif
  70. // EACompilerMemoryBarrier
  71. #if defined(EA_COMPILER_MSVC)
  72. #define EACompilerMemoryBarrier() _ReadWriteBarrier()
  73. #elif defined(EA_COMPILER_GNUC) || defined(EA_COMPILER_CLANG)
  74. #define EACompilerMemoryBarrier() __asm__ __volatile__ ("":::"memory")
  75. #else
  76. #define EACompilerMemoryBarrier() // Possibly `EAT_ASSERT(false)` here?
  77. #endif
  78. #endif // EA_PROCESSOR_X86
  79. #endif // EATHREAD_X86_64_EATHREAD_SYNC_X86_64_H