SDL_atomic.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. 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__) && defined(_LP64)
  116. return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)&a->value, (uint64_t)oldval, (uint64_t)newval) == oldval);
  117. #elif defined(__SOLARIS__) && !defined(_LP64)
  118. return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)&a->value, (uint32_t)oldval, (uint32_t)newval) == oldval);
  119. #elif EMULATE_CAS
  120. SDL_bool retval = SDL_FALSE;
  121. enterLock(a);
  122. if (a->value == oldval) {
  123. a->value = newval;
  124. retval = SDL_TRUE;
  125. }
  126. leaveLock(a);
  127. return retval;
  128. #else
  129. #error Please define your platform.
  130. #endif
  131. }
  132. SDL_bool
  133. SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
  134. {
  135. #if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
  136. return (_InterlockedCompareExchange((long*)a, (long)newval, (long)oldval) == (long)oldval);
  137. #elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
  138. return (_InterlockedCompareExchangePointer(a, newval, oldval) == oldval);
  139. #elif defined(HAVE_WATCOM_ATOMICS)
  140. return (SDL_bool) _SDL_cmpxchg_watcom((int *)a, (long)newval, (long)oldval);
  141. #elif defined(HAVE_GCC_ATOMICS)
  142. return __sync_bool_compare_and_swap(a, oldval, newval);
  143. #elif defined(__MACOSX__) && defined(__LP64__) /* this is deprecated in 10.12 sdk; favor gcc atomics. */
  144. return (SDL_bool) OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t*) a);
  145. #elif defined(__MACOSX__) && !defined(__LP64__) /* this is deprecated in 10.12 sdk; favor gcc atomics. */
  146. return (SDL_bool) OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t*) a);
  147. #elif defined(__SOLARIS__)
  148. return (SDL_bool) (atomic_cas_ptr(a, oldval, newval) == oldval);
  149. #elif EMULATE_CAS
  150. SDL_bool retval = SDL_FALSE;
  151. enterLock(a);
  152. if (*a == oldval) {
  153. *a = newval;
  154. retval = SDL_TRUE;
  155. }
  156. leaveLock(a);
  157. return retval;
  158. #else
  159. #error Please define your platform.
  160. #endif
  161. }
  162. int
  163. SDL_AtomicSet(SDL_atomic_t *a, int v)
  164. {
  165. #ifdef HAVE_MSC_ATOMICS
  166. return _InterlockedExchange((long*)&a->value, v);
  167. #elif defined(HAVE_WATCOM_ATOMICS)
  168. return _SDL_xchg_watcom(&a->value, v);
  169. #elif defined(HAVE_GCC_ATOMICS)
  170. return __sync_lock_test_and_set(&a->value, v);
  171. #elif defined(__SOLARIS__) && defined(_LP64)
  172. return (int) atomic_swap_64((volatile uint64_t*)&a->value, (uint64_t)v);
  173. #elif defined(__SOLARIS__) && !defined(_LP64)
  174. return (int) atomic_swap_32((volatile uint32_t*)&a->value, (uint32_t)v);
  175. #else
  176. int value;
  177. do {
  178. value = a->value;
  179. } while (!SDL_AtomicCAS(a, value, v));
  180. return value;
  181. #endif
  182. }
  183. void*
  184. SDL_AtomicSetPtr(void **a, void *v)
  185. {
  186. #if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
  187. return (void *) _InterlockedExchange((long *)a, (long) v);
  188. #elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
  189. return _InterlockedExchangePointer(a, v);
  190. #elif defined(HAVE_WATCOM_ATOMICS)
  191. return (void *) _SDL_xchg_watcom((int *)a, (long)v);
  192. #elif defined(HAVE_GCC_ATOMICS)
  193. return __sync_lock_test_and_set(a, v);
  194. #elif defined(__SOLARIS__)
  195. return atomic_swap_ptr(a, v);
  196. #else
  197. void *value;
  198. do {
  199. value = *a;
  200. } while (!SDL_AtomicCASPtr(a, value, v));
  201. return value;
  202. #endif
  203. }
  204. int
  205. SDL_AtomicAdd(SDL_atomic_t *a, int v)
  206. {
  207. #ifdef HAVE_MSC_ATOMICS
  208. return _InterlockedExchangeAdd((long*)&a->value, v);
  209. #elif defined(HAVE_WATCOM_ATOMICS)
  210. return _SDL_xadd_watcom(&a->value, v);
  211. #elif defined(HAVE_GCC_ATOMICS)
  212. return __sync_fetch_and_add(&a->value, v);
  213. #elif defined(__SOLARIS__)
  214. int pv = a->value;
  215. membar_consumer();
  216. #if defined(_LP64)
  217. atomic_add_64((volatile uint64_t*)&a->value, v);
  218. #elif !defined(_LP64)
  219. atomic_add_32((volatile uint32_t*)&a->value, v);
  220. #endif
  221. return pv;
  222. #else
  223. int value;
  224. do {
  225. value = a->value;
  226. } while (!SDL_AtomicCAS(a, value, (value + v)));
  227. return value;
  228. #endif
  229. }
  230. int
  231. SDL_AtomicGet(SDL_atomic_t *a)
  232. {
  233. #ifdef HAVE_ATOMIC_LOAD_N
  234. return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST);
  235. #else
  236. int value;
  237. do {
  238. value = a->value;
  239. } while (!SDL_AtomicCAS(a, value, value));
  240. return value;
  241. #endif
  242. }
  243. void *
  244. SDL_AtomicGetPtr(void **a)
  245. {
  246. #ifdef HAVE_ATOMIC_LOAD_N
  247. return __atomic_load_n(a, __ATOMIC_SEQ_CST);
  248. #else
  249. void *value;
  250. do {
  251. value = *a;
  252. } while (!SDL_AtomicCASPtr(a, value, value));
  253. return value;
  254. #endif
  255. }
  256. #ifdef SDL_MEMORY_BARRIER_USES_FUNCTION
  257. #error This file should be built in arm mode so the mcr instruction is available for memory barriers
  258. #endif
  259. void
  260. SDL_MemoryBarrierReleaseFunction(void)
  261. {
  262. SDL_MemoryBarrierRelease();
  263. }
  264. void
  265. SDL_MemoryBarrierAcquireFunction(void)
  266. {
  267. SDL_MemoryBarrierAcquire();
  268. }
  269. /* vi: set ts=4 sw=4 expandtab: */