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