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:
|
|
|
|
|
2014-10-20 16:26:18 -04:00
|
|
|
#ifdef _DEBUG
|
2012-06-14 09:06:06 -04:00
|
|
|
cCriticalSection::cCriticalSection()
|
|
|
|
{
|
2014-10-20 16:26:18 -04:00
|
|
|
m_IsLocked = 0;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-10-20 16:26:18 -04:00
|
|
|
#endif // _DEBUG
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cCriticalSection::Lock()
|
|
|
|
{
|
2014-10-20 16:26:18 -04:00
|
|
|
m_Mutex.lock();
|
2012-09-01 18:32:11 -04:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2012-12-14 17:36:12 -05:00
|
|
|
m_IsLocked += 1;
|
2014-10-18 19:29:34 -04:00
|
|
|
m_OwningThreadID = std::this_thread::get_id();
|
2012-09-01 18:32:11 -04:00
|
|
|
#endif // _DEBUG
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cCriticalSection::Unlock()
|
|
|
|
{
|
2012-09-01 18:32:11 -04:00
|
|
|
#ifdef _DEBUG
|
2012-12-14 17:36:12 -05:00
|
|
|
ASSERT(m_IsLocked > 0);
|
|
|
|
m_IsLocked -= 1;
|
2012-09-01 18:32:11 -04:00
|
|
|
#endif // _DEBUG
|
|
|
|
|
2014-10-20 16:26:18 -04:00
|
|
|
m_Mutex.unlock();
|
2012-09-01 18:32:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-02 11:40:39 -04:00
|
|
|
#ifdef _DEBUG
|
2012-09-01 18:32:11 -04:00
|
|
|
bool cCriticalSection::IsLocked(void)
|
|
|
|
{
|
2012-12-14 17:36:12 -05:00
|
|
|
return (m_IsLocked > 0);
|
2012-09-01 18:32:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cCriticalSection::IsLockedByCurrentThread(void)
|
|
|
|
{
|
2014-10-18 19:29:34 -04:00
|
|
|
return ((m_IsLocked > 0) && (m_OwningThreadID == std::this_thread::get_id()));
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-09-02 11:40:39 -04:00
|
|
|
#endif // _DEBUG
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|