2012-06-14 13:06:06 +00:00
|
|
|
|
|
|
|
#pragma once
|
2014-10-20 20:26:18 +00:00
|
|
|
#include <mutex>
|
2014-10-23 22:58:01 +00:00
|
|
|
#include <thread>
|
2012-06-14 13:06:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cCriticalSection
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Lock(void);
|
|
|
|
void Unlock(void);
|
|
|
|
|
2015-05-09 07:25:09 +00:00
|
|
|
// IsLocked / IsLockedByCurrentThread are only used in ASSERT statements, but because of the changes with ASSERT they must always be defined
|
2014-01-05 22:08:30 +00:00
|
|
|
// The fake versions (in Release) will not effect the program in any way
|
2012-09-01 22:32:11 +00:00
|
|
|
#ifdef _DEBUG
|
2015-03-22 18:46:08 +00:00
|
|
|
cCriticalSection(void);
|
|
|
|
bool IsLocked(void);
|
|
|
|
bool IsLockedByCurrentThread(void);
|
2014-01-05 22:08:30 +00:00
|
|
|
#else
|
2015-03-22 18:46:08 +00:00
|
|
|
bool IsLocked(void) { return false; }
|
|
|
|
bool IsLockedByCurrentThread(void) { return false; }
|
2012-09-01 22:32:11 +00:00
|
|
|
#endif // _DEBUG
|
|
|
|
|
2012-06-14 13:06:06 +00:00
|
|
|
private:
|
2015-03-22 18:46:08 +00:00
|
|
|
|
2012-09-01 22:32:11 +00:00
|
|
|
#ifdef _DEBUG
|
2012-12-14 22:36:12 +00:00
|
|
|
int m_IsLocked; // Number of times this CS is locked
|
2014-10-18 23:29:34 +00:00
|
|
|
std::thread::id m_OwningThreadID;
|
2012-09-01 22:32:11 +00:00
|
|
|
#endif // _DEBUG
|
|
|
|
|
2014-10-20 20:26:18 +00:00
|
|
|
std::recursive_mutex m_Mutex;
|
2012-06-14 13:06:06 +00:00
|
|
|
} ALIGN_8;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-07-31 14:49:10 +00:00
|
|
|
/** RAII for cCriticalSection - locks the CS on creation, unlocks on destruction */
|
2012-06-14 13:06:06 +00:00
|
|
|
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
|
2014-07-17 20:50:58 +00:00
|
|
|
// In Windows, it is an error to call cCriticalSection::Unlock() multiple times if the lock is not held,
|
2012-06-14 13:06:06 +00:00
|
|
|
// 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);
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-07-31 14:49:10 +00:00
|
|
|
/** Temporary RAII unlock for a cCSLock. Useful for unlock-wait-relock scenarios */
|
2012-06-14 13:06:06 +00:00
|
|
|
class cCSUnlock
|
|
|
|
{
|
|
|
|
cCSLock & m_Lock;
|
|
|
|
public:
|
|
|
|
cCSUnlock(cCSLock & a_Lock);
|
|
|
|
~cCSUnlock();
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(cCSUnlock);
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|