2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
2012-09-23 17:23:33 -04:00
|
|
|
#include "IsThread.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cCriticalSection:
|
|
|
|
|
|
|
|
cCriticalSection::cCriticalSection()
|
|
|
|
{
|
2012-10-26 11:03:44 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
InitializeCriticalSection(&m_CriticalSection);
|
|
|
|
#else
|
|
|
|
pthread_mutexattr_init(&m_Attributes);
|
|
|
|
pthread_mutexattr_settype(&m_Attributes, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
|
|
|
|
if (pthread_mutex_init(&m_CriticalSection, &m_Attributes) != 0)
|
|
|
|
{
|
|
|
|
LOGERROR("Could not initialize Critical Section!");
|
|
|
|
}
|
|
|
|
#endif
|
2012-12-14 17:36:12 -05:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
m_IsLocked = 0;
|
|
|
|
#endif // _DEBUG
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cCriticalSection::~cCriticalSection()
|
|
|
|
{
|
2012-10-26 11:03:44 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
DeleteCriticalSection(&m_CriticalSection);
|
|
|
|
#else
|
|
|
|
if (pthread_mutex_destroy(&m_CriticalSection) != 0)
|
|
|
|
{
|
|
|
|
LOGWARNING("Could not destroy Critical Section!");
|
|
|
|
}
|
|
|
|
pthread_mutexattr_destroy(&m_Attributes);
|
|
|
|
#endif
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cCriticalSection::Lock()
|
|
|
|
{
|
2012-09-01 18:32:11 -04:00
|
|
|
#ifdef _WIN32
|
2012-10-26 11:03:44 -04:00
|
|
|
EnterCriticalSection(&m_CriticalSection);
|
2012-09-01 18:32:11 -04:00
|
|
|
#else
|
2012-10-26 11:03:44 -04:00
|
|
|
pthread_mutex_lock(&m_CriticalSection);
|
2012-09-01 18:32:11 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2012-12-14 17:36:12 -05:00
|
|
|
m_IsLocked += 1;
|
2012-09-01 18:32:11 -04:00
|
|
|
m_OwningThreadID = cIsThread::GetCurrentID();
|
|
|
|
#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
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2012-10-26 11:03:44 -04:00
|
|
|
LeaveCriticalSection(&m_CriticalSection);
|
2012-09-01 18:32:11 -04:00
|
|
|
#else
|
2012-10-26 11:03:44 -04:00
|
|
|
pthread_mutex_unlock(&m_CriticalSection);
|
2012-09-01 18:32:11 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2012-12-14 17:36:12 -05:00
|
|
|
return ((m_IsLocked > 0) && (m_OwningThreadID == cIsThread::GetCurrentID()));
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cCSLock
|
|
|
|
|
|
|
|
cCSLock::cCSLock(cCriticalSection * a_CS)
|
|
|
|
: m_CS(a_CS)
|
|
|
|
, m_IsLocked(false)
|
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cCSLock::cCSLock(cCriticalSection & a_CS)
|
|
|
|
: 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cCSUnlock:
|
|
|
|
|
|
|
|
cCSUnlock::cCSUnlock(cCSLock & a_Lock) :
|
|
|
|
m_Lock(a_Lock)
|
|
|
|
{
|
|
|
|
m_Lock.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cCSUnlock::~cCSUnlock()
|
|
|
|
{
|
|
|
|
m_Lock.Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|