Made MLT thread-safe
This commit is contained in:
parent
7d117ce97c
commit
5eee4acb74
|
|
@ -8,6 +8,12 @@
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||||
|
#include <windows.h>
|
||||||
|
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||||
|
#include <pthread.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
struct Block
|
struct Block
|
||||||
|
|
@ -27,7 +33,7 @@ namespace
|
||||||
const char* nextFreeFile = "Internal error";
|
const char* nextFreeFile = "Internal error";
|
||||||
unsigned int nextFreeLine = 0;
|
unsigned int nextFreeLine = 0;
|
||||||
|
|
||||||
static Block ptrList =
|
Block ptrList =
|
||||||
{
|
{
|
||||||
0,
|
0,
|
||||||
nullptr,
|
nullptr,
|
||||||
|
|
@ -37,6 +43,12 @@ namespace
|
||||||
0,
|
0,
|
||||||
magic
|
magic
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||||
|
CRITICAL_SECTION mutex;
|
||||||
|
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||||
|
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
NzMemoryManager::NzMemoryManager()
|
NzMemoryManager::NzMemoryManager()
|
||||||
|
|
@ -53,6 +65,12 @@ void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file,
|
||||||
if (!initialized)
|
if (!initialized)
|
||||||
Initialize();
|
Initialize();
|
||||||
|
|
||||||
|
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||||
|
EnterCriticalSection(&mutex);
|
||||||
|
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||||
|
pthread_mutex_lock(&mutex);
|
||||||
|
#endif
|
||||||
|
|
||||||
Block* ptr = reinterpret_cast<Block*>(std::malloc(size+sizeof(Block)));
|
Block* ptr = reinterpret_cast<Block*>(std::malloc(size+sizeof(Block)));
|
||||||
if (!ptr)
|
if (!ptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
@ -68,6 +86,12 @@ void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file,
|
||||||
ptrList.prev->next = ptr;
|
ptrList.prev->next = ptr;
|
||||||
ptrList.prev = ptr;
|
ptrList.prev = ptr;
|
||||||
|
|
||||||
|
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||||
|
LeaveCriticalSection(&mutex);
|
||||||
|
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||||
|
pthread_mutex_unlock(&mutex);
|
||||||
|
#endif
|
||||||
|
|
||||||
return reinterpret_cast<char*>(ptr)+sizeof(Block);
|
return reinterpret_cast<char*>(ptr)+sizeof(Block);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,6 +104,12 @@ void NzMemoryManager::Free(void* pointer, bool multi)
|
||||||
if (ptr->magic != magic)
|
if (ptr->magic != magic)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||||
|
EnterCriticalSection(&mutex);
|
||||||
|
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||||
|
pthread_mutex_lock(&mutex);
|
||||||
|
#endif
|
||||||
|
|
||||||
if (ptr->array != multi)
|
if (ptr->array != multi)
|
||||||
{
|
{
|
||||||
char* time = TimeInfo();
|
char* time = TimeInfo();
|
||||||
|
|
@ -114,6 +144,12 @@ void NzMemoryManager::Free(void* pointer, bool multi)
|
||||||
ptr->next->prev = ptr->prev;
|
ptr->next->prev = ptr->prev;
|
||||||
|
|
||||||
std::free(ptr);
|
std::free(ptr);
|
||||||
|
|
||||||
|
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||||
|
LeaveCriticalSection(&mutex);
|
||||||
|
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||||
|
pthread_mutex_unlock(&mutex);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void NzMemoryManager::NextFree(const char* file, unsigned int line)
|
void NzMemoryManager::NextFree(const char* file, unsigned int line)
|
||||||
|
|
@ -139,6 +175,10 @@ void NzMemoryManager::Initialize()
|
||||||
static NzMemoryManager manager;
|
static NzMemoryManager manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef NAZARA_PLATFORM_WINDOWS
|
||||||
|
InitializeCriticalSection(&mutex);
|
||||||
|
#endif
|
||||||
|
|
||||||
initialized = true;
|
initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -154,6 +194,10 @@ char* NzMemoryManager::TimeInfo()
|
||||||
|
|
||||||
void NzMemoryManager::Uninitialize()
|
void NzMemoryManager::Uninitialize()
|
||||||
{
|
{
|
||||||
|
#ifdef NAZARA_PLATFORM_WINDOWS
|
||||||
|
DeleteCriticalSection(&mutex);
|
||||||
|
#endif
|
||||||
|
|
||||||
FILE* log = std::fopen(MLTFileName, "a");
|
FILE* log = std::fopen(MLTFileName, "a");
|
||||||
|
|
||||||
char* time = TimeInfo();
|
char* time = TimeInfo();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue