SDL_atomic.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. #include "SDL_atomic.h"
  20. #if defined(_MSC_VER) && (_MSC_VER >= 1500)
  21. #include <intrin.h>
  22. #define HAVE_MSC_ATOMICS 1
  23. #endif
  24. #if defined(__MACOSX__) /* !!! FIXME: should we favor gcc atomics? */
  25. #include <libkern/OSAtomic.h>
  26. #endif
  27. #if !defined(HAVE_GCC_ATOMICS) && defined(__SOLARIS__)
  28. #include <atomic.h>
  29. #endif
  30. /*
  31. If any of the operations are not provided then we must emulate some
  32. of them. That means we need a nice implementation of spin locks
  33. that avoids the "one big lock" problem. We use a vector of spin
  34. locks and pick which one to use based on the address of the operand
  35. of the function.
  36. To generate the index of the lock we first shift by 3 bits to get
  37. rid on the zero bits that result from 32 and 64 bit allignment of
  38. data. We then mask off all but 5 bits and use those 5 bits as an
  39. index into the table.
  40. Picking the lock this way insures that accesses to the same data at
  41. the same time will go to the same lock. OTOH, accesses to different
  42. data have only a 1/32 chance of hitting the same lock. That should
  43. pretty much eliminate the chances of several atomic operations on
  44. different data from waiting on the same "big lock". If it isn't
  45. then the table of locks can be expanded to a new size so long as
  46. the new size is a power of two.
  47. Contributed by Bob Pendleton, [email protected]
  48. */
  49. #if !defined(HAVE_MSC_ATOMICS) && !defined(HAVE_GCC_ATOMICS) && !defined(__MACOSX__) && !defined(__SOLARIS__)
  50. #define EMULATE_CAS 1
  51. #endif
  52. #if EMULATE_CAS
  53. static SDL_SpinLock locks[32];
  54. static SDL_INLINE void
  55. enterLock(void *a)
  56. {
  57. uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
  58. SDL_AtomicLock(&locks[index]);
  59. }
  60. static SDL_INLINE void
  61. leaveLock(void *a)
  62. {
  63. uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
  64. SDL_AtomicUnlock(&locks[index]);
  65. }
  66. #endif
  67. SDL_bool
  68. SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
  69. {
  70. #ifdef HAVE_MSC_ATOMICS
  71. return (_InterlockedCompareExchange((long*)&a->value, (long)newval, (long)oldval) == (long)oldval);
  72. #elif defined(__MACOSX__) /* !!! FIXME: should we favor gcc atomics? */
  73. return (SDL_bool) OSAtomicCompareAndSwap32Barrier(oldval, newval, &a->value);
  74. #elif defined(HAVE_GCC_ATOMICS)
  75. return (SDL_bool) __sync_bool_compare_and_swap(&a->value, oldval, newval);
  76. #elif defined(__SOLARIS__) && defined(_LP64)
  77. return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)&a->value, (uint64_t)oldval, (uint64_t)newval) == oldval);
  78. #elif defined(__SOLARIS__) && !defined(_LP64)
  79. return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)&a->value, (uint32_t)oldval, (uint32_t)newval) == oldval);
  80. #elif EMULATE_CAS
  81. SDL_bool retval = SDL_FALSE;
  82. enterLock(a);
  83. if (a->value == oldval) {
  84. a->value = newval;
  85. retval = SDL_TRUE;
  86. }
  87. leaveLock(a);
  88. return retval;
  89. #else
  90. #error Please define your platform.
  91. #endif
  92. }
  93. SDL_bool
  94. SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
  95. {
  96. #if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
  97. return (_InterlockedCompareExchange((long*)a, (long)newval, (long)oldval) == (long)oldval);
  98. #elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
  99. return (_InterlockedCompareExchangePointer(a, newval, oldval) == oldval);
  100. #elif defined(__MACOSX__) && defined(__LP64__) /* !!! FIXME: should we favor gcc atomics? */
  101. return (SDL_bool) OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t*) a);
  102. #elif defined(__MACOSX__) && !defined(__LP64__) /* !!! FIXME: should we favor gcc atomics? */
  103. return (SDL_bool) OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t*) a);
  104. #elif defined(HAVE_GCC_ATOMICS)
  105. return __sync_bool_compare_and_swap(a, oldval, newval);
  106. #elif defined(__SOLARIS__)
  107. return (SDL_bool) (atomic_cas_ptr(a, oldval, newval) == oldval);
  108. #elif EMULATE_CAS
  109. SDL_bool retval = SDL_FALSE;
  110. enterLock(a);
  111. if (*a == oldval) {
  112. *a = newval;
  113. retval = SDL_TRUE;
  114. }
  115. leaveLock(a);
  116. return retval;
  117. #else
  118. #error Please define your platform.
  119. #endif
  120. }
  121. int
  122. SDL_AtomicSet(SDL_atomic_t *a, int v)
  123. {
  124. #ifdef HAVE_MSC_ATOMICS
  125. return _InterlockedExchange((long*)&a->value, v);
  126. #elif defined(HAVE_GCC_ATOMICS)
  127. return __sync_lock_test_and_set(&a->value, v);
  128. #elif defined(__SOLARIS__) && defined(_LP64)
  129. return (int) atomic_swap_64((volatile uint64_t*)&a->value, (uint64_t)v);
  130. #elif defined(__SOLARIS__) && !defined(_LP64)
  131. return (int) atomic_swap_32((volatile uint32_t*)&a->value, (uint32_t)v);
  132. #else
  133. int value;
  134. do {
  135. value = a->value;
  136. } while (!SDL_AtomicCAS(a, value, v));
  137. return value;
  138. #endif
  139. }
  140. void*
  141. SDL_AtomicSetPtr(void **a, void *v)
  142. {
  143. #if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
  144. return (void *) _InterlockedExchange((long *)a, (long) v);
  145. #elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
  146. return _InterlockedExchangePointer(a, v);
  147. #elif defined(HAVE_GCC_ATOMICS)
  148. return __sync_lock_test_and_set(a, v);
  149. #elif defined(__SOLARIS__)
  150. return atomic_swap_ptr(a, v);
  151. #else
  152. void *value;
  153. do {
  154. value = *a;
  155. } while (!SDL_AtomicCASPtr(a, value, v));
  156. return value;
  157. #endif
  158. }
  159. int
  160. SDL_AtomicAdd(SDL_atomic_t *a, int v)
  161. {
  162. #ifdef HAVE_MSC_ATOMICS
  163. return _InterlockedExchangeAdd((long*)&a->value, v);
  164. #elif defined(HAVE_GCC_ATOMICS)
  165. return __sync_fetch_and_add(&a->value, v);
  166. #elif defined(__SOLARIS__)
  167. int pv = a->value;
  168. membar_consumer();
  169. #if defined(_LP64)
  170. atomic_add_64((volatile uint64_t*)&a->value, v);
  171. #elif !defined(_LP64)
  172. atomic_add_32((volatile uint32_t*)&a->value, v);
  173. #endif
  174. return pv;
  175. #else
  176. int value;
  177. do {
  178. value = a->value;
  179. } while (!SDL_AtomicCAS(a, value, (value + v)));
  180. return value;
  181. #endif
  182. }
  183. int
  184. SDL_AtomicGet(SDL_atomic_t *a)
  185. {
  186. int value;
  187. do {
  188. value = a->value;
  189. } while (!SDL_AtomicCAS(a, value, value));
  190. return value;
  191. }
  192. void *
  193. SDL_AtomicGetPtr(void **a)
  194. {
  195. void *value;
  196. do {
  197. value = *a;
  198. } while (!SDL_AtomicCASPtr(a, value, value));
  199. return value;
  200. }
  201. #ifdef __thumb__
  202. #if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__)
  203. __asm__(
  204. " .align 2\n"
  205. " .globl _SDL_MemoryBarrierRelease\n"
  206. " .globl _SDL_MemoryBarrierAcquire\n"
  207. "_SDL_MemoryBarrierRelease:\n"
  208. "_SDL_MemoryBarrierAcquire:\n"
  209. " mov r0, #0\n"
  210. " mcr p15, 0, r0, c7, c10, 5\n"
  211. " bx lr\n"
  212. );
  213. #endif
  214. #endif
  215. /* vi: set ts=4 sw=4 expandtab: */