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