From 2e1b4f512d4abe0817222fbc839187448860fbf6 Mon Sep 17 00:00:00 2001 From: daan Date: Wed, 22 Jul 2020 13:45:04 -0700 Subject: [PATCH] make max update in the stats atomic --- include/mimalloc-atomic.h | 26 ++++++++++++++++++++++---- src/stats.c | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/include/mimalloc-atomic.h b/include/mimalloc-atomic.h index 8577dbc5..a0452f5c 100644 --- a/include/mimalloc-atomic.h +++ b/include/mimalloc-atomic.h @@ -27,10 +27,6 @@ terms of the MIT license. A copy of the license can be found in the file // Atomic operations specialized for mimalloc // ------------------------------------------------------ -// Atomically add a 64-bit value; returns the previous value. -// Note: not using _Atomic(int64_t) as it is only used for statistics. -static inline void mi_atomic_addi64(volatile int64_t* p, int64_t add); - // Atomically add a value; returns the previous value. Memory ordering is relaxed. static inline uintptr_t mi_atomic_add(volatile _Atomic(uintptr_t)* p, uintptr_t add); @@ -65,6 +61,14 @@ static inline void mi_atomic_write(volatile _Atomic(uintptr_t)* p, uintptr_t x); // Yield static inline void mi_atomic_yield(void); +// Atomically add a 64-bit value; returns the previous value. +// Note: not using _Atomic(int64_t) as it is only used for statistics. +static inline void mi_atomic_addi64(volatile int64_t* p, int64_t add); + +// Atomically take the maximum a 64-bit value; returns the previous value. +// Note: not using _Atomic(int64_t) as it is only used for statistics. +static inline void mi_atomic_maxi64(volatile int64_t* p, int64_t x); + // Atomically subtract a value; returns the previous value. static inline uintptr_t mi_atomic_sub(volatile _Atomic(uintptr_t)* p, uintptr_t sub) { @@ -177,6 +181,13 @@ static inline void mi_atomic_addi64(volatile _Atomic(int64_t)* p, int64_t add) { #endif } +static inline void mi_atomic_maxi64(volatile _Atomic(int64_t)*p, int64_t x) { + int64_t current; + do { + current = *p; + } while (current < x && _InterlockedCompareExchange64(p, x, current) != current); +} + #else #ifdef __cplusplus #define MI_USING_STD using namespace std; @@ -187,6 +198,13 @@ static inline void mi_atomic_addi64(volatile int64_t* p, int64_t add) { MI_USING_STD atomic_fetch_add_explicit((volatile _Atomic(int64_t)*)p, add, memory_order_relaxed); } +static inline void mi_atomic_maxi64(volatile int64_t* p, int64_t x) { + MI_USING_STD + int64_t current; + do { + current = atomic_load_explicit((volatile _Atomic(int64_t)*)p, memory_order_relaxed); + } while (current < x && !atomic_compare_exchange_weak_explicit((volatile _Atomic(int64_t)*)p, ¤t, x, memory_order_acq_rel, memory_order_relaxed)); +} static inline uintptr_t mi_atomic_add(volatile _Atomic(uintptr_t)* p, uintptr_t add) { MI_USING_STD return atomic_fetch_add_explicit(p, add, memory_order_relaxed); diff --git a/src/stats.c b/src/stats.c index e0c7f01a..06858d1c 100644 --- a/src/stats.c +++ b/src/stats.c @@ -27,7 +27,7 @@ static void mi_stat_update(mi_stat_count_t* stat, int64_t amount) { { // add atomically (for abandoned pages) mi_atomic_addi64(&stat->current,amount); - if (stat->current > stat->peak) stat->peak = stat->current; // racing.. it's ok + mi_atomic_maxi64(&stat->peak, stat->current); if (amount > 0) { mi_atomic_addi64(&stat->allocated,amount); }