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