The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
stdatomic.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * An implementation of C11 stdatomic.h directly borrowed from FreeBSD
4  * (original copyright follows), with minor modifications for
5  * portability to other systems. Works for recent Clang (that
6  * implement the feature c_atomic) and GCC 4.7+; includes
7  * compatibility for GCC below 4.7 but I wouldn't recommend it.
8  *
9  * Caveats and limitations:
10  * - Only the ``_Atomic parentheses'' notation is implemented, while
11  * the ``_Atomic space'' one is not.
12  * - _Atomic types must be typedef'ed, or programs using them will
13  * not type check correctly (incompatible anonymous structure
14  * types).
15  * - Non-scalar _Atomic types would require runtime support for
16  * runtime locking, which, as far as I know, is not currently
17  * available on any system.
18  */
19 
20 /*-
21  * @copyright (c) 2011 Ed Schouten (ed@FreeBSD.org)
22  * David Chisnall <theraven@FreeBSD.org>
23  * All rights reserved.
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the above copyright
29  * notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  * notice, this list of conditions and the following disclaimer in the
32  * documentation and/or other materials provided with the distribution.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
35  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
38  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  *
46  * $FreeBSD: src/include/stdatomic.h,v 1.10.2.2 2012/05/30 19:21:54 theraven Exp $
47  */
48 #include <stddef.h>
49 #include <stdint.h>
50 
51 #if !defined(__has_feature)
52 #define __has_feature(x) 0
53 #endif
54 #if !defined(__has_builtin)
55 #define __has_builtin(x) 0
56 #endif
57 #if !defined(__GNUC_PREREQ__)
58 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
59 #define __GNUC_PREREQ__(maj, min) \
60  ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
61 #else
62 #define __GNUC_PREREQ__(maj, min) 0
63 #endif
64 #endif
65 
66 #if !defined(__CLANG_ATOMICS) && !defined(__GNUC_ATOMICS)
67 #if __has_feature(c_atomic)
68 #define __CLANG_ATOMICS
69 #elif __GNUC_PREREQ__(4, 7)
70 #define __GNUC_ATOMICS
71 #elif !defined(__GNUC__)
72 #error "stdatomic.h does not support your compiler"
73 #endif
74 #endif
75 
76 #if !defined(__CLANG_ATOMICS)
77 #define _Atomic(T) struct { volatile __typeof__(T) __val; }
78 #endif
79 
80 /*
81  * 7.17.2 Initialization.
82  */
83 
84 #if defined(__CLANG_ATOMICS)
85 #define ATOMIC_VAR_INIT(value) (value)
86 #define atomic_init(obj, value) __c11_atomic_init(obj, value)
87 #else
88 #define ATOMIC_VAR_INIT(value) { .__val = (value) }
89 #define atomic_init(obj, value) do { \
90  (obj)->__val = (value); \
91 } while (0)
92 #endif
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  */
125 
133 };
134 
135 typedef enum memory_order memory_order;
136 
137 /*
138  * 7.17.4 Fences.
139  */
140 
141 #ifdef __CLANG_ATOMICS
142 #define atomic_thread_fence(order) __c11_atomic_thread_fence(order)
143 #define atomic_signal_fence(order) __c11_atomic_signal_fence(order)
144 #elif defined(__GNUC_ATOMICS)
145 #define atomic_thread_fence(order) __atomic_thread_fence(order)
146 #define atomic_signal_fence(order) __atomic_signal_fence(order)
147 #else
148 #define atomic_thread_fence(order) __sync_synchronize()
149 #define atomic_signal_fence(order) __asm volatile ("" : : : "memory")
150 #endif
151 
152 /*
153  * 7.17.5 Lock-free property.
154  */
155 
156 #if defined(__CLANG_ATOMICS)
157 #define atomic_is_lock_free(obj) \
158  __c11_atomic_is_lock_free(sizeof(obj))
159 #elif defined(__GNUC_ATOMICS)
160 #define atomic_is_lock_free(obj) \
161  __atomic_is_lock_free(sizeof((obj)->__val))
162 #else
163 #define atomic_is_lock_free(obj) \
164  (sizeof((obj)->__val) <= sizeof(void *))
165 #endif
166 
167 /*
168  * 7.17.6 Atomic integer types.
169  */
170 
171 typedef _Atomic(_Bool) atomic_bool;
172 typedef _Atomic(char) atomic_char;
173 typedef _Atomic(signed char) atomic_schar;
174 typedef _Atomic(unsigned char) atomic_uchar;
175 typedef _Atomic(short) atomic_short;
176 typedef _Atomic(unsigned short) atomic_ushort;
177 typedef _Atomic(int) atomic_int;
178 typedef _Atomic(unsigned int) atomic_uint;
179 typedef _Atomic(long) atomic_long;
180 typedef _Atomic(unsigned long) atomic_ulong;
181 typedef _Atomic(long long) atomic_llong;
182 typedef _Atomic(unsigned long long) atomic_ullong;
183 #if 0
184 typedef _Atomic(char16_t) atomic_char16_t;
185 typedef _Atomic(char32_t) atomic_char32_t;
186 #endif
187 typedef _Atomic(wchar_t) atomic_wchar_t;
188 typedef _Atomic(int_least8_t) atomic_int_least8_t;
189 typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
190 typedef _Atomic(int_least16_t) atomic_int_least16_t;
191 typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
192 typedef _Atomic(int_least32_t) atomic_int_least32_t;
193 typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
194 typedef _Atomic(int_least64_t) atomic_int_least64_t;
195 typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
196 typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
197 typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
198 typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
199 typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
200 typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
201 typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
202 typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
203 typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
204 typedef _Atomic(intptr_t) atomic_intptr_t;
205 typedef _Atomic(uintptr_t) atomic_uintptr_t;
206 typedef _Atomic(size_t) atomic_size_t;
207 typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
208 typedef _Atomic(intmax_t) atomic_intmax_t;
209 typedef _Atomic(uintmax_t) atomic_uintmax_t;
210 
211 /*
212  * 7.17.7 Operations on atomic types.
213  */
214 
215 /*
216  * Compiler-specific operations.
217  */
218 
219 #if defined(__CLANG_ATOMICS)
220 #define atomic_compare_exchange_strong_explicit(object, expected, \
221  desired, success, failure) \
222  __c11_atomic_compare_exchange_strong(object, expected, desired, \
223  success, failure)
224 #define atomic_compare_exchange_weak_explicit(object, expected, \
225  desired, success, failure) \
226  __c11_atomic_compare_exchange_weak(object, expected, desired, \
227  success, failure)
228 #define atomic_exchange_explicit(object, desired, order) \
229  __c11_atomic_exchange(object, desired, order)
230 #define atomic_fetch_add_explicit(object, operand, order) \
231  __c11_atomic_fetch_add(object, operand, order)
232 #define atomic_fetch_and_explicit(object, operand, order) \
233  __c11_atomic_fetch_and(object, operand, order)
234 #define atomic_fetch_or_explicit(object, operand, order) \
235  __c11_atomic_fetch_or(object, operand, order)
236 #define atomic_fetch_sub_explicit(object, operand, order) \
237  __c11_atomic_fetch_sub(object, operand, order)
238 #define atomic_fetch_xor_explicit(object, operand, order) \
239  __c11_atomic_fetch_xor(object, operand, order)
240 #define atomic_load_explicit(object, order) \
241  __c11_atomic_load(object, order)
242 #define atomic_store_explicit(object, desired, order) \
243  __c11_atomic_store(object, desired, order)
244 #elif defined(__GNUC_ATOMICS)
245 #define atomic_compare_exchange_strong_explicit(object, expected, \
246  desired, success, failure) \
247  __atomic_compare_exchange_n(&(object)->__val, expected, \
248  desired, 0, success, failure)
249 #define atomic_compare_exchange_weak_explicit(object, expected, \
250  desired, success, failure) \
251  __atomic_compare_exchange_n(&(object)->__val, expected, \
252  desired, 1, success, failure)
253 #define atomic_exchange_explicit(object, desired, order) \
254  __atomic_exchange_n(&(object)->__val, desired, order)
255 #define atomic_fetch_add_explicit(object, operand, order) \
256  __atomic_fetch_add(&(object)->__val, operand, order)
257 #define atomic_fetch_and_explicit(object, operand, order) \
258  __atomic_fetch_and(&(object)->__val, operand, order)
259 #define atomic_fetch_or_explicit(object, operand, order) \
260  __atomic_fetch_or(&(object)->__val, operand, order)
261 #define atomic_fetch_sub_explicit(object, operand, order) \
262  __atomic_fetch_sub(&(object)->__val, operand, order)
263 #define atomic_fetch_xor_explicit(object, operand, order) \
264  __atomic_fetch_xor(&(object)->__val, operand, order)
265 #define atomic_load_explicit(object, order) \
266  __atomic_load_n(&(object)->__val, order)
267 #define atomic_store_explicit(object, desired, order) \
268  __atomic_store_n(&(object)->__val, desired, order)
269 #else
270 #define atomic_compare_exchange_strong_explicit(object, expected, \
271  desired, success, failure) ({ \
272  __typeof__((object)->__val) __v; \
273  _Bool __r; \
274  __v = __sync_val_compare_and_swap(&(object)->__val, \
275  *(expected), desired); \
276  __r = *(expected) == __v; \
277  *(expected) = __v; \
278  __r; \
279 })
280 
281 #define atomic_compare_exchange_weak_explicit(object, expected, \
282  desired, success, failure) \
283  atomic_compare_exchange_strong_explicit(object, expected, \
284  desired, success, failure)
285 #if __has_builtin(__sync_swap)
286 /* Clang provides a full-barrier atomic exchange - use it if available. */
287 #define atomic_exchange_explicit(object, desired, order) \
288  __sync_swap(&(object)->__val, desired)
289 #else
290 /*
291  * __sync_lock_test_and_set() is only an acquire barrier in theory (although in
292  * practice it is usually a full barrier) so we need an explicit barrier after
293  * it.
294  */
295 #define atomic_exchange_explicit(object, desired, order) ({ \
296  __typeof__((object)->__val) __v; \
297  __v = __sync_lock_test_and_set(&(object)->__val, desired); \
298  __sync_synchronize(); \
299  __v; \
300 })
301 #endif
302 #define atomic_fetch_add_explicit(object, operand, order) \
303  __sync_fetch_and_add(&(object)->__val, operand)
304 #define atomic_fetch_and_explicit(object, operand, order) \
305  __sync_fetch_and_and(&(object)->__val, operand)
306 #define atomic_fetch_or_explicit(object, operand, order) \
307  __sync_fetch_and_or(&(object)->__val, operand)
308 #define atomic_fetch_sub_explicit(object, operand, order) \
309  __sync_fetch_and_sub(&(object)->__val, operand)
310 #define atomic_fetch_xor_explicit(object, operand, order) \
311  __sync_fetch_and_xor(&(object)->__val, operand)
312 #define atomic_load_explicit(object, order) \
313  __sync_fetch_and_add(&(object)->__val, 0)
314 #define atomic_store_explicit(object, desired, order) do { \
315  __sync_synchronize(); \
316  (object)->__val = (desired); \
317  __sync_synchronize(); \
318 } while (0)
319 #endif
320 
321 /*
322  * Convenience functions.
323  */
324 
325 #define atomic_compare_exchange_strong(object, expected, desired) \
326  atomic_compare_exchange_strong_explicit(object, expected, \
327  desired, memory_order_seq_cst, memory_order_seq_cst)
328 #define atomic_compare_exchange_weak(object, expected, desired) \
329  atomic_compare_exchange_weak_explicit(object, expected, \
330  desired, memory_order_seq_cst, memory_order_seq_cst)
331 #define atomic_exchange(object, desired) \
332  atomic_exchange_explicit(object, desired, memory_order_seq_cst)
333 #define atomic_fetch_add(object, operand) \
334  atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
335 #define atomic_fetch_and(object, operand) \
336  atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
337 #define atomic_fetch_or(object, operand) \
338  atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
339 #define atomic_fetch_sub(object, operand) \
340  atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
341 #define atomic_fetch_xor(object, operand) \
342  atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
343 #define atomic_load(object) \
344  atomic_load_explicit(object, memory_order_seq_cst)
345 #define atomic_store(object, desired) \
346  atomic_store_explicit(object, desired, memory_order_seq_cst)
347 
348 /*
349  * 7.17.8 Atomic flag type and operations.
350  */
351 
352 typedef atomic_bool atomic_flag;
353 
354 #define ATOMIC_FLAG_INIT ATOMIC_VAR_INIT(0)
355 
356 #define atomic_flag_clear_explicit(object, order) \
357  atomic_store_explicit(object, 0, order)
358 #define atomic_flag_test_and_set_explicit(object, order) \
359  atomic_compare_exchange_strong_explicit(object, 0, 1, order, order)
360 
361 #define atomic_flag_clear(object) \
362  atomic_flag_clear_explicit(object, memory_order_seq_cst)
363 #define atomic_flag_test_and_set(object) \
364  atomic_flag_test_and_set_explicit(object, memory_order_seq_cst)
#define __ATOMIC_SEQ_CST
Definition: stdatomic.h:116
memory_order
Definition: stdatomic.h:126
@ memory_order_consume
Definition: stdatomic.h:128
@ memory_order_seq_cst
Definition: stdatomic.h:132
@ memory_order_release
Definition: stdatomic.h:130
@ memory_order_relaxed
Definition: stdatomic.h:127
@ memory_order_acq_rel
Definition: stdatomic.h:131
@ memory_order_acquire
Definition: stdatomic.h:129
#define _Atomic(T)
Definition: stdatomic.h:77
atomic_bool atomic_flag
Definition: stdatomic.h:352
#define __ATOMIC_RELEASE
Definition: stdatomic.h:110
#define __ATOMIC_ACQUIRE
Definition: stdatomic.h:107
#define __ATOMIC_CONSUME
Definition: stdatomic.h:104
#define __ATOMIC_RELAXED
Definition: stdatomic.h:101
#define __ATOMIC_ACQ_REL
Definition: stdatomic.h:113