From 23af977d68ee85a3a1b88d1e59c26bf33f5189df Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Wed, 3 Sep 2014 17:49:10 +0100 Subject: [PATCH] Multi-threading bugfix, DLL could be used from other threads before the main thread initializes it. Thanks to user xor88 for reporting this one. Signed-off-by: Christoph M. Wintersteiger --- src/util/memory_manager.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/util/memory_manager.cpp b/src/util/memory_manager.cpp index 3f2e224d9..45088d5d4 100644 --- a/src/util/memory_manager.cpp +++ b/src/util/memory_manager.cpp @@ -35,6 +35,7 @@ static long long g_memory_max_used_size = 0; static long long g_memory_watermark = 0; static bool g_exit_when_out_of_memory = false; static char const * g_out_of_memory_msg = "ERROR: out of memory"; +static volatile bool g_memory_fully_initialized = false; void memory::exit_when_out_of_memory(bool flag, char const * msg) { g_exit_when_out_of_memory = flag; @@ -83,10 +84,18 @@ void memory::initialize(size_t max_size) { initialize = true; } } - if (!initialize) - return; - g_memory_out_of_memory = false; - mem_initialize(); + if (initialize) { + g_memory_out_of_memory = false; + mem_initialize(); + g_memory_fully_initialized = true; + } + else { + // Delay the current thread until the DLL is fully initialized + // Without this, multiple threads can start to call API functions + // before memory::initialize(...) finishes. + while (!g_memory_fully_initialized) + /* wait */ ; + } } bool memory::is_out_of_memory() { @@ -98,9 +107,9 @@ bool memory::is_out_of_memory() { return r; } -void memory::set_high_watermark(size_t watermak) { +void memory::set_high_watermark(size_t watermark) { // This method is only safe to invoke at initialization time, that is, before the threads are created. - g_memory_watermark = watermak; + g_memory_watermark = watermark; } bool memory::above_high_watermark() {