From 1e89045f82efe8532abc0403918e0ffcf8b269a9 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 10 Jun 2023 00:11:33 +0800 Subject: [PATCH] ... --- src/common.h | 10 ++++++++++ src/memory.h | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/common.h b/src/common.h index 59f0616b..98c17a81 100644 --- a/src/common.h +++ b/src/common.h @@ -62,8 +62,18 @@ #if PK_ENABLE_THREAD #define THREAD_LOCAL thread_local +#include + +struct GIL { + inline static std::mutex _mutex; + explicit GIL() { _mutex.lock(); } + ~GIL() { _mutex.unlock(); } +}; +#define GLOBAL_SCOPE_LOCK() auto _lock = GIL(); + #else #define THREAD_LOCAL +#define GLOBAL_SCOPE_LOCK() #endif /*******************************************************************************/ diff --git a/src/memory.h b/src/memory.h index 9a0387a7..996952ae 100644 --- a/src/memory.h +++ b/src/memory.h @@ -175,6 +175,7 @@ struct MemoryPool{ void* alloc() { return alloc(sizeof(__T)); } void* alloc(size_t size){ + GLOBAL_SCOPE_LOCK(); #if DEBUG_NO_MEMORY_POOL return malloc(size); #endif @@ -199,6 +200,7 @@ struct MemoryPool{ } void dealloc(void* p){ + GLOBAL_SCOPE_LOCK(); #if DEBUG_NO_MEMORY_POOL free(p); return; @@ -238,7 +240,6 @@ struct MemoryPool{ } }; -// TODO: make them thread-safe inline MemoryPool<64> pool64; inline MemoryPool<128> pool128;