blob: b487fd100ed4ead199598dfe395c3eb033364e71 [file] [log] [blame]
Elliott Hughese6c57fc2014-05-23 20:06:03 -07001/*-
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>
Hans Boehm019d3952014-08-14 15:26:03 -070034
Dan Albert3ce07692014-10-16 07:52:51 -070035#if defined(__cplusplus) && __cplusplus >= 201103L && defined(_USING_LIBCXX)
Hans Boehm32429602014-08-28 15:21:32 -070036# ifdef __clang__
37# if __has_feature(cxx_atomic)
38# define _STDATOMIC_HAVE_ATOMIC
39# endif
40# else /* gcc */
Elliott Hughesf4840502016-06-06 17:35:53 -070041# define _STDATOMIC_HAVE_ATOMIC
Hans Boehm32429602014-08-28 15:21:32 -070042# endif
43#endif
44
45#ifdef _STDATOMIC_HAVE_ATOMIC
Hans Boehm019d3952014-08-14 15:26:03 -070046
47/* We have a usable C++ <atomic>; use it instead. */
48
49#include <atomic>
50
Hans Boehm3e4a0092014-08-26 15:58:15 -070051#undef _Atomic
52 /* Also defined by <atomic> for gcc. But not used in macros. */
53 /* Also a clang intrinsic. */
54 /* Should not be used by client code before this file is */
55 /* included. The definitions in <atomic> themselves see */
56 /* the old definition, as they should. */
57 /* Client code sees the following definition. */
Hans Boehm32429602014-08-28 15:21:32 -070058
Hans Boehm019d3952014-08-14 15:26:03 -070059#define _Atomic(t) std::atomic<t>
60
61using std::atomic_is_lock_free;
62using std::atomic_init;
63using std::atomic_store;
64using std::atomic_store_explicit;
65using std::atomic_load;
66using std::atomic_load_explicit;
67using std::atomic_exchange;
68using std::atomic_exchange_explicit;
69using std::atomic_compare_exchange_strong;
70using std::atomic_compare_exchange_strong_explicit;
71using std::atomic_compare_exchange_weak;
72using std::atomic_compare_exchange_weak_explicit;
73using std::atomic_fetch_add;
74using std::atomic_fetch_add_explicit;
75using std::atomic_fetch_sub;
76using std::atomic_fetch_sub_explicit;
77using std::atomic_fetch_or;
78using std::atomic_fetch_or_explicit;
79using std::atomic_fetch_xor;
80using std::atomic_fetch_xor_explicit;
81using std::atomic_fetch_and;
82using std::atomic_fetch_and_explicit;
83using std::atomic_thread_fence;
84using std::atomic_signal_fence;
85
86using std::memory_order;
87using std::memory_order_relaxed;
88using std::memory_order_consume;
Hans Boehm76ac4d02014-09-30 18:31:04 -070089using std::memory_order_acquire;
Hans Boehm019d3952014-08-14 15:26:03 -070090using std::memory_order_release;
91using std::memory_order_acq_rel;
92using std::memory_order_seq_cst;
93
94using std::atomic_bool;
95using std::atomic_char;
96using std::atomic_schar;
97using std::atomic_uchar;
98using std::atomic_short;
99using std::atomic_ushort;
100using std::atomic_int;
101using std::atomic_uint;
102using std::atomic_long;
103using std::atomic_ulong;
104using std::atomic_llong;
105using std::atomic_ullong;
106using std::atomic_char16_t;
107using std::atomic_char32_t;
108using std::atomic_wchar_t;
109using std::atomic_int_least8_t;
110using std::atomic_uint_least8_t;
111using std::atomic_int_least16_t;
112using std::atomic_uint_least16_t;
113using std::atomic_int_least32_t;
114using std::atomic_uint_least32_t;
115using std::atomic_int_least64_t;
116using std::atomic_uint_least64_t;
117using std::atomic_int_fast8_t;
118using std::atomic_uint_fast8_t;
119using std::atomic_int_fast16_t;
120using std::atomic_uint_fast16_t;
121using std::atomic_int_fast32_t;
122using std::atomic_uint_fast32_t;
123using std::atomic_int_fast64_t;
124using std::atomic_uint_fast64_t;
125using std::atomic_intptr_t;
126using std::atomic_uintptr_t;
127using std::atomic_size_t;
128using std::atomic_ptrdiff_t;
129using std::atomic_intmax_t;
130using std::atomic_uintmax_t;
131
132#else /* <atomic> unavailable, possibly because this is C, not C++ */
133
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700134#include <sys/types.h>
135#include <stdbool.h>
136
Hans Boehm019d3952014-08-14 15:26:03 -0700137/*
138 * C: Do it ourselves.
139 * Note that the runtime representation defined here should be compatible
140 * with the C++ one, i.e. an _Atomic(T) needs to contain the same
141 * bits as a T.
142 */
143
Elliott Hughes0fe88852016-07-27 10:45:05 -0700144#include <stddef.h> /* For ptrdiff_t. */
145#include <stdint.h> /* TODO: don't drag in all the macros, just the types. */
146#include <uchar.h> /* For char16_t and char32_t. */
Hans Boehm00aaea32014-08-19 16:14:01 -0700147
Hans Boehm32429602014-08-28 15:21:32 -0700148#ifdef __clang__
149# if __has_extension(c_atomic) || __has_extension(cxx_atomic)
150# define __CLANG_ATOMICS
151# else
152# error "stdatomic.h does not support your compiler"
153# endif
154# if __has_builtin(__sync_swap)
155# define __HAS_BUILTIN_SYNC_SWAP
156# endif
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700157#else
Elliott Hughesf4840502016-06-06 17:35:53 -0700158# define __GNUC_ATOMICS
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700159#endif
160
161/*
162 * 7.17.1 Atomic lock-free macros.
163 */
164
165#ifdef __GCC_ATOMIC_BOOL_LOCK_FREE
166#define ATOMIC_BOOL_LOCK_FREE __GCC_ATOMIC_BOOL_LOCK_FREE
167#endif
168#ifdef __GCC_ATOMIC_CHAR_LOCK_FREE
169#define ATOMIC_CHAR_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE
170#endif
171#ifdef __GCC_ATOMIC_CHAR16_T_LOCK_FREE
172#define ATOMIC_CHAR16_T_LOCK_FREE __GCC_ATOMIC_CHAR16_T_LOCK_FREE
173#endif
174#ifdef __GCC_ATOMIC_CHAR32_T_LOCK_FREE
175#define ATOMIC_CHAR32_T_LOCK_FREE __GCC_ATOMIC_CHAR32_T_LOCK_FREE
176#endif
177#ifdef __GCC_ATOMIC_WCHAR_T_LOCK_FREE
178#define ATOMIC_WCHAR_T_LOCK_FREE __GCC_ATOMIC_WCHAR_T_LOCK_FREE
179#endif
180#ifdef __GCC_ATOMIC_SHORT_LOCK_FREE
181#define ATOMIC_SHORT_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
182#endif
183#ifdef __GCC_ATOMIC_INT_LOCK_FREE
184#define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
185#endif
186#ifdef __GCC_ATOMIC_LONG_LOCK_FREE
187#define ATOMIC_LONG_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
188#endif
189#ifdef __GCC_ATOMIC_LLONG_LOCK_FREE
190#define ATOMIC_LLONG_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
191#endif
192#ifdef __GCC_ATOMIC_POINTER_LOCK_FREE
193#define ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE
194#endif
195
196/*
197 * 7.17.2 Initialization.
198 */
199
200#if defined(__CLANG_ATOMICS)
201#define ATOMIC_VAR_INIT(value) (value)
202#define atomic_init(obj, value) __c11_atomic_init(obj, value)
203#else
204#define ATOMIC_VAR_INIT(value) { .__val = (value) }
205#define atomic_init(obj, value) ((void)((obj)->__val = (value)))
206#endif
207
208/*
209 * Clang and recent GCC both provide predefined macros for the memory
210 * orderings. If we are using a compiler that doesn't define them, use the
211 * clang values - these will be ignored in the fallback path.
212 */
213
214#ifndef __ATOMIC_RELAXED
215#define __ATOMIC_RELAXED 0
216#endif
217#ifndef __ATOMIC_CONSUME
218#define __ATOMIC_CONSUME 1
219#endif
220#ifndef __ATOMIC_ACQUIRE
221#define __ATOMIC_ACQUIRE 2
222#endif
223#ifndef __ATOMIC_RELEASE
224#define __ATOMIC_RELEASE 3
225#endif
226#ifndef __ATOMIC_ACQ_REL
227#define __ATOMIC_ACQ_REL 4
228#endif
229#ifndef __ATOMIC_SEQ_CST
230#define __ATOMIC_SEQ_CST 5
231#endif
232
233/*
234 * 7.17.3 Order and consistency.
235 *
236 * The memory_order_* constants that denote the barrier behaviour of the
237 * atomic operations.
Hans Boehm019d3952014-08-14 15:26:03 -0700238 * The enum values must be identical to those used by the
239 * C++ <atomic> header.
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700240 */
241
242typedef enum {
243 memory_order_relaxed = __ATOMIC_RELAXED,
244 memory_order_consume = __ATOMIC_CONSUME,
245 memory_order_acquire = __ATOMIC_ACQUIRE,
246 memory_order_release = __ATOMIC_RELEASE,
247 memory_order_acq_rel = __ATOMIC_ACQ_REL,
248 memory_order_seq_cst = __ATOMIC_SEQ_CST
249} memory_order;
250
251/*
252 * 7.17.4 Fences.
253 */
254
255static __inline void
Hans Boehm00aaea32014-08-19 16:14:01 -0700256atomic_thread_fence(memory_order __order __attribute__((unused)))
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700257{
258
259#ifdef __CLANG_ATOMICS
260 __c11_atomic_thread_fence(__order);
261#elif defined(__GNUC_ATOMICS)
262 __atomic_thread_fence(__order);
263#else
264 __sync_synchronize();
265#endif
266}
267
268static __inline void
Hans Boehm00aaea32014-08-19 16:14:01 -0700269atomic_signal_fence(memory_order __order __attribute__((unused)))
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700270{
271
272#ifdef __CLANG_ATOMICS
273 __c11_atomic_signal_fence(__order);
274#elif defined(__GNUC_ATOMICS)
275 __atomic_signal_fence(__order);
276#else
277 __asm volatile ("" ::: "memory");
278#endif
279}
280
281/*
282 * 7.17.5 Lock-free property.
283 */
284
285#if defined(_KERNEL)
286/* Atomics in kernelspace are always lock-free. */
287#define atomic_is_lock_free(obj) \
288 ((void)(obj), (_Bool)1)
289#elif defined(__CLANG_ATOMICS)
290#define atomic_is_lock_free(obj) \
Hans Boehm00aaea32014-08-19 16:14:01 -0700291 __c11_atomic_is_lock_free(sizeof(*(obj)))
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700292#elif defined(__GNUC_ATOMICS)
293#define atomic_is_lock_free(obj) \
294 __atomic_is_lock_free(sizeof((obj)->__val), &(obj)->__val)
295#else
296#define atomic_is_lock_free(obj) \
297 ((void)(obj), sizeof((obj)->__val) <= sizeof(void *))
298#endif
299
300/*
301 * 7.17.6 Atomic integer types.
302 */
303
Hans Boehm32429602014-08-28 15:21:32 -0700304#ifndef __CLANG_ATOMICS
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700305/*
306 * No native support for _Atomic(). Place object in structure to prevent
307 * most forms of direct non-atomic access.
308 */
309#define _Atomic(T) struct { T volatile __val; }
310#endif
311
312typedef _Atomic(bool) atomic_bool;
313typedef _Atomic(char) atomic_char;
314typedef _Atomic(signed char) atomic_schar;
315typedef _Atomic(unsigned char) atomic_uchar;
316typedef _Atomic(short) atomic_short;
317typedef _Atomic(unsigned short) atomic_ushort;
318typedef _Atomic(int) atomic_int;
319typedef _Atomic(unsigned int) atomic_uint;
320typedef _Atomic(long) atomic_long;
321typedef _Atomic(unsigned long) atomic_ulong;
322typedef _Atomic(long long) atomic_llong;
323typedef _Atomic(unsigned long long) atomic_ullong;
Hans Boehm8b002362014-07-16 11:33:48 -0700324#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L
325 typedef _Atomic(char16_t) atomic_char16_t;
326 typedef _Atomic(char32_t) atomic_char32_t;
327#endif
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700328typedef _Atomic(wchar_t) atomic_wchar_t;
329typedef _Atomic(int_least8_t) atomic_int_least8_t;
330typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
331typedef _Atomic(int_least16_t) atomic_int_least16_t;
332typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
333typedef _Atomic(int_least32_t) atomic_int_least32_t;
334typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
335typedef _Atomic(int_least64_t) atomic_int_least64_t;
336typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
337typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
338typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
339typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
340typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
341typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
342typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
343typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
344typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
345typedef _Atomic(intptr_t) atomic_intptr_t;
346typedef _Atomic(uintptr_t) atomic_uintptr_t;
347typedef _Atomic(size_t) atomic_size_t;
348typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
349typedef _Atomic(intmax_t) atomic_intmax_t;
350typedef _Atomic(uintmax_t) atomic_uintmax_t;
351
352/*
353 * 7.17.7 Operations on atomic types.
354 */
355
356/*
357 * Compiler-specific operations.
358 */
359
360#if defined(__CLANG_ATOMICS)
361#define atomic_compare_exchange_strong_explicit(object, expected, \
362 desired, success, failure) \
363 __c11_atomic_compare_exchange_strong(object, expected, desired, \
364 success, failure)
365#define atomic_compare_exchange_weak_explicit(object, expected, \
366 desired, success, failure) \
367 __c11_atomic_compare_exchange_weak(object, expected, desired, \
368 success, failure)
369#define atomic_exchange_explicit(object, desired, order) \
370 __c11_atomic_exchange(object, desired, order)
371#define atomic_fetch_add_explicit(object, operand, order) \
372 __c11_atomic_fetch_add(object, operand, order)
373#define atomic_fetch_and_explicit(object, operand, order) \
374 __c11_atomic_fetch_and(object, operand, order)
375#define atomic_fetch_or_explicit(object, operand, order) \
376 __c11_atomic_fetch_or(object, operand, order)
377#define atomic_fetch_sub_explicit(object, operand, order) \
378 __c11_atomic_fetch_sub(object, operand, order)
379#define atomic_fetch_xor_explicit(object, operand, order) \
380 __c11_atomic_fetch_xor(object, operand, order)
381#define atomic_load_explicit(object, order) \
382 __c11_atomic_load(object, order)
383#define atomic_store_explicit(object, desired, order) \
384 __c11_atomic_store(object, desired, order)
385#elif defined(__GNUC_ATOMICS)
386#define atomic_compare_exchange_strong_explicit(object, expected, \
387 desired, success, failure) \
388 __atomic_compare_exchange_n(&(object)->__val, expected, \
389 desired, 0, success, failure)
390#define atomic_compare_exchange_weak_explicit(object, expected, \
391 desired, success, failure) \
392 __atomic_compare_exchange_n(&(object)->__val, expected, \
393 desired, 1, success, failure)
394#define atomic_exchange_explicit(object, desired, order) \
395 __atomic_exchange_n(&(object)->__val, desired, order)
396#define atomic_fetch_add_explicit(object, operand, order) \
397 __atomic_fetch_add(&(object)->__val, operand, order)
398#define atomic_fetch_and_explicit(object, operand, order) \
399 __atomic_fetch_and(&(object)->__val, operand, order)
400#define atomic_fetch_or_explicit(object, operand, order) \
401 __atomic_fetch_or(&(object)->__val, operand, order)
402#define atomic_fetch_sub_explicit(object, operand, order) \
403 __atomic_fetch_sub(&(object)->__val, operand, order)
404#define atomic_fetch_xor_explicit(object, operand, order) \
405 __atomic_fetch_xor(&(object)->__val, operand, order)
406#define atomic_load_explicit(object, order) \
407 __atomic_load_n(&(object)->__val, order)
408#define atomic_store_explicit(object, desired, order) \
409 __atomic_store_n(&(object)->__val, desired, order)
410#else
411#define __atomic_apply_stride(object, operand) \
412 (((__typeof__((object)->__val))0) + (operand))
413#define atomic_compare_exchange_strong_explicit(object, expected, \
414 desired, success, failure) __extension__ ({ \
415 __typeof__(expected) __ep = (expected); \
416 __typeof__(*__ep) __e = *__ep; \
417 (void)(success); (void)(failure); \
418 (bool)((*__ep = __sync_val_compare_and_swap(&(object)->__val, \
419 __e, desired)) == __e); \
420})
421#define atomic_compare_exchange_weak_explicit(object, expected, \
422 desired, success, failure) \
423 atomic_compare_exchange_strong_explicit(object, expected, \
424 desired, success, failure)
Hans Boehm32429602014-08-28 15:21:32 -0700425#ifdef __HAS_BUILTIN_SYNC_SWAP
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700426/* Clang provides a full-barrier atomic exchange - use it if available. */
427#define atomic_exchange_explicit(object, desired, order) \
428 ((void)(order), __sync_swap(&(object)->__val, desired))
429#else
430/*
431 * __sync_lock_test_and_set() is only an acquire barrier in theory (although in
432 * practice it is usually a full barrier) so we need an explicit barrier before
433 * it.
434 */
435#define atomic_exchange_explicit(object, desired, order) \
436__extension__ ({ \
437 __typeof__(object) __o = (object); \
438 __typeof__(desired) __d = (desired); \
439 (void)(order); \
440 __sync_synchronize(); \
441 __sync_lock_test_and_set(&(__o)->__val, __d); \
442})
443#endif
444#define atomic_fetch_add_explicit(object, operand, order) \
445 ((void)(order), __sync_fetch_and_add(&(object)->__val, \
446 __atomic_apply_stride(object, operand)))
447#define atomic_fetch_and_explicit(object, operand, order) \
448 ((void)(order), __sync_fetch_and_and(&(object)->__val, operand))
449#define atomic_fetch_or_explicit(object, operand, order) \
450 ((void)(order), __sync_fetch_and_or(&(object)->__val, operand))
451#define atomic_fetch_sub_explicit(object, operand, order) \
452 ((void)(order), __sync_fetch_and_sub(&(object)->__val, \
453 __atomic_apply_stride(object, operand)))
454#define atomic_fetch_xor_explicit(object, operand, order) \
455 ((void)(order), __sync_fetch_and_xor(&(object)->__val, operand))
456#define atomic_load_explicit(object, order) \
457 ((void)(order), __sync_fetch_and_add(&(object)->__val, 0))
458#define atomic_store_explicit(object, desired, order) \
459 ((void)atomic_exchange_explicit(object, desired, order))
460#endif
461
462/*
463 * Convenience functions.
464 *
465 * Don't provide these in kernel space. In kernel space, we should be
466 * disciplined enough to always provide explicit barriers.
467 */
468
469#ifndef _KERNEL
470#define atomic_compare_exchange_strong(object, expected, desired) \
471 atomic_compare_exchange_strong_explicit(object, expected, \
472 desired, memory_order_seq_cst, memory_order_seq_cst)
473#define atomic_compare_exchange_weak(object, expected, desired) \
474 atomic_compare_exchange_weak_explicit(object, expected, \
475 desired, memory_order_seq_cst, memory_order_seq_cst)
476#define atomic_exchange(object, desired) \
477 atomic_exchange_explicit(object, desired, memory_order_seq_cst)
478#define atomic_fetch_add(object, operand) \
479 atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
480#define atomic_fetch_and(object, operand) \
481 atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
482#define atomic_fetch_or(object, operand) \
483 atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
484#define atomic_fetch_sub(object, operand) \
485 atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
486#define atomic_fetch_xor(object, operand) \
487 atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
488#define atomic_load(object) \
489 atomic_load_explicit(object, memory_order_seq_cst)
490#define atomic_store(object, desired) \
491 atomic_store_explicit(object, desired, memory_order_seq_cst)
492#endif /* !_KERNEL */
493
494/*
495 * 7.17.8 Atomic flag type and operations.
496 *
497 * XXX: Assume atomic_bool can be used as an atomic_flag. Is there some
498 * kind of compiler built-in type we could use?
499 */
500
501typedef struct {
502 atomic_bool __flag;
503} atomic_flag;
504
Hans Boehm00aaea32014-08-19 16:14:01 -0700505#define ATOMIC_FLAG_INIT { ATOMIC_VAR_INIT(false) }
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700506
507static __inline bool
508atomic_flag_test_and_set_explicit(volatile atomic_flag *__object,
509 memory_order __order)
510{
511 return (atomic_exchange_explicit(&__object->__flag, 1, __order));
512}
513
514static __inline void
515atomic_flag_clear_explicit(volatile atomic_flag *__object, memory_order __order)
516{
517
518 atomic_store_explicit(&__object->__flag, 0, __order);
519}
520
521#ifndef _KERNEL
522static __inline bool
523atomic_flag_test_and_set(volatile atomic_flag *__object)
524{
525
526 return (atomic_flag_test_and_set_explicit(__object,
527 memory_order_seq_cst));
528}
529
530static __inline void
531atomic_flag_clear(volatile atomic_flag *__object)
532{
533
534 atomic_flag_clear_explicit(__object, memory_order_seq_cst);
535}
536#endif /* !_KERNEL */
537
Hans Boehm019d3952014-08-14 15:26:03 -0700538#endif /* <atomic> unavailable */
539
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700540#endif /* !_STDATOMIC_H_ */