1*1208bc7eSAndroid Build Coastguard Worker #ifndef JEMALLOC_INTERNAL_ATOMIC_H 2*1208bc7eSAndroid Build Coastguard Worker #define JEMALLOC_INTERNAL_ATOMIC_H 3*1208bc7eSAndroid Build Coastguard Worker 4*1208bc7eSAndroid Build Coastguard Worker #define ATOMIC_INLINE static inline 5*1208bc7eSAndroid Build Coastguard Worker 6*1208bc7eSAndroid Build Coastguard Worker #if defined(JEMALLOC_GCC_ATOMIC_ATOMICS) 7*1208bc7eSAndroid Build Coastguard Worker # include "jemalloc/internal/atomic_gcc_atomic.h" 8*1208bc7eSAndroid Build Coastguard Worker #elif defined(JEMALLOC_GCC_SYNC_ATOMICS) 9*1208bc7eSAndroid Build Coastguard Worker # include "jemalloc/internal/atomic_gcc_sync.h" 10*1208bc7eSAndroid Build Coastguard Worker #elif defined(_MSC_VER) 11*1208bc7eSAndroid Build Coastguard Worker # include "jemalloc/internal/atomic_msvc.h" 12*1208bc7eSAndroid Build Coastguard Worker #elif defined(JEMALLOC_C11_ATOMICS) 13*1208bc7eSAndroid Build Coastguard Worker # include "jemalloc/internal/atomic_c11.h" 14*1208bc7eSAndroid Build Coastguard Worker #else 15*1208bc7eSAndroid Build Coastguard Worker # error "Don't have atomics implemented on this platform." 16*1208bc7eSAndroid Build Coastguard Worker #endif 17*1208bc7eSAndroid Build Coastguard Worker 18*1208bc7eSAndroid Build Coastguard Worker /* 19*1208bc7eSAndroid Build Coastguard Worker * This header gives more or less a backport of C11 atomics. The user can write 20*1208bc7eSAndroid Build Coastguard Worker * JEMALLOC_GENERATE_ATOMICS(type, short_type, lg_sizeof_type); to generate 21*1208bc7eSAndroid Build Coastguard Worker * counterparts of the C11 atomic functions for type, as so: 22*1208bc7eSAndroid Build Coastguard Worker * JEMALLOC_GENERATE_ATOMICS(int *, pi, 3); 23*1208bc7eSAndroid Build Coastguard Worker * and then write things like: 24*1208bc7eSAndroid Build Coastguard Worker * int *some_ptr; 25*1208bc7eSAndroid Build Coastguard Worker * atomic_pi_t atomic_ptr_to_int; 26*1208bc7eSAndroid Build Coastguard Worker * atomic_store_pi(&atomic_ptr_to_int, some_ptr, ATOMIC_RELAXED); 27*1208bc7eSAndroid Build Coastguard Worker * int *prev_value = atomic_exchange_pi(&ptr_to_int, NULL, ATOMIC_ACQ_REL); 28*1208bc7eSAndroid Build Coastguard Worker * assert(some_ptr == prev_value); 29*1208bc7eSAndroid Build Coastguard Worker * and expect things to work in the obvious way. 30*1208bc7eSAndroid Build Coastguard Worker * 31*1208bc7eSAndroid Build Coastguard Worker * Also included (with naming differences to avoid conflicts with the standard 32*1208bc7eSAndroid Build Coastguard Worker * library): 33*1208bc7eSAndroid Build Coastguard Worker * atomic_fence(atomic_memory_order_t) (mimics C11's atomic_thread_fence). 34*1208bc7eSAndroid Build Coastguard Worker * ATOMIC_INIT (mimics C11's ATOMIC_VAR_INIT). 35*1208bc7eSAndroid Build Coastguard Worker */ 36*1208bc7eSAndroid Build Coastguard Worker 37*1208bc7eSAndroid Build Coastguard Worker /* 38*1208bc7eSAndroid Build Coastguard Worker * Pure convenience, so that we don't have to type "atomic_memory_order_" 39*1208bc7eSAndroid Build Coastguard Worker * quite so often. 40*1208bc7eSAndroid Build Coastguard Worker */ 41*1208bc7eSAndroid Build Coastguard Worker #define ATOMIC_RELAXED atomic_memory_order_relaxed 42*1208bc7eSAndroid Build Coastguard Worker #define ATOMIC_ACQUIRE atomic_memory_order_acquire 43*1208bc7eSAndroid Build Coastguard Worker #define ATOMIC_RELEASE atomic_memory_order_release 44*1208bc7eSAndroid Build Coastguard Worker #define ATOMIC_ACQ_REL atomic_memory_order_acq_rel 45*1208bc7eSAndroid Build Coastguard Worker #define ATOMIC_SEQ_CST atomic_memory_order_seq_cst 46*1208bc7eSAndroid Build Coastguard Worker 47*1208bc7eSAndroid Build Coastguard Worker /* 48*1208bc7eSAndroid Build Coastguard Worker * Not all platforms have 64-bit atomics. If we do, this #define exposes that 49*1208bc7eSAndroid Build Coastguard Worker * fact. 50*1208bc7eSAndroid Build Coastguard Worker */ 51*1208bc7eSAndroid Build Coastguard Worker #if (LG_SIZEOF_PTR == 3 || LG_SIZEOF_INT == 3) 52*1208bc7eSAndroid Build Coastguard Worker # define JEMALLOC_ATOMIC_U64 53*1208bc7eSAndroid Build Coastguard Worker #endif 54*1208bc7eSAndroid Build Coastguard Worker 55*1208bc7eSAndroid Build Coastguard Worker JEMALLOC_GENERATE_ATOMICS(void *, p, LG_SIZEOF_PTR) 56*1208bc7eSAndroid Build Coastguard Worker 57*1208bc7eSAndroid Build Coastguard Worker /* 58*1208bc7eSAndroid Build Coastguard Worker * There's no actual guarantee that sizeof(bool) == 1, but it's true on the only 59*1208bc7eSAndroid Build Coastguard Worker * platform that actually needs to know the size, MSVC. 60*1208bc7eSAndroid Build Coastguard Worker */ 61*1208bc7eSAndroid Build Coastguard Worker JEMALLOC_GENERATE_ATOMICS(bool, b, 0) 62*1208bc7eSAndroid Build Coastguard Worker 63*1208bc7eSAndroid Build Coastguard Worker JEMALLOC_GENERATE_INT_ATOMICS(unsigned, u, LG_SIZEOF_INT) 64*1208bc7eSAndroid Build Coastguard Worker 65*1208bc7eSAndroid Build Coastguard Worker JEMALLOC_GENERATE_INT_ATOMICS(size_t, zu, LG_SIZEOF_PTR) 66*1208bc7eSAndroid Build Coastguard Worker 67*1208bc7eSAndroid Build Coastguard Worker JEMALLOC_GENERATE_INT_ATOMICS(ssize_t, zd, LG_SIZEOF_PTR) 68*1208bc7eSAndroid Build Coastguard Worker 69*1208bc7eSAndroid Build Coastguard Worker JEMALLOC_GENERATE_INT_ATOMICS(uint32_t, u32, 2) 70*1208bc7eSAndroid Build Coastguard Worker 71*1208bc7eSAndroid Build Coastguard Worker #ifdef JEMALLOC_ATOMIC_U64 72*1208bc7eSAndroid Build Coastguard Worker JEMALLOC_GENERATE_INT_ATOMICS(uint64_t, u64, 3) 73*1208bc7eSAndroid Build Coastguard Worker #endif 74*1208bc7eSAndroid Build Coastguard Worker 75*1208bc7eSAndroid Build Coastguard Worker #undef ATOMIC_INLINE 76*1208bc7eSAndroid Build Coastguard Worker 77*1208bc7eSAndroid Build Coastguard Worker #endif /* JEMALLOC_INTERNAL_ATOMIC_H */ 78