88 lines
1.5 KiB
C++
88 lines
1.5 KiB
C++
#include "../../include/System/Mutex_W32.h"
|
|
#include "../../include/Str.h"
|
|
#include "../../include/Log.h"
|
|
|
|
namespace lwe
|
|
{
|
|
Mutex::~Mutex()
|
|
{
|
|
if (!initialized)
|
|
return;
|
|
|
|
if (!CloseHandle(hdl))
|
|
LWE_LOG_INT("Error", 0, "Failed to uninitialize mutex with error #" + Str_8::FromNum(GetLastError()) + ".");
|
|
}
|
|
|
|
Mutex::Mutex()
|
|
: hdl(nullptr)
|
|
{
|
|
}
|
|
|
|
Mutex::Mutex(const Mutex& mutex)
|
|
: BaseMutex(mutex), hdl(nullptr)
|
|
{
|
|
}
|
|
|
|
Mutex& Mutex::operator=(const Mutex& mutex)
|
|
{
|
|
if (this == &mutex)
|
|
return *this;
|
|
|
|
BaseMutex::operator=(mutex);
|
|
|
|
hdl = nullptr;
|
|
|
|
return *this;
|
|
}
|
|
|
|
void Mutex::Initialize()
|
|
{
|
|
if (initialized)
|
|
return;
|
|
|
|
hdl = CreateMutexW(nullptr, FALSE, nullptr);
|
|
if (!hdl)
|
|
{
|
|
LWE_LOG_INT("Error", 0, "Failed to create mutex with error #" + Str_8::FromNum(GetLastError()) + ".");
|
|
return;
|
|
}
|
|
|
|
initialized = true;
|
|
}
|
|
|
|
void Mutex::UnInitialize()
|
|
{
|
|
if (!initialized)
|
|
return;
|
|
|
|
if (!CloseHandle(hdl))
|
|
LWE_LOG_INT("Error", 0, "Failed to uninitialize mutex with error #" + Str_8::FromNum(GetLastError()) + ".");
|
|
|
|
initialized = false;
|
|
}
|
|
|
|
void Mutex::Lock()
|
|
{
|
|
if (locked)
|
|
return;
|
|
|
|
if (WaitForSingleObject(hdl, LWE_INFINITE) == WAIT_FAILED)
|
|
{
|
|
LWE_LOG_INT("Error", 0, "Failed to lock mutex with error #" + Str_8::FromNum(GetLastError()) + ".");
|
|
return;
|
|
}
|
|
|
|
locked = true;
|
|
}
|
|
|
|
void Mutex::Unlock()
|
|
{
|
|
if (!locked)
|
|
return;
|
|
|
|
if (!ReleaseMutex(hdl))
|
|
LWE_LOG_INT("Error", 0, "Failed to unlock mutex with error #" + Str_8::FromNum(GetLastError()) + ".");
|
|
|
|
locked = false;
|
|
}
|
|
} |