atomic.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #ifndef AL_ATOMIC_H
  2. #define AL_ATOMIC_H
  3. #include "static_assert.h"
  4. #include "bool.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. typedef void *volatile XchgPtr;
  9. /* Atomics using C11 */
  10. #ifdef HAVE_C11_ATOMIC
  11. #include <stdatomic.h>
  12. inline int ExchangeInt(volatile int *ptr, int newval)
  13. { return atomic_exchange(ptr, newval); }
  14. inline void *ExchangePtr(XchgPtr *ptr, void *newval)
  15. { return atomic_exchange(ptr, newval); }
  16. #define ATOMIC(T) struct { T _Atomic value; }
  17. #define ATOMIC_INIT(_val, _newval) atomic_init(&(_val)->value, (_newval))
  18. #define ATOMIC_INIT_STATIC(_newval) {ATOMIC_VAR_INIT(_newval)}
  19. #define ATOMIC_LOAD(_val) atomic_load(&(_val)->value)
  20. #define ATOMIC_STORE(_val, _newval) atomic_store(&(_val)->value, (_newval))
  21. #define ATOMIC_ADD(T, _val, _incr) atomic_fetch_add(&(_val)->value, (_incr))
  22. #define ATOMIC_SUB(T, _val, _decr) atomic_fetch_sub(&(_val)->value, (_decr))
  23. #define ATOMIC_EXCHANGE(T, _val, _newval) atomic_exchange(&(_val)->value, (_newval))
  24. #define ATOMIC_COMPARE_EXCHANGE_STRONG(T, _val, _oldval, _newval) \
  25. atomic_compare_exchange_strong(&(_val)->value, (_oldval), (_newval))
  26. #define ATOMIC_COMPARE_EXCHANGE_WEAK(T, _val, _oldval, _newval) \
  27. atomic_compare_exchange_weak(&(_val)->value, (_oldval), (_newval))
  28. /* Atomics using GCC intrinsics */
  29. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) && !defined(__QNXNTO__)
  30. inline int ExchangeInt(volatile int *ptr, int newval)
  31. { return __sync_lock_test_and_set(ptr, newval); }
  32. inline void *ExchangePtr(XchgPtr *ptr, void *newval)
  33. { return __sync_lock_test_and_set(ptr, newval); }
  34. #define ATOMIC(T) struct { T volatile value; }
  35. #define ATOMIC_INIT(_val, _newval) do { (_val)->value = (_newval); } while(0)
  36. #define ATOMIC_INIT_STATIC(_newval) {(_newval)}
  37. #define ATOMIC_LOAD(_val) __extension__({ \
  38. __typeof((_val)->value) _r = (_val)->value; \
  39. __asm__ __volatile__("" ::: "memory"); \
  40. _r; \
  41. })
  42. #define ATOMIC_STORE(_val, _newval) do { \
  43. __asm__ __volatile__("" ::: "memory"); \
  44. (_val)->value = (_newval); \
  45. } while(0)
  46. #define ATOMIC_ADD(T, _val, _incr) __extension__({ \
  47. static_assert(sizeof(T)==sizeof((_val)->value), "Type "#T" has incorrect size!"); \
  48. __sync_fetch_and_add(&(_val)->value, (_incr)); \
  49. })
  50. #define ATOMIC_SUB(T, _val, _decr) __extension__({ \
  51. static_assert(sizeof(T)==sizeof((_val)->value), "Type "#T" has incorrect size!"); \
  52. __sync_fetch_and_sub(&(_val)->value, (_decr)); \
  53. })
  54. #define ATOMIC_EXCHANGE(T, _val, _newval) __extension__({ \
  55. static_assert(sizeof(T)==sizeof((_val)->value), "Type "#T" has incorrect size!"); \
  56. __sync_lock_test_and_set(&(_val)->value, (_newval)); \
  57. })
  58. #define ATOMIC_COMPARE_EXCHANGE_STRONG(T, _val, _oldval, _newval) __extension__({ \
  59. static_assert(sizeof(T)==sizeof((_val)->value), "Type "#T" has incorrect size!"); \
  60. T _o = *(_oldval); \
  61. *(_oldval) = __sync_val_compare_and_swap(&(_val)->value, _o, (_newval)); \
  62. *(_oldval) == _o; \
  63. })
  64. /* Atomics using x86/x86-64 GCC inline assembly */
  65. #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  66. #define WRAP_ADD(ret, dest, incr) __asm__ __volatile__( \
  67. "lock; xaddl %0,(%1)" \
  68. : "=r" (ret) \
  69. : "r" (dest), "0" (incr) \
  70. : "memory" \
  71. )
  72. #define WRAP_SUB(ret, dest, decr) __asm__ __volatile__( \
  73. "lock; xaddl %0,(%1)" \
  74. : "=r" (ret) \
  75. : "r" (dest), "0" (-(decr)) \
  76. : "memory" \
  77. )
  78. #define WRAP_XCHG(S, ret, dest, newval) __asm__ __volatile__( \
  79. "lock; xchg"S" %0,(%1)" \
  80. : "=r" (ret) \
  81. : "r" (dest), "0" (newval) \
  82. : "memory" \
  83. )
  84. #define WRAP_CMPXCHG(S, ret, dest, oldval, newval) __asm__ __volatile__( \
  85. "lock; cmpxchg"S" %2,(%1)" \
  86. : "=a" (ret) \
  87. : "r" (dest), "r" (newval), "0" (oldval) \
  88. : "memory" \
  89. )
  90. inline int ExchangeInt(volatile int *dest, int newval)
  91. { int ret; WRAP_XCHG("l", ret, dest, newval); return ret; }
  92. #ifdef __i386__
  93. inline void *ExchangePtr(XchgPtr *dest, void *newval)
  94. { void *ret; WRAP_XCHG("l", ret, dest, newval); return ret; }
  95. #else
  96. inline void *ExchangePtr(XchgPtr *dest, void *newval)
  97. { void *ret; WRAP_XCHG("q", ret, dest, newval); return ret; }
  98. #endif
  99. #define ATOMIC(T) struct { T volatile value; }
  100. #define ATOMIC_INIT(_val, _newval) do { (_val)->value = (_newval); } while(0)
  101. #define ATOMIC_INIT_STATIC(_newval) {(_newval)}
  102. #define ATOMIC_LOAD(_val) __extension__({ \
  103. __typeof((_val)->value) _r = (_val)->value; \
  104. __asm__ __volatile__("" ::: "memory"); \
  105. _r; \
  106. })
  107. #define ATOMIC_STORE(_val, _newval) do { \
  108. __asm__ __volatile__("" ::: "memory"); \
  109. (_val)->value = (_newval); \
  110. } while(0)
  111. #define ATOMIC_ADD(T, _val, _incr) __extension__({ \
  112. static_assert(sizeof(T)==4, "Type "#T" has incorrect size!"); \
  113. static_assert(sizeof(T)==sizeof((_val)->value), "Type "#T" has incorrect size!"); \
  114. T _r; \
  115. WRAP_ADD(_r, &(_val)->value, (T)(_incr)); \
  116. _r; \
  117. })
  118. #define ATOMIC_SUB(T, _val, _decr) __extension__({ \
  119. static_assert(sizeof(T)==4, "Type "#T" has incorrect size!"); \
  120. static_assert(sizeof(T)==sizeof((_val)->value), "Type "#T" has incorrect size!"); \
  121. T _r; \
  122. WRAP_SUB(_r, &(_val)->value, (T)(_decr)); \
  123. _r; \
  124. })
  125. #define ATOMIC_EXCHANGE(T, _val, _newval) __extension__({ \
  126. static_assert(sizeof(T)==4 || sizeof(T)==8, "Type "#T" has incorrect size!"); \
  127. static_assert(sizeof(T)==sizeof((_val)->value), "Type "#T" has incorrect size!"); \
  128. T _r; \
  129. if(sizeof(T) == 4) WRAP_XCHG("l", _r, &(_val)->value, (T)(_newval)); \
  130. else if(sizeof(T) == 8) WRAP_XCHG("q", _r, &(_val)->value, (T)(_newval)); \
  131. _r; \
  132. })
  133. #define ATOMIC_COMPARE_EXCHANGE_STRONG(T, _val, _oldval, _newval) __extension__({ \
  134. static_assert(sizeof(T)==4 || sizeof(T)==8, "Type "#T" has incorrect size!"); \
  135. static_assert(sizeof(T)==sizeof((_val)->value), "Type "#T" has incorrect size!"); \
  136. T _old = *(_oldval); \
  137. if(sizeof(T) == 4) WRAP_CMPXCHG("l", *(_oldval), &(_val)->value, _old, (T)(_newval)); \
  138. else if(sizeof(T) == 8) WRAP_CMPXCHG("q", *(_oldval), &(_val)->value, _old, (T)(_newval)); \
  139. *(_oldval) == _old; \
  140. })
  141. /* Atomics using Windows methods */
  142. #elif defined(_WIN32)
  143. #define WIN32_LEAN_AND_MEAN
  144. #include <windows.h>
  145. /* NOTE: This mess is *extremely* noisy, at least on GCC. It works by wrapping
  146. * Windows' 32-bit and 64-bit atomic methods, which are then casted to use the
  147. * given type based on its size (e.g. int and float use 32-bit atomics). This
  148. * is fine for the swap and compare-and-swap methods, although the add and
  149. * subtract methods only work properly for integer types.
  150. *
  151. * Despite how noisy it is, it's unfortunately the only way that doesn't rely
  152. * on C99 (damn MSVC).
  153. */
  154. inline LONG AtomicAdd32(volatile LONG *dest, LONG incr)
  155. {
  156. return InterlockedExchangeAdd(dest, incr);
  157. }
  158. inline LONG AtomicSub32(volatile LONG *dest, LONG decr)
  159. {
  160. return InterlockedExchangeAdd(dest, -decr);
  161. }
  162. inline LONG AtomicSwap32(volatile LONG *dest, LONG newval)
  163. {
  164. return InterlockedExchange(dest, newval);
  165. }
  166. inline LONGLONG AtomicSwap64(volatile LONGLONG *dest, LONGLONG newval)
  167. {
  168. return InterlockedExchange64(dest, newval);
  169. }
  170. inline bool CompareAndSwap32(volatile LONG *dest, LONG newval, LONG *oldval)
  171. {
  172. LONG old = *oldval;
  173. *oldval = InterlockedCompareExchange(dest, newval, *oldval);
  174. return old == *oldval;
  175. }
  176. inline bool CompareAndSwap64(volatile LONGLONG *dest, LONGLONG newval, LONGLONG *oldval)
  177. {
  178. LONGLONG old = *oldval;
  179. *oldval = InterlockedCompareExchange64(dest, newval, *oldval);
  180. return old == *oldval;
  181. }
  182. #define WRAP_ADDSUB(T, _func, _ptr, _amnt) ((T(*)(T volatile*,T))_func)((_ptr), (_amnt))
  183. #define WRAP_XCHG(T, _func, _ptr, _newval) ((T(*)(T volatile*,T))_func)((_ptr), (_newval))
  184. #define WRAP_CMPXCHG(T, _func, _ptr, _newval, _oldval) ((bool(*)(T volatile*,T,T*))_func)((_ptr), (_newval), (_oldval))
  185. inline int ExchangeInt(volatile int *ptr, int newval)
  186. { return WRAP_XCHG(int,AtomicSwap32,ptr,newval); }
  187. #ifdef _WIN64
  188. inline void *ExchangePtr(XchgPtr *ptr, void *newval)
  189. { return WRAP_XCHG(void*,AtomicSwap64,ptr,newval); }
  190. #else
  191. inline void *ExchangePtr(XchgPtr *ptr, void *newval)
  192. { return WRAP_XCHG(void*,AtomicSwap32,ptr,newval); }
  193. #endif
  194. #define ATOMIC(T) struct { T volatile value; }
  195. #define ATOMIC_INIT(_val, _newval) do { (_val)->value = (_newval); } while(0)
  196. #define ATOMIC_INIT_STATIC(_newval) {(_newval)}
  197. #define ATOMIC_LOAD(_val) ((_val)->value)
  198. #define ATOMIC_STORE(_val, _newval) do { \
  199. (_val)->value = (_newval); \
  200. } while(0)
  201. int _al_invalid_atomic_size(); /* not defined */
  202. #define ATOMIC_ADD(T, _val, _incr) \
  203. ((sizeof(T)==4) ? WRAP_ADDSUB(T, AtomicAdd32, &(_val)->value, (_incr)) : \
  204. (T)_al_invalid_atomic_size())
  205. #define ATOMIC_SUB(T, _val, _decr) \
  206. ((sizeof(T)==4) ? WRAP_ADDSUB(T, AtomicSub32, &(_val)->value, (_decr)) : \
  207. (T)_al_invalid_atomic_size())
  208. #define ATOMIC_EXCHANGE(T, _val, _newval) \
  209. ((sizeof(T)==4) ? WRAP_XCHG(T, AtomicSwap32, &(_val)->value, (_newval)) : \
  210. (sizeof(T)==8) ? WRAP_XCHG(T, AtomicSwap64, &(_val)->value, (_newval)) : \
  211. (T)_al_invalid_atomic_size())
  212. #define ATOMIC_COMPARE_EXCHANGE_STRONG(T, _val, _oldval, _newval) \
  213. ((sizeof(T)==4) ? WRAP_CMPXCHG(T, CompareAndSwap32, &(_val)->value, (_newval), (_oldval)) : \
  214. (sizeof(T)==8) ? WRAP_CMPXCHG(T, CompareAndSwap64, &(_val)->value, (_newval), (_oldval)) : \
  215. (bool)_al_invalid_atomic_size())
  216. #else
  217. #error "No atomic functions available on this platform!"
  218. #define ATOMIC(T) T
  219. #define ATOMIC_INIT_STATIC(_newval) (0)
  220. #define ATOMIC_LOAD_UNSAFE(_val) (0)
  221. #define ATOMIC_STORE_UNSAFE(_val, _newval) ((void)0)
  222. #define ATOMIC_LOAD(_val) (0)
  223. #define ATOMIC_STORE(_val, _newval) ((void)0)
  224. #define ATOMIC_ADD(T, _val, _incr) (0)
  225. #define ATOMIC_SUB(T, _val, _decr) (0)
  226. #define ATOMIC_EXCHANGE(T, _val, _newval) (0)
  227. #define ATOMIC_COMPARE_EXCHANGE_STRONG(T, _val, _oldval, _newval) (0)
  228. #endif
  229. /* If no weak cmpxchg is provided (not all systems will have one), substitute a
  230. * strong cmpxchg. */
  231. #ifndef ATOMIC_COMPARE_EXCHANGE_WEAK
  232. #define ATOMIC_COMPARE_EXCHANGE_WEAK(a, b, c, d) ATOMIC_COMPARE_EXCHANGE_STRONG(a, b, c, d)
  233. #endif
  234. /* This is *NOT* atomic, but is a handy utility macro to compare-and-swap non-
  235. * atomic variables. */
  236. #define COMPARE_EXCHANGE(_val, _oldval, _newval) ((*(_val) == *(_oldval)) ? ((*(_val)=(_newval)),true) : ((*(_oldval)=*(_val)),false))
  237. typedef unsigned int uint;
  238. typedef ATOMIC(uint) RefCount;
  239. inline void InitRef(RefCount *ptr, uint value)
  240. { ATOMIC_INIT(ptr, value); }
  241. inline uint ReadRef(RefCount *ptr)
  242. { return ATOMIC_LOAD(ptr); }
  243. inline uint IncrementRef(RefCount *ptr)
  244. { return ATOMIC_ADD(uint, ptr, 1)+1; }
  245. inline uint DecrementRef(RefCount *ptr)
  246. { return ATOMIC_SUB(uint, ptr, 1)-1; }
  247. #ifdef __cplusplus
  248. }
  249. #endif
  250. #endif /* AL_ATOMIC_H */