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