blob: ebdc9e5b6a4a81b062b44087806d8fb9dd97572e [file] [log] [blame]
Tom Cherry76e2b152019-07-18 13:15:47 -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#pragma once
31
32#include <sys/cdefs.h>
33#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. */
Tom Cherry6f2e8102020-04-10 10:50:09 -070044#include <stdint.h>
Tom Cherry76e2b152019-07-18 13:15:47 -070045// 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
135} memory_order;
136
Elliott Hughes5b5bab52024-10-17 16:38:05 +0000137#define kill_dependency(y) (y)
138
Tom Cherry76e2b152019-07-18 13:15:47 -0700139/*
140 * 7.17.4 Fences.
141 */
142
Dan Albert2237fcf2024-05-14 17:53:58 +0000143static __inline void atomic_thread_fence(memory_order __order __attribute__((__unused__))) {
Tom Cherry76e2b152019-07-18 13:15:47 -0700144 __c11_atomic_thread_fence(__order);
145}
146
Dan Albert2237fcf2024-05-14 17:53:58 +0000147static __inline void atomic_signal_fence(memory_order __order __attribute__((__unused__))) {
Tom Cherry76e2b152019-07-18 13:15:47 -0700148 __c11_atomic_signal_fence(__order);
149}
150
151/*
152 * 7.17.5 Lock-free property.
153 */
154
155#define atomic_is_lock_free(obj) __c11_atomic_is_lock_free(sizeof(*(obj)))
156
157/*
158 * 7.17.6 Atomic integer types.
159 */
160
161typedef _Atomic(bool) atomic_bool;
162typedef _Atomic(char) atomic_char;
163typedef _Atomic(signed char) atomic_schar;
164typedef _Atomic(unsigned char) atomic_uchar;
165typedef _Atomic(short) atomic_short;
166typedef _Atomic(unsigned short) atomic_ushort;
167typedef _Atomic(int) atomic_int;
168typedef _Atomic(unsigned int) atomic_uint;
169typedef _Atomic(long) atomic_long;
170typedef _Atomic(unsigned long) atomic_ulong;
171typedef _Atomic(long long) atomic_llong;
172typedef _Atomic(unsigned long long) atomic_ullong;
173#if defined(__BIONIC__) || (defined(__cplusplus) && __cplusplus >= 201103L)
174 typedef _Atomic(char16_t) atomic_char16_t;
175 typedef _Atomic(char32_t) atomic_char32_t;
176#endif
177typedef _Atomic(wchar_t) atomic_wchar_t;
178typedef _Atomic(int_least8_t) atomic_int_least8_t;
179typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
180typedef _Atomic(int_least16_t) atomic_int_least16_t;
181typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
182typedef _Atomic(int_least32_t) atomic_int_least32_t;
183typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
184typedef _Atomic(int_least64_t) atomic_int_least64_t;
185typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
186typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
187typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
188typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
189typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
190typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
191typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
192typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
193typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
194typedef _Atomic(intptr_t) atomic_intptr_t;
195typedef _Atomic(uintptr_t) atomic_uintptr_t;
196typedef _Atomic(size_t) atomic_size_t;
197typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
198typedef _Atomic(intmax_t) atomic_intmax_t;
199typedef _Atomic(uintmax_t) atomic_uintmax_t;
200
201/*
202 * 7.17.7 Operations on atomic types.
203 */
204
205/*
206 * Compiler-specific operations.
207 */
208
209#define atomic_compare_exchange_strong_explicit(object, expected, \
210 desired, success, failure) \
211 __c11_atomic_compare_exchange_strong(object, expected, desired, \
212 success, failure)
213#define atomic_compare_exchange_weak_explicit(object, expected, \
214 desired, success, failure) \
215 __c11_atomic_compare_exchange_weak(object, expected, desired, \
216 success, failure)
217#define atomic_exchange_explicit(object, desired, order) \
218 __c11_atomic_exchange(object, desired, order)
219#define atomic_fetch_add_explicit(object, operand, order) \
220 __c11_atomic_fetch_add(object, operand, order)
221#define atomic_fetch_and_explicit(object, operand, order) \
222 __c11_atomic_fetch_and(object, operand, order)
223#define atomic_fetch_or_explicit(object, operand, order) \
224 __c11_atomic_fetch_or(object, operand, order)
225#define atomic_fetch_sub_explicit(object, operand, order) \
226 __c11_atomic_fetch_sub(object, operand, order)
227#define atomic_fetch_xor_explicit(object, operand, order) \
228 __c11_atomic_fetch_xor(object, operand, order)
229#define atomic_load_explicit(object, order) \
230 __c11_atomic_load(object, order)
231#define atomic_store_explicit(object, desired, order) \
232 __c11_atomic_store(object, desired, order)
233
234/*
235 * Convenience functions.
236 */
237
238#define atomic_compare_exchange_strong(object, expected, desired) \
239 atomic_compare_exchange_strong_explicit(object, expected, \
240 desired, memory_order_seq_cst, memory_order_seq_cst)
241#define atomic_compare_exchange_weak(object, expected, desired) \
242 atomic_compare_exchange_weak_explicit(object, expected, \
243 desired, memory_order_seq_cst, memory_order_seq_cst)
244#define atomic_exchange(object, desired) \
245 atomic_exchange_explicit(object, desired, memory_order_seq_cst)
246#define atomic_fetch_add(object, operand) \
247 atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
248#define atomic_fetch_and(object, operand) \
249 atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
250#define atomic_fetch_or(object, operand) \
251 atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
252#define atomic_fetch_sub(object, operand) \
253 atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
254#define atomic_fetch_xor(object, operand) \
255 atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
256#define atomic_load(object) \
257 atomic_load_explicit(object, memory_order_seq_cst)
258#define atomic_store(object, desired) \
259 atomic_store_explicit(object, desired, memory_order_seq_cst)
260
261/*
262 * 7.17.8 Atomic flag type and operations.
263 *
264 * XXX: Assume atomic_bool can be used as an atomic_flag. Is there some
265 * kind of compiler built-in type we could use?
266 */
267
268typedef struct {
269 atomic_bool __flag;
270} atomic_flag;
271
272#define ATOMIC_FLAG_INIT { ATOMIC_VAR_INIT(false) }
273
Dan Albert2237fcf2024-05-14 17:53:58 +0000274static __inline bool atomic_flag_test_and_set_explicit(volatile atomic_flag * _Nonnull __object, memory_order __order) {
Tom Cherry76e2b152019-07-18 13:15:47 -0700275 return (atomic_exchange_explicit(&__object->__flag, 1, __order));
276}
277
Dan Albert2237fcf2024-05-14 17:53:58 +0000278static __inline void atomic_flag_clear_explicit(volatile atomic_flag * _Nonnull __object, memory_order __order) {
Tom Cherry76e2b152019-07-18 13:15:47 -0700279 atomic_store_explicit(&__object->__flag, 0, __order);
280}
281
Dan Albert2237fcf2024-05-14 17:53:58 +0000282static __inline bool atomic_flag_test_and_set(volatile atomic_flag * _Nonnull __object) {
Tom Cherry76e2b152019-07-18 13:15:47 -0700283 return (atomic_flag_test_and_set_explicit(__object, memory_order_seq_cst));
284}
285
Dan Albert2237fcf2024-05-14 17:53:58 +0000286static __inline void atomic_flag_clear(volatile atomic_flag * _Nonnull __object) {
Tom Cherry76e2b152019-07-18 13:15:47 -0700287 atomic_flag_clear_explicit(__object, memory_order_seq_cst);
288}