SDL_atomic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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. #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. /*
  44. If any of the operations are not provided then we must emulate some
  45. of them. That means we need a nice implementation of spin locks
  46. that avoids the "one big lock" problem. We use a vector of spin
  47. locks and pick which one to use based on the address of the operand
  48. of the function.
  49. To generate the index of the lock we first shift by 3 bits to get
  50. rid on the zero bits that result from 32 and 64 bit alignment of
  51. data. We then mask off all but 5 bits and use those 5 bits as an
  52. index into the table.
  53. Picking the lock this way insures that accesses to the same data at
  54. the same time will go to the same lock. OTOH, accesses to different
  55. data have only a 1/32 chance of hitting the same lock. That should
  56. pretty much eliminate the chances of several atomic operations on
  57. different data from waiting on the same "big lock". If it isn't
  58. then the table of locks can be expanded to a new size so long as
  59. the new size is a power of two.
  60. Contributed by Bob Pendleton, bob@pendleton.com
  61. */
  62. #if !defined(HAVE_MSC_ATOMICS) && !defined(HAVE_GCC_ATOMICS) && !defined(SDL_PLATFORM_MACOS) && !defined(SDL_PLATFORM_SOLARIS)
  63. #define EMULATE_CAS 1
  64. #endif
  65. #ifdef EMULATE_CAS
  66. static SDL_SpinLock locks[32];
  67. static SDL_INLINE void enterLock(void *a)
  68. {
  69. uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
  70. SDL_LockSpinlock(&locks[index]);
  71. }
  72. static SDL_INLINE void leaveLock(void *a)
  73. {
  74. uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
  75. SDL_UnlockSpinlock(&locks[index]);
  76. }
  77. #endif
  78. bool SDL_CompareAndSwapAtomicInt(SDL_AtomicInt *a, int oldval, int newval)
  79. {
  80. #ifdef HAVE_MSC_ATOMICS
  81. SDL_COMPILE_TIME_ASSERT(atomic_cas, sizeof(long) == sizeof(a->value));
  82. return _InterlockedCompareExchange((long *)&a->value, (long)newval, (long)oldval) == (long)oldval;
  83. #elif defined(HAVE_GCC_ATOMICS)
  84. return __sync_bool_compare_and_swap(&a->value, oldval, newval);
  85. #elif defined(SDL_PLATFORM_MACOS) // this is deprecated in 10.12 sdk; favor gcc atomics.
  86. return OSAtomicCompareAndSwap32Barrier(oldval, newval, &a->value);
  87. #elif defined(SDL_PLATFORM_SOLARIS)
  88. SDL_COMPILE_TIME_ASSERT(atomic_cas, sizeof(uint_t) == sizeof(a->value));
  89. return ((int)atomic_cas_uint((volatile uint_t *)&a->value, (uint_t)oldval, (uint_t)newval) == oldval);
  90. #elif defined(EMULATE_CAS)
  91. bool result = false;
  92. enterLock(a);
  93. if (a->value == oldval) {
  94. a->value = newval;
  95. result = true;
  96. }
  97. leaveLock(a);
  98. return result;
  99. #else
  100. #error Please define your platform.
  101. #endif
  102. }
  103. bool SDL_CompareAndSwapAtomicU32(SDL_AtomicU32 *a, Uint32 oldval, Uint32 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_GCC_ATOMICS)
  109. return __sync_bool_compare_and_swap(&a->value, oldval, newval);
  110. #elif defined(SDL_PLATFORM_MACOS) // this is deprecated in 10.12 sdk; favor gcc atomics.
  111. return OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t *)&a->value);
  112. #elif defined(SDL_PLATFORM_SOLARIS)
  113. SDL_COMPILE_TIME_ASSERT(atomic_cas, sizeof(uint_t) == sizeof(a->value));
  114. return ((Uint32)atomic_cas_uint((volatile uint_t *)&a->value, (uint_t)oldval, (uint_t)newval) == oldval);
  115. #elif defined(EMULATE_CAS)
  116. bool result = false;
  117. enterLock(a);
  118. if (a->value == oldval) {
  119. a->value = newval;
  120. result = true;
  121. }
  122. leaveLock(a);
  123. return result;
  124. #else
  125. #error Please define your platform.
  126. #endif
  127. }
  128. bool SDL_CompareAndSwapAtomicPointer(void **a, void *oldval, void *newval)
  129. {
  130. #ifdef HAVE_MSC_ATOMICS
  131. return _InterlockedCompareExchangePointer(a, newval, oldval) == oldval;
  132. #elif defined(HAVE_GCC_ATOMICS)
  133. return __sync_bool_compare_and_swap(a, oldval, newval);
  134. #elif defined(SDL_PLATFORM_MACOS) && defined(__LP64__) // this is deprecated in 10.12 sdk; favor gcc atomics.
  135. return OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t *)a);
  136. #elif defined(SDL_PLATFORM_MACOS) && !defined(__LP64__) // this is deprecated in 10.12 sdk; favor gcc atomics.
  137. return OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t *)a);
  138. #elif defined(SDL_PLATFORM_SOLARIS)
  139. return (atomic_cas_ptr(a, oldval, newval) == oldval);
  140. #elif defined(EMULATE_CAS)
  141. bool result = false;
  142. enterLock(a);
  143. if (*a == oldval) {
  144. *a = newval;
  145. result = true;
  146. }
  147. leaveLock(a);
  148. return result;
  149. #else
  150. #error Please define your platform.
  151. #endif
  152. }
  153. int SDL_SetAtomicInt(SDL_AtomicInt *a, int v)
  154. {
  155. #ifdef HAVE_MSC_ATOMICS
  156. SDL_COMPILE_TIME_ASSERT(atomic_set, sizeof(long) == sizeof(a->value));
  157. return _InterlockedExchange((long *)&a->value, v);
  158. #elif defined(HAVE_GCC_ATOMICS)
  159. return __sync_lock_test_and_set(&a->value, v);
  160. #elif defined(SDL_PLATFORM_SOLARIS)
  161. SDL_COMPILE_TIME_ASSERT(atomic_set, sizeof(uint_t) == sizeof(a->value));
  162. return (int)atomic_swap_uint((volatile uint_t *)&a->value, v);
  163. #else
  164. int value;
  165. do {
  166. value = a->value;
  167. } while (!SDL_CompareAndSwapAtomicInt(a, value, v));
  168. return value;
  169. #endif
  170. }
  171. Uint32 SDL_SetAtomicU32(SDL_AtomicU32 *a, Uint32 v)
  172. {
  173. #ifdef HAVE_MSC_ATOMICS
  174. SDL_COMPILE_TIME_ASSERT(atomic_set, sizeof(long) == sizeof(a->value));
  175. return _InterlockedExchange((long *)&a->value, v);
  176. #elif defined(HAVE_GCC_ATOMICS)
  177. return __sync_lock_test_and_set(&a->value, v);
  178. #elif defined(SDL_PLATFORM_SOLARIS)
  179. SDL_COMPILE_TIME_ASSERT(atomic_set, sizeof(uint_t) == sizeof(a->value));
  180. return (Uint32)atomic_swap_uint((volatile uint_t *)&a->value, v);
  181. #else
  182. Uint32 value;
  183. do {
  184. value = a->value;
  185. } while (!SDL_CompareAndSwapAtomicU32(a, value, v));
  186. return value;
  187. #endif
  188. }
  189. void *SDL_SetAtomicPointer(void **a, void *v)
  190. {
  191. #ifdef HAVE_MSC_ATOMICS
  192. return _InterlockedExchangePointer(a, v);
  193. #elif defined(HAVE_GCC_ATOMICS)
  194. return __sync_lock_test_and_set(a, v);
  195. #elif defined(SDL_PLATFORM_SOLARIS)
  196. return atomic_swap_ptr(a, v);
  197. #else
  198. void *value;
  199. do {
  200. value = *a;
  201. } while (!SDL_CompareAndSwapAtomicPointer(a, value, v));
  202. return value;
  203. #endif
  204. }
  205. int SDL_AddAtomicInt(SDL_AtomicInt *a, int v)
  206. {
  207. #ifdef HAVE_MSC_ATOMICS
  208. SDL_COMPILE_TIME_ASSERT(atomic_add, sizeof(long) == sizeof(a->value));
  209. return _InterlockedExchangeAdd((long *)&a->value, v);
  210. #elif defined(HAVE_GCC_ATOMICS)
  211. return __sync_fetch_and_add(&a->value, v);
  212. #elif defined(SDL_PLATFORM_SOLARIS)
  213. int pv = a->value;
  214. membar_consumer();
  215. atomic_add_int((volatile uint_t *)&a->value, v);
  216. return pv;
  217. #else
  218. int value;
  219. do {
  220. value = a->value;
  221. } while (!SDL_CompareAndSwapAtomicInt(a, value, (value + v)));
  222. return value;
  223. #endif
  224. }
  225. Uint32 SDL_AddAtomicU32(SDL_AtomicU32 *a, int v)
  226. {
  227. #ifdef HAVE_MSC_ATOMICS
  228. SDL_COMPILE_TIME_ASSERT(atomic_add, sizeof(long) == sizeof(a->value));
  229. return (Uint32)_InterlockedExchangeAdd((long *)&a->value, v);
  230. #elif defined(HAVE_GCC_ATOMICS)
  231. return __sync_fetch_and_add(&a->value, v);
  232. #elif defined(SDL_PLATFORM_SOLARIS)
  233. Uint32 pv = a->value;
  234. membar_consumer();
  235. atomic_add_int((volatile uint_t *)&a->value, v);
  236. return pv;
  237. #else
  238. Uint32 value;
  239. do {
  240. value = a->value;
  241. } while (!SDL_CompareAndSwapAtomicU32(a, value, (value + v)));
  242. return value;
  243. #endif
  244. }
  245. int SDL_GetAtomicInt(SDL_AtomicInt *a)
  246. {
  247. #ifdef HAVE_ATOMIC_LOAD_N
  248. return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST);
  249. #elif defined(HAVE_MSC_ATOMICS)
  250. SDL_COMPILE_TIME_ASSERT(atomic_get, sizeof(long) == sizeof(a->value));
  251. return _InterlockedOr((long *)&a->value, 0);
  252. #elif defined(HAVE_GCC_ATOMICS)
  253. return __sync_or_and_fetch(&a->value, 0);
  254. #elif defined(SDL_PLATFORM_MACOS) // this is deprecated in 10.12 sdk; favor gcc atomics.
  255. return sizeof(a->value) == sizeof(uint32_t) ? OSAtomicOr32Barrier(0, (volatile uint32_t *)&a->value) : OSAtomicAdd64Barrier(0, (volatile int64_t *)&a->value);
  256. #elif defined(SDL_PLATFORM_SOLARIS)
  257. return atomic_or_uint_nv((volatile uint_t *)&a->value, 0);
  258. #else
  259. int value;
  260. do {
  261. value = a->value;
  262. } while (!SDL_CompareAndSwapAtomicInt(a, value, value));
  263. return value;
  264. #endif
  265. }
  266. Uint32 SDL_GetAtomicU32(SDL_AtomicU32 *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 (Uint32)_InterlockedOr((long *)&a->value, 0);
  273. #elif defined(HAVE_GCC_ATOMICS)
  274. return __sync_or_and_fetch(&a->value, 0);
  275. #elif defined(SDL_PLATFORM_MACOS) // this is deprecated in 10.12 sdk; favor gcc atomics.
  276. return OSAtomicOr32Barrier(0, (volatile uint32_t *)&a->value);
  277. #elif defined(SDL_PLATFORM_SOLARIS)
  278. SDL_COMPILE_TIME_ASSERT(atomic_get, sizeof(uint_t) == sizeof(a->value));
  279. return (Uint32)atomic_or_uint_nv((volatile uint_t *)&a->value, 0);
  280. #else
  281. Uint32 value;
  282. do {
  283. value = a->value;
  284. } while (!SDL_CompareAndSwapAtomicU32(a, value, value));
  285. return value;
  286. #endif
  287. }
  288. void *SDL_GetAtomicPointer(void **a)
  289. {
  290. #ifdef HAVE_ATOMIC_LOAD_N
  291. return __atomic_load_n(a, __ATOMIC_SEQ_CST);
  292. #elif defined(HAVE_MSC_ATOMICS)
  293. return _InterlockedCompareExchangePointer(a, NULL, NULL);
  294. #elif defined(HAVE_GCC_ATOMICS)
  295. return __sync_val_compare_and_swap(a, (void *)0, (void *)0);
  296. #elif defined(SDL_PLATFORM_SOLARIS)
  297. return atomic_cas_ptr(a, (void *)0, (void *)0);
  298. #else
  299. void *value;
  300. do {
  301. value = *a;
  302. } while (!SDL_CompareAndSwapAtomicPointer(a, value, value));
  303. return value;
  304. #endif
  305. }
  306. #ifdef SDL_MEMORY_BARRIER_USES_FUNCTION
  307. #error This file should be built in arm mode so the mcr instruction is available for memory barriers
  308. #endif
  309. void SDL_MemoryBarrierReleaseFunction(void)
  310. {
  311. SDL_MemoryBarrierRelease();
  312. }
  313. void SDL_MemoryBarrierAcquireFunction(void)
  314. {
  315. SDL_MemoryBarrierAcquire();
  316. }