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