1
0
cuberite-2a/src/OSSupport/CriticalSection.h

82 lines
1.6 KiB
C
Raw Normal View History

#pragma once
2014-10-20 16:26:18 -04:00
#include <mutex>
2014-10-23 18:58:01 -04:00
#include <thread>
class cCriticalSection
{
public:
void Lock(void);
void Unlock(void);
// IsLocked/IsLockedByCurrentThread are only used in ASSERT statements, but because of the changes with ASSERT they must always be defined
// The fake versions (in Release) will not effect the program in any way
#ifdef _DEBUG
cCriticalSection(void);
bool IsLocked(void);
bool IsLockedByCurrentThread(void);
#else
bool IsLocked(void) { return false; }
bool IsLockedByCurrentThread(void) { return false; }
#endif // _DEBUG
private:
#ifdef _DEBUG
int m_IsLocked; // Number of times this CS is locked
2014-10-18 19:29:34 -04:00
std::thread::id m_OwningThreadID;
#endif // _DEBUG
2014-10-20 16:26:18 -04:00
std::recursive_mutex m_Mutex;
} 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
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,
// 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);
} ;