blob: 2ce6ee6552f218f08b6ae934cee6202c1fc4428b [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 Chiendf4f7662019-09-04 16:45:23 -070030#pragma once
Logan Chien2833ffb2018-10-09 10:03:24 +080031
Logan Chien55afb0a2018-10-15 10:42:14 +080032#include <sys/cdefs.h>
Logan Chien55afb0a2018-10-15 10:42:14 +080033#include <sys/types.h>
34#include <stdbool.h>
35
36/*
37 * C: Do it ourselves.
38 * Note that the runtime representation defined here should be compatible
39 * with the C++ one, i.e. an _Atomic(T) needs to contain the same
40 * bits as a T.
41 */
42
43#include <stddef.h> /* For ptrdiff_t. */
Sasha Smundak33d5ddd2020-05-04 13:37:26 -070044#include <stdint.h>
Logan Chien55afb0a2018-10-15 10:42:14 +080045// Include uchar.h only when available. Bionic's stdatomic.h is also used for
46// the host (via a copy in prebuilts/clang) and uchar.h is not available in the
47// glibc used for the host.
48#if defined(__BIONIC__)
49# include <uchar.h> /* For char16_t and char32_t. */
50#endif
51
52/*
53 * 7.17.1 Atomic lock-free macros.
54 */
55
56#ifdef __GCC_ATOMIC_BOOL_LOCK_FREE
57#define ATOMIC_BOOL_LOCK_FREE __GCC_ATOMIC_BOOL_LOCK_FREE
58#endif
59#ifdef __GCC_ATOMIC_CHAR_LOCK_FREE
60#define ATOMIC_CHAR_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE
61#endif
62#ifdef __GCC_ATOMIC_CHAR16_T_LOCK_FREE
63#define ATOMIC_CHAR16_T_LOCK_FREE __GCC_ATOMIC_CHAR16_T_LOCK_FREE
64#endif
65#ifdef __GCC_ATOMIC_CHAR32_T_LOCK_FREE
66#define ATOMIC_CHAR32_T_LOCK_FREE __GCC_ATOMIC_CHAR32_T_LOCK_FREE
67#endif
68#ifdef __GCC_ATOMIC_WCHAR_T_LOCK_FREE
69#define ATOMIC_WCHAR_T_LOCK_FREE __GCC_ATOMIC_WCHAR_T_LOCK_FREE
70#endif
71#ifdef __GCC_ATOMIC_SHORT_LOCK_FREE
72#define ATOMIC_SHORT_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
73#endif
74#ifdef __GCC_ATOMIC_INT_LOCK_FREE
75#define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
76#endif
77#ifdef __GCC_ATOMIC_LONG_LOCK_FREE
78#define ATOMIC_LONG_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
79#endif
80#ifdef __GCC_ATOMIC_LLONG_LOCK_FREE
81#define ATOMIC_LLONG_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
82#endif
83#ifdef __GCC_ATOMIC_POINTER_LOCK_FREE
84#define ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE
85#endif
86
87/*
88 * 7.17.2 Initialization.
89 */
90
91#define ATOMIC_VAR_INIT(value) (value)
92#define atomic_init(obj, value) __c11_atomic_init(obj, value)
93
94/*
95 * Clang and recent GCC both provide predefined macros for the memory
96 * orderings. If we are using a compiler that doesn't define them, use the
97 * clang values - these will be ignored in the fallback path.
98 */
99
100#ifndef __ATOMIC_RELAXED
101#define __ATOMIC_RELAXED 0
102#endif
103#ifndef __ATOMIC_CONSUME
104#define __ATOMIC_CONSUME 1
105#endif
106#ifndef __ATOMIC_ACQUIRE
107#define __ATOMIC_ACQUIRE 2
108#endif
109#ifndef __ATOMIC_RELEASE
110#define __ATOMIC_RELEASE 3
111#endif
112#ifndef __ATOMIC_ACQ_REL
113#define __ATOMIC_ACQ_REL 4
114#endif
115#ifndef __ATOMIC_SEQ_CST
116#define __ATOMIC_SEQ_CST 5
117#endif
118
119/*
120 * 7.17.3 Order and consistency.
121 *
122 * The memory_order_* constants that denote the barrier behaviour of the
123 * atomic operations.
124 * The enum values must be identical to those used by the
125 * C++ <atomic> header.
126 */
127
128typedef enum {
129 memory_order_relaxed = __ATOMIC_RELAXED,
130 memory_order_consume = __ATOMIC_CONSUME,
131 memory_order_acquire = __ATOMIC_ACQUIRE,
132 memory_order_release = __ATOMIC_RELEASE,
133 memory_order_acq_rel = __ATOMIC_ACQ_REL,
134 memory_order_seq_cst = __ATOMIC_SEQ_CST
Logan Chien2833ffb2018-10-09 10:03:24 +0800135} memory_order;
136
Logan Chien55afb0a2018-10-15 10:42:14 +0800137/*
138 * 7.17.4 Fences.
139 */
Logan Chien2833ffb2018-10-09 10:03:24 +0800140
Logan Chien55afb0a2018-10-15 10:42:14 +0800141static __inline void atomic_thread_fence(memory_order __order __attribute__((unused))) {
142 __c11_atomic_thread_fence(__order);
Logan Chien2833ffb2018-10-09 10:03:24 +0800143}
Logan Chien55afb0a2018-10-15 10:42:14 +0800144
145static __inline void atomic_signal_fence(memory_order __order __attribute__((unused))) {
146 __c11_atomic_signal_fence(__order);
147}
148
149/*
150 * 7.17.5 Lock-free property.
151 */
152
153#define atomic_is_lock_free(obj) __c11_atomic_is_lock_free(sizeof(*(obj)))
154
155/*
156 * 7.17.6 Atomic integer types.
157 */
158
159typedef _Atomic(bool) atomic_bool;
160typedef _Atomic(char) atomic_char;
161typedef _Atomic(signed char) atomic_schar;
162typedef _Atomic(unsigned char) atomic_uchar;
163typedef _Atomic(short) atomic_short;
164typedef _Atomic(unsigned short) atomic_ushort;
165typedef _Atomic(int) atomic_int;
166typedef _Atomic(unsigned int) atomic_uint;
167typedef _Atomic(long) atomic_long;
168typedef _Atomic(unsigned long) atomic_ulong;
169typedef _Atomic(long long) atomic_llong;
170typedef _Atomic(unsigned long long) atomic_ullong;
171#if defined(__BIONIC__) || (defined(__cplusplus) && __cplusplus >= 201103L)
172 typedef _Atomic(char16_t) atomic_char16_t;
173 typedef _Atomic(char32_t) atomic_char32_t;
Logan Chien2833ffb2018-10-09 10:03:24 +0800174#endif
Logan Chien55afb0a2018-10-15 10:42:14 +0800175typedef _Atomic(wchar_t) atomic_wchar_t;
176typedef _Atomic(int_least8_t) atomic_int_least8_t;
177typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
178typedef _Atomic(int_least16_t) atomic_int_least16_t;
179typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
180typedef _Atomic(int_least32_t) atomic_int_least32_t;
181typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
182typedef _Atomic(int_least64_t) atomic_int_least64_t;
183typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
184typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
185typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
186typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
187typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
188typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
189typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
190typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
191typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
192typedef _Atomic(intptr_t) atomic_intptr_t;
193typedef _Atomic(uintptr_t) atomic_uintptr_t;
194typedef _Atomic(size_t) atomic_size_t;
195typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
196typedef _Atomic(intmax_t) atomic_intmax_t;
197typedef _Atomic(uintmax_t) atomic_uintmax_t;
Logan Chien2833ffb2018-10-09 10:03:24 +0800198
Logan Chien55afb0a2018-10-15 10:42:14 +0800199/*
200 * 7.17.7 Operations on atomic types.
201 */
Logan Chien2833ffb2018-10-09 10:03:24 +0800202
Logan Chien55afb0a2018-10-15 10:42:14 +0800203/*
204 * Compiler-specific operations.
205 */
206
207#define atomic_compare_exchange_strong_explicit(object, expected, \
208 desired, success, failure) \
209 __c11_atomic_compare_exchange_strong(object, expected, desired, \
210 success, failure)
211#define atomic_compare_exchange_weak_explicit(object, expected, \
212 desired, success, failure) \
213 __c11_atomic_compare_exchange_weak(object, expected, desired, \
214 success, failure)
215#define atomic_exchange_explicit(object, desired, order) \
216 __c11_atomic_exchange(object, desired, order)
217#define atomic_fetch_add_explicit(object, operand, order) \
218 __c11_atomic_fetch_add(object, operand, order)
219#define atomic_fetch_and_explicit(object, operand, order) \
220 __c11_atomic_fetch_and(object, operand, order)
221#define atomic_fetch_or_explicit(object, operand, order) \
222 __c11_atomic_fetch_or(object, operand, order)
223#define atomic_fetch_sub_explicit(object, operand, order) \
224 __c11_atomic_fetch_sub(object, operand, order)
225#define atomic_fetch_xor_explicit(object, operand, order) \
226 __c11_atomic_fetch_xor(object, operand, order)
227#define atomic_load_explicit(object, order) \
228 __c11_atomic_load(object, order)
229#define atomic_store_explicit(object, desired, order) \
230 __c11_atomic_store(object, desired, order)
231
232/*
233 * Convenience functions.
234 */
235
236#define atomic_compare_exchange_strong(object, expected, desired) \
237 atomic_compare_exchange_strong_explicit(object, expected, \
238 desired, memory_order_seq_cst, memory_order_seq_cst)
239#define atomic_compare_exchange_weak(object, expected, desired) \
240 atomic_compare_exchange_weak_explicit(object, expected, \
241 desired, memory_order_seq_cst, memory_order_seq_cst)
242#define atomic_exchange(object, desired) \
243 atomic_exchange_explicit(object, desired, memory_order_seq_cst)
244#define atomic_fetch_add(object, operand) \
245 atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
246#define atomic_fetch_and(object, operand) \
247 atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
248#define atomic_fetch_or(object, operand) \
249 atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
250#define atomic_fetch_sub(object, operand) \
251 atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
252#define atomic_fetch_xor(object, operand) \
253 atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
254#define atomic_load(object) \
255 atomic_load_explicit(object, memory_order_seq_cst)
256#define atomic_store(object, desired) \
257 atomic_store_explicit(object, desired, memory_order_seq_cst)
258
259/*
260 * 7.17.8 Atomic flag type and operations.
261 *
262 * XXX: Assume atomic_bool can be used as an atomic_flag. Is there some
263 * kind of compiler built-in type we could use?
264 */
265
266typedef struct {
267 atomic_bool __flag;
268} atomic_flag;
269
270#define ATOMIC_FLAG_INIT { ATOMIC_VAR_INIT(false) }
271
272static __inline bool atomic_flag_test_and_set_explicit(volatile atomic_flag *__object, memory_order __order) {
273 return (atomic_exchange_explicit(&__object->__flag, 1, __order));
274}
275
276static __inline void atomic_flag_clear_explicit(volatile atomic_flag *__object, memory_order __order) {
277 atomic_store_explicit(&__object->__flag, 0, __order);
278}
279
280static __inline bool atomic_flag_test_and_set(volatile atomic_flag *__object) {
281 return (atomic_flag_test_and_set_explicit(__object, memory_order_seq_cst));
282}
283
284static __inline void atomic_flag_clear(volatile atomic_flag *__object) {
285 atomic_flag_clear_explicit(__object, memory_order_seq_cst);
286}