2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cCriticalSection
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cCriticalSection(void);
|
|
|
|
~cCriticalSection();
|
|
|
|
|
|
|
|
void Lock(void);
|
|
|
|
void Unlock(void);
|
|
|
|
|
2012-09-01 18:32:11 -04:00
|
|
|
#ifdef _DEBUG
|
|
|
|
bool IsLocked(void);
|
|
|
|
bool IsLockedByCurrentThread(void);
|
|
|
|
#endif // _DEBUG
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
private:
|
2012-09-01 18:32:11 -04:00
|
|
|
#ifdef _DEBUG
|
2012-12-14 17:36:12 -05:00
|
|
|
int m_IsLocked; // Number of times this CS is locked
|
2012-09-01 18:32:11 -04:00
|
|
|
unsigned long m_OwningThreadID;
|
|
|
|
#endif // _DEBUG
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
CRITICAL_SECTION m_CriticalSection;
|
|
|
|
#else // _WIN32
|
2012-10-26 11:03:44 -04:00
|
|
|
pthread_mutex_t m_CriticalSection;
|
|
|
|
pthread_mutexattr_t m_Attributes;
|
2012-06-14 09:06:06 -04:00
|
|
|
#endif // else _WIN32
|
|
|
|
} ALIGN_8;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// RAII for cCriticalSection - locks the CS on creation, unlocks on destruction
|
|
|
|
class cCSLock
|
|
|
|
{
|
|
|
|
cCriticalSection * m_CS;
|
|
|
|
|
|
|
|
// Unlike a cCriticalSection, this object should be used from a single thread, therefore access to m_IsLocked is not threadsafe
|
|
|
|
// In Windows, it is an error to call cCriticalSection::Unlock() multiple times if the lock is not held,
|
|
|
|
// therefore we need to check this value whether we are locked or not.
|
|
|
|
bool m_IsLocked;
|
|
|
|
|
|
|
|
public:
|
|
|
|
cCSLock(cCriticalSection * a_CS);
|
|
|
|
cCSLock(cCriticalSection & a_CS);
|
|
|
|
~cCSLock();
|
|
|
|
|
|
|
|
// Temporarily unlock or re-lock:
|
|
|
|
void Lock(void);
|
|
|
|
void Unlock(void);
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(cCSLock);
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Temporary RAII unlock for a cCSLock. Useful for unlock-wait-relock scenarios
|
|
|
|
class cCSUnlock
|
|
|
|
{
|
|
|
|
cCSLock & m_Lock;
|
|
|
|
public:
|
|
|
|
cCSUnlock(cCSLock & a_Lock);
|
|
|
|
~cCSUnlock();
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(cCSUnlock);
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|