]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - libc/include/stdatomic.h
Merge "Add missing type casts before comparison."
[android-sdk/platform-bionic.git] / libc / include / stdatomic.h
1 /*-
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  */
30 #ifndef _STDATOMIC_H_
31 #define _STDATOMIC_H_
33 #include <sys/cdefs.h>
35 #if defined(__cplusplus) && defined(_USING_LIBCXX) && \
36     (__has_feature(cxx_atomic) || _GNUC_VER >= 407)
38 /* We have a usable C++ <atomic>; use it instead.  */
40 #include <atomic>
42 #undef _Atomic
43         /* Also defined by <atomic> for gcc.  But not used in macros. */
44         /* Also a clang intrinsic.                                    */
45         /* Should not be used by client code before this file is      */
46         /* included.  The definitions in <atomic> themselves see      */
47         /* the old definition, as they should.                        */
48         /* Client code sees the following definition.                 */
49 #define _Atomic(t) std::atomic<t>
51 using std::atomic_is_lock_free;
52 using std::atomic_init;
53 using std::atomic_store;
54 using std::atomic_store_explicit;
55 using std::atomic_load;
56 using std::atomic_load_explicit;
57 using std::atomic_exchange;
58 using std::atomic_exchange_explicit;
59 using std::atomic_compare_exchange_strong;
60 using std::atomic_compare_exchange_strong_explicit;
61 using std::atomic_compare_exchange_weak;
62 using std::atomic_compare_exchange_weak_explicit;
63 using std::atomic_fetch_add;
64 using std::atomic_fetch_add_explicit;
65 using std::atomic_fetch_sub;
66 using std::atomic_fetch_sub_explicit;
67 using std::atomic_fetch_or;
68 using std::atomic_fetch_or_explicit;
69 using std::atomic_fetch_xor;
70 using std::atomic_fetch_xor_explicit;
71 using std::atomic_fetch_and;
72 using std::atomic_fetch_and_explicit;
73 using std::atomic_thread_fence;
74 using std::atomic_signal_fence;
76 using std::memory_order;
77 using std::memory_order_relaxed;
78 using std::memory_order_consume;
79 using std::memory_order_release;
80 using std::memory_order_acq_rel;
81 using std::memory_order_seq_cst;
83 using std::atomic_bool;
84 using std::atomic_char;
85 using std::atomic_schar;
86 using std::atomic_uchar;
87 using std::atomic_short;
88 using std::atomic_ushort;
89 using std::atomic_int;
90 using std::atomic_uint;
91 using std::atomic_long;
92 using std::atomic_ulong;
93 using std::atomic_llong;
94 using std::atomic_ullong;
95 using std::atomic_char16_t;
96 using std::atomic_char32_t;
97 using std::atomic_wchar_t;
98 using std::atomic_int_least8_t;
99 using std::atomic_uint_least8_t;
100 using std::atomic_int_least16_t;
101 using std::atomic_uint_least16_t;
102 using std::atomic_int_least32_t;
103 using std::atomic_uint_least32_t;
104 using std::atomic_int_least64_t;
105 using std::atomic_uint_least64_t;
106 using std::atomic_int_fast8_t;
107 using std::atomic_uint_fast8_t;
108 using std::atomic_int_fast16_t;
109 using std::atomic_uint_fast16_t;
110 using std::atomic_int_fast32_t;
111 using std::atomic_uint_fast32_t;
112 using std::atomic_int_fast64_t;
113 using std::atomic_uint_fast64_t;
114 using std::atomic_intptr_t;
115 using std::atomic_uintptr_t;
116 using std::atomic_size_t;
117 using std::atomic_ptrdiff_t;
118 using std::atomic_intmax_t;
119 using std::atomic_uintmax_t;
121 #else /* <atomic> unavailable, possibly because this is C, not C++ */
123 #include <sys/types.h>
124 #include <stdbool.h>
126 /*
127  * C: Do it ourselves.
128  * Note that the runtime representation defined here should be compatible
129  * with the C++ one, i.e. an _Atomic(T) needs to contain the same
130  * bits as a T.
131  */
133 #include <stddef.h>  /* For ptrdiff_t.                          */
134 #include <stdint.h>  /* TODO: Should pollute namespace less.    */
135 #if __STDC_VERSION__ >= 201112L
136 # include <uchar.h>  /* For char16_t and char32_t.              */
137 #endif
139 #if __has_extension(c_atomic) || __has_extension(cxx_atomic)
140 #define __CLANG_ATOMICS
141 #elif __GNUC_PREREQ__(4, 7)
142 #define __GNUC_ATOMICS
143 #elif defined(__GNUC__)
144 #define __SYNC_ATOMICS
145 #else
146 #error "stdatomic.h does not support your compiler"
147 #endif
149 /*
150  * 7.17.1 Atomic lock-free macros.
151  */
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
184 /*
185  * 7.17.2 Initialization.
186  */
188 #if defined(__CLANG_ATOMICS)
189 #define ATOMIC_VAR_INIT(value)          (value)
190 #define atomic_init(obj, value)         __c11_atomic_init(obj, value)
191 #else
192 #define ATOMIC_VAR_INIT(value)          { .__val = (value) }
193 #define atomic_init(obj, value)         ((void)((obj)->__val = (value)))
194 #endif
196 /*
197  * Clang and recent GCC both provide predefined macros for the memory
198  * orderings.  If we are using a compiler that doesn't define them, use the
199  * clang values - these will be ignored in the fallback path.
200  */
202 #ifndef __ATOMIC_RELAXED
203 #define __ATOMIC_RELAXED                0
204 #endif
205 #ifndef __ATOMIC_CONSUME
206 #define __ATOMIC_CONSUME                1
207 #endif
208 #ifndef __ATOMIC_ACQUIRE
209 #define __ATOMIC_ACQUIRE                2
210 #endif
211 #ifndef __ATOMIC_RELEASE
212 #define __ATOMIC_RELEASE                3
213 #endif
214 #ifndef __ATOMIC_ACQ_REL
215 #define __ATOMIC_ACQ_REL                4
216 #endif
217 #ifndef __ATOMIC_SEQ_CST
218 #define __ATOMIC_SEQ_CST                5
219 #endif
221 /*
222  * 7.17.3 Order and consistency.
223  *
224  * The memory_order_* constants that denote the barrier behaviour of the
225  * atomic operations.
226  * The enum values must be identical to those used by the
227  * C++ <atomic> header.
228  */
230 typedef enum {
231         memory_order_relaxed = __ATOMIC_RELAXED,
232         memory_order_consume = __ATOMIC_CONSUME,
233         memory_order_acquire = __ATOMIC_ACQUIRE,
234         memory_order_release = __ATOMIC_RELEASE,
235         memory_order_acq_rel = __ATOMIC_ACQ_REL,
236         memory_order_seq_cst = __ATOMIC_SEQ_CST
237 } memory_order;
239 /*
240  * 7.17.4 Fences.
241  */
243 static __inline void
244 atomic_thread_fence(memory_order __order __attribute__((unused)))
247 #ifdef __CLANG_ATOMICS
248         __c11_atomic_thread_fence(__order);
249 #elif defined(__GNUC_ATOMICS)
250         __atomic_thread_fence(__order);
251 #else
252         __sync_synchronize();
253 #endif
256 static __inline void
257 atomic_signal_fence(memory_order __order __attribute__((unused)))
260 #ifdef __CLANG_ATOMICS
261         __c11_atomic_signal_fence(__order);
262 #elif defined(__GNUC_ATOMICS)
263         __atomic_signal_fence(__order);
264 #else
265         __asm volatile ("" ::: "memory");
266 #endif
269 /*
270  * 7.17.5 Lock-free property.
271  */
273 #if defined(_KERNEL)
274 /* Atomics in kernelspace are always lock-free. */
275 #define atomic_is_lock_free(obj) \
276         ((void)(obj), (_Bool)1)
277 #elif defined(__CLANG_ATOMICS)
278 #define atomic_is_lock_free(obj) \
279         __c11_atomic_is_lock_free(sizeof(*(obj)))
280 #elif defined(__GNUC_ATOMICS)
281 #define atomic_is_lock_free(obj) \
282         __atomic_is_lock_free(sizeof((obj)->__val), &(obj)->__val)
283 #else
284 #define atomic_is_lock_free(obj) \
285         ((void)(obj), sizeof((obj)->__val) <= sizeof(void *))
286 #endif
288 /*
289  * 7.17.6 Atomic integer types.
290  */
292 #if !__has_extension(c_atomic) && !__has_extension(cxx_atomic)
293 /*
294  * No native support for _Atomic(). Place object in structure to prevent
295  * most forms of direct non-atomic access.
296  */
297 #define _Atomic(T)              struct { T volatile __val; }
298 #endif
300 typedef _Atomic(bool)                   atomic_bool;
301 typedef _Atomic(char)                   atomic_char;
302 typedef _Atomic(signed char)            atomic_schar;
303 typedef _Atomic(unsigned char)          atomic_uchar;
304 typedef _Atomic(short)                  atomic_short;
305 typedef _Atomic(unsigned short)         atomic_ushort;
306 typedef _Atomic(int)                    atomic_int;
307 typedef _Atomic(unsigned int)           atomic_uint;
308 typedef _Atomic(long)                   atomic_long;
309 typedef _Atomic(unsigned long)          atomic_ulong;
310 typedef _Atomic(long long)              atomic_llong;
311 typedef _Atomic(unsigned long long)     atomic_ullong;
312 #if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L
313   typedef _Atomic(char16_t)             atomic_char16_t;
314   typedef _Atomic(char32_t)             atomic_char32_t;
315 #endif
316 typedef _Atomic(wchar_t)                atomic_wchar_t;
317 typedef _Atomic(int_least8_t)           atomic_int_least8_t;
318 typedef _Atomic(uint_least8_t)  atomic_uint_least8_t;
319 typedef _Atomic(int_least16_t)  atomic_int_least16_t;
320 typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
321 typedef _Atomic(int_least32_t)  atomic_int_least32_t;
322 typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
323 typedef _Atomic(int_least64_t)  atomic_int_least64_t;
324 typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
325 typedef _Atomic(int_fast8_t)            atomic_int_fast8_t;
326 typedef _Atomic(uint_fast8_t)           atomic_uint_fast8_t;
327 typedef _Atomic(int_fast16_t)           atomic_int_fast16_t;
328 typedef _Atomic(uint_fast16_t)  atomic_uint_fast16_t;
329 typedef _Atomic(int_fast32_t)           atomic_int_fast32_t;
330 typedef _Atomic(uint_fast32_t)  atomic_uint_fast32_t;
331 typedef _Atomic(int_fast64_t)           atomic_int_fast64_t;
332 typedef _Atomic(uint_fast64_t)  atomic_uint_fast64_t;
333 typedef _Atomic(intptr_t)               atomic_intptr_t;
334 typedef _Atomic(uintptr_t)              atomic_uintptr_t;
335 typedef _Atomic(size_t)         atomic_size_t;
336 typedef _Atomic(ptrdiff_t)              atomic_ptrdiff_t;
337 typedef _Atomic(intmax_t)               atomic_intmax_t;
338 typedef _Atomic(uintmax_t)              atomic_uintmax_t;
340 /*
341  * 7.17.7 Operations on atomic types.
342  */
344 /*
345  * Compiler-specific operations.
346  */
348 #if defined(__CLANG_ATOMICS)
349 #define atomic_compare_exchange_strong_explicit(object, expected,       \
350     desired, success, failure)                                          \
351         __c11_atomic_compare_exchange_strong(object, expected, desired, \
352             success, failure)
353 #define atomic_compare_exchange_weak_explicit(object, expected,         \
354     desired, success, failure)                                          \
355         __c11_atomic_compare_exchange_weak(object, expected, desired,   \
356             success, failure)
357 #define atomic_exchange_explicit(object, desired, order)                \
358         __c11_atomic_exchange(object, desired, order)
359 #define atomic_fetch_add_explicit(object, operand, order)               \
360         __c11_atomic_fetch_add(object, operand, order)
361 #define atomic_fetch_and_explicit(object, operand, order)               \
362         __c11_atomic_fetch_and(object, operand, order)
363 #define atomic_fetch_or_explicit(object, operand, order)                \
364         __c11_atomic_fetch_or(object, operand, order)
365 #define atomic_fetch_sub_explicit(object, operand, order)               \
366         __c11_atomic_fetch_sub(object, operand, order)
367 #define atomic_fetch_xor_explicit(object, operand, order)               \
368         __c11_atomic_fetch_xor(object, operand, order)
369 #define atomic_load_explicit(object, order)                             \
370         __c11_atomic_load(object, order)
371 #define atomic_store_explicit(object, desired, order)                   \
372         __c11_atomic_store(object, desired, order)
373 #elif defined(__GNUC_ATOMICS)
374 #define atomic_compare_exchange_strong_explicit(object, expected,       \
375     desired, success, failure)                                          \
376         __atomic_compare_exchange_n(&(object)->__val, expected,         \
377             desired, 0, success, failure)
378 #define atomic_compare_exchange_weak_explicit(object, expected,         \
379     desired, success, failure)                                          \
380         __atomic_compare_exchange_n(&(object)->__val, expected,         \
381             desired, 1, success, failure)
382 #define atomic_exchange_explicit(object, desired, order)                \
383         __atomic_exchange_n(&(object)->__val, desired, order)
384 #define atomic_fetch_add_explicit(object, operand, order)               \
385         __atomic_fetch_add(&(object)->__val, operand, order)
386 #define atomic_fetch_and_explicit(object, operand, order)               \
387         __atomic_fetch_and(&(object)->__val, operand, order)
388 #define atomic_fetch_or_explicit(object, operand, order)                \
389         __atomic_fetch_or(&(object)->__val, operand, order)
390 #define atomic_fetch_sub_explicit(object, operand, order)               \
391         __atomic_fetch_sub(&(object)->__val, operand, order)
392 #define atomic_fetch_xor_explicit(object, operand, order)               \
393         __atomic_fetch_xor(&(object)->__val, operand, order)
394 #define atomic_load_explicit(object, order)                             \
395         __atomic_load_n(&(object)->__val, order)
396 #define atomic_store_explicit(object, desired, order)                   \
397         __atomic_store_n(&(object)->__val, desired, order)
398 #else
399 #define __atomic_apply_stride(object, operand) \
400         (((__typeof__((object)->__val))0) + (operand))
401 #define atomic_compare_exchange_strong_explicit(object, expected,       \
402     desired, success, failure)  __extension__ ({                        \
403         __typeof__(expected) __ep = (expected);                         \
404         __typeof__(*__ep) __e = *__ep;                                  \
405         (void)(success); (void)(failure);                               \
406         (bool)((*__ep = __sync_val_compare_and_swap(&(object)->__val,   \
407             __e, desired)) == __e);                                     \
408 })
409 #define atomic_compare_exchange_weak_explicit(object, expected,         \
410     desired, success, failure)                                          \
411         atomic_compare_exchange_strong_explicit(object, expected,       \
412                 desired, success, failure)
413 #if __has_builtin(__sync_swap)
414 /* Clang provides a full-barrier atomic exchange - use it if available. */
415 #define atomic_exchange_explicit(object, desired, order)                \
416         ((void)(order), __sync_swap(&(object)->__val, desired))
417 #else
418 /*
419  * __sync_lock_test_and_set() is only an acquire barrier in theory (although in
420  * practice it is usually a full barrier) so we need an explicit barrier before
421  * it.
422  */
423 #define atomic_exchange_explicit(object, desired, order)                \
424 __extension__ ({                                                        \
425         __typeof__(object) __o = (object);                              \
426         __typeof__(desired) __d = (desired);                            \
427         (void)(order);                                                  \
428         __sync_synchronize();                                           \
429         __sync_lock_test_and_set(&(__o)->__val, __d);                   \
430 })
431 #endif
432 #define atomic_fetch_add_explicit(object, operand, order)               \
433         ((void)(order), __sync_fetch_and_add(&(object)->__val,          \
434             __atomic_apply_stride(object, operand)))
435 #define atomic_fetch_and_explicit(object, operand, order)               \
436         ((void)(order), __sync_fetch_and_and(&(object)->__val, operand))
437 #define atomic_fetch_or_explicit(object, operand, order)                \
438         ((void)(order), __sync_fetch_and_or(&(object)->__val, operand))
439 #define atomic_fetch_sub_explicit(object, operand, order)               \
440         ((void)(order), __sync_fetch_and_sub(&(object)->__val,          \
441             __atomic_apply_stride(object, operand)))
442 #define atomic_fetch_xor_explicit(object, operand, order)               \
443         ((void)(order), __sync_fetch_and_xor(&(object)->__val, operand))
444 #define atomic_load_explicit(object, order)                             \
445         ((void)(order), __sync_fetch_and_add(&(object)->__val, 0))
446 #define atomic_store_explicit(object, desired, order)                   \
447         ((void)atomic_exchange_explicit(object, desired, order))
448 #endif
450 /*
451  * Convenience functions.
452  *
453  * Don't provide these in kernel space. In kernel space, we should be
454  * disciplined enough to always provide explicit barriers.
455  */
457 #ifndef _KERNEL
458 #define atomic_compare_exchange_strong(object, expected, desired)       \
459         atomic_compare_exchange_strong_explicit(object, expected,       \
460             desired, memory_order_seq_cst, memory_order_seq_cst)
461 #define atomic_compare_exchange_weak(object, expected, desired)         \
462         atomic_compare_exchange_weak_explicit(object, expected,         \
463             desired, memory_order_seq_cst, memory_order_seq_cst)
464 #define atomic_exchange(object, desired)                                \
465         atomic_exchange_explicit(object, desired, memory_order_seq_cst)
466 #define atomic_fetch_add(object, operand)                               \
467         atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
468 #define atomic_fetch_and(object, operand)                               \
469         atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
470 #define atomic_fetch_or(object, operand)                                \
471         atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
472 #define atomic_fetch_sub(object, operand)                               \
473         atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
474 #define atomic_fetch_xor(object, operand)                               \
475         atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
476 #define atomic_load(object)                                             \
477         atomic_load_explicit(object, memory_order_seq_cst)
478 #define atomic_store(object, desired)                                   \
479         atomic_store_explicit(object, desired, memory_order_seq_cst)
480 #endif /* !_KERNEL */
482 /*
483  * 7.17.8 Atomic flag type and operations.
484  *
485  * XXX: Assume atomic_bool can be used as an atomic_flag. Is there some
486  * kind of compiler built-in type we could use?
487  */
489 typedef struct {
490         atomic_bool     __flag;
491 } atomic_flag;
493 #define ATOMIC_FLAG_INIT                { ATOMIC_VAR_INIT(false) }
495 static __inline bool
496 atomic_flag_test_and_set_explicit(volatile atomic_flag *__object,
497     memory_order __order)
499         return (atomic_exchange_explicit(&__object->__flag, 1, __order));
502 static __inline void
503 atomic_flag_clear_explicit(volatile atomic_flag *__object, memory_order __order)
506         atomic_store_explicit(&__object->__flag, 0, __order);
509 #ifndef _KERNEL
510 static __inline bool
511 atomic_flag_test_and_set(volatile atomic_flag *__object)
514         return (atomic_flag_test_and_set_explicit(__object,
515             memory_order_seq_cst));
518 static __inline void
519 atomic_flag_clear(volatile atomic_flag *__object)
522         atomic_flag_clear_explicit(__object, memory_order_seq_cst);
524 #endif /* !_KERNEL */
526 #endif /* <atomic> unavailable */
528 #endif /* !_STDATOMIC_H_ */