blob: 3db25a78ed39b9936418810aa30745803eea2e02 [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 Boehmf0f66c02014-08-14 15:26:03 -070034
Hans Boehm9ac60bf2014-08-28 15:21:32 -070035
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
Hans Boehmf0f66c02014-08-14 15:26:03 -070049
50/* We have a usable C++ <atomic>; use it instead. */
51
52#include <atomic>
53
Hans Boehm2b10e2f2014-08-26 15:58:15 -070054#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. */
Hans Boehm9ac60bf2014-08-28 15:21:32 -070061
Hans Boehmf0f66c02014-08-14 15:26:03 -070062#define _Atomic(t) std::atomic<t>
63
64using std::atomic_is_lock_free;
65using std::atomic_init;
66using std::atomic_store;
67using std::atomic_store_explicit;
68using std::atomic_load;
69using std::atomic_load_explicit;
70using std::atomic_exchange;
71using std::atomic_exchange_explicit;
72using std::atomic_compare_exchange_strong;
73using std::atomic_compare_exchange_strong_explicit;
74using std::atomic_compare_exchange_weak;
75using std::atomic_compare_exchange_weak_explicit;
76using std::atomic_fetch_add;
77using std::atomic_fetch_add_explicit;
78using std::atomic_fetch_sub;
79using std::atomic_fetch_sub_explicit;
80using std::atomic_fetch_or;
81using std::atomic_fetch_or_explicit;
82using std::atomic_fetch_xor;
83using std::atomic_fetch_xor_explicit;
84using std::atomic_fetch_and;
85using std::atomic_fetch_and_explicit;
86using std::atomic_thread_fence;
87using std::atomic_signal_fence;
88
89using std::memory_order;
90using std::memory_order_relaxed;
91using std::memory_order_consume;
92using std::memory_order_release;
93using std::memory_order_acq_rel;
94using std::memory_order_seq_cst;
95
96using std::atomic_bool;
97using std::atomic_char;
98using std::atomic_schar;
99using std::atomic_uchar;
100using std::atomic_short;
101using std::atomic_ushort;
102using std::atomic_int;
103using std::atomic_uint;
104using std::atomic_long;
105using std::atomic_ulong;
106using std::atomic_llong;
107using std::atomic_ullong;
108using std::atomic_char16_t;
109using std::atomic_char32_t;
110using std::atomic_wchar_t;
111using std::atomic_int_least8_t;
112using std::atomic_uint_least8_t;
113using std::atomic_int_least16_t;
114using std::atomic_uint_least16_t;
115using std::atomic_int_least32_t;
116using std::atomic_uint_least32_t;
117using std::atomic_int_least64_t;
118using std::atomic_uint_least64_t;
119using std::atomic_int_fast8_t;
120using std::atomic_uint_fast8_t;
121using std::atomic_int_fast16_t;
122using std::atomic_uint_fast16_t;
123using std::atomic_int_fast32_t;
124using std::atomic_uint_fast32_t;
125using std::atomic_int_fast64_t;
126using std::atomic_uint_fast64_t;
127using std::atomic_intptr_t;
128using std::atomic_uintptr_t;
129using std::atomic_size_t;
130using std::atomic_ptrdiff_t;
131using std::atomic_intmax_t;
132using std::atomic_uintmax_t;
133
134#else /* <atomic> unavailable, possibly because this is C, not C++ */
135
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700136#include <sys/types.h>
137#include <stdbool.h>
138
Hans Boehmf0f66c02014-08-14 15:26:03 -0700139/*
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
Hans Boehmc8cf3512014-08-19 16:14:01 -0700146#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
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700152#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
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700161#else
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700162# 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
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700170#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
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700178#elif defined(__SYNC_ATOMICS)
179#define ATOMIC_BOOL_LOCK_FREE 2 /* For all modern platforms */
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700180#endif
181#ifdef __GCC_ATOMIC_CHAR_LOCK_FREE
182#define ATOMIC_CHAR_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700183#elif defined(__SYNC_ATOMICS)
184#define ATOMIC_CHAR_LOCK_FREE 2
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700185#endif
186#ifdef __GCC_ATOMIC_CHAR16_T_LOCK_FREE
187#define ATOMIC_CHAR16_T_LOCK_FREE __GCC_ATOMIC_CHAR16_T_LOCK_FREE
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700188#elif defined(__SYNC_ATOMICS)
189#define ATOMIC_CHAR16_T_LOCK_FREE 2
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700190#endif
191#ifdef __GCC_ATOMIC_CHAR32_T_LOCK_FREE
192#define ATOMIC_CHAR32_T_LOCK_FREE __GCC_ATOMIC_CHAR32_T_LOCK_FREE
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700193#elif defined(__SYNC_ATOMICS)
194#define ATOMIC_CHAR32_T_LOCK_FREE 2
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700195#endif
196#ifdef __GCC_ATOMIC_WCHAR_T_LOCK_FREE
197#define ATOMIC_WCHAR_T_LOCK_FREE __GCC_ATOMIC_WCHAR_T_LOCK_FREE
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700198#elif defined(__SYNC_ATOMICS)
199#define ATOMIC_WCHAR_T_LOCK_FREE 2
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700200#endif
201#ifdef __GCC_ATOMIC_SHORT_LOCK_FREE
202#define ATOMIC_SHORT_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700203#elif defined(__SYNC_ATOMICS)
204#define ATOMIC_SHORT_LOCK_FREE 2
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700205#endif
206#ifdef __GCC_ATOMIC_INT_LOCK_FREE
207#define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700208#elif defined(__SYNC_ATOMICS)
209#define ATOMIC_INT_LOCK_FREE 2
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700210#endif
211#ifdef __GCC_ATOMIC_LONG_LOCK_FREE
212#define ATOMIC_LONG_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700213#elif defined(__SYNC_ATOMICS)
214#define ATOMIC_LONG_LOCK_FREE 2
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700215#endif
216#ifdef __GCC_ATOMIC_LLONG_LOCK_FREE
217#define ATOMIC_LLONG_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700218#elif defined(__SYNC_ATOMICS)
219#define ATOMIC_LLONG_LOCK_FREE 1 /* maybe */
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700220#endif
221#ifdef __GCC_ATOMIC_POINTER_LOCK_FREE
222#define ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700223#elif defined(__SYNC_ATOMICS)
224#define ATOMIC_POINTER_LOCK_FREE 2
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700225#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
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700235#ifdef __ATOMICS_AVOID_DOT_INIT
236#define ATOMIC_VAR_INIT(value) { value }
237#else
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700238#define ATOMIC_VAR_INIT(value) { .__val = (value) }
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700239#endif
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700240#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.
Hans Boehmf0f66c02014-08-14 15:26:03 -0700273 * The enum values must be identical to those used by the
274 * C++ <atomic> header.
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700275 */
276
277typedef 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
290static __inline void
Hans Boehmc8cf3512014-08-19 16:14:01 -0700291atomic_thread_fence(memory_order __order __attribute__((unused)))
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700292{
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
303static __inline void
Hans Boehmc8cf3512014-08-19 16:14:01 -0700304atomic_signal_fence(memory_order __order __attribute__((unused)))
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700305{
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) \
Hans Boehmc8cf3512014-08-19 16:14:01 -0700326 __c11_atomic_is_lock_free(sizeof(*(obj)))
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700327#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
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700339#ifndef __CLANG_ATOMICS
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700340/*
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
347typedef _Atomic(bool) atomic_bool;
348typedef _Atomic(char) atomic_char;
349typedef _Atomic(signed char) atomic_schar;
350typedef _Atomic(unsigned char) atomic_uchar;
351typedef _Atomic(short) atomic_short;
352typedef _Atomic(unsigned short) atomic_ushort;
353typedef _Atomic(int) atomic_int;
354typedef _Atomic(unsigned int) atomic_uint;
355typedef _Atomic(long) atomic_long;
356typedef _Atomic(unsigned long) atomic_ulong;
357typedef _Atomic(long long) atomic_llong;
358typedef _Atomic(unsigned long long) atomic_ullong;
Hans Boehmed682212014-07-16 11:33:48 -0700359#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L
360 typedef _Atomic(char16_t) atomic_char16_t;
361 typedef _Atomic(char32_t) atomic_char32_t;
362#endif
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700363typedef _Atomic(wchar_t) atomic_wchar_t;
364typedef _Atomic(int_least8_t) atomic_int_least8_t;
365typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
366typedef _Atomic(int_least16_t) atomic_int_least16_t;
367typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
368typedef _Atomic(int_least32_t) atomic_int_least32_t;
369typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
370typedef _Atomic(int_least64_t) atomic_int_least64_t;
371typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
372typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
373typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
374typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
375typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
376typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
377typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
378typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
379typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
380typedef _Atomic(intptr_t) atomic_intptr_t;
381typedef _Atomic(uintptr_t) atomic_uintptr_t;
382typedef _Atomic(size_t) atomic_size_t;
383typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
384typedef _Atomic(intmax_t) atomic_intmax_t;
385typedef _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)
Hans Boehm9ac60bf2014-08-28 15:21:32 -0700460#ifdef __HAS_BUILTIN_SYNC_SWAP
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700461/* 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
536typedef struct {
537 atomic_bool __flag;
538} atomic_flag;
539
Hans Boehmc8cf3512014-08-19 16:14:01 -0700540#define ATOMIC_FLAG_INIT { ATOMIC_VAR_INIT(false) }
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700541
542static __inline bool
543atomic_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
549static __inline void
550atomic_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
557static __inline bool
558atomic_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
565static __inline void
566atomic_flag_clear(volatile atomic_flag *__object)
567{
568
569 atomic_flag_clear_explicit(__object, memory_order_seq_cst);
570}
571#endif /* !_KERNEL */
572
Hans Boehmf0f66c02014-08-14 15:26:03 -0700573#endif /* <atomic> unavailable */
574
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700575#endif /* !_STDATOMIC_H_ */