SDL_atomic.c 9.6 KB

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