Home | History | Annotate | Download | only in include
      1 /*-
      2  * Copyright (c) 2011 Ed Schouten <ed (at) FreeBSD.org>
      3  *                    David Chisnall <theraven (at) 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 #ifndef _STDATOMIC_H_
     31 #define	_STDATOMIC_H_
     32 
     33 #include <sys/cdefs.h>
     34 
     35 #if defined(__cplusplus) && __cplusplus >= 201103L && defined(_USING_LIBCXX)
     36 # if __has_feature(cxx_atomic)
     37 #  define _STDATOMIC_HAVE_ATOMIC
     38 # endif
     39 #endif
     40 
     41 #ifdef _STDATOMIC_HAVE_ATOMIC
     42 
     43 /* We have a usable C++ <atomic>; use it instead.  */
     44 
     45 #include <atomic>
     46 
     47 #undef _Atomic
     48         /* Also defined by <atomic> for gcc.  But not used in macros. */
     49         /* Also a clang intrinsic.                                    */
     50         /* Should not be used by client code before this file is      */
     51         /* included.  The definitions in <atomic> themselves see      */
     52         /* the old definition, as they should.                        */
     53         /* Client code sees the following definition.                 */
     54 
     55 #define _Atomic(t) std::atomic<t>
     56 
     57 using std::atomic_is_lock_free;
     58 using std::atomic_init;
     59 using std::atomic_store;
     60 using std::atomic_store_explicit;
     61 using std::atomic_load;
     62 using std::atomic_load_explicit;
     63 using std::atomic_exchange;
     64 using std::atomic_exchange_explicit;
     65 using std::atomic_compare_exchange_strong;
     66 using std::atomic_compare_exchange_strong_explicit;
     67 using std::atomic_compare_exchange_weak;
     68 using std::atomic_compare_exchange_weak_explicit;
     69 using std::atomic_fetch_add;
     70 using std::atomic_fetch_add_explicit;
     71 using std::atomic_fetch_sub;
     72 using std::atomic_fetch_sub_explicit;
     73 using std::atomic_fetch_or;
     74 using std::atomic_fetch_or_explicit;
     75 using std::atomic_fetch_xor;
     76 using std::atomic_fetch_xor_explicit;
     77 using std::atomic_fetch_and;
     78 using std::atomic_fetch_and_explicit;
     79 using std::atomic_thread_fence;
     80 using std::atomic_signal_fence;
     81 
     82 using std::memory_order;
     83 using std::memory_order_relaxed;
     84 using std::memory_order_consume;
     85 using std::memory_order_acquire;
     86 using std::memory_order_release;
     87 using std::memory_order_acq_rel;
     88 using std::memory_order_seq_cst;
     89 
     90 using std::atomic_bool;
     91 using std::atomic_char;
     92 using std::atomic_schar;
     93 using std::atomic_uchar;
     94 using std::atomic_short;
     95 using std::atomic_ushort;
     96 using std::atomic_int;
     97 using std::atomic_uint;
     98 using std::atomic_long;
     99 using std::atomic_ulong;
    100 using std::atomic_llong;
    101 using std::atomic_ullong;
    102 using std::atomic_char16_t;
    103 using std::atomic_char32_t;
    104 using std::atomic_wchar_t;
    105 using std::atomic_int_least8_t;
    106 using std::atomic_uint_least8_t;
    107 using std::atomic_int_least16_t;
    108 using std::atomic_uint_least16_t;
    109 using std::atomic_int_least32_t;
    110 using std::atomic_uint_least32_t;
    111 using std::atomic_int_least64_t;
    112 using std::atomic_uint_least64_t;
    113 using std::atomic_int_fast8_t;
    114 using std::atomic_uint_fast8_t;
    115 using std::atomic_int_fast16_t;
    116 using std::atomic_uint_fast16_t;
    117 using std::atomic_int_fast32_t;
    118 using std::atomic_uint_fast32_t;
    119 using std::atomic_int_fast64_t;
    120 using std::atomic_uint_fast64_t;
    121 using std::atomic_intptr_t;
    122 using std::atomic_uintptr_t;
    123 using std::atomic_size_t;
    124 using std::atomic_ptrdiff_t;
    125 using std::atomic_intmax_t;
    126 using std::atomic_uintmax_t;
    127 
    128 #else /* <atomic> unavailable, possibly because this is C, not C++ */
    129 
    130 #include <sys/types.h>
    131 #include <stdbool.h>
    132 
    133 /*
    134  * C: Do it ourselves.
    135  * Note that the runtime representation defined here should be compatible
    136  * with the C++ one, i.e. an _Atomic(T) needs to contain the same
    137  * bits as a T.
    138  */
    139 
    140 #include <stddef.h>  /* For ptrdiff_t. */
    141 #include <stdint.h>  /* TODO: don't drag in all the macros, just the types. */
    142 // Include uchar.h only when available.  Bionic's stdatomic.h is also used for
    143 // the host (via a copy in prebuilts/clang) and uchar.h is not available in the
    144 // glibc used for the host.
    145 #if defined(__BIONIC__)
    146 # include <uchar.h>  /* For char16_t and char32_t.              */
    147 #endif
    148 
    149 /*
    150  * 7.17.1 Atomic lock-free macros.
    151  */
    152 
    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
    183 
    184 /*
    185  * 7.17.2 Initialization.
    186  */
    187 
    188 #define	ATOMIC_VAR_INIT(value)		(value)
    189 #define	atomic_init(obj, value)		__c11_atomic_init(obj, value)
    190 
    191 /*
    192  * Clang and recent GCC both provide predefined macros for the memory
    193  * orderings.  If we are using a compiler that doesn't define them, use the
    194  * clang values - these will be ignored in the fallback path.
    195  */
    196 
    197 #ifndef __ATOMIC_RELAXED
    198 #define __ATOMIC_RELAXED		0
    199 #endif
    200 #ifndef __ATOMIC_CONSUME
    201 #define __ATOMIC_CONSUME		1
    202 #endif
    203 #ifndef __ATOMIC_ACQUIRE
    204 #define __ATOMIC_ACQUIRE		2
    205 #endif
    206 #ifndef __ATOMIC_RELEASE
    207 #define __ATOMIC_RELEASE		3
    208 #endif
    209 #ifndef __ATOMIC_ACQ_REL
    210 #define __ATOMIC_ACQ_REL		4
    211 #endif
    212 #ifndef __ATOMIC_SEQ_CST
    213 #define __ATOMIC_SEQ_CST		5
    214 #endif
    215 
    216 /*
    217  * 7.17.3 Order and consistency.
    218  *
    219  * The memory_order_* constants that denote the barrier behaviour of the
    220  * atomic operations.
    221  * The enum values must be identical to those used by the
    222  * C++ <atomic> header.
    223  */
    224 
    225 typedef enum {
    226 	memory_order_relaxed = __ATOMIC_RELAXED,
    227 	memory_order_consume = __ATOMIC_CONSUME,
    228 	memory_order_acquire = __ATOMIC_ACQUIRE,
    229 	memory_order_release = __ATOMIC_RELEASE,
    230 	memory_order_acq_rel = __ATOMIC_ACQ_REL,
    231 	memory_order_seq_cst = __ATOMIC_SEQ_CST
    232 } memory_order;
    233 
    234 /*
    235  * 7.17.4 Fences.
    236  */
    237 
    238 static __inline void atomic_thread_fence(memory_order __order __attribute__((unused))) {
    239 	__c11_atomic_thread_fence(__order);
    240 }
    241 
    242 static __inline void atomic_signal_fence(memory_order __order __attribute__((unused))) {
    243 	__c11_atomic_signal_fence(__order);
    244 }
    245 
    246 /*
    247  * 7.17.5 Lock-free property.
    248  */
    249 
    250 #define	atomic_is_lock_free(obj) __c11_atomic_is_lock_free(sizeof(*(obj)))
    251 
    252 /*
    253  * 7.17.6 Atomic integer types.
    254  */
    255 
    256 typedef _Atomic(bool)			atomic_bool;
    257 typedef _Atomic(char)			atomic_char;
    258 typedef _Atomic(signed char)		atomic_schar;
    259 typedef _Atomic(unsigned char)		atomic_uchar;
    260 typedef _Atomic(short)			atomic_short;
    261 typedef _Atomic(unsigned short)		atomic_ushort;
    262 typedef _Atomic(int)			atomic_int;
    263 typedef _Atomic(unsigned int)		atomic_uint;
    264 typedef _Atomic(long)			atomic_long;
    265 typedef _Atomic(unsigned long)		atomic_ulong;
    266 typedef _Atomic(long long)		atomic_llong;
    267 typedef _Atomic(unsigned long long)	atomic_ullong;
    268 #if defined(__BIONIC__) || (defined(__cplusplus) && __cplusplus >= 201103L)
    269   typedef _Atomic(char16_t)		atomic_char16_t;
    270   typedef _Atomic(char32_t)		atomic_char32_t;
    271 #endif
    272 typedef _Atomic(wchar_t)		atomic_wchar_t;
    273 typedef _Atomic(int_least8_t)		atomic_int_least8_t;
    274 typedef _Atomic(uint_least8_t)	atomic_uint_least8_t;
    275 typedef _Atomic(int_least16_t)	atomic_int_least16_t;
    276 typedef _Atomic(uint_least16_t)	atomic_uint_least16_t;
    277 typedef _Atomic(int_least32_t)	atomic_int_least32_t;
    278 typedef _Atomic(uint_least32_t)	atomic_uint_least32_t;
    279 typedef _Atomic(int_least64_t)	atomic_int_least64_t;
    280 typedef _Atomic(uint_least64_t)	atomic_uint_least64_t;
    281 typedef _Atomic(int_fast8_t)		atomic_int_fast8_t;
    282 typedef _Atomic(uint_fast8_t)		atomic_uint_fast8_t;
    283 typedef _Atomic(int_fast16_t)		atomic_int_fast16_t;
    284 typedef _Atomic(uint_fast16_t)	atomic_uint_fast16_t;
    285 typedef _Atomic(int_fast32_t)		atomic_int_fast32_t;
    286 typedef _Atomic(uint_fast32_t)	atomic_uint_fast32_t;
    287 typedef _Atomic(int_fast64_t)		atomic_int_fast64_t;
    288 typedef _Atomic(uint_fast64_t)	atomic_uint_fast64_t;
    289 typedef _Atomic(intptr_t)		atomic_intptr_t;
    290 typedef _Atomic(uintptr_t)		atomic_uintptr_t;
    291 typedef _Atomic(size_t)		atomic_size_t;
    292 typedef _Atomic(ptrdiff_t)		atomic_ptrdiff_t;
    293 typedef _Atomic(intmax_t)		atomic_intmax_t;
    294 typedef _Atomic(uintmax_t)		atomic_uintmax_t;
    295 
    296 /*
    297  * 7.17.7 Operations on atomic types.
    298  */
    299 
    300 /*
    301  * Compiler-specific operations.
    302  */
    303 
    304 #define	atomic_compare_exchange_strong_explicit(object, expected,	\
    305     desired, success, failure)						\
    306 	__c11_atomic_compare_exchange_strong(object, expected, desired,	\
    307 	    success, failure)
    308 #define	atomic_compare_exchange_weak_explicit(object, expected,		\
    309     desired, success, failure)						\
    310 	__c11_atomic_compare_exchange_weak(object, expected, desired,	\
    311 	    success, failure)
    312 #define	atomic_exchange_explicit(object, desired, order)		\
    313 	__c11_atomic_exchange(object, desired, order)
    314 #define	atomic_fetch_add_explicit(object, operand, order)		\
    315 	__c11_atomic_fetch_add(object, operand, order)
    316 #define	atomic_fetch_and_explicit(object, operand, order)		\
    317 	__c11_atomic_fetch_and(object, operand, order)
    318 #define	atomic_fetch_or_explicit(object, operand, order)		\
    319 	__c11_atomic_fetch_or(object, operand, order)
    320 #define	atomic_fetch_sub_explicit(object, operand, order)		\
    321 	__c11_atomic_fetch_sub(object, operand, order)
    322 #define	atomic_fetch_xor_explicit(object, operand, order)		\
    323 	__c11_atomic_fetch_xor(object, operand, order)
    324 #define	atomic_load_explicit(object, order)				\
    325 	__c11_atomic_load(object, order)
    326 #define	atomic_store_explicit(object, desired, order)			\
    327 	__c11_atomic_store(object, desired, order)
    328 
    329 /*
    330  * Convenience functions.
    331  */
    332 
    333 #define	atomic_compare_exchange_strong(object, expected, desired)	\
    334 	atomic_compare_exchange_strong_explicit(object, expected,	\
    335 	    desired, memory_order_seq_cst, memory_order_seq_cst)
    336 #define	atomic_compare_exchange_weak(object, expected, desired)		\
    337 	atomic_compare_exchange_weak_explicit(object, expected,		\
    338 	    desired, memory_order_seq_cst, memory_order_seq_cst)
    339 #define	atomic_exchange(object, desired)				\
    340 	atomic_exchange_explicit(object, desired, memory_order_seq_cst)
    341 #define	atomic_fetch_add(object, operand)				\
    342 	atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
    343 #define	atomic_fetch_and(object, operand)				\
    344 	atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
    345 #define	atomic_fetch_or(object, operand)				\
    346 	atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
    347 #define	atomic_fetch_sub(object, operand)				\
    348 	atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
    349 #define	atomic_fetch_xor(object, operand)				\
    350 	atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
    351 #define	atomic_load(object)						\
    352 	atomic_load_explicit(object, memory_order_seq_cst)
    353 #define	atomic_store(object, desired)					\
    354 	atomic_store_explicit(object, desired, memory_order_seq_cst)
    355 
    356 /*
    357  * 7.17.8 Atomic flag type and operations.
    358  *
    359  * XXX: Assume atomic_bool can be used as an atomic_flag. Is there some
    360  * kind of compiler built-in type we could use?
    361  */
    362 
    363 typedef struct {
    364 	atomic_bool	__flag;
    365 } atomic_flag;
    366 
    367 #define	ATOMIC_FLAG_INIT		{ ATOMIC_VAR_INIT(false) }
    368 
    369 static __inline bool atomic_flag_test_and_set_explicit(volatile atomic_flag *__object, memory_order __order) {
    370 	return (atomic_exchange_explicit(&__object->__flag, 1, __order));
    371 }
    372 
    373 static __inline void atomic_flag_clear_explicit(volatile atomic_flag *__object, memory_order __order) {
    374 	atomic_store_explicit(&__object->__flag, 0, __order);
    375 }
    376 
    377 static __inline bool atomic_flag_test_and_set(volatile atomic_flag *__object) {
    378 	return (atomic_flag_test_and_set_explicit(__object, memory_order_seq_cst));
    379 }
    380 
    381 static __inline void atomic_flag_clear(volatile atomic_flag *__object) {
    382 	atomic_flag_clear_explicit(__object, memory_order_seq_cst);
    383 }
    384 
    385 #endif /* <atomic> unavailable */
    386 
    387 #endif /* !_STDATOMIC_H_ */
    388