From 8cdfeafc6acd5860f071d41c772c801b9eaf46be Mon Sep 17 00:00:00 2001 From: JulianATA Date: Wed, 26 Jun 2019 10:51:48 +0800 Subject: [PATCH] Add mutex lock in _mi_stat_counter_increase as the thread safe code --- src/stats.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/stats.c b/src/stats.c index 1f659578..f45ff942 100644 --- a/src/stats.c +++ b/src/stats.c @@ -9,7 +9,8 @@ terms of the MIT license. A copy of the license can be found in the file #include "mimalloc-atomic.h" #include // memset - +#include // mutex + /* ----------------------------------------------------------- Merge thread statistics with the main one. @@ -59,8 +60,13 @@ static void mi_stat_update(mi_stat_count_t* stat, int64_t amount) { void _mi_stat_counter_increase(mi_stat_counter_t* stat, size_t amount) { // TODO: add thread safe code + static pthread_mutex_t stat_counter_mutex = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_lock(&stat_counter_mutex); + stat->count++; stat->total += amount; + + pthread_mutex_unlock(&stat_counter_mutex); }