This commit is contained in:
blueloveTH 2023-06-10 00:11:33 +08:00
parent 049d923958
commit 1e89045f82
2 changed files with 12 additions and 1 deletions

View File

@ -62,8 +62,18 @@
#if PK_ENABLE_THREAD #if PK_ENABLE_THREAD
#define THREAD_LOCAL thread_local #define THREAD_LOCAL thread_local
#include <mutex>
struct GIL {
inline static std::mutex _mutex;
explicit GIL() { _mutex.lock(); }
~GIL() { _mutex.unlock(); }
};
#define GLOBAL_SCOPE_LOCK() auto _lock = GIL();
#else #else
#define THREAD_LOCAL #define THREAD_LOCAL
#define GLOBAL_SCOPE_LOCK()
#endif #endif
/*******************************************************************************/ /*******************************************************************************/

View File

@ -175,6 +175,7 @@ struct MemoryPool{
void* alloc() { return alloc(sizeof(__T)); } void* alloc() { return alloc(sizeof(__T)); }
void* alloc(size_t size){ void* alloc(size_t size){
GLOBAL_SCOPE_LOCK();
#if DEBUG_NO_MEMORY_POOL #if DEBUG_NO_MEMORY_POOL
return malloc(size); return malloc(size);
#endif #endif
@ -199,6 +200,7 @@ struct MemoryPool{
} }
void dealloc(void* p){ void dealloc(void* p){
GLOBAL_SCOPE_LOCK();
#if DEBUG_NO_MEMORY_POOL #if DEBUG_NO_MEMORY_POOL
free(p); free(p);
return; return;
@ -238,7 +240,6 @@ struct MemoryPool{
} }
}; };
// TODO: make them thread-safe
inline MemoryPool<64> pool64; inline MemoryPool<64> pool64;
inline MemoryPool<128> pool128; inline MemoryPool<128> pool128;