HPCToolkit
stdatomic.h
Go to the documentation of this file.
1 /*
2  * An implementation of C11 stdatomic.h directly borrowed from FreeBSD
3  * (original copyright follows), with minor modifications for
4  * portability to other systems. Works for recent Clang (that
5  * implement the feature c_atomic) and GCC 4.7+; includes
6  * compatibility for GCC below 4.7 but I wouldn't recommend it.
7  *
8  * Caveats and limitations:
9  * - Only the ``_Atomic parentheses'' notation is implemented, while
10  * the ``_Atomic space'' one is not.
11  * - _Atomic types must be typedef'ed, or programs using them will
12  * not type check correctly (incompatible anonymous structure
13  * types).
14  * - Non-scalar _Atomic types would require runtime support for
15  * runtime locking, which, as far as I know, is not currently
16  * available on any system.
17  */
18 
19 /*-
20  * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
21  * David Chisnall <theraven@FreeBSD.org>
22  * All rights reserved.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  * 1. Redistributions of source code must retain the above copyright
28  * notice, this list of conditions and the following disclaimer.
29  * 2. Redistributions in binary form must reproduce the above copyright
30  * notice, this list of conditions and the following disclaimer in the
31  * documentation and/or other materials provided with the distribution.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  *
45  * $FreeBSD$
46  */
47 
48 #ifndef _STDATOMIC_H_
49 #define _STDATOMIC_H_
50 
51 #include <stddef.h>
52 #include <stdint.h>
53 
54 #if !defined(__has_feature)
55 #define __has_feature(x) 0
56 #endif
57 #if !defined(__has_builtin)
58 #define __has_builtin(x) 0
59 #endif
60 #if !defined(__GNUC_PREREQ__)
61 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
62 #define __GNUC_PREREQ__(maj, min) \
63  ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
64 #else
65 #define __GNUC_PREREQ__(maj, min) 0
66 #endif
67 #endif
68 
69 #if !defined(__CLANG_ATOMICS) && !defined(__GNUC_ATOMICS)
70 #if __has_feature(c_atomic)
71 #define __CLANG_ATOMICS
72 #elif __GNUC_PREREQ__(4, 7)
73 #define __GNUC_ATOMICS
74 #elif !defined(__GNUC__)
75 #error "stdatomic.h does not support your compiler"
76 #endif
77 #endif
78 
79 /*
80  * language independent type to represent a Boolean value
81  */
82 
83 typedef int __Bool;
84 
85 /*
86  * 7.17.1 Atomic lock-free macros.
87  */
88 
89 #ifdef __GCC_ATOMIC_BOOL_LOCK_FREE
90 #define ATOMIC_BOOL_LOCK_FREE __GCC_ATOMIC_BOOL_LOCK_FREE
91 #endif
92 #ifdef __GCC_ATOMIC_CHAR_LOCK_FREE
93 #define ATOMIC_CHAR_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE
94 #endif
95 #ifdef __GCC_ATOMIC_CHAR16_T_LOCK_FREE
96 #define ATOMIC_CHAR16_T_LOCK_FREE __GCC_ATOMIC_CHAR16_T_LOCK_FREE
97 #endif
98 #ifdef __GCC_ATOMIC_CHAR32_T_LOCK_FREE
99 #define ATOMIC_CHAR32_T_LOCK_FREE __GCC_ATOMIC_CHAR32_T_LOCK_FREE
100 #endif
101 #ifdef __GCC_ATOMIC_WCHAR_T_LOCK_FREE
102 #define ATOMIC_WCHAR_T_LOCK_FREE __GCC_ATOMIC_WCHAR_T_LOCK_FREE
103 #endif
104 #ifdef __GCC_ATOMIC_SHORT_LOCK_FREE
105 #define ATOMIC_SHORT_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
106 #endif
107 #ifdef __GCC_ATOMIC_INT_LOCK_FREE
108 #define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
109 #endif
110 #ifdef __GCC_ATOMIC_LONG_LOCK_FREE
111 #define ATOMIC_LONG_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
112 #endif
113 #ifdef __GCC_ATOMIC_LLONG_LOCK_FREE
114 #define ATOMIC_LLONG_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
115 #endif
116 #ifdef __GCC_ATOMIC_POINTER_LOCK_FREE
117 #define ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE
118 #endif
119 
120 #if !defined(__CLANG_ATOMICS)
121 #define _Atomic(T) struct { volatile __typeof__(T) __val; }
122 #endif
123 
124 /*
125  * 7.17.2 Initialization.
126  */
127 
128 #if defined(__CLANG_ATOMICS)
129 #define ATOMIC_VAR_INIT(value) (value)
130 #define atomic_init(obj, value) __c11_atomic_init(obj, value)
131 #else
132 #define ATOMIC_VAR_INIT(value) { .__val = (value) }
133 #define atomic_init(obj, value) ((void)((obj)->__val = (value)))
134 #endif
135 
136 /*
137  * Clang and recent GCC both provide predefined macros for the memory
138  * orderings. If we are using a compiler that doesn't define them, use the
139  * clang values - these will be ignored in the fallback path.
140  */
141 
142 #ifndef __ATOMIC_RELAXED
143 #define __ATOMIC_RELAXED 0
144 #endif
145 #ifndef __ATOMIC_CONSUME
146 #define __ATOMIC_CONSUME 1
147 #endif
148 #ifndef __ATOMIC_ACQUIRE
149 #define __ATOMIC_ACQUIRE 2
150 #endif
151 #ifndef __ATOMIC_RELEASE
152 #define __ATOMIC_RELEASE 3
153 #endif
154 #ifndef __ATOMIC_ACQ_REL
155 #define __ATOMIC_ACQ_REL 4
156 #endif
157 #ifndef __ATOMIC_SEQ_CST
158 #define __ATOMIC_SEQ_CST 5
159 #endif
160 
161 /*
162  * 7.17.3 Order and consistency.
163  *
164  * The memory_order_* constants that denote the barrier behaviour of the
165  * atomic operations.
166  */
167 
168 typedef enum {
175 } memory_order;
176 
177 /*
178  * 7.17.4 Fences.
179  */
180 
181 #define __unused
182 
183 static __inline void
185 {
186 
187 #ifdef __CLANG_ATOMICS
188  __c11_atomic_thread_fence(__order);
189 #elif defined(__GNUC_ATOMICS)
190  __atomic_thread_fence(__order);
191 #else
192  __sync_synchronize();
193 #endif
194 }
195 
196 static __inline void
198 {
199 
200 #ifdef __CLANG_ATOMICS
201  __c11_atomic_signal_fence(__order);
202 #elif defined(__GNUC_ATOMICS)
203  __atomic_signal_fence(__order);
204 #else
205  __asm volatile ("" ::: "memory");
206 #endif
207 }
208 
209 #undef __unused
210 
211 /*
212  * 7.17.5 Lock-free property.
213  */
214 
215 #if defined(_KERNEL)
216 /* Atomics in kernelspace are always lock-free. */
217 #define atomic_is_lock_free(obj) \
218  ((void)(obj), (__Bool)1)
219 #elif defined(__CLANG_ATOMICS)
220 #define atomic_is_lock_free(obj) \
221  __atomic_is_lock_free(sizeof(*(obj)), obj)
222 #elif defined(__GNUC_ATOMICS)
223 #define atomic_is_lock_free(obj) \
224  __atomic_is_lock_free(sizeof((obj)->__val), &(obj)->__val)
225 #else
226 #define atomic_is_lock_free(obj) \
227  ((void)(obj), sizeof((obj)->__val) <= sizeof(void *))
228 #endif
229 
230 /*
231  * 7.17.6 Atomic integer types.
232  */
233 
234 typedef _Atomic(__Bool) atomic_bool;
235 typedef _Atomic(char) atomic_char;
236 typedef _Atomic(signed char) atomic_schar;
237 typedef _Atomic(unsigned char) atomic_uchar;
238 typedef _Atomic(short) atomic_short;
239 typedef _Atomic(unsigned short) atomic_ushort;
240 typedef _Atomic(int) atomic_int;
241 typedef _Atomic(unsigned int) atomic_uint;
242 typedef _Atomic(long) atomic_long;
243 typedef _Atomic(unsigned long) atomic_ulong;
244 typedef _Atomic(long long) atomic_llong;
245 typedef _Atomic(unsigned long long) atomic_ullong;
246 #if 0
247 typedef _Atomic(char16_t) atomic_char16_t;
248 typedef _Atomic(char32_t) atomic_char32_t;
249 #endif
250 typedef _Atomic(wchar_t) atomic_wchar_t;
251 typedef _Atomic(int_least8_t) atomic_int_least8_t;
252 typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
253 typedef _Atomic(int_least16_t) atomic_int_least16_t;
254 typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
255 typedef _Atomic(int_least32_t) atomic_int_least32_t;
256 typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
257 typedef _Atomic(int_least64_t) atomic_int_least64_t;
258 typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
259 typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
260 typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
261 typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
262 typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
263 typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
264 typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
265 typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
266 typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
267 typedef _Atomic(intptr_t) atomic_intptr_t;
268 typedef _Atomic(uintptr_t) atomic_uintptr_t;
269 typedef _Atomic(size_t) atomic_size_t;
270 typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
271 typedef _Atomic(intmax_t) atomic_intmax_t;
272 typedef _Atomic(uintmax_t) atomic_uintmax_t;
273 
274 /*
275  * 7.17.7 Operations on atomic types.
276  */
277 
278 /*
279  * Compiler-specific operations.
280  */
281 
282 #if defined(__CLANG_ATOMICS)
283 #define atomic_compare_exchange_strong_explicit(object, expected, \
284  desired, success, failure) \
285  __c11_atomic_compare_exchange_strong(object, expected, desired, \
286  success, failure)
287 #define atomic_compare_exchange_weak_explicit(object, expected, \
288  desired, success, failure) \
289  __c11_atomic_compare_exchange_weak(object, expected, desired, \
290  success, failure)
291 #define atomic_exchange_explicit(object, desired, order) \
292  __c11_atomic_exchange(object, desired, order)
293 #define atomic_fetch_add_explicit(object, operand, order) \
294  __c11_atomic_fetch_add(object, operand, order)
295 #define atomic_fetch_and_explicit(object, operand, order) \
296  __c11_atomic_fetch_and(object, operand, order)
297 #define atomic_fetch_or_explicit(object, operand, order) \
298  __c11_atomic_fetch_or(object, operand, order)
299 #define atomic_fetch_sub_explicit(object, operand, order) \
300  __c11_atomic_fetch_sub(object, operand, order)
301 #define atomic_fetch_xor_explicit(object, operand, order) \
302  __c11_atomic_fetch_xor(object, operand, order)
303 #define atomic_load_explicit(object, order) \
304  __c11_atomic_load(object, order)
305 #define atomic_store_explicit(object, desired, order) \
306  __c11_atomic_store(object, desired, order)
307 #elif defined(__GNUC_ATOMICS)
308 #define atomic_compare_exchange_strong_explicit(object, expected, \
309  desired, success, failure) \
310  __atomic_compare_exchange_n(&(object)->__val, expected, \
311  desired, 0, success, failure)
312 #define atomic_compare_exchange_weak_explicit(object, expected, \
313  desired, success, failure) \
314  __atomic_compare_exchange_n(&(object)->__val, expected, \
315  desired, 1, success, failure)
316 #define atomic_exchange_explicit(object, desired, order) \
317  __atomic_exchange_n(&(object)->__val, desired, order)
318 #define atomic_fetch_add_explicit(object, operand, order) \
319  __atomic_fetch_add(&(object)->__val, operand, order)
320 #define atomic_fetch_and_explicit(object, operand, order) \
321  __atomic_fetch_and(&(object)->__val, operand, order)
322 #define atomic_fetch_or_explicit(object, operand, order) \
323  __atomic_fetch_or(&(object)->__val, operand, order)
324 #define atomic_fetch_sub_explicit(object, operand, order) \
325  __atomic_fetch_sub(&(object)->__val, operand, order)
326 #define atomic_fetch_xor_explicit(object, operand, order) \
327  __atomic_fetch_xor(&(object)->__val, operand, order)
328 #define atomic_load_explicit(object, order) \
329  __atomic_load_n(&(object)->__val, order)
330 #define atomic_store_explicit(object, desired, order) \
331  __atomic_store_n(&(object)->__val, desired, order)
332 #else
333 #define __atomic_apply_stride(object, operand) \
334  (((__typeof__((object)->__val))0) + (operand))
335 #define atomic_compare_exchange_strong_explicit(object, expected, \
336  desired, success, failure) __extension__ ({ \
337  __typeof__(expected) __ep = (expected); \
338  __typeof__(*__ep) __e = *__ep; \
339  (void)(success); (void)(failure); \
340  (__Bool)((*__ep = __sync_val_compare_and_swap(&(object)->__val, \
341  __e, desired)) == __e); \
342 })
343 #define atomic_compare_exchange_weak_explicit(object, expected, \
344  desired, success, failure) \
345  atomic_compare_exchange_strong_explicit(object, expected, \
346  desired, success, failure)
347 #if __has_builtin(__sync_swap)
348 /* Clang provides a full-barrier atomic exchange - use it if available. */
349 #define atomic_exchange_explicit(object, desired, order) \
350  ((void)(order), __sync_swap(&(object)->__val, desired))
351 #else
352 /*
353  * __sync_lock_test_and_set() is only an acquire barrier in theory (although in
354  * practice it is usually a full barrier) so we need an explicit barrier before
355  * it.
356  */
357 #define atomic_exchange_explicit(object, desired, order) \
358 __extension__ ({ \
359  __typeof__(object) __o = (object); \
360  __typeof__(desired) __d = (desired); \
361  (void)(order); \
362  __sync_synchronize(); \
363  __sync_lock_test_and_set(&(__o)->__val, __d); \
364 })
365 #endif
366 #define atomic_fetch_add_explicit(object, operand, order) \
367  ((void)(order), __sync_fetch_and_add(&(object)->__val, \
368  __atomic_apply_stride(object, operand)))
369 #define atomic_fetch_and_explicit(object, operand, order) \
370  ((void)(order), __sync_fetch_and_and(&(object)->__val, operand))
371 #define atomic_fetch_or_explicit(object, operand, order) \
372  ((void)(order), __sync_fetch_and_or(&(object)->__val, operand))
373 #define atomic_fetch_sub_explicit(object, operand, order) \
374  ((void)(order), __sync_fetch_and_sub(&(object)->__val, \
375  __atomic_apply_stride(object, operand)))
376 #define atomic_fetch_xor_explicit(object, operand, order) \
377  ((void)(order), __sync_fetch_and_xor(&(object)->__val, operand))
378 #define atomic_load_explicit(object, order) \
379  ((void)(order), __sync_fetch_and_add(&(object)->__val, 0))
380 #define atomic_store_explicit(object, desired, order) \
381  ((void)atomic_exchange_explicit(object, desired, order))
382 #endif
383 
384 /*
385  * Convenience functions.
386  *
387  * Don't provide these in kernel space. In kernel space, we should be
388  * disciplined enough to always provide explicit barriers.
389  */
390 
391 #ifndef _KERNEL
392 #define atomic_compare_exchange_strong(object, expected, desired) \
393  atomic_compare_exchange_strong_explicit(object, expected, \
394  desired, memory_order_seq_cst, memory_order_seq_cst)
395 #define atomic_compare_exchange_weak(object, expected, desired) \
396  atomic_compare_exchange_weak_explicit(object, expected, \
397  desired, memory_order_seq_cst, memory_order_seq_cst)
398 #define atomic_exchange(object, desired) \
399  atomic_exchange_explicit(object, desired, memory_order_seq_cst)
400 #define atomic_fetch_add(object, operand) \
401  atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
402 #define atomic_fetch_and(object, operand) \
403  atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
404 #define atomic_fetch_or(object, operand) \
405  atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
406 #define atomic_fetch_sub(object, operand) \
407  atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
408 #define atomic_fetch_xor(object, operand) \
409  atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
410 #define atomic_load(object) \
411  atomic_load_explicit(object, memory_order_seq_cst)
412 #define atomic_store(object, desired) \
413  atomic_store_explicit(object, desired, memory_order_seq_cst)
414 #endif /* !_KERNEL */
415 
416 /*
417  * 7.17.8 Atomic flag type and operations.
418  *
419  * XXX: Assume atomic_bool can be used as an atomic_flag. Is there some
420  * kind of compiler built-in type we could use?
421  */
422 
423 typedef struct {
424  atomic_bool __flag;
425 } atomic_flag;
426 
427 #define ATOMIC_FLAG_INIT { ATOMIC_VAR_INIT(0) }
428 
429 static __inline __Bool
431  memory_order __order)
432 {
433  return (atomic_exchange_explicit(&__object->__flag, 1, __order));
434 }
435 
436 static __inline void
438 {
439 
440  atomic_store_explicit(&__object->__flag, 0, __order);
441 }
442 
443 #ifndef _KERNEL
444 static __inline __Bool
446 {
447 
448  return (atomic_flag_test_and_set_explicit(__object,
450 }
451 
452 static __inline void
453 atomic_flag_clear(volatile atomic_flag *__object)
454 {
455 
457 }
458 #endif /* !_KERNEL */
459 
460 #endif /* !_STDATOMIC_H_ */
memory_order
Definition: stdatomic.h:168
#define atomic_exchange_explicit(object, desired, order)
Definition: stdatomic.h:357
#define __ATOMIC_RELEASE
Definition: stdatomic.h:152
#define __ATOMIC_ACQ_REL
Definition: stdatomic.h:155
static __inline __Bool atomic_flag_test_and_set(volatile atomic_flag *__object)
Definition: stdatomic.h:445
#define atomic_store_explicit(object, desired, order)
Definition: stdatomic.h:380
#define __unused
Definition: stdatomic.h:181
static __inline void atomic_flag_clear_explicit(volatile atomic_flag *__object, memory_order __order)
Definition: stdatomic.h:437
atomic_bool __flag
Definition: stdatomic.h:424
#define __ATOMIC_CONSUME
Definition: stdatomic.h:146
static __inline __Bool atomic_flag_test_and_set_explicit(volatile atomic_flag *__object, memory_order __order)
Definition: stdatomic.h:430
static __inline void atomic_flag_clear(volatile atomic_flag *__object)
Definition: stdatomic.h:453
static __inline void atomic_signal_fence(memory_order __order __unused)
Definition: stdatomic.h:197
#define _Atomic(T)
Definition: stdatomic.h:121
#define __ATOMIC_SEQ_CST
Definition: stdatomic.h:158
#define __ATOMIC_RELAXED
Definition: stdatomic.h:143
static __inline void atomic_thread_fence(memory_order __order __unused)
Definition: stdatomic.h:184
#define __ATOMIC_ACQUIRE
Definition: stdatomic.h:149
int __Bool
Definition: stdatomic.h:83