SDL_atomic.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #ifndef _SDL_atomic_h_
  2. #define _SDL_atomic_h_
  3. #include "SDL_stdinc.h"
  4. #include "SDL_platform.h"
  5. #include "begin_code.h"
  6. #if defined(_MSC_VER) && (_MSC_VER >= 1500)
  7. #include <intrin.h>
  8. #define HAVE_MSC_ATOMICS 1
  9. #endif
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. typedef int SDL_SpinLock;
  14. typedef SDL_bool SDLCALL tSDL_AtomicTryLock(SDL_SpinLock *lock);
  15. typedef void SDLCALL tSDL_AtomicLock(SDL_SpinLock *lock);
  16. typedef void SDLCALL tSDL_AtomicUnlock(SDL_SpinLock *lock);
  17. #if defined(_MSC_VER) && (_MSC_VER > 1200)
  18. void _ReadWriteBarrier(void);
  19. #pragma intrinsic(_ReadWriteBarrier)
  20. #define SDL_CompilerBarrier() _ReadWriteBarrier()
  21. #elif defined(__GNUC__)
  22. #define SDL_CompilerBarrier() __asm__ __volatile__ ("" : : : "memory")
  23. #else
  24. #define SDL_CompilerBarrier() \
  25. { SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); }
  26. #endif
  27. #if defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  28. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("lwsync" : : : "memory")
  29. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("lwsync" : : : "memory")
  30. #elif defined(__GNUC__) && defined(__arm__)
  31. #if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7EM__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__)
  32. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("dmb ish" : : : "memory")
  33. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("dmb ish" : : : "memory")
  34. #elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__)
  35. #ifdef __thumb__
  36. typedef void SDLCALL tSDL_MemoryBarrierRelease();
  37. typedef void SDLCALL tSDL_MemoryBarrierAcquire();
  38. #else
  39. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
  40. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
  41. #endif
  42. #else
  43. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("" : : : "memory")
  44. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("" : : : "memory")
  45. #endif
  46. #else
  47. #define SDL_MemoryBarrierRelease() SDL_CompilerBarrier()
  48. #define SDL_MemoryBarrierAcquire() SDL_CompilerBarrier()
  49. #endif
  50. #if defined(SDL_ATOMIC_DISABLED) && SDL_ATOMIC_DISABLED
  51. #define SDL_DISABLE_ATOMIC_INLINE
  52. #endif
  53. #ifndef SDL_DISABLE_ATOMIC_INLINE
  54. #ifdef HAVE_MSC_ATOMICS
  55. #define SDL_AtomicSet(a, v) _InterlockedExchange((long*)&(a)->value, (v))
  56. #define SDL_AtomicAdd(a, v) _InterlockedExchangeAdd((long*)&(a)->value, (v))
  57. #define SDL_AtomicCAS(a, oldval, newval) (_InterlockedCompareExchange((long*)&(a)->value, (newval), (oldval)) == (oldval))
  58. #define SDL_AtomicSetPtr(a, v) _InterlockedExchangePointer((a), (v))
  59. #if _M_IX86
  60. #define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchange((long*)(a), (long)(newval), (long)(oldval)) == (long)(oldval))
  61. #else
  62. #define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchangePointer((a), (newval), (oldval)) == (oldval))
  63. #endif
  64. #elif defined(__MACOSX__)
  65. #include <libkern/OSAtomic.h>
  66. #define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((oldval), (newval), &(a)->value)
  67. #ifdef __LP64__
  68. #define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a))
  69. #else
  70. #define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((int32_t)(oldval), (int32_t)(newval), (int32_t*)(a))
  71. #endif
  72. #elif defined(HAVE_GCC_ATOMICS)
  73. #define SDL_AtomicSet(a, v) __sync_lock_test_and_set(&(a)->value, v)
  74. #define SDL_AtomicAdd(a, v) __sync_fetch_and_add(&(a)->value, v)
  75. #define SDL_AtomicSetPtr(a, v) __sync_lock_test_and_set(a, v)
  76. #define SDL_AtomicCAS(a, oldval, newval) __sync_bool_compare_and_swap(&(a)->value, oldval, newval)
  77. #define SDL_AtomicCASPtr(a, oldval, newval) __sync_bool_compare_and_swap(a, oldval, newval)
  78. #endif
  79. #endif
  80. #ifndef SDL_atomic_t_defined
  81. typedef struct { int value; } SDL_atomic_t;
  82. #endif
  83. #ifndef SDL_AtomicCAS
  84. typedef SDL_bool SDLCALL tSDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
  85. #endif
  86. #ifndef SDL_AtomicSet
  87. SDL_FORCE_INLINE int SDL_AtomicSet(SDL_atomic_t *a, int v)
  88. {
  89. int value;
  90. do {
  91. value = a->value;
  92. } while (!SDL_AtomicCAS(a, value, v));
  93. return value;
  94. }
  95. #endif
  96. #ifndef SDL_AtomicGet
  97. SDL_FORCE_INLINE int SDL_AtomicGet(SDL_atomic_t *a)
  98. {
  99. int value = a->value;
  100. SDL_CompilerBarrier();
  101. return value;
  102. }
  103. #endif
  104. #ifndef SDL_AtomicAdd
  105. SDL_FORCE_INLINE int SDL_AtomicAdd(SDL_atomic_t *a, int v)
  106. {
  107. int value;
  108. do {
  109. value = a->value;
  110. } while (!SDL_AtomicCAS(a, value, (value + v)));
  111. return value;
  112. }
  113. #endif
  114. #ifndef SDL_AtomicIncRef
  115. #define SDL_AtomicIncRef(a) SDL_AtomicAdd(a, 1)
  116. #endif
  117. #ifndef SDL_AtomicDecRef
  118. #define SDL_AtomicDecRef(a) (SDL_AtomicAdd(a, -1) == 1)
  119. #endif
  120. #ifndef SDL_AtomicCASPtr
  121. typedef SDL_bool SDLCALL tSDL_AtomicCASPtr(void* *a, void *oldval, void *newval);
  122. #endif
  123. #ifndef SDL_AtomicSetPtr
  124. SDL_FORCE_INLINE void* SDL_AtomicSetPtr(void* *a, void* v)
  125. {
  126. void* value;
  127. do {
  128. value = *a;
  129. } while (!SDL_AtomicCASPtr(a, value, v));
  130. return value;
  131. }
  132. #endif
  133. #ifndef SDL_AtomicGetPtr
  134. SDL_FORCE_INLINE void* SDL_AtomicGetPtr(void* *a)
  135. {
  136. void* value = *a;
  137. SDL_CompilerBarrier();
  138. return value;
  139. }
  140. #endif
  141. extern tSDL_AtomicTryLock *SDL_AtomicTryLock;
  142. extern tSDL_AtomicLock *SDL_AtomicLock;
  143. extern tSDL_AtomicUnlock *SDL_AtomicUnlock;
  144. #ifdef __cplusplus
  145. }
  146. #endif
  147. #include "close_code.h"
  148. #endif