2019-06-19 16:26:12 -07:00
|
|
|
/* ----------------------------------------------------------------------------
|
2020-07-26 18:00:38 -07:00
|
|
|
Copyright (c) 2018,2020 Microsoft Research, Daan Leijen
|
2019-06-19 16:26:12 -07:00
|
|
|
This is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the MIT license. A copy of the license can be found in the file
|
2019-06-23 19:53:34 +08:00
|
|
|
"LICENSE" at the root of this distribution.
|
2019-06-19 16:26:12 -07:00
|
|
|
-----------------------------------------------------------------------------*/
|
|
|
|
#pragma once
|
2019-06-26 12:57:13 +08:00
|
|
|
#ifndef MIMALLOC_ATOMIC_H
|
|
|
|
#define MIMALLOC_ATOMIC_H
|
2019-06-19 16:26:12 -07:00
|
|
|
|
2020-07-26 18:00:38 -07:00
|
|
|
// --------------------------------------------------------------------------------------------
|
2020-01-19 18:35:45 -08:00
|
|
|
// Atomics
|
2019-08-25 22:59:12 -07:00
|
|
|
// We need to be portable between C, C++, and MSVC.
|
2020-07-26 18:00:38 -07:00
|
|
|
// We base the primitives on the C/C++ atomics and create a mimimal wrapper for MSVC in C compilation mode.
|
|
|
|
// This is why we try to use only `uintptr_t` and `<type>*` as atomic types.
|
|
|
|
// To gain better insight in the range of used atomics, we use explicitly named memory order operations
|
|
|
|
// instead of passing the memory order as a parameter.
|
|
|
|
// -----------------------------------------------------------------------------------------------
|
2019-06-19 16:26:12 -07:00
|
|
|
|
2020-07-25 20:55:45 -07:00
|
|
|
#if defined(__cplusplus)
|
2020-07-26 18:00:38 -07:00
|
|
|
// Use C++ atomics
|
2019-08-25 22:59:12 -07:00
|
|
|
#include <atomic>
|
2020-07-26 18:00:38 -07:00
|
|
|
#define _Atomic(tp) std::atomic<tp>
|
|
|
|
#define mi_atomic(name) std::atomic_##name
|
|
|
|
#define mi_memory_order(name) std::memory_order_##name
|
2020-07-25 20:55:45 -07:00
|
|
|
#elif defined(_MSC_VER)
|
2020-07-26 18:00:38 -07:00
|
|
|
// Use MSVC C wrapper for C11 atomics
|
2020-09-03 09:45:53 -07:00
|
|
|
#define _Atomic(tp) tp
|
|
|
|
#define ATOMIC_VAR_INIT(x) x
|
2020-07-26 18:00:38 -07:00
|
|
|
#define mi_atomic(name) mi_atomic_##name
|
|
|
|
#define mi_memory_order(name) mi_memory_order_##name
|
2019-08-25 22:59:12 -07:00
|
|
|
#else
|
2020-07-26 18:00:38 -07:00
|
|
|
// Use C11 atomics
|
2019-08-25 22:59:12 -07:00
|
|
|
#include <stdatomic.h>
|
2020-07-26 18:00:38 -07:00
|
|
|
#define mi_atomic(name) atomic_##name
|
|
|
|
#define mi_memory_order(name) memory_order_##name
|
2019-08-25 22:59:12 -07:00
|
|
|
#endif
|
2019-06-19 16:26:12 -07:00
|
|
|
|
2020-07-26 18:00:38 -07:00
|
|
|
// Various defines for all used memory orders in mimalloc
|
|
|
|
#define mi_atomic_cas_weak(p,expected,desired,mem_success,mem_fail) \
|
|
|
|
mi_atomic(compare_exchange_weak_explicit)(p,expected,desired,mem_success,mem_fail)
|
|
|
|
|
|
|
|
#define mi_atomic_cas_strong(p,expected,desired,mem_success,mem_fail) \
|
|
|
|
mi_atomic(compare_exchange_strong_explicit)(p,expected,desired,mem_success,mem_fail)
|
|
|
|
|
|
|
|
#define mi_atomic_load_acquire(p) mi_atomic(load_explicit)(p,mi_memory_order(acquire))
|
|
|
|
#define mi_atomic_load_relaxed(p) mi_atomic(load_explicit)(p,mi_memory_order(relaxed))
|
|
|
|
#define mi_atomic_store_release(p,x) mi_atomic(store_explicit)(p,x,mi_memory_order(release))
|
|
|
|
#define mi_atomic_store_relaxed(p,x) mi_atomic(store_explicit)(p,x,mi_memory_order(relaxed))
|
|
|
|
#define mi_atomic_exchange_release(p,x) mi_atomic(exchange_explicit)(p,x,mi_memory_order(release))
|
|
|
|
#define mi_atomic_exchange_acq_rel(p,x) mi_atomic(exchange_explicit)(p,x,mi_memory_order(acq_rel))
|
|
|
|
#define mi_atomic_cas_weak_release(p,exp,des) mi_atomic_cas_weak(p,exp,des,mi_memory_order(release),mi_memory_order(relaxed))
|
|
|
|
#define mi_atomic_cas_weak_acq_rel(p,exp,des) mi_atomic_cas_weak(p,exp,des,mi_memory_order(acq_rel),mi_memory_order(acquire))
|
|
|
|
#define mi_atomic_cas_strong_release(p,exp,des) mi_atomic_cas_strong(p,exp,des,mi_memory_order(release),mi_memory_order(relaxed))
|
|
|
|
#define mi_atomic_cas_strong_acq_rel(p,exp,des) mi_atomic_cas_strong(p,exp,des,mi_memory_order(acq_rel),mi_memory_order(acquire))
|
|
|
|
|
|
|
|
#define mi_atomic_add_relaxed(p,x) mi_atomic(fetch_add_explicit)(p,x,mi_memory_order(relaxed))
|
|
|
|
#define mi_atomic_sub_relaxed(p,x) mi_atomic(fetch_sub_explicit)(p,x,mi_memory_order(relaxed))
|
|
|
|
#define mi_atomic_add_acq_rel(p,x) mi_atomic(fetch_add_explicit)(p,x,mi_memory_order(acq_rel))
|
|
|
|
#define mi_atomic_sub_acq_rel(p,x) mi_atomic(fetch_sub_explicit)(p,x,mi_memory_order(acq_rel))
|
|
|
|
#define mi_atomic_and_acq_rel(p,x) mi_atomic(fetch_and_explicit)(p,x,mi_memory_order(acq_rel))
|
|
|
|
#define mi_atomic_or_acq_rel(p,x) mi_atomic(fetch_or_explicit)(p,x,mi_memory_order(acq_rel))
|
|
|
|
|
|
|
|
#define mi_atomic_increment_relaxed(p) mi_atomic_add_relaxed(p,1)
|
|
|
|
#define mi_atomic_decrement_relaxed(p) mi_atomic_sub_relaxed(p,1)
|
|
|
|
#define mi_atomic_increment_acq_rel(p) mi_atomic_add_acq_rel(p,1)
|
|
|
|
#define mi_atomic_decrement_acq_rel(p) mi_atomic_sub_acq_rel(p,1)
|
2019-06-19 16:26:12 -07:00
|
|
|
|
2019-08-25 22:59:12 -07:00
|
|
|
static inline void mi_atomic_yield(void);
|
2020-07-26 18:00:38 -07:00
|
|
|
static inline intptr_t mi_atomic_addi(_Atomic(intptr_t)* p, intptr_t add);
|
|
|
|
static inline intptr_t mi_atomic_subi(_Atomic(intptr_t)* p, intptr_t sub);
|
2019-08-25 22:59:12 -07:00
|
|
|
|
2020-07-22 13:45:04 -07:00
|
|
|
|
2020-07-26 18:00:38 -07:00
|
|
|
#if defined(__cplusplus) || !defined(_MSC_VER)
|
2020-07-22 13:45:04 -07:00
|
|
|
|
2020-07-26 18:00:38 -07:00
|
|
|
// In C++/C11 atomics we have polymorpic atomics so can use the typed `ptr` variants
|
|
|
|
// (where `tp` is the type of atomic value)
|
|
|
|
// We use these macros so we can provide a typed wrapper in MSVC in C compilation mode as well
|
|
|
|
#define mi_atomic_load_ptr_acquire(tp,p) mi_atomic_load_acquire(p)
|
|
|
|
#define mi_atomic_load_ptr_relaxed(tp,p) mi_atomic_load_relaxed(p)
|
|
|
|
#define mi_atomic_store_ptr_release(tp,p,x) mi_atomic_store_release(p,x)
|
|
|
|
#define mi_atomic_store_ptr_relaxed(tp,p,x) mi_atomic_store_relaxed(p,x)
|
|
|
|
#define mi_atomic_cas_ptr_weak_release(tp,p,exp,des) mi_atomic_cas_weak_release(p,exp,des)
|
|
|
|
#define mi_atomic_cas_ptr_weak_acq_rel(tp,p,exp,des) mi_atomic_cas_weak_acq_rel(p,exp,des)
|
|
|
|
#define mi_atomic_cas_ptr_strong_release(tp,p,exp,des) mi_atomic_cas_strong_release(p,exp,des)
|
|
|
|
#define mi_atomic_exchange_ptr_release(tp,p,x) mi_atomic_exchange_release(p,x)
|
|
|
|
#define mi_atomic_exchange_ptr_acq_rel(tp,p,x) mi_atomic_exchange_acq_rel(p,x)
|
2019-08-25 22:59:12 -07:00
|
|
|
|
2020-07-26 18:00:38 -07:00
|
|
|
// These are used by the statistics
|
|
|
|
static inline int64_t mi_atomic_addi64_relaxed(volatile int64_t* p, int64_t add) {
|
|
|
|
return mi_atomic(fetch_add_explicit)((_Atomic(int64_t)*)p, add, mi_memory_order(relaxed));
|
2019-07-15 17:35:43 -07:00
|
|
|
}
|
2020-07-26 18:00:38 -07:00
|
|
|
static inline void mi_atomic_maxi64_relaxed(volatile int64_t* p, int64_t x) {
|
|
|
|
int64_t current = mi_atomic_load_relaxed((_Atomic(int64_t)*)p);
|
|
|
|
while (current < x && !mi_atomic_cas_weak_release((_Atomic(int64_t)*)p, ¤t, x)) { /* nothing */ };
|
2019-08-25 22:59:12 -07:00
|
|
|
}
|
2019-06-19 16:26:12 -07:00
|
|
|
|
2019-08-25 22:59:12 -07:00
|
|
|
|
2020-07-26 18:00:38 -07:00
|
|
|
#elif defined(_MSC_VER)
|
2019-06-19 16:26:12 -07:00
|
|
|
|
2020-07-26 18:00:38 -07:00
|
|
|
// MSVC C compilation wrapper that uses Interlocked operations to model C11 atomics.
|
2019-06-19 16:26:12 -07:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2020-08-29 19:30:38 -07:00
|
|
|
#include <Windows.h>
|
2019-06-19 16:26:12 -07:00
|
|
|
#include <intrin.h>
|
2019-08-25 22:59:12 -07:00
|
|
|
#ifdef _WIN64
|
2019-07-07 21:59:53 -07:00
|
|
|
typedef LONG64 msc_intptr_t;
|
2019-11-06 21:37:23 -08:00
|
|
|
#define MI_64(f) f##64
|
2019-06-19 16:26:12 -07:00
|
|
|
#else
|
2019-07-07 21:59:53 -07:00
|
|
|
typedef LONG msc_intptr_t;
|
2019-11-06 21:37:23 -08:00
|
|
|
#define MI_64(f) f
|
2019-06-19 16:26:12 -07:00
|
|
|
#endif
|
2020-07-26 18:00:38 -07:00
|
|
|
|
|
|
|
typedef enum mi_memory_order_e {
|
|
|
|
mi_memory_order_relaxed,
|
|
|
|
mi_memory_order_consume,
|
|
|
|
mi_memory_order_acquire,
|
|
|
|
mi_memory_order_release,
|
|
|
|
mi_memory_order_acq_rel,
|
|
|
|
mi_memory_order_seq_cst
|
|
|
|
} mi_memory_order;
|
|
|
|
|
|
|
|
static inline uintptr_t mi_atomic_fetch_add_explicit(_Atomic(uintptr_t)* p, uintptr_t add, mi_memory_order mo) {
|
2020-01-22 19:44:54 -08:00
|
|
|
return (uintptr_t)MI_64(_InterlockedExchangeAdd)((volatile msc_intptr_t*)p, (msc_intptr_t)add);
|
2019-11-06 21:37:23 -08:00
|
|
|
}
|
2020-07-26 18:00:38 -07:00
|
|
|
static inline uintptr_t mi_atomic_fetch_sub_explicit(_Atomic(uintptr_t)*p, uintptr_t sub, mi_memory_order mo) {
|
|
|
|
return (uintptr_t)MI_64(_InterlockedExchangeAdd)((volatile msc_intptr_t*)p, -((msc_intptr_t)sub));
|
|
|
|
}
|
|
|
|
static inline uintptr_t mi_atomic_fetch_and_explicit(_Atomic(uintptr_t)* p, uintptr_t x, mi_memory_order mo) {
|
2019-11-06 21:37:23 -08:00
|
|
|
return (uintptr_t)MI_64(_InterlockedAnd)((volatile msc_intptr_t*)p, (msc_intptr_t)x);
|
|
|
|
}
|
2020-07-26 18:00:38 -07:00
|
|
|
static inline uintptr_t mi_atomic_fetch_or_explicit(_Atomic(uintptr_t)* p, uintptr_t x, mi_memory_order mo) {
|
2019-11-06 21:37:23 -08:00
|
|
|
return (uintptr_t)MI_64(_InterlockedOr)((volatile msc_intptr_t*)p, (msc_intptr_t)x);
|
2019-06-19 16:26:12 -07:00
|
|
|
}
|
2020-07-26 18:00:38 -07:00
|
|
|
static inline bool mi_atomic_compare_exchange_strong_explicit(_Atomic(uintptr_t)* p, uintptr_t* expected, uintptr_t desired, mi_memory_order mo1, mi_memory_order mo2) {
|
2020-07-25 22:52:27 -07:00
|
|
|
uintptr_t read = (uintptr_t)MI_64(_InterlockedCompareExchange)((volatile msc_intptr_t*)p, (msc_intptr_t)desired, (msc_intptr_t)(*expected));
|
|
|
|
if (read == *expected) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*expected = read;
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-19 16:26:12 -07:00
|
|
|
}
|
2020-07-26 18:00:38 -07:00
|
|
|
static inline bool mi_atomic_compare_exchange_weak_explicit(_Atomic(uintptr_t)*p, uintptr_t* expected, uintptr_t desired, mi_memory_order mo1, mi_memory_order mo2) {
|
|
|
|
return mi_atomic_compare_exchange_strong_explicit(p, expected, desired, mo1, mo2);
|
2019-06-19 16:26:12 -07:00
|
|
|
}
|
2020-07-26 18:00:38 -07:00
|
|
|
static inline uintptr_t mi_atomic_exchange_explicit(_Atomic(uintptr_t)* p, uintptr_t exchange, mi_memory_order mo) {
|
2019-11-06 21:37:23 -08:00
|
|
|
return (uintptr_t)MI_64(_InterlockedExchange)((volatile msc_intptr_t*)p, (msc_intptr_t)exchange);
|
2019-06-19 16:26:12 -07:00
|
|
|
}
|
2020-07-26 18:00:38 -07:00
|
|
|
static inline mi_atomic_thread_fence(mi_memory_order mo) {
|
|
|
|
_Atomic(uintptr_t)x = 0;
|
|
|
|
mi_atomic_exchange_explicit(&x, 1, mo);
|
2019-07-02 07:23:24 -07:00
|
|
|
}
|
2020-07-26 18:00:38 -07:00
|
|
|
static inline uintptr_t mi_atomic_load_explicit(_Atomic(uintptr_t) const* p, mi_memory_order mo) {
|
|
|
|
#if defined(_M_IX86) || defined(_M_X64)
|
2020-01-22 19:44:54 -08:00
|
|
|
return *p;
|
2020-07-26 18:00:38 -07:00
|
|
|
#else
|
|
|
|
uintptr_t x = *p;
|
|
|
|
if (mo > mi_memory_order_relaxed) {
|
|
|
|
while (!mi_atomic_compare_exchange_weak_explicit(p, &x, x, mo, mi_memory_order_relaxed)) { /* nothing */ };
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
#endif
|
2019-08-26 08:11:15 -07:00
|
|
|
}
|
2020-07-26 18:00:38 -07:00
|
|
|
static inline void mi_atomic_store_explicit(_Atomic(uintptr_t)* p, uintptr_t x, mi_memory_order mo) {
|
2020-01-22 19:44:54 -08:00
|
|
|
#if defined(_M_IX86) || defined(_M_X64)
|
|
|
|
*p = x;
|
|
|
|
#else
|
2020-07-26 18:00:38 -07:00
|
|
|
mi_atomic_exchange_explicit(p,x,mo);
|
2020-01-22 19:44:54 -08:00
|
|
|
#endif
|
2019-07-02 07:23:24 -07:00
|
|
|
}
|
2020-07-26 18:00:38 -07:00
|
|
|
|
|
|
|
// These are used by the statistics
|
2020-07-25 22:52:27 -07:00
|
|
|
static inline int64_t mi_atomic_addi64_relaxed(volatile _Atomic(int64_t)* p, int64_t add) {
|
2019-08-25 22:59:12 -07:00
|
|
|
#ifdef _WIN64
|
2020-07-25 22:52:27 -07:00
|
|
|
return (int64_t)mi_atomic_addi((int64_t*)p,add);
|
2019-06-19 16:26:12 -07:00
|
|
|
#else
|
|
|
|
int64_t current;
|
|
|
|
int64_t sum;
|
|
|
|
do {
|
|
|
|
current = *p;
|
|
|
|
sum = current + add;
|
|
|
|
} while (_InterlockedCompareExchange64(p, sum, current) != current);
|
2020-07-25 22:52:27 -07:00
|
|
|
return current;
|
2019-06-19 16:26:12 -07:00
|
|
|
#endif
|
|
|
|
}
|
2020-07-25 22:52:27 -07:00
|
|
|
static inline void mi_atomic_maxi64_relaxed(volatile _Atomic(int64_t)*p, int64_t x) {
|
2020-07-22 13:45:04 -07:00
|
|
|
int64_t current;
|
|
|
|
do {
|
|
|
|
current = *p;
|
|
|
|
} while (current < x && _InterlockedCompareExchange64(p, x, current) != current);
|
|
|
|
}
|
|
|
|
|
2020-07-26 18:00:38 -07:00
|
|
|
// The pointer macros cast to `uintptr_t`.
|
|
|
|
#define mi_atomic_load_ptr_acquire(tp,p) (tp*)mi_atomic_load_acquire((_Atomic(uintptr_t)*)(p))
|
|
|
|
#define mi_atomic_load_ptr_relaxed(tp,p) (tp*)mi_atomic_load_relaxed((_Atomic(uintptr_t)*)(p))
|
|
|
|
#define mi_atomic_store_ptr_release(tp,p,x) mi_atomic_store_release((_Atomic(uintptr_t)*)(p),(uintptr_t)(x))
|
|
|
|
#define mi_atomic_store_ptr_relaxed(tp,p,x) mi_atomic_store_relaxed((_Atomic(uintptr_t)*)(p),(uintptr_t)(x))
|
|
|
|
#define mi_atomic_cas_ptr_weak_release(tp,p,exp,des) mi_atomic_cas_weak_release((_Atomic(uintptr_t)*)(p),(uintptr_t*)exp,(uintptr_t)des)
|
|
|
|
#define mi_atomic_cas_ptr_weak_acq_rel(tp,p,exp,des) mi_atomic_cas_weak_acq_rel((_Atomic(uintptr_t)*)(p),(uintptr_t*)exp,(uintptr_t)des)
|
|
|
|
#define mi_atomic_cas_ptr_strong_release(tp,p,exp,des) mi_atomic_cas_strong_release((_Atomic(uintptr_t)*)(p),(uintptr_t*)exp,(uintptr_t)des)
|
|
|
|
#define mi_atomic_exchange_ptr_release(tp,p,x) (tp*)mi_atomic_exchange_release((_Atomic(uintptr_t)*)(p),(uintptr_t)x)
|
|
|
|
#define mi_atomic_exchange_ptr_acq_rel(tp,p,x) (tp*)mi_atomic_exchange_acq_rel((_Atomic(uintptr_t)*)(p),(uintptr_t)x)
|
2020-07-22 14:16:18 -07:00
|
|
|
|
2019-06-19 16:26:12 -07:00
|
|
|
#endif
|
2020-07-26 18:00:38 -07:00
|
|
|
|
|
|
|
|
|
|
|
// Atomically add a signed value; returns the previous value.
|
|
|
|
static inline intptr_t mi_atomic_addi(_Atomic(intptr_t)*p, intptr_t add) {
|
|
|
|
return (intptr_t)mi_atomic_add_acq_rel((_Atomic(uintptr_t)*)p, (uintptr_t)add);
|
2020-07-22 14:16:18 -07:00
|
|
|
}
|
2020-07-26 18:00:38 -07:00
|
|
|
|
|
|
|
// Atomically subtract a signed value; returns the previous value.
|
|
|
|
static inline intptr_t mi_atomic_subi(_Atomic(intptr_t)*p, intptr_t sub) {
|
|
|
|
return (intptr_t)mi_atomic_addi(p, -sub);
|
2020-07-22 14:16:18 -07:00
|
|
|
}
|
2019-06-19 16:26:12 -07:00
|
|
|
|
2020-07-26 18:00:38 -07:00
|
|
|
// Yield
|
2019-06-19 16:26:12 -07:00
|
|
|
#if defined(__cplusplus)
|
2020-07-26 00:15:57 -07:00
|
|
|
#include <thread>
|
|
|
|
static inline void mi_atomic_yield(void) {
|
|
|
|
std::this_thread::yield();
|
|
|
|
}
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
static inline void mi_atomic_yield(void) {
|
|
|
|
YieldProcessor();
|
|
|
|
}
|
2019-06-23 16:53:00 +08:00
|
|
|
#elif (defined(__GNUC__) || defined(__clang__)) && \
|
|
|
|
(defined(__x86_64__) || defined(__i386__) || defined(__arm__) || defined(__aarch64__))
|
|
|
|
#if defined(__x86_64__) || defined(__i386__)
|
2019-06-25 12:16:36 +02:00
|
|
|
static inline void mi_atomic_yield(void) {
|
2020-08-09 17:55:17 -07:00
|
|
|
__asm__ volatile ("pause" ::: "memory");
|
2019-06-19 16:26:12 -07:00
|
|
|
}
|
2019-06-23 16:53:00 +08:00
|
|
|
#elif defined(__arm__) || defined(__aarch64__)
|
2019-06-25 12:16:36 +02:00
|
|
|
static inline void mi_atomic_yield(void) {
|
2020-08-09 17:55:17 -07:00
|
|
|
__asm__ volatile("yield");
|
2019-06-23 16:53:00 +08:00
|
|
|
}
|
|
|
|
#endif
|
2019-07-10 20:23:20 +02:00
|
|
|
#elif defined(__wasi__)
|
2020-07-26 00:15:57 -07:00
|
|
|
#include <sched.h>
|
|
|
|
static inline void mi_atomic_yield(void) {
|
|
|
|
sched_yield();
|
|
|
|
}
|
2019-06-19 16:26:12 -07:00
|
|
|
#else
|
2020-07-26 00:15:57 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
static inline void mi_atomic_yield(void) {
|
|
|
|
sleep(0);
|
|
|
|
}
|
2019-06-19 16:26:12 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#endif // __MIMALLOC_ATOMIC_H
|