atomic.h 18 KB

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