2011-10-03 14:41:19 -04:00
|
|
|
|
2012-01-29 14:28:19 -05:00
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cCriticalSection:
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cCriticalSection::cCriticalSection()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2012-02-13 16:47:03 -05:00
|
|
|
InitializeCriticalSection( &m_CriticalSection );
|
2011-10-03 14:41:19 -04:00
|
|
|
#else
|
|
|
|
m_Attributes = new pthread_mutexattr_t;
|
|
|
|
pthread_mutexattr_init((pthread_mutexattr_t*)m_Attributes);
|
|
|
|
pthread_mutexattr_settype((pthread_mutexattr_t*)m_Attributes, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
|
|
|
|
m_CriticalSectionPtr = new pthread_mutex_t;
|
|
|
|
if( pthread_mutex_init( (pthread_mutex_t*)m_CriticalSectionPtr, (pthread_mutexattr_t*)m_Attributes ) != 0 )
|
|
|
|
{
|
|
|
|
LOG("ERROR: Could not initialize Critical Section!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cCriticalSection::~cCriticalSection()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2012-02-13 16:47:03 -05:00
|
|
|
DeleteCriticalSection( &m_CriticalSection );
|
2011-10-03 14:41:19 -04:00
|
|
|
#else
|
|
|
|
if( pthread_mutex_destroy( (pthread_mutex_t*)m_CriticalSectionPtr ) != 0 )
|
|
|
|
{
|
|
|
|
LOG("ERROR: Could not destroy Critical Section!");
|
|
|
|
}
|
|
|
|
delete (pthread_mutex_t*)m_CriticalSectionPtr;
|
|
|
|
pthread_mutexattr_destroy( (pthread_mutexattr_t*)m_Attributes );
|
|
|
|
delete (pthread_mutexattr_t*)m_Attributes;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cCriticalSection::Lock()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2012-02-13 16:47:03 -05:00
|
|
|
EnterCriticalSection( &m_CriticalSection );
|
2011-10-03 14:41:19 -04:00
|
|
|
#else
|
|
|
|
pthread_mutex_lock( (pthread_mutex_t*)m_CriticalSectionPtr );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cCriticalSection::Unlock()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2012-02-13 16:47:03 -05:00
|
|
|
LeaveCriticalSection( &m_CriticalSection );
|
2011-10-03 14:41:19 -04:00
|
|
|
#else
|
|
|
|
pthread_mutex_unlock( (pthread_mutex_t*)m_CriticalSectionPtr );
|
|
|
|
#endif
|
|
|
|
}
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cCSLock
|
|
|
|
|
2012-01-29 16:51:36 -05:00
|
|
|
cCSLock::cCSLock(cCriticalSection * a_CS)
|
|
|
|
: m_CS(a_CS)
|
|
|
|
, m_IsLocked(false)
|
2012-01-29 09:29:26 -05:00
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-30 11:01:45 -05:00
|
|
|
cCSLock::cCSLock(cCriticalSection & a_CS)
|
|
|
|
: m_CS(&a_CS)
|
|
|
|
, m_IsLocked(false)
|
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
cCSLock::~cCSLock()
|
|
|
|
{
|
2012-01-30 16:54:40 -05:00
|
|
|
if (!m_IsLocked)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-01-29 09:29:26 -05:00
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cCSLock::Lock(void)
|
|
|
|
{
|
2012-02-19 18:00:00 -05:00
|
|
|
ASSERT(!m_IsLocked);
|
2012-01-29 09:29:26 -05:00
|
|
|
m_IsLocked = true;
|
|
|
|
m_CS->Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cCSLock::Unlock(void)
|
|
|
|
{
|
2012-02-19 18:00:00 -05:00
|
|
|
ASSERT(m_IsLocked);
|
2012-01-29 09:29:26 -05:00
|
|
|
m_IsLocked = false;
|
|
|
|
m_CS->Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cCSUnlock:
|
|
|
|
|
|
|
|
cCSUnlock::cCSUnlock(cCSLock & a_Lock) :
|
|
|
|
m_Lock(a_Lock)
|
|
|
|
{
|
|
|
|
m_Lock.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cCSUnlock::~cCSUnlock()
|
|
|
|
{
|
|
|
|
m_Lock.Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|