SDL_atomic.c 9.5 KB

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