SDL_atomic.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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(HAVE_GCC_ATOMICS)
  32. # if defined(__clang__)
  33. # if __has_builtin(__atomic_load_n)
  34. /* !!! FIXME: this advertises as available in the NDK but uses an external symbol we don't have.
  35. It might be in a later NDK or we might need an extra library? --ryan. */
  36. # if !defined(__ANDROID__)
  37. # define HAVE_ATOMIC_LOAD_N 1
  38. # endif
  39. # endif
  40. # elif defined(__GNUC__)
  41. # if (__GNUC__ >= 5)
  42. # define HAVE_ATOMIC_LOAD_N 1
  43. # endif
  44. # endif
  45. #endif
  46. #if defined(__WATCOMC__) && defined(__386__)
  47. SDL_COMPILE_TIME_ASSERT(intsize, 4==sizeof(int));
  48. #define HAVE_WATCOM_ATOMICS
  49. extern _inline int _SDL_xchg_watcom(volatile int *a, int v);
  50. #pragma aux _SDL_xchg_watcom = \
  51. "lock xchg [ecx], eax" \
  52. parm [ecx] [eax] \
  53. value [eax] \
  54. modify exact [eax];
  55. extern _inline unsigned char _SDL_cmpxchg_watcom(volatile int *a, int newval, int oldval);
  56. #pragma aux _SDL_cmpxchg_watcom = \
  57. "lock cmpxchg [edx], ecx" \
  58. "setz al" \
  59. parm [edx] [ecx] [eax] \
  60. value [al] \
  61. modify exact [eax];
  62. extern _inline int _SDL_xadd_watcom(volatile int *a, int v);
  63. #pragma aux _SDL_xadd_watcom = \
  64. "lock xadd [ecx], eax" \
  65. parm [ecx] [eax] \
  66. value [eax] \
  67. modify exact [eax];
  68. #endif /* __WATCOMC__ && __386__ */
  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 1
  90. #endif
  91. #if EMULATE_CAS
  92. static SDL_SpinLock locks[32];
  93. static SDL_INLINE void
  94. enterLock(void *a)
  95. {
  96. uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
  97. SDL_AtomicLock(&locks[index]);
  98. }
  99. static SDL_INLINE void
  100. leaveLock(void *a)
  101. {
  102. uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
  103. SDL_AtomicUnlock(&locks[index]);
  104. }
  105. #endif
  106. SDL_bool
  107. SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
  108. {
  109. #ifdef HAVE_MSC_ATOMICS
  110. return (_InterlockedCompareExchange((long*)&a->value, (long)newval, (long)oldval) == (long)oldval);
  111. #elif defined(HAVE_WATCOM_ATOMICS)
  112. return (SDL_bool) _SDL_cmpxchg_watcom(&a->value, newval, oldval);
  113. #elif defined(HAVE_GCC_ATOMICS)
  114. return (SDL_bool) __sync_bool_compare_and_swap(&a->value, oldval, newval);
  115. #elif defined(__MACOSX__) /* this is deprecated in 10.12 sdk; favor gcc atomics. */
  116. return (SDL_bool) OSAtomicCompareAndSwap32Barrier(oldval, newval, &a->value);
  117. #elif defined(__SOLARIS__) && defined(_LP64)
  118. return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)&a->value, (uint64_t)oldval, (uint64_t)newval) == oldval);
  119. #elif defined(__SOLARIS__) && !defined(_LP64)
  120. return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)&a->value, (uint32_t)oldval, (uint32_t)newval) == oldval);
  121. #elif EMULATE_CAS
  122. SDL_bool retval = SDL_FALSE;
  123. enterLock(a);
  124. if (a->value == oldval) {
  125. a->value = newval;
  126. retval = SDL_TRUE;
  127. }
  128. leaveLock(a);
  129. return retval;
  130. #else
  131. #error Please define your platform.
  132. #endif
  133. }
  134. SDL_bool
  135. SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
  136. {
  137. #if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
  138. return (_InterlockedCompareExchange((long*)a, (long)newval, (long)oldval) == (long)oldval);
  139. #elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
  140. return (_InterlockedCompareExchangePointer(a, newval, oldval) == oldval);
  141. #elif defined(HAVE_WATCOM_ATOMICS)
  142. return (SDL_bool) _SDL_cmpxchg_watcom((int *)a, (long)newval, (long)oldval);
  143. #elif defined(HAVE_GCC_ATOMICS)
  144. return __sync_bool_compare_and_swap(a, oldval, newval);
  145. #elif defined(__MACOSX__) && defined(__LP64__) /* this is deprecated in 10.12 sdk; favor gcc atomics. */
  146. return (SDL_bool) OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t*) a);
  147. #elif defined(__MACOSX__) && !defined(__LP64__) /* this is deprecated in 10.12 sdk; favor gcc atomics. */
  148. return (SDL_bool) OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t*) a);
  149. #elif defined(__SOLARIS__)
  150. return (SDL_bool) (atomic_cas_ptr(a, oldval, newval) == oldval);
  151. #elif EMULATE_CAS
  152. SDL_bool retval = SDL_FALSE;
  153. enterLock(a);
  154. if (*a == oldval) {
  155. *a = newval;
  156. retval = SDL_TRUE;
  157. }
  158. leaveLock(a);
  159. return retval;
  160. #else
  161. #error Please define your platform.
  162. #endif
  163. }
  164. int
  165. SDL_AtomicSet(SDL_atomic_t *a, int v)
  166. {
  167. #ifdef HAVE_MSC_ATOMICS
  168. return _InterlockedExchange((long*)&a->value, v);
  169. #elif defined(HAVE_WATCOM_ATOMICS)
  170. return _SDL_xchg_watcom(&a->value, v);
  171. #elif defined(HAVE_GCC_ATOMICS)
  172. return __sync_lock_test_and_set(&a->value, v);
  173. #elif defined(__SOLARIS__) && defined(_LP64)
  174. return (int) atomic_swap_64((volatile uint64_t*)&a->value, (uint64_t)v);
  175. #elif defined(__SOLARIS__) && !defined(_LP64)
  176. return (int) atomic_swap_32((volatile uint32_t*)&a->value, (uint32_t)v);
  177. #else
  178. int value;
  179. do {
  180. value = a->value;
  181. } while (!SDL_AtomicCAS(a, value, v));
  182. return value;
  183. #endif
  184. }
  185. void*
  186. SDL_AtomicSetPtr(void **a, void *v)
  187. {
  188. #if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
  189. return (void *) _InterlockedExchange((long *)a, (long) v);
  190. #elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
  191. return _InterlockedExchangePointer(a, v);
  192. #elif defined(HAVE_WATCOM_ATOMICS)
  193. return (void *) _SDL_xchg_watcom((int *)a, (long)v);
  194. #elif defined(HAVE_GCC_ATOMICS)
  195. return __sync_lock_test_and_set(a, v);
  196. #elif defined(__SOLARIS__)
  197. return atomic_swap_ptr(a, v);
  198. #else
  199. void *value;
  200. do {
  201. value = *a;
  202. } while (!SDL_AtomicCASPtr(a, value, v));
  203. return value;
  204. #endif
  205. }
  206. int
  207. SDL_AtomicAdd(SDL_atomic_t *a, int v)
  208. {
  209. #ifdef HAVE_MSC_ATOMICS
  210. return _InterlockedExchangeAdd((long*)&a->value, v);
  211. #elif defined(HAVE_WATCOM_ATOMICS)
  212. return _SDL_xadd_watcom(&a->value, v);
  213. #elif defined(HAVE_GCC_ATOMICS)
  214. return __sync_fetch_and_add(&a->value, v);
  215. #elif defined(__SOLARIS__)
  216. int pv = a->value;
  217. membar_consumer();
  218. #if defined(_LP64)
  219. atomic_add_64((volatile uint64_t*)&a->value, v);
  220. #elif !defined(_LP64)
  221. atomic_add_32((volatile uint32_t*)&a->value, v);
  222. #endif
  223. return pv;
  224. #else
  225. int value;
  226. do {
  227. value = a->value;
  228. } while (!SDL_AtomicCAS(a, value, (value + v)));
  229. return value;
  230. #endif
  231. }
  232. int
  233. SDL_AtomicGet(SDL_atomic_t *a)
  234. {
  235. #ifdef HAVE_ATOMIC_LOAD_N
  236. return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST);
  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. #else
  251. void *value;
  252. do {
  253. value = *a;
  254. } while (!SDL_AtomicCASPtr(a, value, value));
  255. return value;
  256. #endif
  257. }
  258. #ifdef SDL_MEMORY_BARRIER_USES_FUNCTION
  259. #error This file should be built in arm mode so the mcr instruction is available for memory barriers
  260. #endif
  261. void
  262. SDL_MemoryBarrierReleaseFunction(void)
  263. {
  264. SDL_MemoryBarrierRelease();
  265. }
  266. void
  267. SDL_MemoryBarrierAcquireFunction(void)
  268. {
  269. SDL_MemoryBarrierAcquire();
  270. }
  271. /* vi: set ts=4 sw=4 expandtab: */