atomic.h 13 KB

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