2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
2015-01-12 09:45:30 -05:00
|
|
|
#include "CriticalSection.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-06-14 09:06:06 -04:00
|
|
|
// cCriticalSection:
|
|
|
|
|
2017-01-17 16:38:04 -05:00
|
|
|
cCriticalSection::cCriticalSection():
|
|
|
|
m_RecursionCount(0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cCriticalSection::Lock()
|
|
|
|
{
|
2014-10-20 16:26:18 -04:00
|
|
|
m_Mutex.lock();
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2017-01-17 16:38:04 -05:00
|
|
|
m_RecursionCount += 1;
|
|
|
|
m_OwningThreadID = std::this_thread::get_id();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cCriticalSection::Unlock()
|
|
|
|
{
|
2017-01-17 16:38:04 -05:00
|
|
|
ASSERT(IsLockedByCurrentThread());
|
|
|
|
m_RecursionCount -= 1;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-10-20 16:26:18 -04:00
|
|
|
m_Mutex.unlock();
|
2012-09-01 18:32:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cCriticalSection::IsLocked(void)
|
|
|
|
{
|
2017-01-17 16:38:04 -05:00
|
|
|
return (m_RecursionCount > 0);
|
2012-09-01 18:32:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cCriticalSection::IsLockedByCurrentThread(void)
|
|
|
|
{
|
2017-01-17 16:38:04 -05:00
|
|
|
return ((m_RecursionCount > 0) && (m_OwningThreadID == std::this_thread::get_id()));
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-06-14 09:06:06 -04:00
|
|
|
// cCSLock
|
|
|
|
|
2014-07-17 16:50:58 -04:00
|
|
|
cCSLock::cCSLock(cCriticalSection * a_CS)
|
2012-06-14 09:06:06 -04:00
|
|
|
: m_CS(a_CS)
|
|
|
|
, m_IsLocked(false)
|
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:50:58 -04:00
|
|
|
cCSLock::cCSLock(cCriticalSection & a_CS)
|
2012-06-14 09:06:06 -04:00
|
|
|
: m_CS(&a_CS)
|
|
|
|
, m_IsLocked(false)
|
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cCSLock::~cCSLock()
|
|
|
|
{
|
|
|
|
if (!m_IsLocked)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cCSLock::Lock(void)
|
|
|
|
{
|
|
|
|
ASSERT(!m_IsLocked);
|
|
|
|
m_IsLocked = true;
|
|
|
|
m_CS->Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cCSLock::Unlock(void)
|
|
|
|
{
|
|
|
|
ASSERT(m_IsLocked);
|
|
|
|
m_IsLocked = false;
|
|
|
|
m_CS->Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-06-14 09:06:06 -04:00
|
|
|
// cCSUnlock:
|
|
|
|
|
|
|
|
cCSUnlock::cCSUnlock(cCSLock & a_Lock) :
|
|
|
|
m_Lock(a_Lock)
|
|
|
|
{
|
|
|
|
m_Lock.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cCSUnlock::~cCSUnlock()
|
|
|
|
{
|
|
|
|
m_Lock.Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|