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