SDL_atomic.c 8.9 KB

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