atomic.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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_FLAG atomic_flag
  20. #define ATOMIC_INIT atomic_init
  21. #define ATOMIC_INIT_STATIC ATOMIC_VAR_INIT
  22. /*#define ATOMIC_FLAG_INIT ATOMIC_FLAG_INIT*/
  23. #define ATOMIC_LOAD atomic_load_explicit
  24. #define ATOMIC_STORE atomic_store_explicit
  25. #define ATOMIC_ADD atomic_fetch_add_explicit
  26. #define ATOMIC_SUB atomic_fetch_sub_explicit
  27. #define ATOMIC_EXCHANGE atomic_exchange_explicit
  28. #define ATOMIC_COMPARE_EXCHANGE_STRONG atomic_compare_exchange_strong_explicit
  29. #define ATOMIC_COMPARE_EXCHANGE_WEAK atomic_compare_exchange_weak_explicit
  30. #define ATOMIC_FLAG_TEST_AND_SET atomic_flag_test_and_set_explicit
  31. #define ATOMIC_FLAG_CLEAR atomic_flag_clear_explicit
  32. #define ATOMIC_THREAD_FENCE atomic_thread_fence
  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_FLAG ATOMIC(int)
  45. #define ATOMIC_INIT(_val, _newval) do { (_val)->value = (_newval); } while(0)
  46. #define ATOMIC_INIT_STATIC(_newval) {(_newval)}
  47. #define ATOMIC_FLAG_INIT ATOMIC_INIT_STATIC(0)
  48. #define ATOMIC_LOAD(_val, _MO) __extension__({ \
  49. __typeof((_val)->value) _r = (_val)->value; \
  50. __asm__ __volatile__("" ::: "memory"); \
  51. _r; \
  52. })
  53. #define ATOMIC_STORE(_val, _newval, _MO) do { \
  54. __asm__ __volatile__("" ::: "memory"); \
  55. (_val)->value = (_newval); \
  56. } while(0)
  57. #define ATOMIC_ADD(_val, _incr, _MO) __sync_fetch_and_add(&(_val)->value, (_incr))
  58. #define ATOMIC_SUB(_val, _decr, _MO) __sync_fetch_and_sub(&(_val)->value, (_decr))
  59. #define ATOMIC_EXCHANGE(_val, _newval, _MO) __extension__({ \
  60. __asm__ __volatile__("" ::: "memory"); \
  61. __sync_lock_test_and_set(&(_val)->value, (_newval)); \
  62. })
  63. #define ATOMIC_COMPARE_EXCHANGE_STRONG(_val, _oldval, _newval, _MO1, _MO2) __extension__({ \
  64. __typeof(*(_oldval)) _o = *(_oldval); \
  65. *(_oldval) = __sync_val_compare_and_swap(&(_val)->value, _o, (_newval)); \
  66. *(_oldval) == _o; \
  67. })
  68. #define ATOMIC_FLAG_TEST_AND_SET(_val, _MO) __extension__({ \
  69. __asm__ __volatile__("" ::: "memory"); \
  70. __sync_lock_test_and_set(&(_val)->value, 1); \
  71. })
  72. #define ATOMIC_FLAG_CLEAR(_val, _MO) __extension__({ \
  73. __sync_lock_release(&(_val)->value); \
  74. __asm__ __volatile__("" ::: "memory"); \
  75. })
  76. #define ATOMIC_THREAD_FENCE(order) do { \
  77. enum { must_be_constant = (order) }; \
  78. const int _o = must_be_constant; \
  79. if(_o > almemory_order_relaxed) \
  80. __asm__ __volatile__("" ::: "memory"); \
  81. } while(0)
  82. /* Atomics using x86/x86-64 GCC inline assembly */
  83. #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  84. #define WRAP_ADD(S, ret, dest, incr) __asm__ __volatile__( \
  85. "lock; xadd"S" %0,(%1)" \
  86. : "=r" (ret) \
  87. : "r" (dest), "0" (incr) \
  88. : "memory" \
  89. )
  90. #define WRAP_SUB(S, ret, dest, decr) __asm__ __volatile__( \
  91. "lock; xadd"S" %0,(%1)" \
  92. : "=r" (ret) \
  93. : "r" (dest), "0" (-(decr)) \
  94. : "memory" \
  95. )
  96. #define WRAP_XCHG(S, ret, dest, newval) __asm__ __volatile__( \
  97. "lock; xchg"S" %0,(%1)" \
  98. : "=r" (ret) \
  99. : "r" (dest), "0" (newval) \
  100. : "memory" \
  101. )
  102. #define WRAP_CMPXCHG(S, ret, dest, oldval, newval) __asm__ __volatile__( \
  103. "lock; cmpxchg"S" %2,(%1)" \
  104. : "=a" (ret) \
  105. : "r" (dest), "r" (newval), "0" (oldval) \
  106. : "memory" \
  107. )
  108. enum almemory_order {
  109. almemory_order_relaxed,
  110. almemory_order_consume,
  111. almemory_order_acquire,
  112. almemory_order_release,
  113. almemory_order_acq_rel,
  114. almemory_order_seq_cst
  115. };
  116. #define ATOMIC(T) struct { T volatile value; }
  117. #define ATOMIC_INIT(_val, _newval) do { (_val)->value = (_newval); } while(0)
  118. #define ATOMIC_INIT_STATIC(_newval) {(_newval)}
  119. #define ATOMIC_LOAD(_val, _MO) __extension__({ \
  120. __typeof((_val)->value) _r = (_val)->value; \
  121. __asm__ __volatile__("" ::: "memory"); \
  122. _r; \
  123. })
  124. #define ATOMIC_STORE(_val, _newval, _MO) do { \
  125. __asm__ __volatile__("" ::: "memory"); \
  126. (_val)->value = (_newval); \
  127. } while(0)
  128. #define ATOMIC_ADD(_val, _incr, _MO) __extension__({ \
  129. static_assert(sizeof((_val)->value)==4 || sizeof((_val)->value)==8, "Unsupported size!"); \
  130. __typeof((_val)->value) _r; \
  131. if(sizeof((_val)->value) == 4) WRAP_ADD("l", _r, &(_val)->value, _incr); \
  132. else if(sizeof((_val)->value) == 8) WRAP_ADD("q", _r, &(_val)->value, _incr); \
  133. _r; \
  134. })
  135. #define ATOMIC_SUB(_val, _decr, _MO) __extension__({ \
  136. static_assert(sizeof((_val)->value)==4 || sizeof((_val)->value)==8, "Unsupported size!"); \
  137. __typeof((_val)->value) _r; \
  138. if(sizeof((_val)->value) == 4) WRAP_SUB("l", _r, &(_val)->value, _decr); \
  139. else if(sizeof((_val)->value) == 8) WRAP_SUB("q", _r, &(_val)->value, _decr); \
  140. _r; \
  141. })
  142. #define ATOMIC_EXCHANGE(_val, _newval, _MO) __extension__({ \
  143. __typeof((_val)->value) _r; \
  144. if(sizeof((_val)->value) == 4) WRAP_XCHG("l", _r, &(_val)->value, (_newval)); \
  145. else if(sizeof((_val)->value) == 8) WRAP_XCHG("q", _r, &(_val)->value, (_newval)); \
  146. _r; \
  147. })
  148. #define ATOMIC_COMPARE_EXCHANGE_STRONG(_val, _oldval, _newval, _MO1, _MO2) __extension__({ \
  149. __typeof(*(_oldval)) _old = *(_oldval); \
  150. if(sizeof((_val)->value) == 4) WRAP_CMPXCHG("l", *(_oldval), &(_val)->value, _old, (_newval)); \
  151. else if(sizeof((_val)->value) == 8) WRAP_CMPXCHG("q", *(_oldval), &(_val)->value, _old, (_newval)); \
  152. *(_oldval) == _old; \
  153. })
  154. #define ATOMIC_EXCHANGE_PTR(_val, _newval, _MO) __extension__({ \
  155. void *_r; \
  156. if(sizeof(void*) == 4) WRAP_XCHG("l", _r, &(_val)->value, (_newval)); \
  157. else if(sizeof(void*) == 8) WRAP_XCHG("q", _r, &(_val)->value, (_newval));\
  158. _r; \
  159. })
  160. #define ATOMIC_COMPARE_EXCHANGE_PTR_STRONG(_val, _oldval, _newval, _MO1, _MO2) __extension__({ \
  161. void *_old = *(_oldval); \
  162. if(sizeof(void*) == 4) WRAP_CMPXCHG("l", *(_oldval), &(_val)->value, _old, (_newval)); \
  163. else if(sizeof(void*) == 8) WRAP_CMPXCHG("q", *(_oldval), &(_val)->value, _old, (_newval)); \
  164. *(_oldval) == _old; \
  165. })
  166. #define ATOMIC_THREAD_FENCE(order) do { \
  167. enum { must_be_constant = (order) }; \
  168. const int _o = must_be_constant; \
  169. if(_o > almemory_order_relaxed) \
  170. __asm__ __volatile__("" ::: "memory"); \
  171. } while(0)
  172. /* Atomics using Windows methods */
  173. #elif defined(_WIN32)
  174. #define WIN32_LEAN_AND_MEAN
  175. #include <windows.h>
  176. /* NOTE: This mess is *extremely* touchy. It lacks quite a bit of safety
  177. * checking due to the lack of multi-statement expressions, typeof(), and C99
  178. * compound literals. It is incapable of properly exchanging floats, which get
  179. * casted to LONG/int, and could cast away potential warnings.
  180. *
  181. * Unfortunately, it's the only semi-safe way that doesn't rely on C99 (because
  182. * MSVC).
  183. */
  184. inline LONG AtomicAdd32(volatile LONG *dest, LONG incr)
  185. {
  186. return InterlockedExchangeAdd(dest, incr);
  187. }
  188. inline LONGLONG AtomicAdd64(volatile LONGLONG *dest, LONGLONG incr)
  189. {
  190. return InterlockedExchangeAdd64(dest, incr);
  191. }
  192. inline LONG AtomicSub32(volatile LONG *dest, LONG decr)
  193. {
  194. return InterlockedExchangeAdd(dest, -decr);
  195. }
  196. inline LONGLONG AtomicSub64(volatile LONGLONG *dest, LONGLONG decr)
  197. {
  198. return InterlockedExchangeAdd64(dest, -decr);
  199. }
  200. inline LONG AtomicSwap32(volatile LONG *dest, LONG newval)
  201. {
  202. return InterlockedExchange(dest, newval);
  203. }
  204. inline LONGLONG AtomicSwap64(volatile LONGLONG *dest, LONGLONG newval)
  205. {
  206. return InterlockedExchange64(dest, newval);
  207. }
  208. inline void *AtomicSwapPtr(void *volatile *dest, void *newval)
  209. {
  210. return InterlockedExchangePointer(dest, newval);
  211. }
  212. inline bool CompareAndSwap32(volatile LONG *dest, LONG newval, LONG *oldval)
  213. {
  214. LONG old = *oldval;
  215. *oldval = InterlockedCompareExchange(dest, newval, *oldval);
  216. return old == *oldval;
  217. }
  218. inline bool CompareAndSwap64(volatile LONGLONG *dest, LONGLONG newval, LONGLONG *oldval)
  219. {
  220. LONGLONG old = *oldval;
  221. *oldval = InterlockedCompareExchange64(dest, newval, *oldval);
  222. return old == *oldval;
  223. }
  224. inline bool CompareAndSwapPtr(void *volatile *dest, void *newval, void **oldval)
  225. {
  226. void *old = *oldval;
  227. *oldval = InterlockedCompareExchangePointer(dest, newval, *oldval);
  228. return old == *oldval;
  229. }
  230. #define WRAP_ADDSUB(T, _func, _ptr, _amnt) _func((T volatile*)(_ptr), (_amnt))
  231. #define WRAP_XCHG(T, _func, _ptr, _newval) _func((T volatile*)(_ptr), (_newval))
  232. #define WRAP_CMPXCHG(T, _func, _ptr, _newval, _oldval) _func((T volatile*)(_ptr), (_newval), (T*)(_oldval))
  233. enum almemory_order {
  234. almemory_order_relaxed,
  235. almemory_order_consume,
  236. almemory_order_acquire,
  237. almemory_order_release,
  238. almemory_order_acq_rel,
  239. almemory_order_seq_cst
  240. };
  241. #define ATOMIC(T) struct { T volatile value; }
  242. #define ATOMIC_INIT(_val, _newval) do { (_val)->value = (_newval); } while(0)
  243. #define ATOMIC_INIT_STATIC(_newval) {(_newval)}
  244. #define ATOMIC_LOAD(_val, _MO) ((_val)->value)
  245. #define ATOMIC_STORE(_val, _newval, _MO) do { \
  246. (_val)->value = (_newval); \
  247. } while(0)
  248. int _al_invalid_atomic_size(); /* not defined */
  249. #define ATOMIC_ADD(_val, _incr, _MO) \
  250. ((sizeof((_val)->value)==4) ? WRAP_ADDSUB(LONG, AtomicAdd32, &(_val)->value, (_incr)) : \
  251. (sizeof((_val)->value)==8) ? WRAP_ADDSUB(LONGLONG, AtomicAdd64, &(_val)->value, (_incr)) : \
  252. _al_invalid_atomic_size())
  253. #define ATOMIC_SUB(_val, _decr, _MO) \
  254. ((sizeof((_val)->value)==4) ? WRAP_ADDSUB(LONG, AtomicSub32, &(_val)->value, (_decr)) : \
  255. (sizeof((_val)->value)==8) ? WRAP_ADDSUB(LONGLONG, AtomicSub64, &(_val)->value, (_decr)) : \
  256. _al_invalid_atomic_size())
  257. #define ATOMIC_EXCHANGE(_val, _newval, _MO) \
  258. ((sizeof((_val)->value)==4) ? WRAP_XCHG(LONG, AtomicSwap32, &(_val)->value, (_newval)) : \
  259. (sizeof((_val)->value)==8) ? WRAP_XCHG(LONGLONG, AtomicSwap64, &(_val)->value, (_newval)) : \
  260. (LONG)_al_invalid_atomic_size())
  261. #define ATOMIC_COMPARE_EXCHANGE_STRONG(_val, _oldval, _newval, _MO1, _MO2) \
  262. ((sizeof((_val)->value)==4) ? WRAP_CMPXCHG(LONG, CompareAndSwap32, &(_val)->value, (_newval), (_oldval)) : \
  263. (sizeof((_val)->value)==8) ? WRAP_CMPXCHG(LONGLONG, CompareAndSwap64, &(_val)->value, (_newval), (_oldval)) : \
  264. (bool)_al_invalid_atomic_size())
  265. #define ATOMIC_EXCHANGE_PTR(_val, _newval, _MO) \
  266. ((sizeof((_val)->value)==sizeof(void*)) ? AtomicSwapPtr((void*volatile*)&(_val)->value, (_newval)) : \
  267. (void*)_al_invalid_atomic_size())
  268. #define ATOMIC_COMPARE_EXCHANGE_PTR_STRONG(_val, _oldval, _newval, _MO1, _MO2)\
  269. ((sizeof((_val)->value)==sizeof(void*)) ? CompareAndSwapPtr((void*volatile*)&(_val)->value, (_newval), (void**)(_oldval)) : \
  270. (bool)_al_invalid_atomic_size())
  271. #define ATOMIC_THREAD_FENCE(order) do { \
  272. enum { must_be_constant = (order) }; \
  273. const int _o = must_be_constant; \
  274. if(_o > almemory_order_relaxed) \
  275. _ReadWriteBarrier(); \
  276. } while(0)
  277. #else
  278. #error "No atomic functions available on this platform!"
  279. #define ATOMIC(T) T
  280. #define ATOMIC_INIT(_val, _newval) ((void)0)
  281. #define ATOMIC_INIT_STATIC(_newval) (0)
  282. #define ATOMIC_LOAD(...) (0)
  283. #define ATOMIC_STORE(...) ((void)0)
  284. #define ATOMIC_ADD(...) (0)
  285. #define ATOMIC_SUB(...) (0)
  286. #define ATOMIC_EXCHANGE(...) (0)
  287. #define ATOMIC_COMPARE_EXCHANGE_STRONG(...) (0)
  288. #define ATOMIC_THREAD_FENCE(...) ((void)0)
  289. #endif
  290. /* If no PTR xchg variants are provided, the normal ones can handle it. */
  291. #ifndef ATOMIC_EXCHANGE_PTR
  292. #define ATOMIC_EXCHANGE_PTR ATOMIC_EXCHANGE
  293. #define ATOMIC_COMPARE_EXCHANGE_PTR_STRONG ATOMIC_COMPARE_EXCHANGE_STRONG
  294. #define ATOMIC_COMPARE_EXCHANGE_PTR_WEAK ATOMIC_COMPARE_EXCHANGE_WEAK
  295. #endif
  296. /* If no weak cmpxchg is provided (not all systems will have one), substitute a
  297. * strong cmpxchg. */
  298. #ifndef ATOMIC_COMPARE_EXCHANGE_WEAK
  299. #define ATOMIC_COMPARE_EXCHANGE_WEAK ATOMIC_COMPARE_EXCHANGE_STRONG
  300. #endif
  301. #ifndef ATOMIC_COMPARE_EXCHANGE_PTR_WEAK
  302. #define ATOMIC_COMPARE_EXCHANGE_PTR_WEAK ATOMIC_COMPARE_EXCHANGE_PTR_STRONG
  303. #endif
  304. /* If no ATOMIC_FLAG is defined, simulate one with an atomic int using exchange
  305. * and store ops.
  306. */
  307. #ifndef ATOMIC_FLAG
  308. #define ATOMIC_FLAG ATOMIC(int)
  309. #define ATOMIC_FLAG_INIT ATOMIC_INIT_STATIC(0)
  310. #define ATOMIC_FLAG_TEST_AND_SET(_val, _MO) ATOMIC_EXCHANGE(_val, 1, _MO)
  311. #define ATOMIC_FLAG_CLEAR(_val, _MO) ATOMIC_STORE(_val, 0, _MO)
  312. #endif
  313. #define ATOMIC_LOAD_SEQ(_val) ATOMIC_LOAD(_val, almemory_order_seq_cst)
  314. #define ATOMIC_STORE_SEQ(_val, _newval) ATOMIC_STORE(_val, _newval, almemory_order_seq_cst)
  315. #define ATOMIC_ADD_SEQ(_val, _incr) ATOMIC_ADD(_val, _incr, almemory_order_seq_cst)
  316. #define ATOMIC_SUB_SEQ(_val, _decr) ATOMIC_SUB(_val, _decr, almemory_order_seq_cst)
  317. #define ATOMIC_EXCHANGE_SEQ(_val, _newval) ATOMIC_EXCHANGE(_val, _newval, almemory_order_seq_cst)
  318. #define ATOMIC_COMPARE_EXCHANGE_STRONG_SEQ(_val, _oldval, _newval) \
  319. ATOMIC_COMPARE_EXCHANGE_STRONG(_val, _oldval, _newval, almemory_order_seq_cst, almemory_order_seq_cst)
  320. #define ATOMIC_COMPARE_EXCHANGE_WEAK_SEQ(_val, _oldval, _newval) \
  321. ATOMIC_COMPARE_EXCHANGE_WEAK(_val, _oldval, _newval, almemory_order_seq_cst, almemory_order_seq_cst)
  322. #define ATOMIC_EXCHANGE_PTR_SEQ(_val, _newval) ATOMIC_EXCHANGE_PTR(_val, _newval, almemory_order_seq_cst)
  323. #define ATOMIC_COMPARE_EXCHANGE_PTR_STRONG_SEQ(_val, _oldval, _newval) \
  324. ATOMIC_COMPARE_EXCHANGE_PTR_STRONG(_val, _oldval, _newval, almemory_order_seq_cst, almemory_order_seq_cst)
  325. #define ATOMIC_COMPARE_EXCHANGE_PTR_WEAK_SEQ(_val, _oldval, _newval) \
  326. ATOMIC_COMPARE_EXCHANGE_PTR_WEAK(_val, _oldval, _newval, almemory_order_seq_cst, almemory_order_seq_cst)
  327. typedef unsigned int uint;
  328. typedef ATOMIC(uint) RefCount;
  329. inline void InitRef(RefCount *ptr, uint value)
  330. { ATOMIC_INIT(ptr, value); }
  331. inline uint ReadRef(RefCount *ptr)
  332. { return ATOMIC_LOAD_SEQ(ptr); }
  333. inline uint IncrementRef(RefCount *ptr)
  334. { return ATOMIC_ADD_SEQ(ptr, 1)+1; }
  335. inline uint DecrementRef(RefCount *ptr)
  336. { return ATOMIC_SUB_SEQ(ptr, 1)-1; }
  337. /* WARNING: A livelock is theoretically possible if another thread keeps
  338. * changing the head without giving this a chance to actually swap in the new
  339. * one (practically impossible with this little code, but...).
  340. */
  341. #define ATOMIC_REPLACE_HEAD(T, _head, _entry) do { \
  342. T _first = ATOMIC_LOAD(_head, almemory_order_acquire); \
  343. do { \
  344. ATOMIC_STORE(&(_entry)->next, _first, almemory_order_relaxed); \
  345. } while(ATOMIC_COMPARE_EXCHANGE_PTR_WEAK(_head, &_first, _entry, \
  346. almemory_order_acq_rel, almemory_order_acquire) == 0); \
  347. } while(0)
  348. #ifdef __cplusplus
  349. }
  350. #endif
  351. #endif /* AL_ATOMIC_H */