2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cCriticalSection
|
|
|
|
{
|
2017-01-17 16:38:04 -05:00
|
|
|
friend class cDeadlockDetect; // Allow the DeadlockDetect to read the internals, so that it may output some statistics
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
public:
|
|
|
|
void Lock(void);
|
|
|
|
void Unlock(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2017-01-17 16:38:04 -05:00
|
|
|
cCriticalSection(void);
|
|
|
|
|
|
|
|
/** Returns true if the CS is currently locked.
|
|
|
|
Note that since it relies on the m_RecursionCount value, it is inherently thread-unsafe, prone to false positives.
|
|
|
|
Also, due to multithreading, the state can change between this when function is evaluated and the returned value is used.
|
|
|
|
To be used in ASSERT(IsLocked()) only. */
|
|
|
|
bool IsLocked(void);
|
|
|
|
|
|
|
|
/** Returns true if the CS is currently locked by the thread calling this function.
|
|
|
|
Note that since it relies on the m_RecursionCount value, it is inherently thread-unsafe, prone to false positives.
|
|
|
|
Also, due to multithreading, the state can change between this when function is evaluated and the returned value is used.
|
|
|
|
To be used in ASSERT(IsLockedByCurrentThread()) only. */
|
|
|
|
bool IsLockedByCurrentThread(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
private:
|
2015-03-22 14:46:08 -04:00
|
|
|
|
2017-01-17 16:38:04 -05:00
|
|
|
/** Number of times that this CS is currently locked (levels of recursion). Zero if not locked.
|
|
|
|
Note that this value should be considered true only when the CS is locked; without the lock, it is UndefinedBehavior to even read it,
|
|
|
|
but making it std::atomic would impose too much of a runtime penalty.
|
|
|
|
It is only ever read without the lock in the DeadlockDetect, where the server is terminating anyway. */
|
|
|
|
int m_RecursionCount;
|
|
|
|
|
|
|
|
/** ID of the thread that is currently holding the CS.
|
|
|
|
Note that this value should be considered true only when the CS is locked; without the lock, it is UndefinedBehavior to even read it,
|
|
|
|
but making it std::atomic would impose too much of a runtime penalty.
|
|
|
|
When unlocked, the value stored here has no meaning, it may be an ID of a previous holder, or it could be any garbage.
|
|
|
|
It is only ever read without the lock in the DeadlockDetect, where the server is terminating anyway. */
|
2014-10-18 19:29:34 -04:00
|
|
|
std::thread::id m_OwningThreadID;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-10-20 16:26:18 -04:00
|
|
|
std::recursive_mutex m_Mutex;
|
2017-07-20 08:23:24 -04:00
|
|
|
};
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** RAII for cCriticalSection - locks the CS on creation, unlocks on destruction */
|
2012-06-14 09:06:06 -04: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 16:50:58 -04:00
|
|
|
// In Windows, it is an error to call cCriticalSection::Unlock() multiple times if the lock is not held,
|
2012-06-14 09:06:06 -04:00
|
|
|
// therefore we need to check this value whether we are locked or not.
|
|
|
|
bool m_IsLocked;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
public:
|
|
|
|
cCSLock(cCriticalSection * a_CS);
|
|
|
|
cCSLock(cCriticalSection & a_CS);
|
|
|
|
~cCSLock();
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Temporarily unlock or re-lock:
|
|
|
|
void Lock(void);
|
|
|
|
void Unlock(void);
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(cCSLock);
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Temporary RAII unlock for a cCSLock. Useful for unlock-wait-relock scenarios */
|
2012-06-14 09:06:06 -04:00
|
|
|
class cCSUnlock
|
|
|
|
{
|
|
|
|
cCSLock & m_Lock;
|
|
|
|
public:
|
|
|
|
cCSUnlock(cCSLock & a_Lock);
|
|
|
|
~cCSUnlock();
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(cCSUnlock);
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|