blob: 8347f83cc00ffe9bf7407d75976756b613279619 [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
Hans Boehm00aaea32014-08-19 16:14:01 -0700144#include <stddef.h> /* For ptrdiff_t. */
145#include <stdint.h> /* TODO: Should pollute namespace less. */
146#if __STDC_VERSION__ >= 201112L
147# include <uchar.h> /* For char16_t and char32_t. */
148#endif
149
Hans Boehm32429602014-08-28 15:21:32 -0700150#ifdef __clang__
151# if __has_extension(c_atomic) || __has_extension(cxx_atomic)
152# define __CLANG_ATOMICS
153# else
154# error "stdatomic.h does not support your compiler"
155# endif
156# if __has_builtin(__sync_swap)
157# define __HAS_BUILTIN_SYNC_SWAP
158# endif
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700159#else
Elliott Hughesf4840502016-06-06 17:35:53 -0700160# define __GNUC_ATOMICS
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700161#endif
162
163/*
164 * 7.17.1 Atomic lock-free macros.
165 */
166
167#ifdef __GCC_ATOMIC_BOOL_LOCK_FREE
168#define ATOMIC_BOOL_LOCK_FREE __GCC_ATOMIC_BOOL_LOCK_FREE
169#endif
170#ifdef __GCC_ATOMIC_CHAR_LOCK_FREE
171#define ATOMIC_CHAR_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE
172#endif
173#ifdef __GCC_ATOMIC_CHAR16_T_LOCK_FREE
174#define ATOMIC_CHAR16_T_LOCK_FREE __GCC_ATOMIC_CHAR16_T_LOCK_FREE
175#endif
176#ifdef __GCC_ATOMIC_CHAR32_T_LOCK_FREE
177#define ATOMIC_CHAR32_T_LOCK_FREE __GCC_ATOMIC_CHAR32_T_LOCK_FREE
178#endif
179#ifdef __GCC_ATOMIC_WCHAR_T_LOCK_FREE
180#define ATOMIC_WCHAR_T_LOCK_FREE __GCC_ATOMIC_WCHAR_T_LOCK_FREE
181#endif
182#ifdef __GCC_ATOMIC_SHORT_LOCK_FREE
183#define ATOMIC_SHORT_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
184#endif
185#ifdef __GCC_ATOMIC_INT_LOCK_FREE
186#define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
187#endif
188#ifdef __GCC_ATOMIC_LONG_LOCK_FREE
189#define ATOMIC_LONG_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
190#endif
191#ifdef __GCC_ATOMIC_LLONG_LOCK_FREE
192#define ATOMIC_LLONG_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
193#endif
194#ifdef __GCC_ATOMIC_POINTER_LOCK_FREE
195#define ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE
196#endif
197
198/*
199 * 7.17.2 Initialization.
200 */
201
202#if defined(__CLANG_ATOMICS)
203#define ATOMIC_VAR_INIT(value) (value)
204#define atomic_init(obj, value) __c11_atomic_init(obj, value)
205#else
206#define ATOMIC_VAR_INIT(value) { .__val = (value) }
207#define atomic_init(obj, value) ((void)((obj)->__val = (value)))
208#endif
209
210/*
211 * Clang and recent GCC both provide predefined macros for the memory
212 * orderings. If we are using a compiler that doesn't define them, use the
213 * clang values - these will be ignored in the fallback path.
214 */
215
216#ifndef __ATOMIC_RELAXED
217#define __ATOMIC_RELAXED 0
218#endif
219#ifndef __ATOMIC_CONSUME
220#define __ATOMIC_CONSUME 1
221#endif
222#ifndef __ATOMIC_ACQUIRE
223#define __ATOMIC_ACQUIRE 2
224#endif
225#ifndef __ATOMIC_RELEASE
226#define __ATOMIC_RELEASE 3
227#endif
228#ifndef __ATOMIC_ACQ_REL
229#define __ATOMIC_ACQ_REL 4
230#endif
231#ifndef __ATOMIC_SEQ_CST
232#define __ATOMIC_SEQ_CST 5
233#endif
234
235/*
236 * 7.17.3 Order and consistency.
237 *
238 * The memory_order_* constants that denote the barrier behaviour of the
239 * atomic operations.
Hans Boehm019d3952014-08-14 15:26:03 -0700240 * The enum values must be identical to those used by the
241 * C++ <atomic> header.
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700242 */
243
244typedef enum {
245 memory_order_relaxed = __ATOMIC_RELAXED,
246 memory_order_consume = __ATOMIC_CONSUME,
247 memory_order_acquire = __ATOMIC_ACQUIRE,
248 memory_order_release = __ATOMIC_RELEASE,
249 memory_order_acq_rel = __ATOMIC_ACQ_REL,
250 memory_order_seq_cst = __ATOMIC_SEQ_CST
251} memory_order;
252
253/*
254 * 7.17.4 Fences.
255 */
256
257static __inline void
Hans Boehm00aaea32014-08-19 16:14:01 -0700258atomic_thread_fence(memory_order __order __attribute__((unused)))
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700259{
260
261#ifdef __CLANG_ATOMICS
262 __c11_atomic_thread_fence(__order);
263#elif defined(__GNUC_ATOMICS)
264 __atomic_thread_fence(__order);
265#else
266 __sync_synchronize();
267#endif
268}
269
270static __inline void
Hans Boehm00aaea32014-08-19 16:14:01 -0700271atomic_signal_fence(memory_order __order __attribute__((unused)))
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700272{
273
274#ifdef __CLANG_ATOMICS
275 __c11_atomic_signal_fence(__order);
276#elif defined(__GNUC_ATOMICS)
277 __atomic_signal_fence(__order);
278#else
279 __asm volatile ("" ::: "memory");
280#endif
281}
282
283/*
284 * 7.17.5 Lock-free property.
285 */
286
287#if defined(_KERNEL)
288/* Atomics in kernelspace are always lock-free. */
289#define atomic_is_lock_free(obj) \
290 ((void)(obj), (_Bool)1)
291#elif defined(__CLANG_ATOMICS)
292#define atomic_is_lock_free(obj) \
Hans Boehm00aaea32014-08-19 16:14:01 -0700293 __c11_atomic_is_lock_free(sizeof(*(obj)))
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700294#elif defined(__GNUC_ATOMICS)
295#define atomic_is_lock_free(obj) \
296 __atomic_is_lock_free(sizeof((obj)->__val), &(obj)->__val)
297#else
298#define atomic_is_lock_free(obj) \
299 ((void)(obj), sizeof((obj)->__val) <= sizeof(void *))
300#endif
301
302/*
303 * 7.17.6 Atomic integer types.
304 */
305
Hans Boehm32429602014-08-28 15:21:32 -0700306#ifndef __CLANG_ATOMICS
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700307/*
308 * No native support for _Atomic(). Place object in structure to prevent
309 * most forms of direct non-atomic access.
310 */
311#define _Atomic(T) struct { T volatile __val; }
312#endif
313
314typedef _Atomic(bool) atomic_bool;
315typedef _Atomic(char) atomic_char;
316typedef _Atomic(signed char) atomic_schar;
317typedef _Atomic(unsigned char) atomic_uchar;
318typedef _Atomic(short) atomic_short;
319typedef _Atomic(unsigned short) atomic_ushort;
320typedef _Atomic(int) atomic_int;
321typedef _Atomic(unsigned int) atomic_uint;
322typedef _Atomic(long) atomic_long;
323typedef _Atomic(unsigned long) atomic_ulong;
324typedef _Atomic(long long) atomic_llong;
325typedef _Atomic(unsigned long long) atomic_ullong;
Hans Boehm8b002362014-07-16 11:33:48 -0700326#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L
327 typedef _Atomic(char16_t) atomic_char16_t;
328 typedef _Atomic(char32_t) atomic_char32_t;
329#endif
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700330typedef _Atomic(wchar_t) atomic_wchar_t;
331typedef _Atomic(int_least8_t) atomic_int_least8_t;
332typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
333typedef _Atomic(int_least16_t) atomic_int_least16_t;
334typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
335typedef _Atomic(int_least32_t) atomic_int_least32_t;
336typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
337typedef _Atomic(int_least64_t) atomic_int_least64_t;
338typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
339typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
340typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
341typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
342typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
343typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
344typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
345typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
346typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
347typedef _Atomic(intptr_t) atomic_intptr_t;
348typedef _Atomic(uintptr_t) atomic_uintptr_t;
349typedef _Atomic(size_t) atomic_size_t;
350typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
351typedef _Atomic(intmax_t) atomic_intmax_t;
352typedef _Atomic(uintmax_t) atomic_uintmax_t;
353
354/*
355 * 7.17.7 Operations on atomic types.
356 */
357
358/*
359 * Compiler-specific operations.
360 */
361
362#if defined(__CLANG_ATOMICS)
363#define atomic_compare_exchange_strong_explicit(object, expected, \
364 desired, success, failure) \
365 __c11_atomic_compare_exchange_strong(object, expected, desired, \
366 success, failure)
367#define atomic_compare_exchange_weak_explicit(object, expected, \
368 desired, success, failure) \
369 __c11_atomic_compare_exchange_weak(object, expected, desired, \
370 success, failure)
371#define atomic_exchange_explicit(object, desired, order) \
372 __c11_atomic_exchange(object, desired, order)
373#define atomic_fetch_add_explicit(object, operand, order) \
374 __c11_atomic_fetch_add(object, operand, order)
375#define atomic_fetch_and_explicit(object, operand, order) \
376 __c11_atomic_fetch_and(object, operand, order)
377#define atomic_fetch_or_explicit(object, operand, order) \
378 __c11_atomic_fetch_or(object, operand, order)
379#define atomic_fetch_sub_explicit(object, operand, order) \
380 __c11_atomic_fetch_sub(object, operand, order)
381#define atomic_fetch_xor_explicit(object, operand, order) \
382 __c11_atomic_fetch_xor(object, operand, order)
383#define atomic_load_explicit(object, order) \
384 __c11_atomic_load(object, order)
385#define atomic_store_explicit(object, desired, order) \
386 __c11_atomic_store(object, desired, order)
387#elif defined(__GNUC_ATOMICS)
388#define atomic_compare_exchange_strong_explicit(object, expected, \
389 desired, success, failure) \
390 __atomic_compare_exchange_n(&(object)->__val, expected, \
391 desired, 0, success, failure)
392#define atomic_compare_exchange_weak_explicit(object, expected, \
393 desired, success, failure) \
394 __atomic_compare_exchange_n(&(object)->__val, expected, \
395 desired, 1, success, failure)
396#define atomic_exchange_explicit(object, desired, order) \
397 __atomic_exchange_n(&(object)->__val, desired, order)
398#define atomic_fetch_add_explicit(object, operand, order) \
399 __atomic_fetch_add(&(object)->__val, operand, order)
400#define atomic_fetch_and_explicit(object, operand, order) \
401 __atomic_fetch_and(&(object)->__val, operand, order)
402#define atomic_fetch_or_explicit(object, operand, order) \
403 __atomic_fetch_or(&(object)->__val, operand, order)
404#define atomic_fetch_sub_explicit(object, operand, order) \
405 __atomic_fetch_sub(&(object)->__val, operand, order)
406#define atomic_fetch_xor_explicit(object, operand, order) \
407 __atomic_fetch_xor(&(object)->__val, operand, order)
408#define atomic_load_explicit(object, order) \
409 __atomic_load_n(&(object)->__val, order)
410#define atomic_store_explicit(object, desired, order) \
411 __atomic_store_n(&(object)->__val, desired, order)
412#else
413#define __atomic_apply_stride(object, operand) \
414 (((__typeof__((object)->__val))0) + (operand))
415#define atomic_compare_exchange_strong_explicit(object, expected, \
416 desired, success, failure) __extension__ ({ \
417 __typeof__(expected) __ep = (expected); \
418 __typeof__(*__ep) __e = *__ep; \
419 (void)(success); (void)(failure); \
420 (bool)((*__ep = __sync_val_compare_and_swap(&(object)->__val, \
421 __e, desired)) == __e); \
422})
423#define atomic_compare_exchange_weak_explicit(object, expected, \
424 desired, success, failure) \
425 atomic_compare_exchange_strong_explicit(object, expected, \
426 desired, success, failure)
Hans Boehm32429602014-08-28 15:21:32 -0700427#ifdef __HAS_BUILTIN_SYNC_SWAP
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700428/* Clang provides a full-barrier atomic exchange - use it if available. */
429#define atomic_exchange_explicit(object, desired, order) \
430 ((void)(order), __sync_swap(&(object)->__val, desired))
431#else
432/*
433 * __sync_lock_test_and_set() is only an acquire barrier in theory (although in
434 * practice it is usually a full barrier) so we need an explicit barrier before
435 * it.
436 */
437#define atomic_exchange_explicit(object, desired, order) \
438__extension__ ({ \
439 __typeof__(object) __o = (object); \
440 __typeof__(desired) __d = (desired); \
441 (void)(order); \
442 __sync_synchronize(); \
443 __sync_lock_test_and_set(&(__o)->__val, __d); \
444})
445#endif
446#define atomic_fetch_add_explicit(object, operand, order) \
447 ((void)(order), __sync_fetch_and_add(&(object)->__val, \
448 __atomic_apply_stride(object, operand)))
449#define atomic_fetch_and_explicit(object, operand, order) \
450 ((void)(order), __sync_fetch_and_and(&(object)->__val, operand))
451#define atomic_fetch_or_explicit(object, operand, order) \
452 ((void)(order), __sync_fetch_and_or(&(object)->__val, operand))
453#define atomic_fetch_sub_explicit(object, operand, order) \
454 ((void)(order), __sync_fetch_and_sub(&(object)->__val, \
455 __atomic_apply_stride(object, operand)))
456#define atomic_fetch_xor_explicit(object, operand, order) \
457 ((void)(order), __sync_fetch_and_xor(&(object)->__val, operand))
458#define atomic_load_explicit(object, order) \
459 ((void)(order), __sync_fetch_and_add(&(object)->__val, 0))
460#define atomic_store_explicit(object, desired, order) \
461 ((void)atomic_exchange_explicit(object, desired, order))
462#endif
463
464/*
465 * Convenience functions.
466 *
467 * Don't provide these in kernel space. In kernel space, we should be
468 * disciplined enough to always provide explicit barriers.
469 */
470
471#ifndef _KERNEL
472#define atomic_compare_exchange_strong(object, expected, desired) \
473 atomic_compare_exchange_strong_explicit(object, expected, \
474 desired, memory_order_seq_cst, memory_order_seq_cst)
475#define atomic_compare_exchange_weak(object, expected, desired) \
476 atomic_compare_exchange_weak_explicit(object, expected, \
477 desired, memory_order_seq_cst, memory_order_seq_cst)
478#define atomic_exchange(object, desired) \
479 atomic_exchange_explicit(object, desired, memory_order_seq_cst)
480#define atomic_fetch_add(object, operand) \
481 atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
482#define atomic_fetch_and(object, operand) \
483 atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
484#define atomic_fetch_or(object, operand) \
485 atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
486#define atomic_fetch_sub(object, operand) \
487 atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
488#define atomic_fetch_xor(object, operand) \
489 atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
490#define atomic_load(object) \
491 atomic_load_explicit(object, memory_order_seq_cst)
492#define atomic_store(object, desired) \
493 atomic_store_explicit(object, desired, memory_order_seq_cst)
494#endif /* !_KERNEL */
495
496/*
497 * 7.17.8 Atomic flag type and operations.
498 *
499 * XXX: Assume atomic_bool can be used as an atomic_flag. Is there some
500 * kind of compiler built-in type we could use?
501 */
502
503typedef struct {
504 atomic_bool __flag;
505} atomic_flag;
506
Hans Boehm00aaea32014-08-19 16:14:01 -0700507#define ATOMIC_FLAG_INIT { ATOMIC_VAR_INIT(false) }
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700508
509static __inline bool
510atomic_flag_test_and_set_explicit(volatile atomic_flag *__object,
511 memory_order __order)
512{
513 return (atomic_exchange_explicit(&__object->__flag, 1, __order));
514}
515
516static __inline void
517atomic_flag_clear_explicit(volatile atomic_flag *__object, memory_order __order)
518{
519
520 atomic_store_explicit(&__object->__flag, 0, __order);
521}
522
523#ifndef _KERNEL
524static __inline bool
525atomic_flag_test_and_set(volatile atomic_flag *__object)
526{
527
528 return (atomic_flag_test_and_set_explicit(__object,
529 memory_order_seq_cst));
530}
531
532static __inline void
533atomic_flag_clear(volatile atomic_flag *__object)
534{
535
536 atomic_flag_clear_explicit(__object, memory_order_seq_cst);
537}
538#endif /* !_KERNEL */
539
Hans Boehm019d3952014-08-14 15:26:03 -0700540#endif /* <atomic> unavailable */
541
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700542#endif /* !_STDATOMIC_H_ */