Unified · Split
| @@ -0,0 +1,121 @@ | ||
| 1 | +#include <stddef.h> | |
| 2 | +#include <stdint.h> | |
| 3 | + | |
| 4 | +#ifdef __cplusplus | |
| 5 | +extern "C" { | |
| 6 | +#endif | |
| 7 | + | |
| 8 | +/* Memory ordering */ | |
| 9 | +typedef enum { | |
| 10 | + memory_order_relaxed = __ATOMIC_RELAXED, | |
| 11 | + memory_order_consume = __ATOMIC_CONSUME, | |
| 12 | + memory_order_acquire = __ATOMIC_ACQUIRE, | |
| 13 | + memory_order_release = __ATOMIC_RELEASE, | |
| 14 | + memory_order_acq_rel = __ATOMIC_ACQ_REL, | |
| 15 | + memory_order_seq_cst = __ATOMIC_SEQ_CST, | |
| 16 | +} memory_order; | |
| 17 | + | |
| 18 | +/* Atomic typedefs */ | |
| 19 | +#ifdef __cplusplus | |
| 20 | +typedef _Atomic(bool) atomic_bool; | |
| 21 | +#else | |
| 22 | +typedef _Atomic(_Bool) atomic_bool; | |
| 23 | +#endif | |
| 24 | +typedef _Atomic(char) atomic_char; | |
| 25 | +typedef _Atomic(signed char) atomic_schar; | |
| 26 | +typedef _Atomic(unsigned char) atomic_uchar; | |
| 27 | +typedef _Atomic(short) atomic_short; | |
| 28 | +typedef _Atomic(unsigned short) atomic_ushort; | |
| 29 | +typedef _Atomic(int) atomic_int; | |
| 30 | +typedef _Atomic(unsigned int) atomic_uint; | |
| 31 | +typedef _Atomic(long) atomic_long; | |
| 32 | +typedef _Atomic(unsigned long) atomic_ulong; | |
| 33 | +typedef _Atomic(long long) atomic_llong; | |
| 34 | +typedef _Atomic(unsigned long long) atomic_ullong; | |
| 35 | +typedef _Atomic(uint_least16_t) atomic_char16_t; | |
| 36 | +typedef _Atomic(uint_least32_t) atomic_char32_t; | |
| 37 | +typedef _Atomic(wchar_t) atomic_wchar_t; | |
| 38 | +typedef _Atomic(int_least8_t) atomic_int_least8_t; | |
| 39 | +typedef _Atomic(uint_least8_t) atomic_uint_least8_t; | |
| 40 | +typedef _Atomic(int_least16_t) atomic_int_least16_t; | |
| 41 | +typedef _Atomic(uint_least16_t) atomic_uint_least16_t; | |
| 42 | +typedef _Atomic(int_least32_t) atomic_int_least32_t; | |
| 43 | +typedef _Atomic(uint_least32_t) atomic_uint_least32_t; | |
| 44 | +typedef _Atomic(int_least64_t) atomic_int_least64_t; | |
| 45 | +typedef _Atomic(uint_least64_t) atomic_uint_least64_t; | |
| 46 | +typedef _Atomic(int_fast8_t) atomic_int_fast8_t; | |
| 47 | +typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t; | |
| 48 | +typedef _Atomic(int_fast16_t) atomic_int_fast16_t; | |
| 49 | +typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t; | |
| 50 | +typedef _Atomic(int_fast32_t) atomic_int_fast32_t; | |
| 51 | +typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t; | |
| 52 | +typedef _Atomic(int_fast64_t) atomic_int_fast64_t; | |
| 53 | +typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t; | |
| 54 | +typedef _Atomic(intptr_t) atomic_intptr_t; | |
| 55 | +typedef _Atomic(uintptr_t) atomic_uintptr_t; | |
| 56 | +typedef _Atomic(size_t) atomic_size_t; | |
| 57 | +typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t; | |
| 58 | +typedef _Atomic(intmax_t) atomic_intmax_t; | |
| 59 | +typedef _Atomic(uintmax_t) atomic_uintmax_t; | |
| 60 | + | |
| 61 | +/* Atomic flag */ | |
| 62 | +typedef struct { | |
| 63 | + atomic_bool value; | |
| 64 | +} atomic_flag; | |
| 65 | + | |
| 66 | +#define ATOMIC_FLAG_INIT { 0 } | |
| 67 | + | |
| 68 | +#define atomic_flag_test_and_set(object) \ | |
| 69 | + __atomic_exchange(&(object)->value, 1, __ATOMIC_SEQ_CST) | |
| 70 | +#define atomic_flag_test_and_set_explicit(object, order) \ | |
| 71 | + __atomic_exchange(&(object)->value, 1, order) | |
| 72 | + | |
| 73 | +#define atomic_flag_clear(object) \ | |
| 74 | + __atomic_store(&(object)->value, 0, __ATOMIC_SEQ_CST) | |
| 75 | +#define atomic_flag_clear_explicit(object, order) \ | |
| 76 | + __atomic_store(&(object)->value, 0, order) | |
| 77 | + | |
| 78 | +/* Generic routines */ | |
| 79 | +#define atomic_store(object, desired) \ | |
| 80 | + __atomic_store(object, desired, __ATOMIC_SEQ_CST) | |
| 81 | +#define atomic_store_explicit __atomic_store | |
| 82 | + | |
| 83 | +#define atomic_load(object) \ | |
| 84 | + __atomic_load(object, __ATOMIC_SEQ_CST) | |
| 85 | +#define atomic_load_explicit __atomic_load | |
| 86 | + | |
| 87 | +#define atomic_exchange(object, desired) \ | |
| 88 | + __atomic_exchange(object, desired, __ATOMIC_SEQ_CST) | |
| 89 | +#define atomic_exchange_explicit __atomic_exchange | |
| 90 | + | |
| 91 | +#define atomic_compare_exchange_strong(object, expected, desired) \ | |
| 92 | + __atomic_compare_exchange_strong(object, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) | |
| 93 | +#define atomic_compare_exchange_strong_explicit __atomic_compare_exchange_strong | |
| 94 | + | |
| 95 | +#define atomic_compare_exchange_weak(object, expected, desired) \ | |
| 96 | + __atomic_compare_exchange_weak(object, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) | |
| 97 | +#define atomic_compare_exchange_weak_explicit __atomic_compare_exchange_weak | |
| 98 | + | |
| 99 | +#define atomic_fetch_add(object, operand) \ | |
| 100 | + __atomic_fetch_add(object, operand, __ATOMIC_SEQ_CST) | |
| 101 | +#define atomic_fetch_add_explicit __atomic_fetch_add | |
| 102 | + | |
| 103 | +#define atomic_fetch_sub(object, operand) \ | |
| 104 | + __atomic_fetch_sub(object, operand, __ATOMIC_SEQ_CST) | |
| 105 | +#define atomic_fetch_sub_explicit __atomic_fetch_sub | |
| 106 | + | |
| 107 | +#define atomic_fetch_or(object, operand) \ | |
| 108 | + __atomic_fetch_or(object, operand, __ATOMIC_SEQ_CST) | |
| 109 | +#define atomic_fetch_or_explicit __atomic_fetch_or | |
| 110 | + | |
| 111 | +#define atomic_fetch_xor(object, operand) \ | |
| 112 | + __atomic_fetch_xor(object, operand, __ATOMIC_SEQ_CST) | |
| 113 | +#define atomic_fetch_xor_explicit __atomic_fetch_xor | |
| 114 | + | |
| 115 | +#define atomic_fetch_and(object, operand) \ | |
| 116 | + __atomic_fetch_and(object, operand, __ATOMIC_SEQ_CST) | |
| 117 | +#define atomic_fetch_and_explicit __atomic_fetch_and | |
| 118 | + | |
| 119 | +#ifdef __cplusplus | |
| 120 | +} /* extern "C" */ | |
| 121 | +#endif | |