blob: 3c613346da00f21d109e1d3b9325dce46b036541 [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
Dan Albert3ce07692014-10-16 07:52:51 -070035#if defined(__cplusplus) && __cplusplus >= 201103L && defined(_USING_LIBCXX)
Elliott Hughes0d1a8a52018-07-24 19:36:51 +000036# if __has_feature(cxx_atomic)
Elliott Hughesf4840502016-06-06 17:35:53 -070037# define _STDATOMIC_HAVE_ATOMIC
Hans Boehm32429602014-08-28 15:21:32 -070038# endif
39#endif
40
41#ifdef _STDATOMIC_HAVE_ATOMIC
Hans Boehm019d3952014-08-14 15:26:03 -070042
43/* We have a usable C++ <atomic>; use it instead. */
44
45#include <atomic>
46
Hans Boehm3e4a0092014-08-26 15:58:15 -070047#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. */
Hans Boehm32429602014-08-28 15:21:32 -070054
Hans Boehm019d3952014-08-14 15:26:03 -070055#define _Atomic(t) std::atomic<t>
56
57using 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;
Hans Boehm76ac4d02014-09-30 18:31:04 -070085using std::memory_order_acquire;
Hans Boehm019d3952014-08-14 15:26:03 -070086using 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
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700130#include <sys/types.h>
131#include <stdbool.h>
132
Hans Boehm019d3952014-08-14 15:26:03 -0700133/*
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
Elliott Hughes0fe88852016-07-27 10:45:05 -0700140#include <stddef.h> /* For ptrdiff_t. */
141#include <stdint.h> /* TODO: don't drag in all the macros, just the types. */
Pirama Arumuga Nainar6c0eab92018-07-09 11:42:23 -0700142// 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
Pirama Arumuga Nainared3c7872016-08-04 09:40:18 -0700144// glibc used for the host.
Pirama Arumuga Nainar6c0eab92018-07-09 11:42:23 -0700145#if defined(__BIONIC__)
Pirama Arumuga Nainared3c7872016-08-04 09:40:18 -0700146# include <uchar.h> /* For char16_t and char32_t. */
147#endif
148
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700149/*
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
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700188#define ATOMIC_VAR_INIT(value) (value)
189#define atomic_init(obj, value) __c11_atomic_init(obj, value)
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700190
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.
Hans Boehm019d3952014-08-14 15:26:03 -0700221 * The enum values must be identical to those used by the
222 * C++ <atomic> header.
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700223 */
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
232} memory_order;
233
234/*
235 * 7.17.4 Fences.
236 */
237
Elliott Hughes0d1a8a52018-07-24 19:36:51 +0000238static __inline void atomic_thread_fence(memory_order __order __attribute__((unused))) {
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700239 __c11_atomic_thread_fence(__order);
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700240}
241
Elliott Hughes0d1a8a52018-07-24 19:36:51 +0000242static __inline void atomic_signal_fence(memory_order __order __attribute__((unused))) {
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700243 __c11_atomic_signal_fence(__order);
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700244}
245
246/*
247 * 7.17.5 Lock-free property.
248 */
249
Elliott Hughes0d1a8a52018-07-24 19:36:51 +0000250#define atomic_is_lock_free(obj) __c11_atomic_is_lock_free(sizeof(*(obj)))
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700251
252/*
253 * 7.17.6 Atomic integer types.
254 */
255
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700256typedef _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;
Pirama Arumuga Nainar6c0eab92018-07-09 11:42:23 -0700268#if defined(__BIONIC__) || (defined(__cplusplus) && __cplusplus >= 201103L)
Hans Boehm8b002362014-07-16 11:33:48 -0700269 typedef _Atomic(char16_t) atomic_char16_t;
270 typedef _Atomic(char32_t) atomic_char32_t;
271#endif
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700272typedef _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;
295
296/*
297 * 7.17.7 Operations on atomic types.
298 */
299
300/*
301 * Compiler-specific operations.
302 */
303
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700304#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)
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700328
329/*
330 * Convenience functions.
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700331 */
332
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700333#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)
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700355
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
Hans Boehm00aaea32014-08-19 16:14:01 -0700367#define ATOMIC_FLAG_INIT { ATOMIC_VAR_INIT(false) }
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700368
Elliott Hughes0d1a8a52018-07-24 19:36:51 +0000369static __inline bool atomic_flag_test_and_set_explicit(volatile atomic_flag *__object, memory_order __order) {
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700370 return (atomic_exchange_explicit(&__object->__flag, 1, __order));
371}
372
Elliott Hughes0d1a8a52018-07-24 19:36:51 +0000373static __inline void atomic_flag_clear_explicit(volatile atomic_flag *__object, memory_order __order) {
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700374 atomic_store_explicit(&__object->__flag, 0, __order);
375}
376
Elliott Hughes0d1a8a52018-07-24 19:36:51 +0000377static __inline bool atomic_flag_test_and_set(volatile atomic_flag *__object) {
378 return (atomic_flag_test_and_set_explicit(__object, memory_order_seq_cst));
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700379}
380
Elliott Hughes0d1a8a52018-07-24 19:36:51 +0000381static __inline void atomic_flag_clear(volatile atomic_flag *__object) {
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700382 atomic_flag_clear_explicit(__object, memory_order_seq_cst);
383}
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700384
Hans Boehm019d3952014-08-14 15:26:03 -0700385#endif /* <atomic> unavailable */
386
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700387#endif /* !_STDATOMIC_H_ */