From 30df80b05ad30d6d66b85c6d100ab9312bfef3ec Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 20 Mar 2023 11:50:05 -0700 Subject: [PATCH 1/2] proper prototype --- src/prim/osx/alloc-override-zone.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prim/osx/alloc-override-zone.c b/src/prim/osx/alloc-override-zone.c index a517ddea..80bcfa93 100644 --- a/src/prim/osx/alloc-override-zone.c +++ b/src/prim/osx/alloc-override-zone.c @@ -420,7 +420,7 @@ __attribute__((constructor(0))) #else __attribute__((constructor)) // seems not supported by g++-11 on the M1 #endif -static void _mi_macos_override_malloc() { +static void _mi_macos_override_malloc(void) { malloc_zone_t* purgeable_zone = NULL; #if defined(MAC_OS_X_VERSION_10_6) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) From 0b4c3da2e90c90dc3f379618195e2e9388c9201c Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 20 Mar 2023 13:55:39 -0700 Subject: [PATCH 2/2] make process init race free (issue #701) --- include/mimalloc/atomic.h | 9 +++++++++ src/init.c | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/mimalloc/atomic.h b/include/mimalloc/atomic.h index 971b374a..fe79fbca 100644 --- a/include/mimalloc/atomic.h +++ b/include/mimalloc/atomic.h @@ -275,6 +275,15 @@ static inline intptr_t mi_atomic_subi(_Atomic(intptr_t)*p, intptr_t sub) { return (intptr_t)mi_atomic_addi(p, -sub); } +typedef _Atomic(uintptr_t) mi_atomic_once_t; + +// Returns true only on the first invocation +static inline bool mi_atomic_once( mi_atomic_once_t* once ) { + if (mi_atomic_load_relaxed(once) != 0) return false; // quick test + uintptr_t expected = 0; + return mi_atomic_cas_strong_acq_rel(once, &expected, 1); // try to set to 1 +} + // Yield #if defined(__cplusplus) #include diff --git a/src/init.c b/src/init.c index 8c5c8049..9a11f6e0 100644 --- a/src/init.c +++ b/src/init.c @@ -511,7 +511,8 @@ static void mi_detect_cpu_features(void) { // Initialize the process; called by thread_init or the process loader void mi_process_init(void) mi_attr_noexcept { // ensure we are called once - if (_mi_process_is_initialized) return; + static mi_atomic_once_t process_init; + if (!mi_atomic_once(&process_init)) return; _mi_verbose_message("process init: 0x%zx\n", _mi_thread_id()); _mi_process_is_initialized = true; mi_process_setup_auto_thread_done();