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