SDL_atomic.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
  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. /* The __atomic_load_n() intrinsic showed up in different times for different compilers. */
  31. #if defined(HAVE_GCC_ATOMICS)
  32. # if defined(__clang__)
  33. # if __has_builtin(__atomic_load_n)
  34. # define HAVE_ATOMIC_LOAD_N 1
  35. # endif
  36. # elif defined(__GNUC__)
  37. # if (__GNUC__ >= 5)
  38. # define HAVE_ATOMIC_LOAD_N 1
  39. # endif
  40. # endif
  41. #endif
  42. /*
  43. If any of the operations are not provided then we must emulate some
  44. of them. That means we need a nice implementation of spin locks
  45. that avoids the "one big lock" problem. We use a vector of spin
  46. locks and pick which one to use based on the address of the operand
  47. of the function.
  48. To generate the index of the lock we first shift by 3 bits to get
  49. rid on the zero bits that result from 32 and 64 bit allignment of
  50. data. We then mask off all but 5 bits and use those 5 bits as an
  51. index into the table.
  52. Picking the lock this way insures that accesses to the same data at
  53. the same time will go to the same lock. OTOH, accesses to different
  54. data have only a 1/32 chance of hitting the same lock. That should
  55. pretty much eliminate the chances of several atomic operations on
  56. different data from waiting on the same "big lock". If it isn't
  57. then the table of locks can be expanded to a new size so long as
  58. the new size is a power of two.
  59. Contributed by Bob Pendleton, bob@pendleton.com
  60. */
  61. #if !defined(HAVE_MSC_ATOMICS) && !defined(HAVE_GCC_ATOMICS) && !defined(__MACOSX__) && !defined(__SOLARIS__)
  62. #define EMULATE_CAS 1
  63. #endif
  64. #if EMULATE_CAS
  65. static SDL_SpinLock locks[32];
  66. static SDL_INLINE void
  67. enterLock(void *a)
  68. {
  69. uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
  70. SDL_AtomicLock(&locks[index]);
  71. }
  72. static SDL_INLINE void
  73. leaveLock(void *a)
  74. {
  75. uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
  76. SDL_AtomicUnlock(&locks[index]);
  77. }
  78. #endif
  79. SDL_bool
  80. SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
  81. {
  82. #ifdef HAVE_MSC_ATOMICS
  83. return (_InterlockedCompareExchange((long*)&a->value, (long)newval, (long)oldval) == (long)oldval);
  84. #elif defined(__MACOSX__) /* !!! FIXME: should we favor gcc atomics? */
  85. return (SDL_bool) OSAtomicCompareAndSwap32Barrier(oldval, newval, &a->value);
  86. #elif defined(HAVE_GCC_ATOMICS)
  87. return (SDL_bool) __sync_bool_compare_and_swap(&a->value, oldval, newval);
  88. #elif defined(__SOLARIS__) && defined(_LP64)
  89. return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)&a->value, (uint64_t)oldval, (uint64_t)newval) == oldval);
  90. #elif defined(__SOLARIS__) && !defined(_LP64)
  91. return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)&a->value, (uint32_t)oldval, (uint32_t)newval) == oldval);
  92. #elif EMULATE_CAS
  93. SDL_bool retval = SDL_FALSE;
  94. enterLock(a);
  95. if (a->value == oldval) {
  96. a->value = newval;
  97. retval = SDL_TRUE;
  98. }
  99. leaveLock(a);
  100. return retval;
  101. #else
  102. #error Please define your platform.
  103. #endif
  104. }
  105. SDL_bool
  106. SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
  107. {
  108. #if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
  109. return (_InterlockedCompareExchange((long*)a, (long)newval, (long)oldval) == (long)oldval);
  110. #elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
  111. return (_InterlockedCompareExchangePointer(a, newval, oldval) == oldval);
  112. #elif defined(__MACOSX__) && defined(__LP64__) /* !!! FIXME: should we favor gcc atomics? */
  113. return (SDL_bool) OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t*) a);
  114. #elif defined(__MACOSX__) && !defined(__LP64__) /* !!! FIXME: should we favor gcc atomics? */
  115. return (SDL_bool) OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t*) a);
  116. #elif defined(HAVE_GCC_ATOMICS)
  117. return __sync_bool_compare_and_swap(a, oldval, newval);
  118. #elif defined(__SOLARIS__)
  119. return (SDL_bool) (atomic_cas_ptr(a, oldval, newval) == oldval);
  120. #elif EMULATE_CAS
  121. SDL_bool retval = SDL_FALSE;
  122. enterLock(a);
  123. if (*a == oldval) {
  124. *a = newval;
  125. retval = SDL_TRUE;
  126. }
  127. leaveLock(a);
  128. return retval;
  129. #else
  130. #error Please define your platform.
  131. #endif
  132. }
  133. int
  134. SDL_AtomicSet(SDL_atomic_t *a, int v)
  135. {
  136. #ifdef HAVE_MSC_ATOMICS
  137. return _InterlockedExchange((long*)&a->value, v);
  138. #elif defined(HAVE_GCC_ATOMICS)
  139. return __sync_lock_test_and_set(&a->value, v);
  140. #elif defined(__SOLARIS__) && defined(_LP64)
  141. return (int) atomic_swap_64((volatile uint64_t*)&a->value, (uint64_t)v);
  142. #elif defined(__SOLARIS__) && !defined(_LP64)
  143. return (int) atomic_swap_32((volatile uint32_t*)&a->value, (uint32_t)v);
  144. #else
  145. int value;
  146. do {
  147. value = a->value;
  148. } while (!SDL_AtomicCAS(a, value, v));
  149. return value;
  150. #endif
  151. }
  152. void*
  153. SDL_AtomicSetPtr(void **a, void *v)
  154. {
  155. #if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
  156. return (void *) _InterlockedExchange((long *)a, (long) v);
  157. #elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
  158. return _InterlockedExchangePointer(a, v);
  159. #elif defined(HAVE_GCC_ATOMICS)
  160. return __sync_lock_test_and_set(a, v);
  161. #elif defined(__SOLARIS__)
  162. return atomic_swap_ptr(a, v);
  163. #else
  164. void *value;
  165. do {
  166. value = *a;
  167. } while (!SDL_AtomicCASPtr(a, value, v));
  168. return value;
  169. #endif
  170. }
  171. int
  172. SDL_AtomicAdd(SDL_atomic_t *a, int v)
  173. {
  174. #ifdef HAVE_MSC_ATOMICS
  175. return _InterlockedExchangeAdd((long*)&a->value, v);
  176. #elif defined(HAVE_GCC_ATOMICS)
  177. return __sync_fetch_and_add(&a->value, v);
  178. #elif defined(__SOLARIS__)
  179. int pv = a->value;
  180. membar_consumer();
  181. #if defined(_LP64)
  182. atomic_add_64((volatile uint64_t*)&a->value, v);
  183. #elif !defined(_LP64)
  184. atomic_add_32((volatile uint32_t*)&a->value, v);
  185. #endif
  186. return pv;
  187. #else
  188. int value;
  189. do {
  190. value = a->value;
  191. } while (!SDL_AtomicCAS(a, value, (value + v)));
  192. return value;
  193. #endif
  194. }
  195. int
  196. SDL_AtomicGet(SDL_atomic_t *a)
  197. {
  198. #ifdef HAVE_ATOMIC_LOAD_N
  199. return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST);
  200. #else
  201. int value;
  202. do {
  203. value = a->value;
  204. } while (!SDL_AtomicCAS(a, value, value));
  205. return value;
  206. #endif
  207. }
  208. void *
  209. SDL_AtomicGetPtr(void **a)
  210. {
  211. #ifdef HAVE_ATOMIC_LOAD_N
  212. return __atomic_load_n(a, __ATOMIC_SEQ_CST);
  213. #else
  214. void *value;
  215. do {
  216. value = *a;
  217. } while (!SDL_AtomicCASPtr(a, value, value));
  218. return value;
  219. #endif
  220. }
  221. void
  222. SDL_MemoryBarrierReleaseFunction(void)
  223. {
  224. SDL_MemoryBarrierRelease();
  225. }
  226. void
  227. SDL_MemoryBarrierAcquireFunction(void)
  228. {
  229. SDL_MemoryBarrierAcquire();
  230. }
  231. /* vi: set ts=4 sw=4 expandtab: */