xref: /freebsd/sys/sys/stdatomic.h (revision 4c860db34a1b8cff79e7f0d5184423221613e8e1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
30 #ifndef __STDC_VERSION_STDATOMIC_H__
31 #define __STDC_VERSION_STDATOMIC_H__	202311L
32 
33 #include <sys/cdefs.h>
34 #include <sys/_types.h>
35 
36 #if (__has_extension(c_atomic) || __has_extension(cxx_atomic)) && \
37     defined(__clang__)
38 #define	__CLANG_ATOMICS
39 #elif __GNUC_PREREQ__(4, 7)
40 #define	__GNUC_ATOMICS
41 #elif defined(__GNUC__)
42 #define	__SYNC_ATOMICS
43 #else
44 #error "stdatomic.h does not support your compiler"
45 #endif
46 
47 /*
48  * 7.17.1 Atomic lock-free macros.
49  */
50 
51 #ifdef __GCC_ATOMIC_BOOL_LOCK_FREE
52 #define	ATOMIC_BOOL_LOCK_FREE		__GCC_ATOMIC_BOOL_LOCK_FREE
53 #endif
54 #ifdef __GCC_ATOMIC_CHAR_LOCK_FREE
55 #define	ATOMIC_CHAR_LOCK_FREE		__GCC_ATOMIC_CHAR_LOCK_FREE
56 #endif
57 #if __ISO_C_VISIBLE >= 2023 && defined(__GCC_ATOMIC_CHAR8_T_LOCK_FREE)
58 #define	ATOMIC_CHAR8_T_LOCK_FREE	__GCC_ATOMIC_CHAR8_T_LOCK_FREE
59 #endif
60 #ifdef __GCC_ATOMIC_CHAR16_T_LOCK_FREE
61 #define	ATOMIC_CHAR16_T_LOCK_FREE	__GCC_ATOMIC_CHAR16_T_LOCK_FREE
62 #endif
63 #ifdef __GCC_ATOMIC_CHAR32_T_LOCK_FREE
64 #define	ATOMIC_CHAR32_T_LOCK_FREE	__GCC_ATOMIC_CHAR32_T_LOCK_FREE
65 #endif
66 #ifdef __GCC_ATOMIC_WCHAR_T_LOCK_FREE
67 #define	ATOMIC_WCHAR_T_LOCK_FREE	__GCC_ATOMIC_WCHAR_T_LOCK_FREE
68 #endif
69 #ifdef __GCC_ATOMIC_SHORT_LOCK_FREE
70 #define	ATOMIC_SHORT_LOCK_FREE		__GCC_ATOMIC_SHORT_LOCK_FREE
71 #endif
72 #ifdef __GCC_ATOMIC_INT_LOCK_FREE
73 #define	ATOMIC_INT_LOCK_FREE		__GCC_ATOMIC_INT_LOCK_FREE
74 #endif
75 #ifdef __GCC_ATOMIC_LONG_LOCK_FREE
76 #define	ATOMIC_LONG_LOCK_FREE		__GCC_ATOMIC_LONG_LOCK_FREE
77 #endif
78 #ifdef __GCC_ATOMIC_LLONG_LOCK_FREE
79 #define	ATOMIC_LLONG_LOCK_FREE		__GCC_ATOMIC_LLONG_LOCK_FREE
80 #endif
81 #ifdef __GCC_ATOMIC_POINTER_LOCK_FREE
82 #define	ATOMIC_POINTER_LOCK_FREE	__GCC_ATOMIC_POINTER_LOCK_FREE
83 #endif
84 
85 /*
86  * 7.17.2 Initialization.
87  */
88 
89 #if __BSD_VISIBLE || __ISO_C_VISIBLE < 2023 || defined(__cplusplus)
90 #if defined(__CLANG_ATOMICS)
91 #define	ATOMIC_VAR_INIT(value)		(value)
92 #else
93 #define	ATOMIC_VAR_INIT(value)		{ .__val = (value) }
94 #endif
95 #endif
96 
97 #if defined(__CLANG_ATOMICS)
98 #define	atomic_init(obj, value)		__c11_atomic_init(obj, value)
99 #elif defined(__GNUC_ATOMICS)
100 #define	atomic_init(obj, value)		atomic_store_explicit(obj,	\
101 	    value, __ATOMIC_RELAXED)
102 #else
103 #define	atomic_init(obj, value)		((void)((obj)->__val = (value)))
104 #endif
105 
106 /*
107  * 7.17.3 Order and consistency.
108  *
109  * The memory_order_* constants that denote the barrier behaviour of the
110  * atomic operations.
111  */
112 
113 #ifndef __ATOMIC_RELAXED
114 #define __ATOMIC_RELAXED		0
115 #endif
116 #ifndef __ATOMIC_CONSUME
117 #define __ATOMIC_CONSUME		1
118 #endif
119 #ifndef __ATOMIC_ACQUIRE
120 #define __ATOMIC_ACQUIRE		2
121 #endif
122 #ifndef __ATOMIC_RELEASE
123 #define __ATOMIC_RELEASE		3
124 #endif
125 #ifndef __ATOMIC_ACQ_REL
126 #define __ATOMIC_ACQ_REL		4
127 #endif
128 #ifndef __ATOMIC_SEQ_CST
129 #define __ATOMIC_SEQ_CST		5
130 #endif
131 
132 typedef enum {
133 	memory_order_relaxed = __ATOMIC_RELAXED,
134 	memory_order_consume = __ATOMIC_CONSUME,
135 	memory_order_acquire = __ATOMIC_ACQUIRE,
136 	memory_order_release = __ATOMIC_RELEASE,
137 	memory_order_acq_rel = __ATOMIC_ACQ_REL,
138 	memory_order_seq_cst = __ATOMIC_SEQ_CST
139 } memory_order;
140 
141 #define	kill_dependency(y)		(y)
142 
143 /*
144  * 7.17.4 Fences.
145  */
146 
147 static __inline void
atomic_thread_fence(memory_order __order __unused)148 atomic_thread_fence(memory_order __order __unused)
149 {
150 
151 #ifdef __CLANG_ATOMICS
152 	__c11_atomic_thread_fence(__order);
153 #elif defined(__GNUC_ATOMICS)
154 	__atomic_thread_fence(__order);
155 #else
156 	__sync_synchronize();
157 #endif
158 }
159 
160 static __inline void
atomic_signal_fence(memory_order __order __unused)161 atomic_signal_fence(memory_order __order __unused)
162 {
163 
164 #ifdef __CLANG_ATOMICS
165 	__c11_atomic_signal_fence(__order);
166 #elif defined(__GNUC_ATOMICS)
167 	__atomic_signal_fence(__order);
168 #else
169 	__asm volatile ("" ::: "memory");
170 #endif
171 }
172 
173 #if defined(__cplusplus) && !defined(_Bool)
174 #define	_Bool	bool
175 #define	__bool_locally_defined
176 #endif
177 
178 /*
179  * 7.17.5 Lock-free property.
180  */
181 
182 #if defined(_KERNEL)
183 /* Atomics in kernelspace are always lock-free. */
184 #define	atomic_is_lock_free(obj) \
185 	((void)(obj), (_Bool)1)
186 #elif defined(__CLANG_ATOMICS) || defined(__GNUC_ATOMICS)
187 #define	atomic_is_lock_free(obj) \
188 	__atomic_is_lock_free(sizeof(*(obj)), obj)
189 #else
190 #define	atomic_is_lock_free(obj) \
191 	((void)(obj), sizeof((obj)->__val) <= sizeof(void *))
192 #endif
193 
194 /*
195  * 7.17.6 Atomic integer types.
196  */
197 
198 typedef _Atomic(_Bool)			atomic_bool;
199 typedef _Atomic(char)			atomic_char;
200 typedef _Atomic(signed char)		atomic_schar;
201 typedef _Atomic(unsigned char)		atomic_uchar;
202 typedef _Atomic(short)			atomic_short;
203 typedef _Atomic(unsigned short)		atomic_ushort;
204 typedef _Atomic(int)			atomic_int;
205 typedef _Atomic(unsigned int)		atomic_uint;
206 typedef _Atomic(long)			atomic_long;
207 typedef _Atomic(unsigned long)		atomic_ulong;
208 typedef _Atomic(long long)		atomic_llong;
209 typedef _Atomic(unsigned long long)	atomic_ullong;
210 #if __ISO_C_VISIBLE >= 2023
211 typedef _Atomic(unsigned char)		atomic_char8_t;
212 #endif
213 typedef _Atomic(__char16_t)		atomic_char16_t;
214 typedef _Atomic(__char32_t)		atomic_char32_t;
215 typedef _Atomic(___wchar_t)		atomic_wchar_t;
216 typedef _Atomic(__int_least8_t)		atomic_int_least8_t;
217 typedef _Atomic(__uint_least8_t)	atomic_uint_least8_t;
218 typedef _Atomic(__int_least16_t)	atomic_int_least16_t;
219 typedef _Atomic(__uint_least16_t)	atomic_uint_least16_t;
220 typedef _Atomic(__int_least32_t)	atomic_int_least32_t;
221 typedef _Atomic(__uint_least32_t)	atomic_uint_least32_t;
222 typedef _Atomic(__int_least64_t)	atomic_int_least64_t;
223 typedef _Atomic(__uint_least64_t)	atomic_uint_least64_t;
224 typedef _Atomic(__int_fast8_t)		atomic_int_fast8_t;
225 typedef _Atomic(__uint_fast8_t)		atomic_uint_fast8_t;
226 typedef _Atomic(__int_fast16_t)		atomic_int_fast16_t;
227 typedef _Atomic(__uint_fast16_t)	atomic_uint_fast16_t;
228 typedef _Atomic(__int_fast32_t)		atomic_int_fast32_t;
229 typedef _Atomic(__uint_fast32_t)	atomic_uint_fast32_t;
230 typedef _Atomic(__int_fast64_t)		atomic_int_fast64_t;
231 typedef _Atomic(__uint_fast64_t)	atomic_uint_fast64_t;
232 typedef _Atomic(__intptr_t)		atomic_intptr_t;
233 typedef _Atomic(__uintptr_t)		atomic_uintptr_t;
234 typedef _Atomic(__size_t)		atomic_size_t;
235 typedef _Atomic(__ptrdiff_t)		atomic_ptrdiff_t;
236 typedef _Atomic(__intmax_t)		atomic_intmax_t;
237 typedef _Atomic(__uintmax_t)		atomic_uintmax_t;
238 
239 /*
240  * 7.17.7 Operations on atomic types.
241  */
242 
243 /*
244  * Compiler-specific operations.
245  */
246 
247 #if defined(__CLANG_ATOMICS)
248 #define	atomic_store_explicit(object, desired, order)			\
249 	__c11_atomic_store(object, desired, order)
250 #define	atomic_load_explicit(object, order)				\
251 	__c11_atomic_load(object, order)
252 #define	atomic_exchange_explicit(object, desired, order)		\
253 	__c11_atomic_exchange(object, desired, order)
254 #define	atomic_compare_exchange_strong_explicit(object, expected,	\
255     desired, success, failure)						\
256 	__c11_atomic_compare_exchange_strong(object, expected, desired,	\
257 	    success, failure)
258 #define	atomic_compare_exchange_weak_explicit(object, expected,		\
259     desired, success, failure)						\
260 	__c11_atomic_compare_exchange_weak(object, expected, desired,	\
261 	    success, failure)
262 #define	atomic_fetch_add_explicit(object, operand, order)		\
263 	__c11_atomic_fetch_add(object, operand, order)
264 #define	atomic_fetch_sub_explicit(object, operand, order)		\
265 	__c11_atomic_fetch_sub(object, operand, order)
266 #define	atomic_fetch_or_explicit(object, operand, order)		\
267 	__c11_atomic_fetch_or(object, operand, order)
268 #define	atomic_fetch_xor_explicit(object, operand, order)		\
269 	__c11_atomic_fetch_xor(object, operand, order)
270 #define	atomic_fetch_and_explicit(object, operand, order)		\
271 	__c11_atomic_fetch_and(object, operand, order)
272 #elif defined(__GNUC_ATOMICS)
273 #define	__atomic_apply_stride(object, operand)				\
274 	(((__typeof__(*(object)))0) + (operand))
275 #define	atomic_store_explicit(object, desired, order)			\
276 	__atomic_store_n(object, desired, order)
277 #define	atomic_load_explicit(object, order)				\
278 	__atomic_load_n(object, order)
279 #define	atomic_exchange_explicit(object, desired, order)		\
280 	__atomic_exchange_n(object, desired, order)
281 #define	atomic_compare_exchange_strong_explicit(object, expected,	\
282     desired, success, failure)						\
283 	__atomic_compare_exchange_n(object, expected,			\
284 	    desired, 0, success, failure)
285 #define	atomic_compare_exchange_weak_explicit(object, expected,		\
286     desired, success, failure)						\
287 	__atomic_compare_exchange_n(object, expected,			\
288 	    desired, 1, success, failure)
289 #define	atomic_fetch_add_explicit(object, operand, order)		\
290 	__atomic_fetch_add(object,					\
291 	    __atomic_apply_stride(object, operand), order)
292 #define	atomic_fetch_sub_explicit(object, operand, order)		\
293 	__atomic_fetch_sub(object,					\
294 	    __atomic_apply_stride(object, operand), order)
295 #define	atomic_fetch_or_explicit(object, operand, order)		\
296 	__atomic_fetch_or(object, operand, order)
297 #define	atomic_fetch_xor_explicit(object, operand, order)		\
298 	__atomic_fetch_xor(object, operand, order)
299 #define	atomic_fetch_and_explicit(object, operand, order)		\
300 	__atomic_fetch_and(object, operand, order)
301 #else
302 #define	__atomic_apply_stride(object, operand) \
303 	(((__typeof__((object)->__val))0) + (operand))
304 #define	atomic_store_explicit(object, desired, order)			\
305 	((void)atomic_exchange_explicit(object, desired, order))
306 #define	atomic_load_explicit(object, order)				\
307 	((void)(order), __sync_fetch_and_add(&(object)->__val, 0))
308 #if __has_builtin(__sync_swap)
309 /* Clang provides a full-barrier atomic exchange - use it if available. */
310 #define	atomic_exchange_explicit(object, desired, order)		\
311 	((void)(order), __sync_swap(&(object)->__val, desired))
312 #else
313 /*
314  * __sync_lock_test_and_set() is only an acquire barrier in theory (although in
315  * practice it is usually a full barrier) so we need an explicit barrier before
316  * it.
317  */
318 #define	atomic_exchange_explicit(object, desired, order)		\
319 __extension__ ({							\
320 	__typeof__(object) __o = (object);				\
321 	__typeof__(desired) __d = (desired);				\
322 	(void)(order);							\
323 	__sync_synchronize();						\
324 	__sync_lock_test_and_set(&(__o)->__val, __d);			\
325 })
326 #define	atomic_compare_exchange_strong_explicit(object, expected,	\
327     desired, success, failure)	__extension__ ({			\
328 	__typeof__(expected) __ep = (expected);				\
329 	__typeof__(*__ep) __e = *__ep;					\
330 	(void)(success); (void)(failure);				\
331 	(_Bool)((*__ep = __sync_val_compare_and_swap(&(object)->__val,	\
332 	    __e, desired)) == __e);					\
333 })
334 #define	atomic_compare_exchange_weak_explicit(object, expected,		\
335     desired, success, failure)						\
336 	atomic_compare_exchange_strong_explicit(object, expected,	\
337 		desired, success, failure)
338 #endif
339 #define	atomic_fetch_add_explicit(object, operand, order)		\
340 	((void)(order), __sync_fetch_and_add(&(object)->__val,		\
341 	    __atomic_apply_stride(object, operand)))
342 #define	atomic_fetch_sub_explicit(object, operand, order)		\
343 	((void)(order), __sync_fetch_and_sub(&(object)->__val,		\
344 	    __atomic_apply_stride(object, operand)))
345 #define	atomic_fetch_or_explicit(object, operand, order)		\
346 	((void)(order), __sync_fetch_and_or(&(object)->__val, operand))
347 #define	atomic_fetch_xor_explicit(object, operand, order)		\
348 	((void)(order), __sync_fetch_and_xor(&(object)->__val, operand))
349 #define	atomic_fetch_and_explicit(object, operand, order)		\
350 	((void)(order), __sync_fetch_and_and(&(object)->__val, operand))
351 #endif
352 
353 /*
354  * Convenience functions.
355  *
356  * Don't provide these in kernel space. In kernel space, we should be
357  * disciplined enough to always provide explicit barriers.
358  */
359 
360 #ifndef _KERNEL
361 #define	atomic_store(object, desired)					\
362 	atomic_store_explicit(object, desired, memory_order_seq_cst)
363 #define	atomic_load(object)						\
364 	atomic_load_explicit(object, memory_order_seq_cst)
365 #define	atomic_exchange(object, desired)				\
366 	atomic_exchange_explicit(object, desired, memory_order_seq_cst)
367 #define	atomic_compare_exchange_strong(object, expected, desired)	\
368 	atomic_compare_exchange_strong_explicit(object, expected,	\
369 	    desired, memory_order_seq_cst, memory_order_seq_cst)
370 #define	atomic_compare_exchange_weak(object, expected, desired)		\
371 	atomic_compare_exchange_weak_explicit(object, expected,		\
372 	    desired, memory_order_seq_cst, memory_order_seq_cst)
373 #define	atomic_fetch_add(object, operand)				\
374 	atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
375 #define	atomic_fetch_sub(object, operand)				\
376 	atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
377 #define	atomic_fetch_or(object, operand)				\
378 	atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
379 #define	atomic_fetch_xor(object, operand)				\
380 	atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
381 #define	atomic_fetch_and(object, operand)				\
382 	atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
383 #endif /* !_KERNEL */
384 
385 /*
386  * 7.17.8 Atomic flag type and operations.
387  */
388 
389 typedef struct {
390 #if ATOMIC_BOOL_LOCK_FREE == 2
391 	atomic_bool	__flag;
392 #elif ATOMIC_CHAR_LOCK_FREE == 2
393 	atomic_uchar	__flag;
394 #else
395 #error "atomic_flag is required to be lock-free"
396 #endif
397 } atomic_flag;
398 #if __ISO_C_VISIBLE < 2023
399 #define	ATOMIC_FLAG_INIT		{ ATOMIC_VAR_INIT(0) }
400 #else
401 #define	ATOMIC_FLAG_INIT		{ 0 }
402 #endif
403 
404 static __inline _Bool
atomic_flag_test_and_set_explicit(volatile atomic_flag * __object,memory_order __order)405 atomic_flag_test_and_set_explicit(volatile atomic_flag *__object,
406     memory_order __order)
407 {
408 
409 	return (atomic_exchange_explicit(&__object->__flag, 1, __order) != 0);
410 }
411 
412 static __inline void
atomic_flag_clear_explicit(volatile atomic_flag * __object,memory_order __order)413 atomic_flag_clear_explicit(volatile atomic_flag *__object, memory_order __order)
414 {
415 
416 	atomic_store_explicit(&__object->__flag, 0, __order);
417 }
418 
419 #ifndef _KERNEL
420 static __inline _Bool
atomic_flag_test_and_set(volatile atomic_flag * __object)421 atomic_flag_test_and_set(volatile atomic_flag *__object)
422 {
423 
424 	return (atomic_flag_test_and_set_explicit(__object,
425 	    memory_order_seq_cst));
426 }
427 
428 static __inline void
atomic_flag_clear(volatile atomic_flag * __object)429 atomic_flag_clear(volatile atomic_flag *__object)
430 {
431 
432 	atomic_flag_clear_explicit(__object, memory_order_seq_cst);
433 }
434 #endif /* !_KERNEL */
435 
436 #ifdef __bool_locally_defined
437 #undef _Bool
438 #undef __bool_locally_defined
439 #endif
440 
441 #endif /* !__STDC_VERSION_STDATOMIC_H__ */
442