1*42e22086SApple OSS Distributions /*===---- stdatomic.h - Standard header for atomic types and operations -----=== 2*42e22086SApple OSS Distributions * 3*42e22086SApple OSS Distributions * Permission is hereby granted, free of charge, to any person obtaining a copy 4*42e22086SApple OSS Distributions * of this software and associated documentation files (the "Software"), to deal 5*42e22086SApple OSS Distributions * in the Software without restriction, including without limitation the rights 6*42e22086SApple OSS Distributions * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7*42e22086SApple OSS Distributions * copies of the Software, and to permit persons to whom the Software is 8*42e22086SApple OSS Distributions * furnished to do so, subject to the following conditions: 9*42e22086SApple OSS Distributions * 10*42e22086SApple OSS Distributions * The above copyright notice and this permission notice shall be included in 11*42e22086SApple OSS Distributions * all copies or substantial portions of the Software. 12*42e22086SApple OSS Distributions * 13*42e22086SApple OSS Distributions * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14*42e22086SApple OSS Distributions * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15*42e22086SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16*42e22086SApple OSS Distributions * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17*42e22086SApple OSS Distributions * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18*42e22086SApple OSS Distributions * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19*42e22086SApple OSS Distributions * THE SOFTWARE. 20*42e22086SApple OSS Distributions * 21*42e22086SApple OSS Distributions *===-----------------------------------------------------------------------=== 22*42e22086SApple OSS Distributions */ 23*42e22086SApple OSS Distributions 24*42e22086SApple OSS Distributions #ifndef __clang__ 25*42e22086SApple OSS Distributions #error unsupported compiler 26*42e22086SApple OSS Distributions #endif 27*42e22086SApple OSS Distributions 28*42e22086SApple OSS Distributions #ifndef __CLANG_STDATOMIC_H 29*42e22086SApple OSS Distributions #define __CLANG_STDATOMIC_H 30*42e22086SApple OSS Distributions 31*42e22086SApple OSS Distributions /* If we're hosted, fall back to the system's stdatomic.h. FreeBSD, for 32*42e22086SApple OSS Distributions * example, already has a Clang-compatible stdatomic.h header. 33*42e22086SApple OSS Distributions */ 34*42e22086SApple OSS Distributions #if __STDC_HOSTED__ && __has_include_next(<stdatomic.h>) 35*42e22086SApple OSS Distributions # include_next <stdatomic.h> 36*42e22086SApple OSS Distributions #else 37*42e22086SApple OSS Distributions 38*42e22086SApple OSS Distributions #include <stddef.h> 39*42e22086SApple OSS Distributions #include <stdint.h> 40*42e22086SApple OSS Distributions 41*42e22086SApple OSS Distributions #ifdef __cplusplus 42*42e22086SApple OSS Distributions extern "C" { 43*42e22086SApple OSS Distributions #endif 44*42e22086SApple OSS Distributions 45*42e22086SApple OSS Distributions /* 7.17.1 Introduction */ 46*42e22086SApple OSS Distributions 47*42e22086SApple OSS Distributions #define ATOMIC_BOOL_LOCK_FREE __CLANG_ATOMIC_BOOL_LOCK_FREE 48*42e22086SApple OSS Distributions #define ATOMIC_CHAR_LOCK_FREE __CLANG_ATOMIC_CHAR_LOCK_FREE 49*42e22086SApple OSS Distributions #define ATOMIC_CHAR16_T_LOCK_FREE __CLANG_ATOMIC_CHAR16_T_LOCK_FREE 50*42e22086SApple OSS Distributions #define ATOMIC_CHAR32_T_LOCK_FREE __CLANG_ATOMIC_CHAR32_T_LOCK_FREE 51*42e22086SApple OSS Distributions #define ATOMIC_WCHAR_T_LOCK_FREE __CLANG_ATOMIC_WCHAR_T_LOCK_FREE 52*42e22086SApple OSS Distributions #define ATOMIC_SHORT_LOCK_FREE __CLANG_ATOMIC_SHORT_LOCK_FREE 53*42e22086SApple OSS Distributions #define ATOMIC_INT_LOCK_FREE __CLANG_ATOMIC_INT_LOCK_FREE 54*42e22086SApple OSS Distributions #define ATOMIC_LONG_LOCK_FREE __CLANG_ATOMIC_LONG_LOCK_FREE 55*42e22086SApple OSS Distributions #define ATOMIC_LLONG_LOCK_FREE __CLANG_ATOMIC_LLONG_LOCK_FREE 56*42e22086SApple OSS Distributions #define ATOMIC_POINTER_LOCK_FREE __CLANG_ATOMIC_POINTER_LOCK_FREE 57*42e22086SApple OSS Distributions 58*42e22086SApple OSS Distributions /* 7.17.2 Initialization */ 59*42e22086SApple OSS Distributions 60*42e22086SApple OSS Distributions #define ATOMIC_VAR_INIT(value) (value) 61*42e22086SApple OSS Distributions #define atomic_init __c11_atomic_init 62*42e22086SApple OSS Distributions 63*42e22086SApple OSS Distributions /* 7.17.3 Order and consistency */ 64*42e22086SApple OSS Distributions 65*42e22086SApple OSS Distributions typedef enum memory_order { 66*42e22086SApple OSS Distributions memory_order_relaxed = __ATOMIC_RELAXED, 67*42e22086SApple OSS Distributions memory_order_consume = __ATOMIC_CONSUME, 68*42e22086SApple OSS Distributions memory_order_acquire = __ATOMIC_ACQUIRE, 69*42e22086SApple OSS Distributions memory_order_release = __ATOMIC_RELEASE, 70*42e22086SApple OSS Distributions memory_order_acq_rel = __ATOMIC_ACQ_REL, 71*42e22086SApple OSS Distributions memory_order_seq_cst = __ATOMIC_SEQ_CST 72*42e22086SApple OSS Distributions } memory_order; 73*42e22086SApple OSS Distributions 74*42e22086SApple OSS Distributions #define kill_dependency(y) (y) 75*42e22086SApple OSS Distributions 76*42e22086SApple OSS Distributions /* 7.17.4 Fences */ 77*42e22086SApple OSS Distributions 78*42e22086SApple OSS Distributions #ifndef KERNEL 79*42e22086SApple OSS Distributions /* These should be provided by the libc implementation. */ 80*42e22086SApple OSS Distributions void atomic_thread_fence(memory_order); 81*42e22086SApple OSS Distributions void atomic_signal_fence(memory_order); 82*42e22086SApple OSS Distributions #endif 83*42e22086SApple OSS Distributions 84*42e22086SApple OSS Distributions #define atomic_thread_fence(order) __c11_atomic_thread_fence(order) 85*42e22086SApple OSS Distributions #define atomic_signal_fence(order) __c11_atomic_signal_fence(order) 86*42e22086SApple OSS Distributions 87*42e22086SApple OSS Distributions /* 7.17.5 Lock-free property */ 88*42e22086SApple OSS Distributions 89*42e22086SApple OSS Distributions #define atomic_is_lock_free(obj) __c11_atomic_is_lock_free(sizeof(*(obj))) 90*42e22086SApple OSS Distributions 91*42e22086SApple OSS Distributions /* 7.17.6 Atomic integer types */ 92*42e22086SApple OSS Distributions 93*42e22086SApple OSS Distributions #ifdef __cplusplus 94*42e22086SApple OSS Distributions typedef _Atomic(bool) atomic_bool; 95*42e22086SApple OSS Distributions #else 96*42e22086SApple OSS Distributions typedef _Atomic(_Bool) atomic_bool; 97*42e22086SApple OSS Distributions #endif 98*42e22086SApple OSS Distributions typedef _Atomic(char) atomic_char; 99*42e22086SApple OSS Distributions typedef _Atomic(signed char) atomic_schar; 100*42e22086SApple OSS Distributions typedef _Atomic(unsigned char) atomic_uchar; 101*42e22086SApple OSS Distributions typedef _Atomic(short) atomic_short; 102*42e22086SApple OSS Distributions typedef _Atomic(unsigned short) atomic_ushort; 103*42e22086SApple OSS Distributions typedef _Atomic(int) atomic_int; 104*42e22086SApple OSS Distributions typedef _Atomic(unsigned int) atomic_uint; 105*42e22086SApple OSS Distributions typedef _Atomic(long) atomic_long; 106*42e22086SApple OSS Distributions typedef _Atomic(unsigned long) atomic_ulong; 107*42e22086SApple OSS Distributions typedef _Atomic(long long) atomic_llong; 108*42e22086SApple OSS Distributions typedef _Atomic(unsigned long long) atomic_ullong; 109*42e22086SApple OSS Distributions typedef _Atomic(uint_least16_t) atomic_char16_t; 110*42e22086SApple OSS Distributions typedef _Atomic(uint_least32_t) atomic_char32_t; 111*42e22086SApple OSS Distributions typedef _Atomic(wchar_t) atomic_wchar_t; 112*42e22086SApple OSS Distributions typedef _Atomic(int_least8_t) atomic_int_least8_t; 113*42e22086SApple OSS Distributions typedef _Atomic(uint_least8_t) atomic_uint_least8_t; 114*42e22086SApple OSS Distributions typedef _Atomic(int_least16_t) atomic_int_least16_t; 115*42e22086SApple OSS Distributions typedef _Atomic(uint_least16_t) atomic_uint_least16_t; 116*42e22086SApple OSS Distributions typedef _Atomic(int_least32_t) atomic_int_least32_t; 117*42e22086SApple OSS Distributions typedef _Atomic(uint_least32_t) atomic_uint_least32_t; 118*42e22086SApple OSS Distributions typedef _Atomic(int_least64_t) atomic_int_least64_t; 119*42e22086SApple OSS Distributions typedef _Atomic(uint_least64_t) atomic_uint_least64_t; 120*42e22086SApple OSS Distributions typedef _Atomic(int_fast8_t) atomic_int_fast8_t; 121*42e22086SApple OSS Distributions typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t; 122*42e22086SApple OSS Distributions typedef _Atomic(int_fast16_t) atomic_int_fast16_t; 123*42e22086SApple OSS Distributions typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t; 124*42e22086SApple OSS Distributions typedef _Atomic(int_fast32_t) atomic_int_fast32_t; 125*42e22086SApple OSS Distributions typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t; 126*42e22086SApple OSS Distributions typedef _Atomic(int_fast64_t) atomic_int_fast64_t; 127*42e22086SApple OSS Distributions typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t; 128*42e22086SApple OSS Distributions typedef _Atomic(intptr_t) atomic_intptr_t; 129*42e22086SApple OSS Distributions typedef _Atomic(uintptr_t) atomic_uintptr_t; 130*42e22086SApple OSS Distributions typedef _Atomic(size_t) atomic_size_t; 131*42e22086SApple OSS Distributions typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t; 132*42e22086SApple OSS Distributions typedef _Atomic(intmax_t) atomic_intmax_t; 133*42e22086SApple OSS Distributions typedef _Atomic(uintmax_t) atomic_uintmax_t; 134*42e22086SApple OSS Distributions 135*42e22086SApple OSS Distributions /* 7.17.7 Operations on atomic types */ 136*42e22086SApple OSS Distributions 137*42e22086SApple OSS Distributions #define atomic_store(object, desired) __c11_atomic_store(object, desired, __ATOMIC_SEQ_CST) 138*42e22086SApple OSS Distributions #define atomic_store_explicit __c11_atomic_store 139*42e22086SApple OSS Distributions 140*42e22086SApple OSS Distributions #define atomic_load(object) __c11_atomic_load(object, __ATOMIC_SEQ_CST) 141*42e22086SApple OSS Distributions #define atomic_load_explicit __c11_atomic_load 142*42e22086SApple OSS Distributions 143*42e22086SApple OSS Distributions #define atomic_exchange(object, desired) __c11_atomic_exchange(object, desired, __ATOMIC_SEQ_CST) 144*42e22086SApple OSS Distributions #define atomic_exchange_explicit __c11_atomic_exchange 145*42e22086SApple OSS Distributions 146*42e22086SApple OSS Distributions #define atomic_compare_exchange_strong(object, expected, desired) __c11_atomic_compare_exchange_strong(object, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) 147*42e22086SApple OSS Distributions #define atomic_compare_exchange_strong_explicit __c11_atomic_compare_exchange_strong 148*42e22086SApple OSS Distributions 149*42e22086SApple OSS Distributions #define atomic_compare_exchange_weak(object, expected, desired) __c11_atomic_compare_exchange_weak(object, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) 150*42e22086SApple OSS Distributions #define atomic_compare_exchange_weak_explicit __c11_atomic_compare_exchange_weak 151*42e22086SApple OSS Distributions 152*42e22086SApple OSS Distributions #define atomic_fetch_add(object, operand) __c11_atomic_fetch_add(object, operand, __ATOMIC_SEQ_CST) 153*42e22086SApple OSS Distributions #define atomic_fetch_add_explicit __c11_atomic_fetch_add 154*42e22086SApple OSS Distributions 155*42e22086SApple OSS Distributions #define atomic_fetch_sub(object, operand) __c11_atomic_fetch_sub(object, operand, __ATOMIC_SEQ_CST) 156*42e22086SApple OSS Distributions #define atomic_fetch_sub_explicit __c11_atomic_fetch_sub 157*42e22086SApple OSS Distributions 158*42e22086SApple OSS Distributions #define atomic_fetch_or(object, operand) __c11_atomic_fetch_or(object, operand, __ATOMIC_SEQ_CST) 159*42e22086SApple OSS Distributions #define atomic_fetch_or_explicit __c11_atomic_fetch_or 160*42e22086SApple OSS Distributions 161*42e22086SApple OSS Distributions #define atomic_fetch_xor(object, operand) __c11_atomic_fetch_xor(object, operand, __ATOMIC_SEQ_CST) 162*42e22086SApple OSS Distributions #define atomic_fetch_xor_explicit __c11_atomic_fetch_xor 163*42e22086SApple OSS Distributions 164*42e22086SApple OSS Distributions #define atomic_fetch_and(object, operand) __c11_atomic_fetch_and(object, operand, __ATOMIC_SEQ_CST) 165*42e22086SApple OSS Distributions #define atomic_fetch_and_explicit __c11_atomic_fetch_and 166*42e22086SApple OSS Distributions 167*42e22086SApple OSS Distributions /* 7.17.8 Atomic flag type and operations */ 168*42e22086SApple OSS Distributions 169*42e22086SApple OSS Distributions typedef struct atomic_flag { atomic_bool _Value; } atomic_flag; 170*42e22086SApple OSS Distributions 171*42e22086SApple OSS Distributions #define ATOMIC_FLAG_INIT { 0 } 172*42e22086SApple OSS Distributions 173*42e22086SApple OSS Distributions #ifndef KERNEL 174*42e22086SApple OSS Distributions /* These should be provided by the libc implementation. */ 175*42e22086SApple OSS Distributions #ifdef __cplusplus 176*42e22086SApple OSS Distributions bool atomic_flag_test_and_set(volatile atomic_flag *); 177*42e22086SApple OSS Distributions bool atomic_flag_test_and_set_explicit(volatile atomic_flag *, memory_order); 178*42e22086SApple OSS Distributions #else 179*42e22086SApple OSS Distributions _Bool atomic_flag_test_and_set(volatile atomic_flag *); 180*42e22086SApple OSS Distributions _Bool atomic_flag_test_and_set_explicit(volatile atomic_flag *, memory_order); 181*42e22086SApple OSS Distributions #endif 182*42e22086SApple OSS Distributions void atomic_flag_clear(volatile atomic_flag *); 183*42e22086SApple OSS Distributions void atomic_flag_clear_explicit(volatile atomic_flag *, memory_order); 184*42e22086SApple OSS Distributions #endif 185*42e22086SApple OSS Distributions 186*42e22086SApple OSS Distributions #define atomic_flag_test_and_set(object) __c11_atomic_exchange(&(object)->_Value, 1, __ATOMIC_SEQ_CST) 187*42e22086SApple OSS Distributions #define atomic_flag_test_and_set_explicit(object, order) __c11_atomic_exchange(&(object)->_Value, 1, order) 188*42e22086SApple OSS Distributions 189*42e22086SApple OSS Distributions #define atomic_flag_clear(object) __c11_atomic_store(&(object)->_Value, 0, __ATOMIC_SEQ_CST) 190*42e22086SApple OSS Distributions #define atomic_flag_clear_explicit(object, order) __c11_atomic_store(&(object)->_Value, 0, order) 191*42e22086SApple OSS Distributions 192*42e22086SApple OSS Distributions #ifdef __cplusplus 193*42e22086SApple OSS Distributions } 194*42e22086SApple OSS Distributions #endif 195*42e22086SApple OSS Distributions 196*42e22086SApple OSS Distributions #endif /* __STDC_HOSTED__ */ 197*42e22086SApple OSS Distributions #endif /* __CLANG_STDATOMIC_H */ 198*42e22086SApple OSS Distributions 199