blob: 99ab5360315f345be09a41893fc2fcfad07bcdfc [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 Boehm019d3952014-08-14 15:26:03 -070034
35#if defined(__cplusplus) && defined(_USING_LIBCXX) && \
36 (__has_feature(cxx_atomic) || _GNUC_VER >= 407)
37
38/* We have a usable C++ <atomic>; use it instead. */
39
40#include <atomic>
41
Hans Boehm3e4a0092014-08-26 15:58:15 -070042#undef _Atomic
43 /* Also defined by <atomic> for gcc. But not used in macros. */
44 /* Also a clang intrinsic. */
45 /* Should not be used by client code before this file is */
46 /* included. The definitions in <atomic> themselves see */
47 /* the old definition, as they should. */
48 /* Client code sees the following definition. */
Hans Boehm019d3952014-08-14 15:26:03 -070049#define _Atomic(t) std::atomic<t>
50
51using std::atomic_is_lock_free;
52using std::atomic_init;
53using std::atomic_store;
54using std::atomic_store_explicit;
55using std::atomic_load;
56using std::atomic_load_explicit;
57using std::atomic_exchange;
58using std::atomic_exchange_explicit;
59using std::atomic_compare_exchange_strong;
60using std::atomic_compare_exchange_strong_explicit;
61using std::atomic_compare_exchange_weak;
62using std::atomic_compare_exchange_weak_explicit;
63using std::atomic_fetch_add;
64using std::atomic_fetch_add_explicit;
65using std::atomic_fetch_sub;
66using std::atomic_fetch_sub_explicit;
67using std::atomic_fetch_or;
68using std::atomic_fetch_or_explicit;
69using std::atomic_fetch_xor;
70using std::atomic_fetch_xor_explicit;
71using std::atomic_fetch_and;
72using std::atomic_fetch_and_explicit;
73using std::atomic_thread_fence;
74using std::atomic_signal_fence;
75
76using std::memory_order;
77using std::memory_order_relaxed;
78using std::memory_order_consume;
79using std::memory_order_release;
80using std::memory_order_acq_rel;
81using std::memory_order_seq_cst;
82
83using std::atomic_bool;
84using std::atomic_char;
85using std::atomic_schar;
86using std::atomic_uchar;
87using std::atomic_short;
88using std::atomic_ushort;
89using std::atomic_int;
90using std::atomic_uint;
91using std::atomic_long;
92using std::atomic_ulong;
93using std::atomic_llong;
94using std::atomic_ullong;
95using std::atomic_char16_t;
96using std::atomic_char32_t;
97using std::atomic_wchar_t;
98using std::atomic_int_least8_t;
99using std::atomic_uint_least8_t;
100using std::atomic_int_least16_t;
101using std::atomic_uint_least16_t;
102using std::atomic_int_least32_t;
103using std::atomic_uint_least32_t;
104using std::atomic_int_least64_t;
105using std::atomic_uint_least64_t;
106using std::atomic_int_fast8_t;
107using std::atomic_uint_fast8_t;
108using std::atomic_int_fast16_t;
109using std::atomic_uint_fast16_t;
110using std::atomic_int_fast32_t;
111using std::atomic_uint_fast32_t;
112using std::atomic_int_fast64_t;
113using std::atomic_uint_fast64_t;
114using std::atomic_intptr_t;
115using std::atomic_uintptr_t;
116using std::atomic_size_t;
117using std::atomic_ptrdiff_t;
118using std::atomic_intmax_t;
119using std::atomic_uintmax_t;
120
121#else /* <atomic> unavailable, possibly because this is C, not C++ */
122
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700123#include <sys/types.h>
124#include <stdbool.h>
125
Hans Boehm019d3952014-08-14 15:26:03 -0700126/*
127 * C: Do it ourselves.
128 * Note that the runtime representation defined here should be compatible
129 * with the C++ one, i.e. an _Atomic(T) needs to contain the same
130 * bits as a T.
131 */
132
Hans Boehm00aaea32014-08-19 16:14:01 -0700133#include <stddef.h> /* For ptrdiff_t. */
134#include <stdint.h> /* TODO: Should pollute namespace less. */
135#if __STDC_VERSION__ >= 201112L
136# include <uchar.h> /* For char16_t and char32_t. */
137#endif
138
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700139#if __has_extension(c_atomic) || __has_extension(cxx_atomic)
140#define __CLANG_ATOMICS
141#elif __GNUC_PREREQ__(4, 7)
142#define __GNUC_ATOMICS
143#elif defined(__GNUC__)
144#define __SYNC_ATOMICS
145#else
146#error "stdatomic.h does not support your compiler"
147#endif
148
149/*
150 * 7.17.1 Atomic lock-free macros.
151 */
152
153#ifdef __GCC_ATOMIC_BOOL_LOCK_FREE
154#define ATOMIC_BOOL_LOCK_FREE __GCC_ATOMIC_BOOL_LOCK_FREE
155#endif
156#ifdef __GCC_ATOMIC_CHAR_LOCK_FREE
157#define ATOMIC_CHAR_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE
158#endif
159#ifdef __GCC_ATOMIC_CHAR16_T_LOCK_FREE
160#define ATOMIC_CHAR16_T_LOCK_FREE __GCC_ATOMIC_CHAR16_T_LOCK_FREE
161#endif
162#ifdef __GCC_ATOMIC_CHAR32_T_LOCK_FREE
163#define ATOMIC_CHAR32_T_LOCK_FREE __GCC_ATOMIC_CHAR32_T_LOCK_FREE
164#endif
165#ifdef __GCC_ATOMIC_WCHAR_T_LOCK_FREE
166#define ATOMIC_WCHAR_T_LOCK_FREE __GCC_ATOMIC_WCHAR_T_LOCK_FREE
167#endif
168#ifdef __GCC_ATOMIC_SHORT_LOCK_FREE
169#define ATOMIC_SHORT_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
170#endif
171#ifdef __GCC_ATOMIC_INT_LOCK_FREE
172#define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
173#endif
174#ifdef __GCC_ATOMIC_LONG_LOCK_FREE
175#define ATOMIC_LONG_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
176#endif
177#ifdef __GCC_ATOMIC_LLONG_LOCK_FREE
178#define ATOMIC_LLONG_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
179#endif
180#ifdef __GCC_ATOMIC_POINTER_LOCK_FREE
181#define ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE
182#endif
183
184/*
185 * 7.17.2 Initialization.
186 */
187
188#if defined(__CLANG_ATOMICS)
189#define ATOMIC_VAR_INIT(value) (value)
190#define atomic_init(obj, value) __c11_atomic_init(obj, value)
191#else
192#define ATOMIC_VAR_INIT(value) { .__val = (value) }
193#define atomic_init(obj, value) ((void)((obj)->__val = (value)))
194#endif
195
196/*
197 * Clang and recent GCC both provide predefined macros for the memory
198 * orderings. If we are using a compiler that doesn't define them, use the
199 * clang values - these will be ignored in the fallback path.
200 */
201
202#ifndef __ATOMIC_RELAXED
203#define __ATOMIC_RELAXED 0
204#endif
205#ifndef __ATOMIC_CONSUME
206#define __ATOMIC_CONSUME 1
207#endif
208#ifndef __ATOMIC_ACQUIRE
209#define __ATOMIC_ACQUIRE 2
210#endif
211#ifndef __ATOMIC_RELEASE
212#define __ATOMIC_RELEASE 3
213#endif
214#ifndef __ATOMIC_ACQ_REL
215#define __ATOMIC_ACQ_REL 4
216#endif
217#ifndef __ATOMIC_SEQ_CST
218#define __ATOMIC_SEQ_CST 5
219#endif
220
221/*
222 * 7.17.3 Order and consistency.
223 *
224 * The memory_order_* constants that denote the barrier behaviour of the
225 * atomic operations.
Hans Boehm019d3952014-08-14 15:26:03 -0700226 * The enum values must be identical to those used by the
227 * C++ <atomic> header.
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700228 */
229
230typedef enum {
231 memory_order_relaxed = __ATOMIC_RELAXED,
232 memory_order_consume = __ATOMIC_CONSUME,
233 memory_order_acquire = __ATOMIC_ACQUIRE,
234 memory_order_release = __ATOMIC_RELEASE,
235 memory_order_acq_rel = __ATOMIC_ACQ_REL,
236 memory_order_seq_cst = __ATOMIC_SEQ_CST
237} memory_order;
238
239/*
240 * 7.17.4 Fences.
241 */
242
243static __inline void
Hans Boehm00aaea32014-08-19 16:14:01 -0700244atomic_thread_fence(memory_order __order __attribute__((unused)))
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700245{
246
247#ifdef __CLANG_ATOMICS
248 __c11_atomic_thread_fence(__order);
249#elif defined(__GNUC_ATOMICS)
250 __atomic_thread_fence(__order);
251#else
252 __sync_synchronize();
253#endif
254}
255
256static __inline void
Hans Boehm00aaea32014-08-19 16:14:01 -0700257atomic_signal_fence(memory_order __order __attribute__((unused)))
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700258{
259
260#ifdef __CLANG_ATOMICS
261 __c11_atomic_signal_fence(__order);
262#elif defined(__GNUC_ATOMICS)
263 __atomic_signal_fence(__order);
264#else
265 __asm volatile ("" ::: "memory");
266#endif
267}
268
269/*
270 * 7.17.5 Lock-free property.
271 */
272
273#if defined(_KERNEL)
274/* Atomics in kernelspace are always lock-free. */
275#define atomic_is_lock_free(obj) \
276 ((void)(obj), (_Bool)1)
277#elif defined(__CLANG_ATOMICS)
278#define atomic_is_lock_free(obj) \
Hans Boehm00aaea32014-08-19 16:14:01 -0700279 __c11_atomic_is_lock_free(sizeof(*(obj)))
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700280#elif defined(__GNUC_ATOMICS)
281#define atomic_is_lock_free(obj) \
282 __atomic_is_lock_free(sizeof((obj)->__val), &(obj)->__val)
283#else
284#define atomic_is_lock_free(obj) \
285 ((void)(obj), sizeof((obj)->__val) <= sizeof(void *))
286#endif
287
288/*
289 * 7.17.6 Atomic integer types.
290 */
291
292#if !__has_extension(c_atomic) && !__has_extension(cxx_atomic)
293/*
294 * No native support for _Atomic(). Place object in structure to prevent
295 * most forms of direct non-atomic access.
296 */
297#define _Atomic(T) struct { T volatile __val; }
298#endif
299
300typedef _Atomic(bool) atomic_bool;
301typedef _Atomic(char) atomic_char;
302typedef _Atomic(signed char) atomic_schar;
303typedef _Atomic(unsigned char) atomic_uchar;
304typedef _Atomic(short) atomic_short;
305typedef _Atomic(unsigned short) atomic_ushort;
306typedef _Atomic(int) atomic_int;
307typedef _Atomic(unsigned int) atomic_uint;
308typedef _Atomic(long) atomic_long;
309typedef _Atomic(unsigned long) atomic_ulong;
310typedef _Atomic(long long) atomic_llong;
311typedef _Atomic(unsigned long long) atomic_ullong;
Hans Boehm8b002362014-07-16 11:33:48 -0700312#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L
313 typedef _Atomic(char16_t) atomic_char16_t;
314 typedef _Atomic(char32_t) atomic_char32_t;
315#endif
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700316typedef _Atomic(wchar_t) atomic_wchar_t;
317typedef _Atomic(int_least8_t) atomic_int_least8_t;
318typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
319typedef _Atomic(int_least16_t) atomic_int_least16_t;
320typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
321typedef _Atomic(int_least32_t) atomic_int_least32_t;
322typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
323typedef _Atomic(int_least64_t) atomic_int_least64_t;
324typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
325typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
326typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
327typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
328typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
329typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
330typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
331typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
332typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
333typedef _Atomic(intptr_t) atomic_intptr_t;
334typedef _Atomic(uintptr_t) atomic_uintptr_t;
335typedef _Atomic(size_t) atomic_size_t;
336typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
337typedef _Atomic(intmax_t) atomic_intmax_t;
338typedef _Atomic(uintmax_t) atomic_uintmax_t;
339
340/*
341 * 7.17.7 Operations on atomic types.
342 */
343
344/*
345 * Compiler-specific operations.
346 */
347
348#if defined(__CLANG_ATOMICS)
349#define atomic_compare_exchange_strong_explicit(object, expected, \
350 desired, success, failure) \
351 __c11_atomic_compare_exchange_strong(object, expected, desired, \
352 success, failure)
353#define atomic_compare_exchange_weak_explicit(object, expected, \
354 desired, success, failure) \
355 __c11_atomic_compare_exchange_weak(object, expected, desired, \
356 success, failure)
357#define atomic_exchange_explicit(object, desired, order) \
358 __c11_atomic_exchange(object, desired, order)
359#define atomic_fetch_add_explicit(object, operand, order) \
360 __c11_atomic_fetch_add(object, operand, order)
361#define atomic_fetch_and_explicit(object, operand, order) \
362 __c11_atomic_fetch_and(object, operand, order)
363#define atomic_fetch_or_explicit(object, operand, order) \
364 __c11_atomic_fetch_or(object, operand, order)
365#define atomic_fetch_sub_explicit(object, operand, order) \
366 __c11_atomic_fetch_sub(object, operand, order)
367#define atomic_fetch_xor_explicit(object, operand, order) \
368 __c11_atomic_fetch_xor(object, operand, order)
369#define atomic_load_explicit(object, order) \
370 __c11_atomic_load(object, order)
371#define atomic_store_explicit(object, desired, order) \
372 __c11_atomic_store(object, desired, order)
373#elif defined(__GNUC_ATOMICS)
374#define atomic_compare_exchange_strong_explicit(object, expected, \
375 desired, success, failure) \
376 __atomic_compare_exchange_n(&(object)->__val, expected, \
377 desired, 0, success, failure)
378#define atomic_compare_exchange_weak_explicit(object, expected, \
379 desired, success, failure) \
380 __atomic_compare_exchange_n(&(object)->__val, expected, \
381 desired, 1, success, failure)
382#define atomic_exchange_explicit(object, desired, order) \
383 __atomic_exchange_n(&(object)->__val, desired, order)
384#define atomic_fetch_add_explicit(object, operand, order) \
385 __atomic_fetch_add(&(object)->__val, operand, order)
386#define atomic_fetch_and_explicit(object, operand, order) \
387 __atomic_fetch_and(&(object)->__val, operand, order)
388#define atomic_fetch_or_explicit(object, operand, order) \
389 __atomic_fetch_or(&(object)->__val, operand, order)
390#define atomic_fetch_sub_explicit(object, operand, order) \
391 __atomic_fetch_sub(&(object)->__val, operand, order)
392#define atomic_fetch_xor_explicit(object, operand, order) \
393 __atomic_fetch_xor(&(object)->__val, operand, order)
394#define atomic_load_explicit(object, order) \
395 __atomic_load_n(&(object)->__val, order)
396#define atomic_store_explicit(object, desired, order) \
397 __atomic_store_n(&(object)->__val, desired, order)
398#else
399#define __atomic_apply_stride(object, operand) \
400 (((__typeof__((object)->__val))0) + (operand))
401#define atomic_compare_exchange_strong_explicit(object, expected, \
402 desired, success, failure) __extension__ ({ \
403 __typeof__(expected) __ep = (expected); \
404 __typeof__(*__ep) __e = *__ep; \
405 (void)(success); (void)(failure); \
406 (bool)((*__ep = __sync_val_compare_and_swap(&(object)->__val, \
407 __e, desired)) == __e); \
408})
409#define atomic_compare_exchange_weak_explicit(object, expected, \
410 desired, success, failure) \
411 atomic_compare_exchange_strong_explicit(object, expected, \
412 desired, success, failure)
413#if __has_builtin(__sync_swap)
414/* Clang provides a full-barrier atomic exchange - use it if available. */
415#define atomic_exchange_explicit(object, desired, order) \
416 ((void)(order), __sync_swap(&(object)->__val, desired))
417#else
418/*
419 * __sync_lock_test_and_set() is only an acquire barrier in theory (although in
420 * practice it is usually a full barrier) so we need an explicit barrier before
421 * it.
422 */
423#define atomic_exchange_explicit(object, desired, order) \
424__extension__ ({ \
425 __typeof__(object) __o = (object); \
426 __typeof__(desired) __d = (desired); \
427 (void)(order); \
428 __sync_synchronize(); \
429 __sync_lock_test_and_set(&(__o)->__val, __d); \
430})
431#endif
432#define atomic_fetch_add_explicit(object, operand, order) \
433 ((void)(order), __sync_fetch_and_add(&(object)->__val, \
434 __atomic_apply_stride(object, operand)))
435#define atomic_fetch_and_explicit(object, operand, order) \
436 ((void)(order), __sync_fetch_and_and(&(object)->__val, operand))
437#define atomic_fetch_or_explicit(object, operand, order) \
438 ((void)(order), __sync_fetch_and_or(&(object)->__val, operand))
439#define atomic_fetch_sub_explicit(object, operand, order) \
440 ((void)(order), __sync_fetch_and_sub(&(object)->__val, \
441 __atomic_apply_stride(object, operand)))
442#define atomic_fetch_xor_explicit(object, operand, order) \
443 ((void)(order), __sync_fetch_and_xor(&(object)->__val, operand))
444#define atomic_load_explicit(object, order) \
445 ((void)(order), __sync_fetch_and_add(&(object)->__val, 0))
446#define atomic_store_explicit(object, desired, order) \
447 ((void)atomic_exchange_explicit(object, desired, order))
448#endif
449
450/*
451 * Convenience functions.
452 *
453 * Don't provide these in kernel space. In kernel space, we should be
454 * disciplined enough to always provide explicit barriers.
455 */
456
457#ifndef _KERNEL
458#define atomic_compare_exchange_strong(object, expected, desired) \
459 atomic_compare_exchange_strong_explicit(object, expected, \
460 desired, memory_order_seq_cst, memory_order_seq_cst)
461#define atomic_compare_exchange_weak(object, expected, desired) \
462 atomic_compare_exchange_weak_explicit(object, expected, \
463 desired, memory_order_seq_cst, memory_order_seq_cst)
464#define atomic_exchange(object, desired) \
465 atomic_exchange_explicit(object, desired, memory_order_seq_cst)
466#define atomic_fetch_add(object, operand) \
467 atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
468#define atomic_fetch_and(object, operand) \
469 atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
470#define atomic_fetch_or(object, operand) \
471 atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
472#define atomic_fetch_sub(object, operand) \
473 atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
474#define atomic_fetch_xor(object, operand) \
475 atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
476#define atomic_load(object) \
477 atomic_load_explicit(object, memory_order_seq_cst)
478#define atomic_store(object, desired) \
479 atomic_store_explicit(object, desired, memory_order_seq_cst)
480#endif /* !_KERNEL */
481
482/*
483 * 7.17.8 Atomic flag type and operations.
484 *
485 * XXX: Assume atomic_bool can be used as an atomic_flag. Is there some
486 * kind of compiler built-in type we could use?
487 */
488
489typedef struct {
490 atomic_bool __flag;
491} atomic_flag;
492
Hans Boehm00aaea32014-08-19 16:14:01 -0700493#define ATOMIC_FLAG_INIT { ATOMIC_VAR_INIT(false) }
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700494
495static __inline bool
496atomic_flag_test_and_set_explicit(volatile atomic_flag *__object,
497 memory_order __order)
498{
499 return (atomic_exchange_explicit(&__object->__flag, 1, __order));
500}
501
502static __inline void
503atomic_flag_clear_explicit(volatile atomic_flag *__object, memory_order __order)
504{
505
506 atomic_store_explicit(&__object->__flag, 0, __order);
507}
508
509#ifndef _KERNEL
510static __inline bool
511atomic_flag_test_and_set(volatile atomic_flag *__object)
512{
513
514 return (atomic_flag_test_and_set_explicit(__object,
515 memory_order_seq_cst));
516}
517
518static __inline void
519atomic_flag_clear(volatile atomic_flag *__object)
520{
521
522 atomic_flag_clear_explicit(__object, memory_order_seq_cst);
523}
524#endif /* !_KERNEL */
525
Hans Boehm019d3952014-08-14 15:26:03 -0700526#endif /* <atomic> unavailable */
527
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700528#endif /* !_STDATOMIC_H_ */