xref: /f-stack/freebsd/sys/stdatomic.h (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 2011 Ed Schouten <[email protected]>
5a9643ea8Slogwang  *                    David Chisnall <[email protected]>
6a9643ea8Slogwang  * All rights reserved.
7a9643ea8Slogwang  *
8a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
9a9643ea8Slogwang  * modification, are permitted provided that the following conditions
10a9643ea8Slogwang  * are met:
11a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
12a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
13a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
14a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
15a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
16a9643ea8Slogwang  *
17a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20a9643ea8Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27a9643ea8Slogwang  * SUCH DAMAGE.
28a9643ea8Slogwang  *
29a9643ea8Slogwang  * $FreeBSD$
30a9643ea8Slogwang  */
31a9643ea8Slogwang 
32a9643ea8Slogwang #ifndef _STDATOMIC_H_
33a9643ea8Slogwang #define	_STDATOMIC_H_
34a9643ea8Slogwang 
35a9643ea8Slogwang #include <sys/cdefs.h>
36a9643ea8Slogwang #include <sys/_types.h>
37a9643ea8Slogwang 
38a9643ea8Slogwang #if __has_extension(c_atomic) || __has_extension(cxx_atomic)
39a9643ea8Slogwang #define	__CLANG_ATOMICS
40a9643ea8Slogwang #elif __GNUC_PREREQ__(4, 7)
41a9643ea8Slogwang #define	__GNUC_ATOMICS
42a9643ea8Slogwang #elif defined(__GNUC__)
43a9643ea8Slogwang #define	__SYNC_ATOMICS
44a9643ea8Slogwang #else
45a9643ea8Slogwang #error "stdatomic.h does not support your compiler"
46a9643ea8Slogwang #endif
47a9643ea8Slogwang 
48a9643ea8Slogwang /*
49a9643ea8Slogwang  * 7.17.1 Atomic lock-free macros.
50a9643ea8Slogwang  */
51a9643ea8Slogwang 
52a9643ea8Slogwang #ifdef __GCC_ATOMIC_BOOL_LOCK_FREE
53a9643ea8Slogwang #define	ATOMIC_BOOL_LOCK_FREE		__GCC_ATOMIC_BOOL_LOCK_FREE
54a9643ea8Slogwang #endif
55a9643ea8Slogwang #ifdef __GCC_ATOMIC_CHAR_LOCK_FREE
56a9643ea8Slogwang #define	ATOMIC_CHAR_LOCK_FREE		__GCC_ATOMIC_CHAR_LOCK_FREE
57a9643ea8Slogwang #endif
58a9643ea8Slogwang #ifdef __GCC_ATOMIC_CHAR16_T_LOCK_FREE
59a9643ea8Slogwang #define	ATOMIC_CHAR16_T_LOCK_FREE	__GCC_ATOMIC_CHAR16_T_LOCK_FREE
60a9643ea8Slogwang #endif
61a9643ea8Slogwang #ifdef __GCC_ATOMIC_CHAR32_T_LOCK_FREE
62a9643ea8Slogwang #define	ATOMIC_CHAR32_T_LOCK_FREE	__GCC_ATOMIC_CHAR32_T_LOCK_FREE
63a9643ea8Slogwang #endif
64a9643ea8Slogwang #ifdef __GCC_ATOMIC_WCHAR_T_LOCK_FREE
65a9643ea8Slogwang #define	ATOMIC_WCHAR_T_LOCK_FREE	__GCC_ATOMIC_WCHAR_T_LOCK_FREE
66a9643ea8Slogwang #endif
67a9643ea8Slogwang #ifdef __GCC_ATOMIC_SHORT_LOCK_FREE
68a9643ea8Slogwang #define	ATOMIC_SHORT_LOCK_FREE		__GCC_ATOMIC_SHORT_LOCK_FREE
69a9643ea8Slogwang #endif
70a9643ea8Slogwang #ifdef __GCC_ATOMIC_INT_LOCK_FREE
71a9643ea8Slogwang #define	ATOMIC_INT_LOCK_FREE		__GCC_ATOMIC_INT_LOCK_FREE
72a9643ea8Slogwang #endif
73a9643ea8Slogwang #ifdef __GCC_ATOMIC_LONG_LOCK_FREE
74a9643ea8Slogwang #define	ATOMIC_LONG_LOCK_FREE		__GCC_ATOMIC_LONG_LOCK_FREE
75a9643ea8Slogwang #endif
76a9643ea8Slogwang #ifdef __GCC_ATOMIC_LLONG_LOCK_FREE
77a9643ea8Slogwang #define	ATOMIC_LLONG_LOCK_FREE		__GCC_ATOMIC_LLONG_LOCK_FREE
78a9643ea8Slogwang #endif
79a9643ea8Slogwang #ifdef __GCC_ATOMIC_POINTER_LOCK_FREE
80a9643ea8Slogwang #define	ATOMIC_POINTER_LOCK_FREE	__GCC_ATOMIC_POINTER_LOCK_FREE
81a9643ea8Slogwang #endif
82a9643ea8Slogwang 
83a9643ea8Slogwang /*
84a9643ea8Slogwang  * 7.17.2 Initialization.
85a9643ea8Slogwang  */
86a9643ea8Slogwang 
87a9643ea8Slogwang #if defined(__CLANG_ATOMICS)
88a9643ea8Slogwang #define	ATOMIC_VAR_INIT(value)		(value)
89a9643ea8Slogwang #define	atomic_init(obj, value)		__c11_atomic_init(obj, value)
90a9643ea8Slogwang #else
91a9643ea8Slogwang #define	ATOMIC_VAR_INIT(value)		{ .__val = (value) }
92a9643ea8Slogwang #define	atomic_init(obj, value)		((void)((obj)->__val = (value)))
93a9643ea8Slogwang #endif
94a9643ea8Slogwang 
95a9643ea8Slogwang /*
96a9643ea8Slogwang  * Clang and recent GCC both provide predefined macros for the memory
97a9643ea8Slogwang  * orderings.  If we are using a compiler that doesn't define them, use the
98a9643ea8Slogwang  * clang values - these will be ignored in the fallback path.
99a9643ea8Slogwang  */
100a9643ea8Slogwang 
101a9643ea8Slogwang #ifndef __ATOMIC_RELAXED
102a9643ea8Slogwang #define __ATOMIC_RELAXED		0
103a9643ea8Slogwang #endif
104a9643ea8Slogwang #ifndef __ATOMIC_CONSUME
105a9643ea8Slogwang #define __ATOMIC_CONSUME		1
106a9643ea8Slogwang #endif
107a9643ea8Slogwang #ifndef __ATOMIC_ACQUIRE
108a9643ea8Slogwang #define __ATOMIC_ACQUIRE		2
109a9643ea8Slogwang #endif
110a9643ea8Slogwang #ifndef __ATOMIC_RELEASE
111a9643ea8Slogwang #define __ATOMIC_RELEASE		3
112a9643ea8Slogwang #endif
113a9643ea8Slogwang #ifndef __ATOMIC_ACQ_REL
114a9643ea8Slogwang #define __ATOMIC_ACQ_REL		4
115a9643ea8Slogwang #endif
116a9643ea8Slogwang #ifndef __ATOMIC_SEQ_CST
117a9643ea8Slogwang #define __ATOMIC_SEQ_CST		5
118a9643ea8Slogwang #endif
119a9643ea8Slogwang 
120a9643ea8Slogwang /*
121a9643ea8Slogwang  * 7.17.3 Order and consistency.
122a9643ea8Slogwang  *
123a9643ea8Slogwang  * The memory_order_* constants that denote the barrier behaviour of the
124a9643ea8Slogwang  * atomic operations.
125a9643ea8Slogwang  */
126a9643ea8Slogwang 
127a9643ea8Slogwang typedef enum {
128a9643ea8Slogwang 	memory_order_relaxed = __ATOMIC_RELAXED,
129a9643ea8Slogwang 	memory_order_consume = __ATOMIC_CONSUME,
130a9643ea8Slogwang 	memory_order_acquire = __ATOMIC_ACQUIRE,
131a9643ea8Slogwang 	memory_order_release = __ATOMIC_RELEASE,
132a9643ea8Slogwang 	memory_order_acq_rel = __ATOMIC_ACQ_REL,
133a9643ea8Slogwang 	memory_order_seq_cst = __ATOMIC_SEQ_CST
134a9643ea8Slogwang } memory_order;
135a9643ea8Slogwang 
136a9643ea8Slogwang /*
137a9643ea8Slogwang  * 7.17.4 Fences.
138a9643ea8Slogwang  */
139a9643ea8Slogwang 
140a9643ea8Slogwang static __inline void
atomic_thread_fence(memory_order __order __unused)141a9643ea8Slogwang atomic_thread_fence(memory_order __order __unused)
142a9643ea8Slogwang {
143a9643ea8Slogwang 
144a9643ea8Slogwang #ifdef __CLANG_ATOMICS
145a9643ea8Slogwang 	__c11_atomic_thread_fence(__order);
146a9643ea8Slogwang #elif defined(__GNUC_ATOMICS)
147a9643ea8Slogwang 	__atomic_thread_fence(__order);
148a9643ea8Slogwang #else
149a9643ea8Slogwang 	__sync_synchronize();
150a9643ea8Slogwang #endif
151a9643ea8Slogwang }
152a9643ea8Slogwang 
153a9643ea8Slogwang static __inline void
atomic_signal_fence(memory_order __order __unused)154a9643ea8Slogwang atomic_signal_fence(memory_order __order __unused)
155a9643ea8Slogwang {
156a9643ea8Slogwang 
157a9643ea8Slogwang #ifdef __CLANG_ATOMICS
158a9643ea8Slogwang 	__c11_atomic_signal_fence(__order);
159a9643ea8Slogwang #elif defined(__GNUC_ATOMICS)
160a9643ea8Slogwang 	__atomic_signal_fence(__order);
161a9643ea8Slogwang #else
162a9643ea8Slogwang 	__asm volatile ("" ::: "memory");
163a9643ea8Slogwang #endif
164a9643ea8Slogwang }
165a9643ea8Slogwang 
166a9643ea8Slogwang /*
167a9643ea8Slogwang  * 7.17.5 Lock-free property.
168a9643ea8Slogwang  */
169a9643ea8Slogwang 
170a9643ea8Slogwang #if defined(_KERNEL)
171a9643ea8Slogwang /* Atomics in kernelspace are always lock-free. */
172a9643ea8Slogwang #define	atomic_is_lock_free(obj) \
173a9643ea8Slogwang 	((void)(obj), (_Bool)1)
174*22ce4affSfengbojiang #elif defined(__CLANG_ATOMICS) || defined(__GNUC_ATOMICS)
175a9643ea8Slogwang #define	atomic_is_lock_free(obj) \
176a9643ea8Slogwang 	__atomic_is_lock_free(sizeof(*(obj)), obj)
177a9643ea8Slogwang #else
178a9643ea8Slogwang #define	atomic_is_lock_free(obj) \
179a9643ea8Slogwang 	((void)(obj), sizeof((obj)->__val) <= sizeof(void *))
180a9643ea8Slogwang #endif
181a9643ea8Slogwang 
182a9643ea8Slogwang /*
183a9643ea8Slogwang  * 7.17.6 Atomic integer types.
184a9643ea8Slogwang  */
185a9643ea8Slogwang 
186a9643ea8Slogwang typedef _Atomic(_Bool)			atomic_bool;
187a9643ea8Slogwang typedef _Atomic(char)			atomic_char;
188a9643ea8Slogwang typedef _Atomic(signed char)		atomic_schar;
189a9643ea8Slogwang typedef _Atomic(unsigned char)		atomic_uchar;
190a9643ea8Slogwang typedef _Atomic(short)			atomic_short;
191a9643ea8Slogwang typedef _Atomic(unsigned short)		atomic_ushort;
192a9643ea8Slogwang typedef _Atomic(int)			atomic_int;
193a9643ea8Slogwang typedef _Atomic(unsigned int)		atomic_uint;
194a9643ea8Slogwang typedef _Atomic(long)			atomic_long;
195a9643ea8Slogwang typedef _Atomic(unsigned long)		atomic_ulong;
196a9643ea8Slogwang typedef _Atomic(long long)		atomic_llong;
197a9643ea8Slogwang typedef _Atomic(unsigned long long)	atomic_ullong;
198a9643ea8Slogwang typedef _Atomic(__char16_t)		atomic_char16_t;
199a9643ea8Slogwang typedef _Atomic(__char32_t)		atomic_char32_t;
200a9643ea8Slogwang typedef _Atomic(___wchar_t)		atomic_wchar_t;
201a9643ea8Slogwang typedef _Atomic(__int_least8_t)		atomic_int_least8_t;
202a9643ea8Slogwang typedef _Atomic(__uint_least8_t)	atomic_uint_least8_t;
203a9643ea8Slogwang typedef _Atomic(__int_least16_t)	atomic_int_least16_t;
204a9643ea8Slogwang typedef _Atomic(__uint_least16_t)	atomic_uint_least16_t;
205a9643ea8Slogwang typedef _Atomic(__int_least32_t)	atomic_int_least32_t;
206a9643ea8Slogwang typedef _Atomic(__uint_least32_t)	atomic_uint_least32_t;
207a9643ea8Slogwang typedef _Atomic(__int_least64_t)	atomic_int_least64_t;
208a9643ea8Slogwang typedef _Atomic(__uint_least64_t)	atomic_uint_least64_t;
209a9643ea8Slogwang typedef _Atomic(__int_fast8_t)		atomic_int_fast8_t;
210a9643ea8Slogwang typedef _Atomic(__uint_fast8_t)		atomic_uint_fast8_t;
211a9643ea8Slogwang typedef _Atomic(__int_fast16_t)		atomic_int_fast16_t;
212a9643ea8Slogwang typedef _Atomic(__uint_fast16_t)	atomic_uint_fast16_t;
213a9643ea8Slogwang typedef _Atomic(__int_fast32_t)		atomic_int_fast32_t;
214a9643ea8Slogwang typedef _Atomic(__uint_fast32_t)	atomic_uint_fast32_t;
215a9643ea8Slogwang typedef _Atomic(__int_fast64_t)		atomic_int_fast64_t;
216a9643ea8Slogwang typedef _Atomic(__uint_fast64_t)	atomic_uint_fast64_t;
217a9643ea8Slogwang typedef _Atomic(__intptr_t)		atomic_intptr_t;
218a9643ea8Slogwang typedef _Atomic(__uintptr_t)		atomic_uintptr_t;
219a9643ea8Slogwang typedef _Atomic(__size_t)		atomic_size_t;
220a9643ea8Slogwang typedef _Atomic(__ptrdiff_t)		atomic_ptrdiff_t;
221a9643ea8Slogwang typedef _Atomic(__intmax_t)		atomic_intmax_t;
222a9643ea8Slogwang typedef _Atomic(__uintmax_t)		atomic_uintmax_t;
223a9643ea8Slogwang 
224a9643ea8Slogwang /*
225a9643ea8Slogwang  * 7.17.7 Operations on atomic types.
226a9643ea8Slogwang  */
227a9643ea8Slogwang 
228a9643ea8Slogwang /*
229a9643ea8Slogwang  * Compiler-specific operations.
230a9643ea8Slogwang  */
231a9643ea8Slogwang 
232a9643ea8Slogwang #if defined(__CLANG_ATOMICS)
233a9643ea8Slogwang #define	atomic_compare_exchange_strong_explicit(object, expected,	\
234a9643ea8Slogwang     desired, success, failure)						\
235a9643ea8Slogwang 	__c11_atomic_compare_exchange_strong(object, expected, desired,	\
236a9643ea8Slogwang 	    success, failure)
237a9643ea8Slogwang #define	atomic_compare_exchange_weak_explicit(object, expected,		\
238a9643ea8Slogwang     desired, success, failure)						\
239a9643ea8Slogwang 	__c11_atomic_compare_exchange_weak(object, expected, desired,	\
240a9643ea8Slogwang 	    success, failure)
241a9643ea8Slogwang #define	atomic_exchange_explicit(object, desired, order)		\
242a9643ea8Slogwang 	__c11_atomic_exchange(object, desired, order)
243a9643ea8Slogwang #define	atomic_fetch_add_explicit(object, operand, order)		\
244a9643ea8Slogwang 	__c11_atomic_fetch_add(object, operand, order)
245a9643ea8Slogwang #define	atomic_fetch_and_explicit(object, operand, order)		\
246a9643ea8Slogwang 	__c11_atomic_fetch_and(object, operand, order)
247a9643ea8Slogwang #define	atomic_fetch_or_explicit(object, operand, order)		\
248a9643ea8Slogwang 	__c11_atomic_fetch_or(object, operand, order)
249a9643ea8Slogwang #define	atomic_fetch_sub_explicit(object, operand, order)		\
250a9643ea8Slogwang 	__c11_atomic_fetch_sub(object, operand, order)
251a9643ea8Slogwang #define	atomic_fetch_xor_explicit(object, operand, order)		\
252a9643ea8Slogwang 	__c11_atomic_fetch_xor(object, operand, order)
253a9643ea8Slogwang #define	atomic_load_explicit(object, order)				\
254a9643ea8Slogwang 	__c11_atomic_load(object, order)
255a9643ea8Slogwang #define	atomic_store_explicit(object, desired, order)			\
256a9643ea8Slogwang 	__c11_atomic_store(object, desired, order)
257a9643ea8Slogwang #elif defined(__GNUC_ATOMICS)
258a9643ea8Slogwang #define	atomic_compare_exchange_strong_explicit(object, expected,	\
259a9643ea8Slogwang     desired, success, failure)						\
260*22ce4affSfengbojiang 	__atomic_compare_exchange_n(object, expected,			\
261a9643ea8Slogwang 	    desired, 0, success, failure)
262a9643ea8Slogwang #define	atomic_compare_exchange_weak_explicit(object, expected,		\
263a9643ea8Slogwang     desired, success, failure)						\
264*22ce4affSfengbojiang 	__atomic_compare_exchange_n(object, expected,			\
265a9643ea8Slogwang 	    desired, 1, success, failure)
266a9643ea8Slogwang #define	atomic_exchange_explicit(object, desired, order)		\
267*22ce4affSfengbojiang 	__atomic_exchange_n(object, desired, order)
268a9643ea8Slogwang #define	atomic_fetch_add_explicit(object, operand, order)		\
269*22ce4affSfengbojiang 	__atomic_fetch_add(object, operand, order)
270a9643ea8Slogwang #define	atomic_fetch_and_explicit(object, operand, order)		\
271*22ce4affSfengbojiang 	__atomic_fetch_and(object, operand, order)
272a9643ea8Slogwang #define	atomic_fetch_or_explicit(object, operand, order)		\
273*22ce4affSfengbojiang 	__atomic_fetch_or(object, operand, order)
274a9643ea8Slogwang #define	atomic_fetch_sub_explicit(object, operand, order)		\
275*22ce4affSfengbojiang 	__atomic_fetch_sub(object, operand, order)
276a9643ea8Slogwang #define	atomic_fetch_xor_explicit(object, operand, order)		\
277*22ce4affSfengbojiang 	__atomic_fetch_xor(object, operand, order)
278a9643ea8Slogwang #define	atomic_load_explicit(object, order)				\
279*22ce4affSfengbojiang 	__atomic_load_n(object, order)
280a9643ea8Slogwang #define	atomic_store_explicit(object, desired, order)			\
281*22ce4affSfengbojiang 	__atomic_store_n(object, desired, order)
282a9643ea8Slogwang #else
283a9643ea8Slogwang #define	__atomic_apply_stride(object, operand) \
284a9643ea8Slogwang 	(((__typeof__((object)->__val))0) + (operand))
285a9643ea8Slogwang #define	atomic_compare_exchange_strong_explicit(object, expected,	\
286a9643ea8Slogwang     desired, success, failure)	__extension__ ({			\
287a9643ea8Slogwang 	__typeof__(expected) __ep = (expected);				\
288a9643ea8Slogwang 	__typeof__(*__ep) __e = *__ep;					\
289a9643ea8Slogwang 	(void)(success); (void)(failure);				\
290a9643ea8Slogwang 	(_Bool)((*__ep = __sync_val_compare_and_swap(&(object)->__val,	\
291a9643ea8Slogwang 	    __e, desired)) == __e);					\
292a9643ea8Slogwang })
293a9643ea8Slogwang #define	atomic_compare_exchange_weak_explicit(object, expected,		\
294a9643ea8Slogwang     desired, success, failure)						\
295a9643ea8Slogwang 	atomic_compare_exchange_strong_explicit(object, expected,	\
296a9643ea8Slogwang 		desired, success, failure)
297a9643ea8Slogwang #if __has_builtin(__sync_swap)
298a9643ea8Slogwang /* Clang provides a full-barrier atomic exchange - use it if available. */
299a9643ea8Slogwang #define	atomic_exchange_explicit(object, desired, order)		\
300a9643ea8Slogwang 	((void)(order), __sync_swap(&(object)->__val, desired))
301a9643ea8Slogwang #else
302a9643ea8Slogwang /*
303a9643ea8Slogwang  * __sync_lock_test_and_set() is only an acquire barrier in theory (although in
304a9643ea8Slogwang  * practice it is usually a full barrier) so we need an explicit barrier before
305a9643ea8Slogwang  * it.
306a9643ea8Slogwang  */
307a9643ea8Slogwang #define	atomic_exchange_explicit(object, desired, order)		\
308a9643ea8Slogwang __extension__ ({							\
309a9643ea8Slogwang 	__typeof__(object) __o = (object);				\
310a9643ea8Slogwang 	__typeof__(desired) __d = (desired);				\
311a9643ea8Slogwang 	(void)(order);							\
312a9643ea8Slogwang 	__sync_synchronize();						\
313a9643ea8Slogwang 	__sync_lock_test_and_set(&(__o)->__val, __d);			\
314a9643ea8Slogwang })
315a9643ea8Slogwang #endif
316a9643ea8Slogwang #define	atomic_fetch_add_explicit(object, operand, order)		\
317a9643ea8Slogwang 	((void)(order), __sync_fetch_and_add(&(object)->__val,		\
318a9643ea8Slogwang 	    __atomic_apply_stride(object, operand)))
319a9643ea8Slogwang #define	atomic_fetch_and_explicit(object, operand, order)		\
320a9643ea8Slogwang 	((void)(order), __sync_fetch_and_and(&(object)->__val, operand))
321a9643ea8Slogwang #define	atomic_fetch_or_explicit(object, operand, order)		\
322a9643ea8Slogwang 	((void)(order), __sync_fetch_and_or(&(object)->__val, operand))
323a9643ea8Slogwang #define	atomic_fetch_sub_explicit(object, operand, order)		\
324a9643ea8Slogwang 	((void)(order), __sync_fetch_and_sub(&(object)->__val,		\
325a9643ea8Slogwang 	    __atomic_apply_stride(object, operand)))
326a9643ea8Slogwang #define	atomic_fetch_xor_explicit(object, operand, order)		\
327a9643ea8Slogwang 	((void)(order), __sync_fetch_and_xor(&(object)->__val, operand))
328a9643ea8Slogwang #define	atomic_load_explicit(object, order)				\
329a9643ea8Slogwang 	((void)(order), __sync_fetch_and_add(&(object)->__val, 0))
330a9643ea8Slogwang #define	atomic_store_explicit(object, desired, order)			\
331a9643ea8Slogwang 	((void)atomic_exchange_explicit(object, desired, order))
332a9643ea8Slogwang #endif
333a9643ea8Slogwang 
334a9643ea8Slogwang /*
335a9643ea8Slogwang  * Convenience functions.
336a9643ea8Slogwang  *
337a9643ea8Slogwang  * Don't provide these in kernel space. In kernel space, we should be
338a9643ea8Slogwang  * disciplined enough to always provide explicit barriers.
339a9643ea8Slogwang  */
340a9643ea8Slogwang 
341a9643ea8Slogwang #ifndef _KERNEL
342a9643ea8Slogwang #define	atomic_compare_exchange_strong(object, expected, desired)	\
343a9643ea8Slogwang 	atomic_compare_exchange_strong_explicit(object, expected,	\
344a9643ea8Slogwang 	    desired, memory_order_seq_cst, memory_order_seq_cst)
345a9643ea8Slogwang #define	atomic_compare_exchange_weak(object, expected, desired)		\
346a9643ea8Slogwang 	atomic_compare_exchange_weak_explicit(object, expected,		\
347a9643ea8Slogwang 	    desired, memory_order_seq_cst, memory_order_seq_cst)
348a9643ea8Slogwang #define	atomic_exchange(object, desired)				\
349a9643ea8Slogwang 	atomic_exchange_explicit(object, desired, memory_order_seq_cst)
350a9643ea8Slogwang #define	atomic_fetch_add(object, operand)				\
351a9643ea8Slogwang 	atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
352a9643ea8Slogwang #define	atomic_fetch_and(object, operand)				\
353a9643ea8Slogwang 	atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
354a9643ea8Slogwang #define	atomic_fetch_or(object, operand)				\
355a9643ea8Slogwang 	atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
356a9643ea8Slogwang #define	atomic_fetch_sub(object, operand)				\
357a9643ea8Slogwang 	atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
358a9643ea8Slogwang #define	atomic_fetch_xor(object, operand)				\
359a9643ea8Slogwang 	atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
360a9643ea8Slogwang #define	atomic_load(object)						\
361a9643ea8Slogwang 	atomic_load_explicit(object, memory_order_seq_cst)
362a9643ea8Slogwang #define	atomic_store(object, desired)					\
363a9643ea8Slogwang 	atomic_store_explicit(object, desired, memory_order_seq_cst)
364a9643ea8Slogwang #endif /* !_KERNEL */
365a9643ea8Slogwang 
366a9643ea8Slogwang /*
367a9643ea8Slogwang  * 7.17.8 Atomic flag type and operations.
368a9643ea8Slogwang  *
369a9643ea8Slogwang  * XXX: Assume atomic_bool can be used as an atomic_flag. Is there some
370a9643ea8Slogwang  * kind of compiler built-in type we could use?
371a9643ea8Slogwang  */
372a9643ea8Slogwang 
373a9643ea8Slogwang typedef struct {
374a9643ea8Slogwang 	atomic_bool	__flag;
375a9643ea8Slogwang } atomic_flag;
376a9643ea8Slogwang #define	ATOMIC_FLAG_INIT		{ ATOMIC_VAR_INIT(0) }
377a9643ea8Slogwang 
378a9643ea8Slogwang static __inline _Bool
atomic_flag_test_and_set_explicit(volatile atomic_flag * __object,memory_order __order)379a9643ea8Slogwang atomic_flag_test_and_set_explicit(volatile atomic_flag *__object,
380a9643ea8Slogwang     memory_order __order)
381a9643ea8Slogwang {
382a9643ea8Slogwang 	return (atomic_exchange_explicit(&__object->__flag, 1, __order));
383a9643ea8Slogwang }
384a9643ea8Slogwang 
385a9643ea8Slogwang static __inline void
atomic_flag_clear_explicit(volatile atomic_flag * __object,memory_order __order)386a9643ea8Slogwang atomic_flag_clear_explicit(volatile atomic_flag *__object, memory_order __order)
387a9643ea8Slogwang {
388a9643ea8Slogwang 
389a9643ea8Slogwang 	atomic_store_explicit(&__object->__flag, 0, __order);
390a9643ea8Slogwang }
391a9643ea8Slogwang 
392a9643ea8Slogwang #ifndef _KERNEL
393a9643ea8Slogwang static __inline _Bool
atomic_flag_test_and_set(volatile atomic_flag * __object)394a9643ea8Slogwang atomic_flag_test_and_set(volatile atomic_flag *__object)
395a9643ea8Slogwang {
396a9643ea8Slogwang 
397a9643ea8Slogwang 	return (atomic_flag_test_and_set_explicit(__object,
398a9643ea8Slogwang 	    memory_order_seq_cst));
399a9643ea8Slogwang }
400a9643ea8Slogwang 
401a9643ea8Slogwang static __inline void
atomic_flag_clear(volatile atomic_flag * __object)402a9643ea8Slogwang atomic_flag_clear(volatile atomic_flag *__object)
403a9643ea8Slogwang {
404a9643ea8Slogwang 
405a9643ea8Slogwang 	atomic_flag_clear_explicit(__object, memory_order_seq_cst);
406a9643ea8Slogwang }
407a9643ea8Slogwang #endif /* !_KERNEL */
408a9643ea8Slogwang 
409a9643ea8Slogwang #endif /* !_STDATOMIC_H_ */
410