stdatomic.h revision 236326
1215976Sjmallett/*- 2215976Sjmallett * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org> 3215976Sjmallett * David Chisnall <theraven@FreeBSD.org> 4215976Sjmallett * All rights reserved. 5215976Sjmallett * 6215976Sjmallett * Redistribution and use in source and binary forms, with or without 7215976Sjmallett * modification, are permitted provided that the following conditions 8215976Sjmallett * are met: 9215976Sjmallett * 1. Redistributions of source code must retain the above copyright 10215976Sjmallett * notice, this list of conditions and the following disclaimer. 11215976Sjmallett * 2. Redistributions in binary form must reproduce the above copyright 12215976Sjmallett * notice, this list of conditions and the following disclaimer in the 13215976Sjmallett * documentation and/or other materials provided with the distribution. 14215976Sjmallett * 15215976Sjmallett * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16215976Sjmallett * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17215976Sjmallett * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18215976Sjmallett * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19215976Sjmallett * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20215976Sjmallett * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21215976Sjmallett * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22215976Sjmallett * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23215976Sjmallett * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24215976Sjmallett * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25215976Sjmallett * SUCH DAMAGE. 26215976Sjmallett * 27215976Sjmallett * $FreeBSD: stable/9/include/stdatomic.h 236326 2012-05-30 19:21:54Z theraven $ 28215976Sjmallett */ 29215976Sjmallett 30215976Sjmallett#ifndef _STDATOMIC_H_ 31215976Sjmallett#define _STDATOMIC_H_ 32215976Sjmallett 33215976Sjmallett#include <sys/cdefs.h> 34215976Sjmallett#include <sys/_types.h> 35215976Sjmallett 36215976Sjmallett#if __has_feature(cxx_atomic) 37215976Sjmallett#define __CLANG_ATOMICS 38215976Sjmallett#elif __GNUC_PREREQ__(4, 7) 39215976Sjmallett#define __GNUC_ATOMICS 40215976Sjmallett#elif !defined(__GNUC__) 41215976Sjmallett#error "stdatomic.h does not support your compiler" 42215976Sjmallett#endif 43215976Sjmallett 44215976Sjmallett#if !defined(__CLANG_ATOMICS) 45215976Sjmallett#define _Atomic(T) struct { volatile T __val; } 46215976Sjmallett#endif 47215976Sjmallett 48215976Sjmallett/* 49215976Sjmallett * 7.17.2 Initialization. 50215976Sjmallett */ 51215976Sjmallett 52215976Sjmallett#if defined(__CLANG_ATOMICS) 53215976Sjmallett#define ATOMIC_VAR_INIT(value) (value) 54215976Sjmallett#define atomic_init(obj, value) __c11_atomic_init(obj, value) 55215976Sjmallett#else 56215976Sjmallett#define ATOMIC_VAR_INIT(value) { .__val = (value) } 57215976Sjmallett#define atomic_init(obj, value) do { \ 58215976Sjmallett (obj)->__val = (value); \ 59215976Sjmallett} while (0) 60215976Sjmallett#endif 61215976Sjmallett 62215976Sjmallett/* 63215976Sjmallett * Clang and recent GCC both provide predefined macros for the memory 64215976Sjmallett * orderings. If we are using a compiler that doesn't define them, use the 65215976Sjmallett * clang values - these will be ignored in the fallback path. 66215976Sjmallett */ 67215976Sjmallett 68215976Sjmallett#ifndef __ATOMIC_RELAXED 69215976Sjmallett#define __ATOMIC_RELAXED 0 70215976Sjmallett#endif 71215976Sjmallett#ifndef __ATOMIC_CONSUME 72215976Sjmallett#define __ATOMIC_CONSUME 1 73215976Sjmallett#endif 74215976Sjmallett#ifndef __ATOMIC_ACQUIRE 75215976Sjmallett#define __ATOMIC_ACQUIRE 2 76215976Sjmallett#endif 77215976Sjmallett#ifndef __ATOMIC_RELEASE 78215976Sjmallett#define __ATOMIC_RELEASE 3 79215976Sjmallett#endif 80215976Sjmallett#ifndef __ATOMIC_ACQ_REL 81215976Sjmallett#define __ATOMIC_ACQ_REL 4 82215976Sjmallett#endif 83215976Sjmallett#ifndef __ATOMIC_SEQ_CST 84215976Sjmallett#define __ATOMIC_SEQ_CST 5 85215976Sjmallett#endif 86215976Sjmallett 87215976Sjmallett/* 88215976Sjmallett * 7.17.3 Order and consistency. 89215976Sjmallett * 90215976Sjmallett * The memory_order_* constants that denote the barrier behaviour of the 91215976Sjmallett * atomic operations. 92215976Sjmallett */ 93215976Sjmallett 94215976Sjmallettenum memory_order { 95215976Sjmallett memory_order_relaxed = __ATOMIC_RELAXED, 96215976Sjmallett memory_order_consume = __ATOMIC_CONSUME, 97215976Sjmallett memory_order_acquire = __ATOMIC_ACQUIRE, 98215976Sjmallett memory_order_release = __ATOMIC_RELEASE, 99215976Sjmallett memory_order_acq_rel = __ATOMIC_ACQ_REL, 100215976Sjmallett memory_order_seq_cst = __ATOMIC_SEQ_CST 101215976Sjmallett}; 102215976Sjmallett 103215976Sjmallett/* 104215976Sjmallett * 7.17.4 Fences. 105215976Sjmallett */ 106215976Sjmallett 107215976Sjmallett#ifdef __CLANG_ATOMICS 108215976Sjmallett#define atomic_thread_fence(order) __c11_atomic_thread_fence(order) 109215976Sjmallett#define atomic_signal_fence(order) __c11_atomic_signal_fence(order) 110215976Sjmallett#elif defined(__GNUC_ATOMICS) 111215976Sjmallett#define atomic_thread_fence(order) __atomic_thread_fence(order) 112215976Sjmallett#define atomic_signal_fence(order) __atomic_signal_fence(order) 113215976Sjmallett#else 114215976Sjmallett#define atomic_thread_fence(order) __sync_synchronize() 115215976Sjmallett#define atomic_signal_fence(order) __asm volatile ("" : : : "memory") 116215976Sjmallett#endif 117215976Sjmallett 118215976Sjmallett/* 119215976Sjmallett * 7.17.5 Lock-free property. 120215976Sjmallett */ 121215976Sjmallett 122215976Sjmallett#if defined(__CLANG_ATOMICS) 123215976Sjmallett#define atomic_is_lock_free(obj) \ 124215976Sjmallett __c11_atomic_is_lock_free(sizeof(obj)) 125215976Sjmallett#elif defined(__GNUC_ATOMICS) 126215976Sjmallett#define atomic_is_lock_free(obj) \ 127215976Sjmallett __atomic_is_lock_free(sizeof((obj)->__val)) 128215976Sjmallett#else 129215976Sjmallett#define atomic_is_lock_free(obj) \ 130215976Sjmallett (sizeof((obj)->__val) <= sizeof(void *)) 131215976Sjmallett#endif 132215976Sjmallett 133215976Sjmallett/* 134215976Sjmallett * 7.17.6 Atomic integer types. 135215976Sjmallett */ 136215976Sjmallett 137215976Sjmalletttypedef _Atomic(_Bool) atomic_bool; 138215976Sjmalletttypedef _Atomic(char) atomic_char; 139215976Sjmalletttypedef _Atomic(signed char) atomic_schar; 140215976Sjmalletttypedef _Atomic(unsigned char) atomic_uchar; 141215976Sjmalletttypedef _Atomic(short) atomic_short; 142215976Sjmalletttypedef _Atomic(unsigned short) atomic_ushort; 143215976Sjmalletttypedef _Atomic(int) atomic_int; 144215976Sjmalletttypedef _Atomic(unsigned int) atomic_uint; 145215976Sjmalletttypedef _Atomic(long) atomic_long; 146215976Sjmalletttypedef _Atomic(unsigned long) atomic_ulong; 147215976Sjmalletttypedef _Atomic(long long) atomic_llong; 148215976Sjmalletttypedef _Atomic(unsigned long long) atomic_ullong; 149215976Sjmallett#if 0 150215976Sjmalletttypedef _Atomic(__char16_t) atomic_char16_t; 151215976Sjmalletttypedef _Atomic(__char32_t) atomic_char32_t; 152215976Sjmallett#endif 153215976Sjmalletttypedef _Atomic(__wchar_t) atomic_wchar_t; 154215976Sjmalletttypedef _Atomic(__int_least8_t) atomic_int_least8_t; 155215976Sjmalletttypedef _Atomic(__uint_least8_t) atomic_uint_least8_t; 156215976Sjmalletttypedef _Atomic(__int_least16_t) atomic_int_least16_t; 157215976Sjmalletttypedef _Atomic(__uint_least16_t) atomic_uint_least16_t; 158215976Sjmalletttypedef _Atomic(__int_least32_t) atomic_int_least32_t; 159215976Sjmalletttypedef _Atomic(__uint_least32_t) atomic_uint_least32_t; 160215976Sjmalletttypedef _Atomic(__int_least64_t) atomic_int_least64_t; 161215976Sjmalletttypedef _Atomic(__uint_least64_t) atomic_uint_least64_t; 162215976Sjmalletttypedef _Atomic(__int_fast8_t) atomic_int_fast8_t; 163215976Sjmalletttypedef _Atomic(__uint_fast8_t) atomic_uint_fast8_t; 164215976Sjmalletttypedef _Atomic(__int_fast16_t) atomic_int_fast16_t; 165215976Sjmalletttypedef _Atomic(__uint_fast16_t) atomic_uint_fast16_t; 166215976Sjmalletttypedef _Atomic(__int_fast32_t) atomic_int_fast32_t; 167215976Sjmalletttypedef _Atomic(__uint_fast32_t) atomic_uint_fast32_t; 168215976Sjmalletttypedef _Atomic(__int_fast64_t) atomic_int_fast64_t; 169215976Sjmalletttypedef _Atomic(__uint_fast64_t) atomic_uint_fast64_t; 170215976Sjmalletttypedef _Atomic(__intptr_t) atomic_intptr_t; 171215976Sjmalletttypedef _Atomic(__uintptr_t) atomic_uintptr_t; 172215976Sjmalletttypedef _Atomic(__size_t) atomic_size_t; 173215976Sjmalletttypedef _Atomic(__ptrdiff_t) atomic_ptrdiff_t; 174215976Sjmalletttypedef _Atomic(__intmax_t) atomic_intmax_t; 175215976Sjmalletttypedef _Atomic(__uintmax_t) atomic_uintmax_t; 176215976Sjmallett 177215976Sjmallett/* 178215976Sjmallett * 7.17.7 Operations on atomic types. 179215976Sjmallett */ 180215976Sjmallett 181215976Sjmallett/* 182215976Sjmallett * Compiler-specific operations. 183215976Sjmallett */ 184215976Sjmallett 185215976Sjmallett#if defined(__CLANG_ATOMICS) 186215976Sjmallett#define atomic_compare_exchange_strong_explicit(object, expected, \ 187215976Sjmallett desired, success, failure) \ 188215976Sjmallett __c11_atomic_compare_exchange_strong(object, expected, desired, \ 189215976Sjmallett success, failure) 190215976Sjmallett#define atomic_compare_exchange_weak_explicit(object, expected, \ 191215976Sjmallett desired, success, failure) \ 192215976Sjmallett __c11_atomic_compare_exchange_weak(object, expected, desired, \ 193215976Sjmallett success, failure) 194215976Sjmallett#define atomic_exchange_explicit(object, desired, order) \ 195215976Sjmallett __c11_atomic_exchange(object, desired, order) 196215976Sjmallett#define atomic_fetch_add_explicit(object, operand, order) \ 197215976Sjmallett __c11_atomic_fetch_add(object, operand, order) 198215976Sjmallett#define atomic_fetch_and_explicit(object, operand, order) \ 199215976Sjmallett __c11_atomic_fetch_and(object, operand, order) 200215976Sjmallett#define atomic_fetch_or_explicit(object, operand, order) \ 201215976Sjmallett __c11_atomic_fetch_or(object, operand, order) 202215976Sjmallett#define atomic_fetch_sub_explicit(object, operand, order) \ 203215976Sjmallett __c11_atomic_fetch_sub(object, operand, order) 204215976Sjmallett#define atomic_fetch_xor_explicit(object, operand, order) \ 205215976Sjmallett __c11_atomic_fetch_xor(object, operand, order) 206215976Sjmallett#define atomic_load_explicit(object, order) \ 207215976Sjmallett __c11_atomic_load(object, order) 208215976Sjmallett#define atomic_store_explicit(object, desired, order) \ 209215976Sjmallett __c11_atomic_store(object, desired, order) 210215976Sjmallett#elif defined(__GNUC_ATOMICS) 211215976Sjmallett#define atomic_compare_exchange_strong_explicit(object, expected, \ 212215976Sjmallett desired, success, failure) \ 213215976Sjmallett __atomic_compare_exchange_n(&(object)->__val, expected, \ 214215976Sjmallett desired, 0, success, failure) 215215976Sjmallett#define atomic_compare_exchange_weak_explicit(object, expected, \ 216215976Sjmallett desired, success, failure) \ 217215976Sjmallett __atomic_compare_exchange_n(&(object)->__val, expected, \ 218215976Sjmallett desired, 1, success, failure) 219215976Sjmallett#define atomic_exchange_explicit(object, desired, order) \ 220215976Sjmallett __atomic_exchange_n(&(object)->__val, desired, order) 221215976Sjmallett#define atomic_fetch_add_explicit(object, operand, order) \ 222215976Sjmallett __atomic_fetch_add(&(object)->__val, operand, order) 223215976Sjmallett#define atomic_fetch_and_explicit(object, operand, order) \ 224215976Sjmallett __atomic_fetch_and(&(object)->__val, operand, order) 225215976Sjmallett#define atomic_fetch_or_explicit(object, operand, order) \ 226215976Sjmallett __atomic_fetch_or(&(object)->__val, operand, order) 227215976Sjmallett#define atomic_fetch_sub_explicit(object, operand, order) \ 228215976Sjmallett __atomic_fetch_sub(&(object)->__val, operand, order) 229215976Sjmallett#define atomic_fetch_xor_explicit(object, operand, order) \ 230215976Sjmallett __atomic_fetch_xor(&(object)->__val, operand, order) 231215976Sjmallett#define atomic_load_explicit(object, order) \ 232215976Sjmallett __atomic_load_n(&(object)->__val, order) 233215976Sjmallett#define atomic_store_explicit(object, desired, order) \ 234215976Sjmallett __atomic_store_n(&(object)->__val, desired, order) 235215976Sjmallett#else 236215976Sjmallett#define atomic_compare_exchange_strong_explicit(object, expected, \ 237215976Sjmallett desired, success, failure) ({ \ 238215976Sjmallett __typeof__((object)->__val) __v; \ 239215976Sjmallett _Bool __r; \ 240215976Sjmallett __v = __sync_val_compare_and_swap(&(object)->__val, \ 241215976Sjmallett *(expected), desired); \ 242215976Sjmallett __r = *(expected) == __v; \ 243215976Sjmallett *(expected) = __v; \ 244215976Sjmallett __r; \ 245215976Sjmallett}) 246215976Sjmallett 247215976Sjmallett#define atomic_compare_exchange_weak_explicit(object, expected, \ 248215976Sjmallett desired, success, failure) \ 249215976Sjmallett atomic_compare_exchange_strong_explicit(object, expected, \ 250215976Sjmallett desired, success, failure) 251215976Sjmallett#if __has_builtin(__sync_swap) 252215976Sjmallett/* Clang provides a full-barrier atomic exchange - use it if available. */ 253215976Sjmallett#define atomic_exchange_explicit(object, desired, order) \ 254215976Sjmallett __sync_swap(&(object)->__val, desired) 255215976Sjmallett#else 256215976Sjmallett/* 257215976Sjmallett * __sync_lock_test_and_set() is only an acquire barrier in theory (although in 258215976Sjmallett * practice it is usually a full barrier) so we need an explicit barrier after 259215976Sjmallett * it. 260215976Sjmallett */ 261215976Sjmallett#define atomic_exchange_explicit(object, desired, order) ({ \ 262215976Sjmallett __typeof__((object)->__val) __v; \ 263215976Sjmallett __v = __sync_lock_test_and_set(&(object)->__val, desired); \ 264215976Sjmallett __sync_synchronize(); \ 265215976Sjmallett __v; \ 266215976Sjmallett}) 267215976Sjmallett#endif 268215976Sjmallett#define atomic_fetch_add_explicit(object, operand, order) \ 269215976Sjmallett __sync_fetch_and_add(&(object)->__val, operand) 270215976Sjmallett#define atomic_fetch_and_explicit(object, operand, order) \ 271215976Sjmallett __sync_fetch_and_and(&(object)->__val, operand) 272215976Sjmallett#define atomic_fetch_or_explicit(object, operand, order) \ 273215976Sjmallett __sync_fetch_and_or(&(object)->__val, operand) 274215976Sjmallett#define atomic_fetch_sub_explicit(object, operand, order) \ 275215976Sjmallett __sync_fetch_and_sub(&(object)->__val, operand) 276215976Sjmallett#define atomic_fetch_xor_explicit(object, operand, order) \ 277215976Sjmallett __sync_fetch_and_xor(&(object)->__val, operand) 278215976Sjmallett#define atomic_load_explicit(object, order) \ 279215976Sjmallett __sync_fetch_and_add(&(object)->__val, 0) 280215976Sjmallett#define atomic_store_explicit(object, desired, order) do { \ 281215976Sjmallett __sync_synchronize(); \ 282215976Sjmallett (object)->__val = (desired); \ 283215976Sjmallett __sync_synchronize(); \ 284215976Sjmallett} while (0) 285215976Sjmallett#endif 286215976Sjmallett 287215976Sjmallett/* 288215976Sjmallett * Convenience functions. 289215976Sjmallett */ 290215976Sjmallett 291215976Sjmallett#define atomic_compare_exchange_strong(object, expected, desired) \ 292215976Sjmallett atomic_compare_exchange_strong_explicit(object, expected, \ 293215976Sjmallett desired, memory_order_seq_cst, memory_order_seq_cst) 294215976Sjmallett#define atomic_compare_exchange_weak(object, expected, desired) \ 295215976Sjmallett atomic_compare_exchange_weak_explicit(object, expected, \ 296215976Sjmallett desired, memory_order_seq_cst, memory_order_seq_cst) 297215976Sjmallett#define atomic_exchange(object, desired) \ 298215976Sjmallett atomic_exchange_explicit(object, desired, memory_order_seq_cst) 299215976Sjmallett#define atomic_fetch_add(object, operand) \ 300215976Sjmallett atomic_fetch_add_explicit(object, operand, memory_order_seq_cst) 301215976Sjmallett#define atomic_fetch_and(object, operand) \ 302215976Sjmallett atomic_fetch_and_explicit(object, operand, memory_order_seq_cst) 303215976Sjmallett#define atomic_fetch_or(object, operand) \ 304215976Sjmallett atomic_fetch_or_explicit(object, operand, memory_order_seq_cst) 305215976Sjmallett#define atomic_fetch_sub(object, operand) \ 306215976Sjmallett atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst) 307215976Sjmallett#define atomic_fetch_xor(object, operand) \ 308215976Sjmallett atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst) 309215976Sjmallett#define atomic_load(object) \ 310215976Sjmallett atomic_load_explicit(object, memory_order_seq_cst) 311215976Sjmallett#define atomic_store(object, desired) \ 312215976Sjmallett atomic_store_explicit(object, desired, memory_order_seq_cst) 313215976Sjmallett 314215976Sjmallett/* 315215976Sjmallett * 7.17.8 Atomic flag type and operations. 316215976Sjmallett */ 317215976Sjmallett 318215976Sjmalletttypedef atomic_bool atomic_flag; 319215976Sjmallett 320215976Sjmallett#define ATOMIC_FLAG_INIT ATOMIC_VAR_INIT(0) 321215976Sjmallett 322215976Sjmallett#define atomic_flag_clear_explicit(object, order) \ 323215976Sjmallett atomic_store_explicit(object, 0, order) 324215976Sjmallett#define atomic_flag_test_and_set_explicit(object, order) \ 325215976Sjmallett atomic_compare_exchange_strong_explicit(object, 0, 1, order, order) 326215976Sjmallett 327215976Sjmallett#define atomic_flag_clear(object) \ 328215976Sjmallett atomic_flag_clear_explicit(object, memory_order_seq_cst) 329215976Sjmallett#define atomic_flag_test_and_set(object) \ 330215976Sjmallett atomic_flag_test_and_set_explicit(object, memory_order_seq_cst) 331215976Sjmallett 332215976Sjmallett#endif /* !_STDATOMIC_H_ */ 333215976Sjmallett