blob: 3c613346da00f21d109e1d3b9325dce46b036541 [file] [log] [blame]
Logan Chien55afb0a2018-10-15 10:42:14 +08001/*-
2 * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
3 * David Chisnall <theraven@FreeBSD.org>
4 * All rights reserved.
Logan Chien2833ffb2018-10-09 10:03:24 +08005 *
Logan Chien55afb0a2018-10-15 10:42:14 +08006 * 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.
Logan Chien2833ffb2018-10-09 10:03:24 +080014 *
Logan Chien55afb0a2018-10-15 10:42:14 +080015 * 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.
Logan Chien2833ffb2018-10-09 10:03:24 +080026 *
Logan Chien55afb0a2018-10-15 10:42:14 +080027 * $FreeBSD$
Logan Chien2833ffb2018-10-09 10:03:24 +080028 */
29
Logan Chien55afb0a2018-10-15 10:42:14 +080030#ifndef _STDATOMIC_H_
31#define _STDATOMIC_H_
Logan Chien2833ffb2018-10-09 10:03:24 +080032
Logan Chien55afb0a2018-10-15 10:42:14 +080033#include <sys/cdefs.h>
Logan Chien2833ffb2018-10-09 10:03:24 +080034
Logan Chien55afb0a2018-10-15 10:42:14 +080035#if defined(__cplusplus) && __cplusplus >= 201103L && defined(_USING_LIBCXX)
36# if __has_feature(cxx_atomic)
37# define _STDATOMIC_HAVE_ATOMIC
38# endif
Logan Chien2833ffb2018-10-09 10:03:24 +080039#endif
40
Logan Chien55afb0a2018-10-15 10:42:14 +080041#ifdef _STDATOMIC_HAVE_ATOMIC
Logan Chien2833ffb2018-10-09 10:03:24 +080042
Logan Chien55afb0a2018-10-15 10:42:14 +080043/* We have a usable C++ <atomic>; use it instead. */
Logan Chien2833ffb2018-10-09 10:03:24 +080044
Logan Chien55afb0a2018-10-15 10:42:14 +080045#include <atomic>
Logan Chien2833ffb2018-10-09 10:03:24 +080046
Logan Chien55afb0a2018-10-15 10:42:14 +080047#undef _Atomic
48 /* Also defined by <atomic> for gcc. But not used in macros. */
49 /* Also a clang intrinsic. */
50 /* Should not be used by client code before this file is */
51 /* included. The definitions in <atomic> themselves see */
52 /* the old definition, as they should. */
53 /* Client code sees the following definition. */
Logan Chien2833ffb2018-10-09 10:03:24 +080054
Logan Chien55afb0a2018-10-15 10:42:14 +080055#define _Atomic(t) std::atomic<t>
Logan Chien2833ffb2018-10-09 10:03:24 +080056
Logan Chien55afb0a2018-10-15 10:42:14 +080057using std::atomic_is_lock_free;
58using std::atomic_init;
59using std::atomic_store;
60using std::atomic_store_explicit;
61using std::atomic_load;
62using std::atomic_load_explicit;
63using std::atomic_exchange;
64using std::atomic_exchange_explicit;
65using std::atomic_compare_exchange_strong;
66using std::atomic_compare_exchange_strong_explicit;
67using std::atomic_compare_exchange_weak;
68using std::atomic_compare_exchange_weak_explicit;
69using std::atomic_fetch_add;
70using std::atomic_fetch_add_explicit;
71using std::atomic_fetch_sub;
72using std::atomic_fetch_sub_explicit;
73using std::atomic_fetch_or;
74using std::atomic_fetch_or_explicit;
75using std::atomic_fetch_xor;
76using std::atomic_fetch_xor_explicit;
77using std::atomic_fetch_and;
78using std::atomic_fetch_and_explicit;
79using std::atomic_thread_fence;
80using std::atomic_signal_fence;
81
82using std::memory_order;
83using std::memory_order_relaxed;
84using std::memory_order_consume;
85using std::memory_order_acquire;
86using std::memory_order_release;
87using std::memory_order_acq_rel;
88using std::memory_order_seq_cst;
89
90using std::atomic_bool;
91using std::atomic_char;
92using std::atomic_schar;
93using std::atomic_uchar;
94using std::atomic_short;
95using std::atomic_ushort;
96using std::atomic_int;
97using std::atomic_uint;
98using std::atomic_long;
99using std::atomic_ulong;
100using std::atomic_llong;
101using std::atomic_ullong;
102using std::atomic_char16_t;
103using std::atomic_char32_t;
104using std::atomic_wchar_t;
105using std::atomic_int_least8_t;
106using std::atomic_uint_least8_t;
107using std::atomic_int_least16_t;
108using std::atomic_uint_least16_t;
109using std::atomic_int_least32_t;
110using std::atomic_uint_least32_t;
111using std::atomic_int_least64_t;
112using std::atomic_uint_least64_t;
113using std::atomic_int_fast8_t;
114using std::atomic_uint_fast8_t;
115using std::atomic_int_fast16_t;
116using std::atomic_uint_fast16_t;
117using std::atomic_int_fast32_t;
118using std::atomic_uint_fast32_t;
119using std::atomic_int_fast64_t;
120using std::atomic_uint_fast64_t;
121using std::atomic_intptr_t;
122using std::atomic_uintptr_t;
123using std::atomic_size_t;
124using std::atomic_ptrdiff_t;
125using std::atomic_intmax_t;
126using std::atomic_uintmax_t;
127
128#else /* <atomic> unavailable, possibly because this is C, not C++ */
129
130#include <sys/types.h>
131#include <stdbool.h>
132
133/*
134 * C: Do it ourselves.
135 * Note that the runtime representation defined here should be compatible
136 * with the C++ one, i.e. an _Atomic(T) needs to contain the same
137 * bits as a T.
138 */
139
140#include <stddef.h> /* For ptrdiff_t. */
141#include <stdint.h> /* TODO: don't drag in all the macros, just the types. */
142// Include uchar.h only when available. Bionic's stdatomic.h is also used for
143// the host (via a copy in prebuilts/clang) and uchar.h is not available in the
144// glibc used for the host.
145#if defined(__BIONIC__)
146# include <uchar.h> /* For char16_t and char32_t. */
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#define ATOMIC_VAR_INIT(value) (value)
189#define atomic_init(obj, value) __c11_atomic_init(obj, value)
190
191/*
192 * Clang and recent GCC both provide predefined macros for the memory
193 * orderings. If we are using a compiler that doesn't define them, use the
194 * clang values - these will be ignored in the fallback path.
195 */
196
197#ifndef __ATOMIC_RELAXED
198#define __ATOMIC_RELAXED 0
199#endif
200#ifndef __ATOMIC_CONSUME
201#define __ATOMIC_CONSUME 1
202#endif
203#ifndef __ATOMIC_ACQUIRE
204#define __ATOMIC_ACQUIRE 2
205#endif
206#ifndef __ATOMIC_RELEASE
207#define __ATOMIC_RELEASE 3
208#endif
209#ifndef __ATOMIC_ACQ_REL
210#define __ATOMIC_ACQ_REL 4
211#endif
212#ifndef __ATOMIC_SEQ_CST
213#define __ATOMIC_SEQ_CST 5
214#endif
215
216/*
217 * 7.17.3 Order and consistency.
218 *
219 * The memory_order_* constants that denote the barrier behaviour of the
220 * atomic operations.
221 * The enum values must be identical to those used by the
222 * C++ <atomic> header.
223 */
224
225typedef enum {
226 memory_order_relaxed = __ATOMIC_RELAXED,
227 memory_order_consume = __ATOMIC_CONSUME,
228 memory_order_acquire = __ATOMIC_ACQUIRE,
229 memory_order_release = __ATOMIC_RELEASE,
230 memory_order_acq_rel = __ATOMIC_ACQ_REL,
231 memory_order_seq_cst = __ATOMIC_SEQ_CST
Logan Chien2833ffb2018-10-09 10:03:24 +0800232} memory_order;
233
Logan Chien55afb0a2018-10-15 10:42:14 +0800234/*
235 * 7.17.4 Fences.
236 */
Logan Chien2833ffb2018-10-09 10:03:24 +0800237
Logan Chien55afb0a2018-10-15 10:42:14 +0800238static __inline void atomic_thread_fence(memory_order __order __attribute__((unused))) {
239 __c11_atomic_thread_fence(__order);
Logan Chien2833ffb2018-10-09 10:03:24 +0800240}
Logan Chien55afb0a2018-10-15 10:42:14 +0800241
242static __inline void atomic_signal_fence(memory_order __order __attribute__((unused))) {
243 __c11_atomic_signal_fence(__order);
244}
245
246/*
247 * 7.17.5 Lock-free property.
248 */
249
250#define atomic_is_lock_free(obj) __c11_atomic_is_lock_free(sizeof(*(obj)))
251
252/*
253 * 7.17.6 Atomic integer types.
254 */
255
256typedef _Atomic(bool) atomic_bool;
257typedef _Atomic(char) atomic_char;
258typedef _Atomic(signed char) atomic_schar;
259typedef _Atomic(unsigned char) atomic_uchar;
260typedef _Atomic(short) atomic_short;
261typedef _Atomic(unsigned short) atomic_ushort;
262typedef _Atomic(int) atomic_int;
263typedef _Atomic(unsigned int) atomic_uint;
264typedef _Atomic(long) atomic_long;
265typedef _Atomic(unsigned long) atomic_ulong;
266typedef _Atomic(long long) atomic_llong;
267typedef _Atomic(unsigned long long) atomic_ullong;
268#if defined(__BIONIC__) || (defined(__cplusplus) && __cplusplus >= 201103L)
269 typedef _Atomic(char16_t) atomic_char16_t;
270 typedef _Atomic(char32_t) atomic_char32_t;
Logan Chien2833ffb2018-10-09 10:03:24 +0800271#endif
Logan Chien55afb0a2018-10-15 10:42:14 +0800272typedef _Atomic(wchar_t) atomic_wchar_t;
273typedef _Atomic(int_least8_t) atomic_int_least8_t;
274typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
275typedef _Atomic(int_least16_t) atomic_int_least16_t;
276typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
277typedef _Atomic(int_least32_t) atomic_int_least32_t;
278typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
279typedef _Atomic(int_least64_t) atomic_int_least64_t;
280typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
281typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
282typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
283typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
284typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
285typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
286typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
287typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
288typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
289typedef _Atomic(intptr_t) atomic_intptr_t;
290typedef _Atomic(uintptr_t) atomic_uintptr_t;
291typedef _Atomic(size_t) atomic_size_t;
292typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
293typedef _Atomic(intmax_t) atomic_intmax_t;
294typedef _Atomic(uintmax_t) atomic_uintmax_t;
Logan Chien2833ffb2018-10-09 10:03:24 +0800295
Logan Chien55afb0a2018-10-15 10:42:14 +0800296/*
297 * 7.17.7 Operations on atomic types.
298 */
Logan Chien2833ffb2018-10-09 10:03:24 +0800299
Logan Chien55afb0a2018-10-15 10:42:14 +0800300/*
301 * Compiler-specific operations.
302 */
303
304#define atomic_compare_exchange_strong_explicit(object, expected, \
305 desired, success, failure) \
306 __c11_atomic_compare_exchange_strong(object, expected, desired, \
307 success, failure)
308#define atomic_compare_exchange_weak_explicit(object, expected, \
309 desired, success, failure) \
310 __c11_atomic_compare_exchange_weak(object, expected, desired, \
311 success, failure)
312#define atomic_exchange_explicit(object, desired, order) \
313 __c11_atomic_exchange(object, desired, order)
314#define atomic_fetch_add_explicit(object, operand, order) \
315 __c11_atomic_fetch_add(object, operand, order)
316#define atomic_fetch_and_explicit(object, operand, order) \
317 __c11_atomic_fetch_and(object, operand, order)
318#define atomic_fetch_or_explicit(object, operand, order) \
319 __c11_atomic_fetch_or(object, operand, order)
320#define atomic_fetch_sub_explicit(object, operand, order) \
321 __c11_atomic_fetch_sub(object, operand, order)
322#define atomic_fetch_xor_explicit(object, operand, order) \
323 __c11_atomic_fetch_xor(object, operand, order)
324#define atomic_load_explicit(object, order) \
325 __c11_atomic_load(object, order)
326#define atomic_store_explicit(object, desired, order) \
327 __c11_atomic_store(object, desired, order)
328
329/*
330 * Convenience functions.
331 */
332
333#define atomic_compare_exchange_strong(object, expected, desired) \
334 atomic_compare_exchange_strong_explicit(object, expected, \
335 desired, memory_order_seq_cst, memory_order_seq_cst)
336#define atomic_compare_exchange_weak(object, expected, desired) \
337 atomic_compare_exchange_weak_explicit(object, expected, \
338 desired, memory_order_seq_cst, memory_order_seq_cst)
339#define atomic_exchange(object, desired) \
340 atomic_exchange_explicit(object, desired, memory_order_seq_cst)
341#define atomic_fetch_add(object, operand) \
342 atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
343#define atomic_fetch_and(object, operand) \
344 atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
345#define atomic_fetch_or(object, operand) \
346 atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
347#define atomic_fetch_sub(object, operand) \
348 atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
349#define atomic_fetch_xor(object, operand) \
350 atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
351#define atomic_load(object) \
352 atomic_load_explicit(object, memory_order_seq_cst)
353#define atomic_store(object, desired) \
354 atomic_store_explicit(object, desired, memory_order_seq_cst)
355
356/*
357 * 7.17.8 Atomic flag type and operations.
358 *
359 * XXX: Assume atomic_bool can be used as an atomic_flag. Is there some
360 * kind of compiler built-in type we could use?
361 */
362
363typedef struct {
364 atomic_bool __flag;
365} atomic_flag;
366
367#define ATOMIC_FLAG_INIT { ATOMIC_VAR_INIT(false) }
368
369static __inline bool atomic_flag_test_and_set_explicit(volatile atomic_flag *__object, memory_order __order) {
370 return (atomic_exchange_explicit(&__object->__flag, 1, __order));
371}
372
373static __inline void atomic_flag_clear_explicit(volatile atomic_flag *__object, memory_order __order) {
374 atomic_store_explicit(&__object->__flag, 0, __order);
375}
376
377static __inline bool atomic_flag_test_and_set(volatile atomic_flag *__object) {
378 return (atomic_flag_test_and_set_explicit(__object, memory_order_seq_cst));
379}
380
381static __inline void atomic_flag_clear(volatile atomic_flag *__object) {
382 atomic_flag_clear_explicit(__object, memory_order_seq_cst);
383}
384
385#endif /* <atomic> unavailable */
386
387#endif /* !_STDATOMIC_H_ */