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