1 /*-
2 * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
3 * David Chisnall <theraven@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD$
28 */
29
30 #ifndef _STDATOMIC_H_
31 #define _STDATOMIC_H_
32
33 #include <sys/cdefs.h>
34
35
36 #if defined(__cplusplus) && defined(_USING_LIBCXX)
37 # ifdef __clang__
38 # if __has_feature(cxx_atomic)
39 # define _STDATOMIC_HAVE_ATOMIC
40 # endif
41 # else /* gcc */
42 # if __GNUC_PREREQ(4, 7)
43 # define _STDATOMIC_HAVE_ATOMIC
44 # endif
45 # endif
46 #endif
47
48 #ifdef _STDATOMIC_HAVE_ATOMIC
49
50 /* We have a usable C++ <atomic>; use it instead. */
51
52 #include <atomic>
53
54 #undef _Atomic
55 /* Also defined by <atomic> for gcc. But not used in macros. */
56 /* Also a clang intrinsic. */
57 /* Should not be used by client code before this file is */
58 /* included. The definitions in <atomic> themselves see */
59 /* the old definition, as they should. */
60 /* Client code sees the following definition. */
61
62 #define _Atomic(t) std::atomic<t>
63
64 using std::atomic_is_lock_free;
65 using std::atomic_init;
66 using std::atomic_store;
67 using std::atomic_store_explicit;
68 using std::atomic_load;
69 using std::atomic_load_explicit;
70 using std::atomic_exchange;
71 using std::atomic_exchange_explicit;
72 using std::atomic_compare_exchange_strong;
73 using std::atomic_compare_exchange_strong_explicit;
74 using std::atomic_compare_exchange_weak;
75 using std::atomic_compare_exchange_weak_explicit;
76 using std::atomic_fetch_add;
77 using std::atomic_fetch_add_explicit;
78 using std::atomic_fetch_sub;
79 using std::atomic_fetch_sub_explicit;
80 using std::atomic_fetch_or;
81 using std::atomic_fetch_or_explicit;
82 using std::atomic_fetch_xor;
83 using std::atomic_fetch_xor_explicit;
84 using std::atomic_fetch_and;
85 using std::atomic_fetch_and_explicit;
86 using std::atomic_thread_fence;
87 using std::atomic_signal_fence;
88
89 using std::memory_order;
90 using std::memory_order_relaxed;
91 using std::memory_order_consume;
92 using std::memory_order_release;
93 using std::memory_order_acq_rel;
94 using std::memory_order_seq_cst;
95
96 using std::atomic_bool;
97 using std::atomic_char;
98 using std::atomic_schar;
99 using std::atomic_uchar;
100 using std::atomic_short;
101 using std::atomic_ushort;
102 using std::atomic_int;
103 using std::atomic_uint;
104 using std::atomic_long;
105 using std::atomic_ulong;
106 using std::atomic_llong;
107 using std::atomic_ullong;
108 using std::atomic_char16_t;
109 using std::atomic_char32_t;
110 using std::atomic_wchar_t;
111 using std::atomic_int_least8_t;
112 using std::atomic_uint_least8_t;
113 using std::atomic_int_least16_t;
114 using std::atomic_uint_least16_t;
115 using std::atomic_int_least32_t;
116 using std::atomic_uint_least32_t;
117 using std::atomic_int_least64_t;
118 using std::atomic_uint_least64_t;
119 using std::atomic_int_fast8_t;
120 using std::atomic_uint_fast8_t;
121 using std::atomic_int_fast16_t;
122 using std::atomic_uint_fast16_t;
123 using std::atomic_int_fast32_t;
124 using std::atomic_uint_fast32_t;
125 using std::atomic_int_fast64_t;
126 using std::atomic_uint_fast64_t;
127 using std::atomic_intptr_t;
128 using std::atomic_uintptr_t;
129 using std::atomic_size_t;
130 using std::atomic_ptrdiff_t;
131 using std::atomic_intmax_t;
132 using std::atomic_uintmax_t;
133
134 #else /* <atomic> unavailable, possibly because this is C, not C++ */
135
136 #include <sys/types.h>
137 #include <stdbool.h>
138
139 /*
140 * C: Do it ourselves.
141 * Note that the runtime representation defined here should be compatible
142 * with the C++ one, i.e. an _Atomic(T) needs to contain the same
143 * bits as a T.
144 */
145
146 #include <stddef.h> /* For ptrdiff_t. */
147 #include <stdint.h> /* TODO: Should pollute namespace less. */
148 #if __STDC_VERSION__ >= 201112L
149 # include <uchar.h> /* For char16_t and char32_t. */
150 #endif
151
152 #ifdef __clang__
153 # if __has_extension(c_atomic) || __has_extension(cxx_atomic)
154 # define __CLANG_ATOMICS
155 # else
156 # error "stdatomic.h does not support your compiler"
157 # endif
158 # if __has_builtin(__sync_swap)
159 # define __HAS_BUILTIN_SYNC_SWAP
160 # endif
161 #else
162 # if __GNUC_PREREQ(4, 7)
163 # define __GNUC_ATOMICS
164 # else
165 # define __SYNC_ATOMICS
166 # ifdef __cplusplus
167 # define __ATOMICS_AVOID_DOT_INIT
168 # endif
169 # endif
170 #endif
171
172 /*
173 * 7.17.1 Atomic lock-free macros.
174 */
175
176 #ifdef __GCC_ATOMIC_BOOL_LOCK_FREE
177 #define ATOMIC_BOOL_LOCK_FREE __GCC_ATOMIC_BOOL_LOCK_FREE
178 #elif defined(__SYNC_ATOMICS)
179 #define ATOMIC_BOOL_LOCK_FREE 2 /* For all modern platforms */
180 #endif
181 #ifdef __GCC_ATOMIC_CHAR_LOCK_FREE
182 #define ATOMIC_CHAR_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE
183 #elif defined(__SYNC_ATOMICS)
184 #define ATOMIC_CHAR_LOCK_FREE 2
185 #endif
186 #ifdef __GCC_ATOMIC_CHAR16_T_LOCK_FREE
187 #define ATOMIC_CHAR16_T_LOCK_FREE __GCC_ATOMIC_CHAR16_T_LOCK_FREE
188 #elif defined(__SYNC_ATOMICS)
189 #define ATOMIC_CHAR16_T_LOCK_FREE 2
190 #endif
191 #ifdef __GCC_ATOMIC_CHAR32_T_LOCK_FREE
192 #define ATOMIC_CHAR32_T_LOCK_FREE __GCC_ATOMIC_CHAR32_T_LOCK_FREE
193 #elif defined(__SYNC_ATOMICS)
194 #define ATOMIC_CHAR32_T_LOCK_FREE 2
195 #endif
196 #ifdef __GCC_ATOMIC_WCHAR_T_LOCK_FREE
197 #define ATOMIC_WCHAR_T_LOCK_FREE __GCC_ATOMIC_WCHAR_T_LOCK_FREE
198 #elif defined(__SYNC_ATOMICS)
199 #define ATOMIC_WCHAR_T_LOCK_FREE 2
200 #endif
201 #ifdef __GCC_ATOMIC_SHORT_LOCK_FREE
202 #define ATOMIC_SHORT_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
203 #elif defined(__SYNC_ATOMICS)
204 #define ATOMIC_SHORT_LOCK_FREE 2
205 #endif
206 #ifdef __GCC_ATOMIC_INT_LOCK_FREE
207 #define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
208 #elif defined(__SYNC_ATOMICS)
209 #define ATOMIC_INT_LOCK_FREE 2
210 #endif
211 #ifdef __GCC_ATOMIC_LONG_LOCK_FREE
212 #define ATOMIC_LONG_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
213 #elif defined(__SYNC_ATOMICS)
214 #define ATOMIC_LONG_LOCK_FREE 2
215 #endif
216 #ifdef __GCC_ATOMIC_LLONG_LOCK_FREE
217 #define ATOMIC_LLONG_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
218 #elif defined(__SYNC_ATOMICS)
219 #define ATOMIC_LLONG_LOCK_FREE 1 /* maybe */
220 #endif
221 #ifdef __GCC_ATOMIC_POINTER_LOCK_FREE
222 #define ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE
223 #elif defined(__SYNC_ATOMICS)
224 #define ATOMIC_POINTER_LOCK_FREE 2
225 #endif
226
227 /*
228 * 7.17.2 Initialization.
229 */
230
231 #if defined(__CLANG_ATOMICS)
232 #define ATOMIC_VAR_INIT(value) (value)
233 #define atomic_init(obj, value) __c11_atomic_init(obj, value)
234 #else
235 #ifdef __ATOMICS_AVOID_DOT_INIT
236 #define ATOMIC_VAR_INIT(value) { value }
237 #else
238 #define ATOMIC_VAR_INIT(value) { .__val = (value) }
239 #endif
240 #define atomic_init(obj, value) ((void)((obj)->__val = (value)))
241 #endif
242
243 /*
244 * Clang and recent GCC both provide predefined macros for the memory
245 * orderings. If we are using a compiler that doesn't define them, use the
246 * clang values - these will be ignored in the fallback path.
247 */
248
249 #ifndef __ATOMIC_RELAXED
250 #define __ATOMIC_RELAXED 0
251 #endif
252 #ifndef __ATOMIC_CONSUME
253 #define __ATOMIC_CONSUME 1
254 #endif
255 #ifndef __ATOMIC_ACQUIRE
256 #define __ATOMIC_ACQUIRE 2
257 #endif
258 #ifndef __ATOMIC_RELEASE
259 #define __ATOMIC_RELEASE 3
260 #endif
261 #ifndef __ATOMIC_ACQ_REL
262 #define __ATOMIC_ACQ_REL 4
263 #endif
264 #ifndef __ATOMIC_SEQ_CST
265 #define __ATOMIC_SEQ_CST 5
266 #endif
267
268 /*
269 * 7.17.3 Order and consistency.
270 *
271 * The memory_order_* constants that denote the barrier behaviour of the
272 * atomic operations.
273 * The enum values must be identical to those used by the
274 * C++ <atomic> header.
275 */
276
277 typedef enum {
278 memory_order_relaxed = __ATOMIC_RELAXED,
279 memory_order_consume = __ATOMIC_CONSUME,
280 memory_order_acquire = __ATOMIC_ACQUIRE,
281 memory_order_release = __ATOMIC_RELEASE,
282 memory_order_acq_rel = __ATOMIC_ACQ_REL,
283 memory_order_seq_cst = __ATOMIC_SEQ_CST
284 } memory_order;
285
286 /*
287 * 7.17.4 Fences.
288 */
289
290 static __inline void
atomic_thread_fence(memory_order __order)291 atomic_thread_fence(memory_order __order __attribute__((unused)))
292 {
293
294 #ifdef __CLANG_ATOMICS
295 __c11_atomic_thread_fence(__order);
296 #elif defined(__GNUC_ATOMICS)
297 __atomic_thread_fence(__order);
298 #else
299 __sync_synchronize();
300 #endif
301 }
302
303 static __inline void
atomic_signal_fence(memory_order __order)304 atomic_signal_fence(memory_order __order __attribute__((unused)))
305 {
306
307 #ifdef __CLANG_ATOMICS
308 __c11_atomic_signal_fence(__order);
309 #elif defined(__GNUC_ATOMICS)
310 __atomic_signal_fence(__order);
311 #else
312 __asm volatile ("" ::: "memory");
313 #endif
314 }
315
316 /*
317 * 7.17.5 Lock-free property.
318 */
319
320 #if defined(_KERNEL)
321 /* Atomics in kernelspace are always lock-free. */
322 #define atomic_is_lock_free(obj) \
323 ((void)(obj), (_Bool)1)
324 #elif defined(__CLANG_ATOMICS)
325 #define atomic_is_lock_free(obj) \
326 __c11_atomic_is_lock_free(sizeof(*(obj)))
327 #elif defined(__GNUC_ATOMICS)
328 #define atomic_is_lock_free(obj) \
329 __atomic_is_lock_free(sizeof((obj)->__val), &(obj)->__val)
330 #else
331 #define atomic_is_lock_free(obj) \
332 ((void)(obj), sizeof((obj)->__val) <= sizeof(void *))
333 #endif
334
335 /*
336 * 7.17.6 Atomic integer types.
337 */
338
339 #ifndef __CLANG_ATOMICS
340 /*
341 * No native support for _Atomic(). Place object in structure to prevent
342 * most forms of direct non-atomic access.
343 */
344 #define _Atomic(T) struct { T volatile __val; }
345 #endif
346
347 typedef _Atomic(bool) atomic_bool;
348 typedef _Atomic(char) atomic_char;
349 typedef _Atomic(signed char) atomic_schar;
350 typedef _Atomic(unsigned char) atomic_uchar;
351 typedef _Atomic(short) atomic_short;
352 typedef _Atomic(unsigned short) atomic_ushort;
353 typedef _Atomic(int) atomic_int;
354 typedef _Atomic(unsigned int) atomic_uint;
355 typedef _Atomic(long) atomic_long;
356 typedef _Atomic(unsigned long) atomic_ulong;
357 typedef _Atomic(long long) atomic_llong;
358 typedef _Atomic(unsigned long long) atomic_ullong;
359 #if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L
360 typedef _Atomic(char16_t) atomic_char16_t;
361 typedef _Atomic(char32_t) atomic_char32_t;
362 #endif
363 typedef _Atomic(wchar_t) atomic_wchar_t;
364 typedef _Atomic(int_least8_t) atomic_int_least8_t;
365 typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
366 typedef _Atomic(int_least16_t) atomic_int_least16_t;
367 typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
368 typedef _Atomic(int_least32_t) atomic_int_least32_t;
369 typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
370 typedef _Atomic(int_least64_t) atomic_int_least64_t;
371 typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
372 typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
373 typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
374 typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
375 typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
376 typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
377 typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
378 typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
379 typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
380 typedef _Atomic(intptr_t) atomic_intptr_t;
381 typedef _Atomic(uintptr_t) atomic_uintptr_t;
382 typedef _Atomic(size_t) atomic_size_t;
383 typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
384 typedef _Atomic(intmax_t) atomic_intmax_t;
385 typedef _Atomic(uintmax_t) atomic_uintmax_t;
386
387 /*
388 * 7.17.7 Operations on atomic types.
389 */
390
391 /*
392 * Compiler-specific operations.
393 */
394
395 #if defined(__CLANG_ATOMICS)
396 #define atomic_compare_exchange_strong_explicit(object, expected, \
397 desired, success, failure) \
398 __c11_atomic_compare_exchange_strong(object, expected, desired, \
399 success, failure)
400 #define atomic_compare_exchange_weak_explicit(object, expected, \
401 desired, success, failure) \
402 __c11_atomic_compare_exchange_weak(object, expected, desired, \
403 success, failure)
404 #define atomic_exchange_explicit(object, desired, order) \
405 __c11_atomic_exchange(object, desired, order)
406 #define atomic_fetch_add_explicit(object, operand, order) \
407 __c11_atomic_fetch_add(object, operand, order)
408 #define atomic_fetch_and_explicit(object, operand, order) \
409 __c11_atomic_fetch_and(object, operand, order)
410 #define atomic_fetch_or_explicit(object, operand, order) \
411 __c11_atomic_fetch_or(object, operand, order)
412 #define atomic_fetch_sub_explicit(object, operand, order) \
413 __c11_atomic_fetch_sub(object, operand, order)
414 #define atomic_fetch_xor_explicit(object, operand, order) \
415 __c11_atomic_fetch_xor(object, operand, order)
416 #define atomic_load_explicit(object, order) \
417 __c11_atomic_load(object, order)
418 #define atomic_store_explicit(object, desired, order) \
419 __c11_atomic_store(object, desired, order)
420 #elif defined(__GNUC_ATOMICS)
421 #define atomic_compare_exchange_strong_explicit(object, expected, \
422 desired, success, failure) \
423 __atomic_compare_exchange_n(&(object)->__val, expected, \
424 desired, 0, success, failure)
425 #define atomic_compare_exchange_weak_explicit(object, expected, \
426 desired, success, failure) \
427 __atomic_compare_exchange_n(&(object)->__val, expected, \
428 desired, 1, success, failure)
429 #define atomic_exchange_explicit(object, desired, order) \
430 __atomic_exchange_n(&(object)->__val, desired, order)
431 #define atomic_fetch_add_explicit(object, operand, order) \
432 __atomic_fetch_add(&(object)->__val, operand, order)
433 #define atomic_fetch_and_explicit(object, operand, order) \
434 __atomic_fetch_and(&(object)->__val, operand, order)
435 #define atomic_fetch_or_explicit(object, operand, order) \
436 __atomic_fetch_or(&(object)->__val, operand, order)
437 #define atomic_fetch_sub_explicit(object, operand, order) \
438 __atomic_fetch_sub(&(object)->__val, operand, order)
439 #define atomic_fetch_xor_explicit(object, operand, order) \
440 __atomic_fetch_xor(&(object)->__val, operand, order)
441 #define atomic_load_explicit(object, order) \
442 __atomic_load_n(&(object)->__val, order)
443 #define atomic_store_explicit(object, desired, order) \
444 __atomic_store_n(&(object)->__val, desired, order)
445 #else
446 #define __atomic_apply_stride(object, operand) \
447 (((__typeof__((object)->__val))0) + (operand))
448 #define atomic_compare_exchange_strong_explicit(object, expected, \
449 desired, success, failure) __extension__ ({ \
450 __typeof__(expected) __ep = (expected); \
451 __typeof__(*__ep) __e = *__ep; \
452 (void)(success); (void)(failure); \
453 (bool)((*__ep = __sync_val_compare_and_swap(&(object)->__val, \
454 __e, desired)) == __e); \
455 })
456 #define atomic_compare_exchange_weak_explicit(object, expected, \
457 desired, success, failure) \
458 atomic_compare_exchange_strong_explicit(object, expected, \
459 desired, success, failure)
460 #ifdef __HAS_BUILTIN_SYNC_SWAP
461 /* Clang provides a full-barrier atomic exchange - use it if available. */
462 #define atomic_exchange_explicit(object, desired, order) \
463 ((void)(order), __sync_swap(&(object)->__val, desired))
464 #else
465 /*
466 * __sync_lock_test_and_set() is only an acquire barrier in theory (although in
467 * practice it is usually a full barrier) so we need an explicit barrier before
468 * it.
469 */
470 #define atomic_exchange_explicit(object, desired, order) \
471 __extension__ ({ \
472 __typeof__(object) __o = (object); \
473 __typeof__(desired) __d = (desired); \
474 (void)(order); \
475 __sync_synchronize(); \
476 __sync_lock_test_and_set(&(__o)->__val, __d); \
477 })
478 #endif
479 #define atomic_fetch_add_explicit(object, operand, order) \
480 ((void)(order), __sync_fetch_and_add(&(object)->__val, \
481 __atomic_apply_stride(object, operand)))
482 #define atomic_fetch_and_explicit(object, operand, order) \
483 ((void)(order), __sync_fetch_and_and(&(object)->__val, operand))
484 #define atomic_fetch_or_explicit(object, operand, order) \
485 ((void)(order), __sync_fetch_and_or(&(object)->__val, operand))
486 #define atomic_fetch_sub_explicit(object, operand, order) \
487 ((void)(order), __sync_fetch_and_sub(&(object)->__val, \
488 __atomic_apply_stride(object, operand)))
489 #define atomic_fetch_xor_explicit(object, operand, order) \
490 ((void)(order), __sync_fetch_and_xor(&(object)->__val, operand))
491 #define atomic_load_explicit(object, order) \
492 ((void)(order), __sync_fetch_and_add(&(object)->__val, 0))
493 #define atomic_store_explicit(object, desired, order) \
494 ((void)atomic_exchange_explicit(object, desired, order))
495 #endif
496
497 /*
498 * Convenience functions.
499 *
500 * Don't provide these in kernel space. In kernel space, we should be
501 * disciplined enough to always provide explicit barriers.
502 */
503
504 #ifndef _KERNEL
505 #define atomic_compare_exchange_strong(object, expected, desired) \
506 atomic_compare_exchange_strong_explicit(object, expected, \
507 desired, memory_order_seq_cst, memory_order_seq_cst)
508 #define atomic_compare_exchange_weak(object, expected, desired) \
509 atomic_compare_exchange_weak_explicit(object, expected, \
510 desired, memory_order_seq_cst, memory_order_seq_cst)
511 #define atomic_exchange(object, desired) \
512 atomic_exchange_explicit(object, desired, memory_order_seq_cst)
513 #define atomic_fetch_add(object, operand) \
514 atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
515 #define atomic_fetch_and(object, operand) \
516 atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
517 #define atomic_fetch_or(object, operand) \
518 atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
519 #define atomic_fetch_sub(object, operand) \
520 atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
521 #define atomic_fetch_xor(object, operand) \
522 atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
523 #define atomic_load(object) \
524 atomic_load_explicit(object, memory_order_seq_cst)
525 #define atomic_store(object, desired) \
526 atomic_store_explicit(object, desired, memory_order_seq_cst)
527 #endif /* !_KERNEL */
528
529 /*
530 * 7.17.8 Atomic flag type and operations.
531 *
532 * XXX: Assume atomic_bool can be used as an atomic_flag. Is there some
533 * kind of compiler built-in type we could use?
534 */
535
536 typedef struct {
537 atomic_bool __flag;
538 } atomic_flag;
539
540 #define ATOMIC_FLAG_INIT { ATOMIC_VAR_INIT(false) }
541
542 static __inline bool
atomic_flag_test_and_set_explicit(volatile atomic_flag * __object,memory_order __order)543 atomic_flag_test_and_set_explicit(volatile atomic_flag *__object,
544 memory_order __order)
545 {
546 return (atomic_exchange_explicit(&__object->__flag, 1, __order));
547 }
548
549 static __inline void
atomic_flag_clear_explicit(volatile atomic_flag * __object,memory_order __order)550 atomic_flag_clear_explicit(volatile atomic_flag *__object, memory_order __order)
551 {
552
553 atomic_store_explicit(&__object->__flag, 0, __order);
554 }
555
556 #ifndef _KERNEL
557 static __inline bool
atomic_flag_test_and_set(volatile atomic_flag * __object)558 atomic_flag_test_and_set(volatile atomic_flag *__object)
559 {
560
561 return (atomic_flag_test_and_set_explicit(__object,
562 memory_order_seq_cst));
563 }
564
565 static __inline void
atomic_flag_clear(volatile atomic_flag * __object)566 atomic_flag_clear(volatile atomic_flag *__object)
567 {
568
569 atomic_flag_clear_explicit(__object, memory_order_seq_cst);
570 }
571 #endif /* !_KERNEL */
572
573 #endif /* <atomic> unavailable */
574
575 #endif /* !_STDATOMIC_H_ */
576