WebSocket_CPP_98/CC_SDK_VS/CCMutex.cpp
2025-12-29 09:55:17 +08:00

67 lines
833 B
C++
Executable File

#include "CCMutex.h"
CTL::AutoLock::AutoLock(Mutex& mutex_t)
{
this->Mutex_M = &mutex_t;
if (this->Mutex_M)
{
this->Mutex_M->lock();
}
}
CTL::AutoLock::~AutoLock()
{
if (this->Mutex_M)
{
this->Mutex_M->unlock();
}
}
CTL::AutoLock& CTL::AutoLock::operator=(AutoLock&)
{
return *this;
}
CTL::Mutex::Mutex()
{
g_Mutex = ::CreateMutex(NULL, FALSE, NULL);
}
CTL::Mutex::~Mutex()
{
if (g_Mutex)
{
CloseHandle(g_Mutex);
}
g_Mutex = nullptr;
}
bool CTL::Mutex::lock()
{
TryLock_M = true;
if (g_Mutex)
{
return WaitForSingleObject(g_Mutex, INFINITE) == WAIT_OBJECT_0;
}
return false;
}
void CTL::Mutex::unlock()
{
TryLock_M = false;
if (g_Mutex)
{
ReleaseMutex(g_Mutex);
}
}
bool CTL::Mutex::try_lock()
{
if (g_Mutex)
{
return WaitForSingleObject(g_Mutex, 0) == WAIT_OBJECT_0;
}
return false;
}