SDL_atomic.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. #if defined(_MSC_VER) && (_MSC_VER >= 1900)
  20. #include <intrin.h>
  21. #define HAVE_MSC_ATOMICS 1
  22. #endif
  23. #ifdef SDL_PLATFORM_MACOS // !!! FIXME: should we favor gcc atomics?
  24. #include <libkern/OSAtomic.h>
  25. #endif
  26. #if !defined(HAVE_GCC_ATOMICS) && defined(SDL_PLATFORM_SOLARIS)
  27. #include <atomic.h>
  28. #endif
  29. // The __atomic_load_n() intrinsic showed up in different times for different compilers.
  30. #ifdef __clang__
  31. #if __has_builtin(__atomic_load_n) || defined(HAVE_GCC_ATOMICS)
  32. /* !!! FIXME: this advertises as available in the NDK but uses an external symbol we don't have.
  33. It might be in a later NDK or we might need an extra library? --ryan. */
  34. #ifndef SDL_PLATFORM_ANDROID
  35. #define HAVE_ATOMIC_LOAD_N 1
  36. #endif
  37. #endif
  38. #elif defined(__GNUC__)
  39. #if (__GNUC__ >= 5)
  40. #define HAVE_ATOMIC_LOAD_N 1
  41. #endif
  42. #endif
  43. /* *INDENT-OFF* */ // clang-format off
  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. /* *INDENT-ON* */ // clang-format on
  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 alignment 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, [email protected]
  86. */
  87. #if !defined(HAVE_MSC_ATOMICS) && !defined(HAVE_GCC_ATOMICS) && !defined(SDL_PLATFORM_MACOS) && !defined(SDL_PLATFORM_SOLARIS) && !defined(HAVE_WATCOM_ATOMICS)
  88. #define EMULATE_CAS 1
  89. #endif
  90. #ifdef EMULATE_CAS
  91. static SDL_SpinLock locks[32];
  92. static SDL_INLINE void enterLock(void *a)
  93. {
  94. uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
  95. SDL_LockSpinlock(&locks[index]);
  96. }
  97. static SDL_INLINE void leaveLock(void *a)
  98. {
  99. uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
  100. SDL_UnlockSpinlock(&locks[index]);
  101. }
  102. #endif
  103. bool SDL_CompareAndSwapAtomicInt(SDL_AtomicInt *a, int oldval, int newval)
  104. {
  105. #ifdef HAVE_MSC_ATOMICS
  106. SDL_COMPILE_TIME_ASSERT(atomic_cas, sizeof(long) == sizeof(a->value));
  107. return _InterlockedCompareExchange((long *)&a->value, (long)newval, (long)oldval) == (long)oldval;
  108. #elif defined(HAVE_WATCOM_ATOMICS)
  109. return _SDL_cmpxchg_watcom((volatile int *)&a->value, newval, oldval);
  110. #elif defined(HAVE_GCC_ATOMICS)
  111. return __sync_bool_compare_and_swap(&a->value, oldval, newval);
  112. #elif defined(SDL_PLATFORM_MACOS) // this is deprecated in 10.12 sdk; favor gcc atomics.
  113. return OSAtomicCompareAndSwap32Barrier(oldval, newval, &a->value);
  114. #elif defined(SDL_PLATFORM_SOLARIS)
  115. SDL_COMPILE_TIME_ASSERT(atomic_cas, sizeof(uint_t) == sizeof(a->value));
  116. return ((int)atomic_cas_uint((volatile uint_t *)&a->value, (uint_t)oldval, (uint_t)newval) == oldval);
  117. #elif defined(EMULATE_CAS)
  118. bool result = false;
  119. enterLock(a);
  120. if (a->value == oldval) {
  121. a->value = newval;
  122. result = true;
  123. }
  124. leaveLock(a);
  125. return result;
  126. #else
  127. #error Please define your platform.
  128. #endif
  129. }
  130. bool SDL_CompareAndSwapAtomicU32(SDL_AtomicU32 *a, Uint32 oldval, Uint32 newval)
  131. {
  132. #ifdef HAVE_MSC_ATOMICS
  133. SDL_COMPILE_TIME_ASSERT(atomic_cas, sizeof(long) == sizeof(a->value));
  134. return _InterlockedCompareExchange((long *)&a->value, (long)newval, (long)oldval) == (long)oldval;
  135. #elif defined(HAVE_WATCOM_ATOMICS)
  136. SDL_COMPILE_TIME_ASSERT(atomic_cas, sizeof(int) == sizeof(a->value));
  137. return _SDL_cmpxchg_watcom((volatile int *)&a->value, (int)newval, (int)oldval);
  138. #elif defined(HAVE_GCC_ATOMICS)
  139. return __sync_bool_compare_and_swap(&a->value, oldval, newval);
  140. #elif defined(SDL_PLATFORM_MACOS) // this is deprecated in 10.12 sdk; favor gcc atomics.
  141. return OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t*)&a->value);
  142. #elif defined(SDL_PLATFORM_SOLARIS)
  143. SDL_COMPILE_TIME_ASSERT(atomic_cas, sizeof(uint_t) == sizeof(a->value));
  144. return ((Uint32)atomic_cas_uint((volatile uint_t *)&a->value, (uint_t)oldval, (uint_t)newval) == oldval);
  145. #elif defined(EMULATE_CAS)
  146. bool result = false;
  147. enterLock(a);
  148. if (a->value == oldval) {
  149. a->value = newval;
  150. result = true;
  151. }
  152. leaveLock(a);
  153. return result;
  154. #else
  155. #error Please define your platform.
  156. #endif
  157. }
  158. bool SDL_CompareAndSwapAtomicPointer(void **a, void *oldval, void *newval)
  159. {
  160. #ifdef HAVE_MSC_ATOMICS
  161. return _InterlockedCompareExchangePointer(a, newval, oldval) == oldval;
  162. #elif defined(HAVE_WATCOM_ATOMICS)
  163. return _SDL_cmpxchg_watcom((int *)a, (long)newval, (long)oldval);
  164. #elif defined(HAVE_GCC_ATOMICS)
  165. return __sync_bool_compare_and_swap(a, oldval, newval);
  166. #elif defined(SDL_PLATFORM_MACOS) && defined(__LP64__) // this is deprecated in 10.12 sdk; favor gcc atomics.
  167. return OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t *)a);
  168. #elif defined(SDL_PLATFORM_MACOS) && !defined(__LP64__) // this is deprecated in 10.12 sdk; favor gcc atomics.
  169. return OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t *)a);
  170. #elif defined(SDL_PLATFORM_SOLARIS)
  171. return (atomic_cas_ptr(a, oldval, newval) == oldval);
  172. #elif defined(EMULATE_CAS)
  173. bool result = false;
  174. enterLock(a);
  175. if (*a == oldval) {
  176. *a = newval;
  177. result = true;
  178. }
  179. leaveLock(a);
  180. return result;
  181. #else
  182. #error Please define your platform.
  183. #endif
  184. }
  185. int SDL_SetAtomicInt(SDL_AtomicInt *a, int v)
  186. {
  187. #ifdef HAVE_MSC_ATOMICS
  188. SDL_COMPILE_TIME_ASSERT(atomic_set, sizeof(long) == sizeof(a->value));
  189. return _InterlockedExchange((long *)&a->value, v);
  190. #elif defined(HAVE_WATCOM_ATOMICS)
  191. return _SDL_xchg_watcom(&a->value, v);
  192. #elif defined(HAVE_GCC_ATOMICS)
  193. return __sync_lock_test_and_set(&a->value, v);
  194. #elif defined(SDL_PLATFORM_SOLARIS)
  195. SDL_COMPILE_TIME_ASSERT(atomic_set, sizeof(uint_t) == sizeof(a->value));
  196. return (int)atomic_swap_uint((volatile uint_t *)&a->value, v);
  197. #else
  198. int value;
  199. do {
  200. value = a->value;
  201. } while (!SDL_CompareAndSwapAtomicInt(a, value, v));
  202. return value;
  203. #endif
  204. }
  205. Uint32 SDL_SetAtomicU32(SDL_AtomicU32 *a, Uint32 v)
  206. {
  207. #ifdef HAVE_MSC_ATOMICS
  208. SDL_COMPILE_TIME_ASSERT(atomic_set, sizeof(long) == sizeof(a->value));
  209. return _InterlockedExchange((long *)&a->value, v);
  210. #elif defined(HAVE_WATCOM_ATOMICS)
  211. return _SDL_xchg_watcom(&a->value, v);
  212. #elif defined(HAVE_GCC_ATOMICS)
  213. return __sync_lock_test_and_set(&a->value, v);
  214. #elif defined(SDL_PLATFORM_SOLARIS)
  215. SDL_COMPILE_TIME_ASSERT(atomic_set, sizeof(uint_t) == sizeof(a->value));
  216. return (Uint32)atomic_swap_uint((volatile uint_t *)&a->value, v);
  217. #else
  218. Uint32 value;
  219. do {
  220. value = a->value;
  221. } while (!SDL_CompareAndSwapAtomicU32(a, value, v));
  222. return value;
  223. #endif
  224. }
  225. void *SDL_SetAtomicPointer(void **a, void *v)
  226. {
  227. #ifdef HAVE_MSC_ATOMICS
  228. return _InterlockedExchangePointer(a, v);
  229. #elif defined(HAVE_WATCOM_ATOMICS)
  230. return (void *)_SDL_xchg_watcom((int *)a, (long)v);
  231. #elif defined(HAVE_GCC_ATOMICS)
  232. return __sync_lock_test_and_set(a, v);
  233. #elif defined(SDL_PLATFORM_SOLARIS)
  234. return atomic_swap_ptr(a, v);
  235. #else
  236. void *value;
  237. do {
  238. value = *a;
  239. } while (!SDL_CompareAndSwapAtomicPointer(a, value, v));
  240. return value;
  241. #endif
  242. }
  243. int SDL_AddAtomicInt(SDL_AtomicInt *a, int v)
  244. {
  245. #ifdef HAVE_MSC_ATOMICS
  246. SDL_COMPILE_TIME_ASSERT(atomic_add, sizeof(long) == sizeof(a->value));
  247. return _InterlockedExchangeAdd((long *)&a->value, v);
  248. #elif defined(HAVE_WATCOM_ATOMICS)
  249. SDL_COMPILE_TIME_ASSERT(atomic_add, sizeof(int) == sizeof(a->value));
  250. return _SDL_xadd_watcom((volatile int *)&a->value, v);
  251. #elif defined(HAVE_GCC_ATOMICS)
  252. return __sync_fetch_and_add(&a->value, v);
  253. #elif defined(SDL_PLATFORM_SOLARIS)
  254. int pv = a->value;
  255. membar_consumer();
  256. atomic_add_int((volatile uint_t *)&a->value, v);
  257. return pv;
  258. #else
  259. int value;
  260. do {
  261. value = a->value;
  262. } while (!SDL_CompareAndSwapAtomicInt(a, value, (value + v)));
  263. return value;
  264. #endif
  265. }
  266. int SDL_GetAtomicInt(SDL_AtomicInt *a)
  267. {
  268. #ifdef HAVE_ATOMIC_LOAD_N
  269. return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST);
  270. #elif defined(HAVE_MSC_ATOMICS)
  271. SDL_COMPILE_TIME_ASSERT(atomic_get, sizeof(long) == sizeof(a->value));
  272. return _InterlockedOr((long *)&a->value, 0);
  273. #elif defined(HAVE_WATCOM_ATOMICS)
  274. return _SDL_xadd_watcom(&a->value, 0);
  275. #elif defined(HAVE_GCC_ATOMICS)
  276. return __sync_or_and_fetch(&a->value, 0);
  277. #elif defined(SDL_PLATFORM_MACOS) // this is deprecated in 10.12 sdk; favor gcc atomics.
  278. return sizeof(a->value) == sizeof(uint32_t) ? OSAtomicOr32Barrier(0, (volatile uint32_t *)&a->value) : OSAtomicAdd64Barrier(0, (volatile int64_t *)&a->value);
  279. #elif defined(SDL_PLATFORM_SOLARIS)
  280. return atomic_or_uint_nv((volatile uint_t *)&a->value, 0);
  281. #else
  282. int value;
  283. do {
  284. value = a->value;
  285. } while (!SDL_CompareAndSwapAtomicInt(a, value, value));
  286. return value;
  287. #endif
  288. }
  289. Uint32 SDL_GetAtomicU32(SDL_AtomicU32 *a)
  290. {
  291. #ifdef HAVE_ATOMIC_LOAD_N
  292. return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST);
  293. #elif defined(HAVE_MSC_ATOMICS)
  294. SDL_COMPILE_TIME_ASSERT(atomic_get, sizeof(long) == sizeof(a->value));
  295. return (Uint32)_InterlockedOr((long *)&a->value, 0);
  296. #elif defined(HAVE_WATCOM_ATOMICS)
  297. SDL_COMPILE_TIME_ASSERT(atomic_get, sizeof(int) == sizeof(a->value));
  298. return (Uint32)_SDL_xadd_watcom((volatile int *)&a->value, 0);
  299. #elif defined(HAVE_GCC_ATOMICS)
  300. return __sync_or_and_fetch(&a->value, 0);
  301. #elif defined(SDL_PLATFORM_MACOS) // this is deprecated in 10.12 sdk; favor gcc atomics.
  302. return OSAtomicOr32Barrier(0, (volatile uint32_t *)&a->value);
  303. #elif defined(SDL_PLATFORM_SOLARIS)
  304. SDL_COMPILE_TIME_ASSERT(atomic_get, sizeof(uint_t) == sizeof(a->value));
  305. return (Uint32)atomic_or_uint_nv((volatile uint_t *)&a->value, 0);
  306. #else
  307. Uint32 value;
  308. do {
  309. value = a->value;
  310. } while (!SDL_CompareAndSwapAtomicU32(a, value, value));
  311. return value;
  312. #endif
  313. }
  314. void *SDL_GetAtomicPointer(void **a)
  315. {
  316. #ifdef HAVE_ATOMIC_LOAD_N
  317. return __atomic_load_n(a, __ATOMIC_SEQ_CST);
  318. #elif defined(HAVE_MSC_ATOMICS)
  319. return _InterlockedCompareExchangePointer(a, NULL, NULL);
  320. #elif defined(HAVE_GCC_ATOMICS)
  321. return __sync_val_compare_and_swap(a, (void *)0, (void *)0);
  322. #elif defined(SDL_PLATFORM_SOLARIS)
  323. return atomic_cas_ptr(a, (void *)0, (void *)0);
  324. #else
  325. void *value;
  326. do {
  327. value = *a;
  328. } while (!SDL_CompareAndSwapAtomicPointer(a, value, value));
  329. return value;
  330. #endif
  331. }
  332. #ifdef SDL_MEMORY_BARRIER_USES_FUNCTION
  333. #error This file should be built in arm mode so the mcr instruction is available for memory barriers
  334. #endif
  335. void SDL_MemoryBarrierReleaseFunction(void)
  336. {
  337. SDL_MemoryBarrierRelease();
  338. }
  339. void SDL_MemoryBarrierAcquireFunction(void)
  340. {
  341. SDL_MemoryBarrierAcquire();
  342. }