Cuberite
A lightweight, fast and extensible game server for Minecraft
CriticalSection.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 
5 
6 
7 
9 {
10  friend class cDeadlockDetect; // Allow the DeadlockDetect to read the internals, so that it may output some statistics
11 
12 public:
13  void Lock(void);
14  void Unlock(void);
15 
16  cCriticalSection(void);
17 
22  bool IsLocked(void);
23 
28  bool IsLockedByCurrentThread(void);
29 
30 private:
31 
37 
43  std::thread::id m_OwningThreadID;
44 
45  std::recursive_mutex m_Mutex;
46 };
47 
48 
49 
50 
52 class cCSLock
53 {
55 
56  // Unlike a cCriticalSection, this object should be used from a single thread, therefore access to m_IsLocked is not threadsafe
57  // In Windows, it is an error to call cCriticalSection::Unlock() multiple times if the lock is not held,
58  // therefore we need to check this value whether we are locked or not.
59  bool m_IsLocked;
60 
61 public:
62  cCSLock(cCriticalSection * a_CS);
63  cCSLock(cCriticalSection & a_CS);
64  ~cCSLock();
65 
66  // Temporarily unlock or re-lock:
67  void Lock(void);
68  void Unlock(void);
69 
70 private:
72 } ;
73 
74 
75 
76 
77 
79 class cCSUnlock
80 {
82 public:
83  cCSUnlock(cCSLock & a_Lock);
84  ~cCSUnlock();
85 
86 private:
88 } ;
89 
90 
91 
92 
cCriticalSection::cCriticalSection
cCriticalSection(void)
Definition: CriticalSection.cpp:12
cCSLock::cCSLock
cCSLock(cCriticalSection *a_CS)
Definition: CriticalSection.cpp:66
cCSUnlock
Temporary RAII unlock for a cCSLock.
Definition: CriticalSection.h:79
cCriticalSection::m_Mutex
std::recursive_mutex m_Mutex
Definition: CriticalSection.h:45
cCSLock::~cCSLock
~cCSLock()
Definition: CriticalSection.cpp:88
cCSUnlock::m_Lock
cCSLock & m_Lock
Definition: CriticalSection.h:81
cCriticalSection::IsLocked
bool IsLocked(void)
Returns true if the CS is currently locked.
Definition: CriticalSection.cpp:45
cCSUnlock::~cCSUnlock
~cCSUnlock()
Definition: CriticalSection.cpp:136
cCSLock::m_CS
cCriticalSection * m_CS
Definition: CriticalSection.h:54
cDeadlockDetect
Definition: DeadlockDetect.h:23
cCSLock::DISALLOW_COPY_AND_ASSIGN
DISALLOW_COPY_AND_ASSIGN(cCSLock)
cCriticalSection::Unlock
void Unlock(void)
Definition: CriticalSection.cpp:33
cCSLock::Lock
void Lock(void)
Definition: CriticalSection.cpp:101
cCSUnlock::DISALLOW_COPY_AND_ASSIGN
DISALLOW_COPY_AND_ASSIGN(cCSUnlock)
cCSLock
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.
Definition: CriticalSection.h:52
cCriticalSection::IsLockedByCurrentThread
bool IsLockedByCurrentThread(void)
Returns true if the CS is currently locked by the thread calling this function.
Definition: CriticalSection.cpp:54
cCriticalSection::Lock
void Lock(void)
Definition: CriticalSection.cpp:21
cCSUnlock::cCSUnlock
cCSUnlock(cCSLock &a_Lock)
Definition: CriticalSection.cpp:126
cCriticalSection
Definition: CriticalSection.h:8
cCSLock::Unlock
void Unlock(void)
Definition: CriticalSection.cpp:112
cCriticalSection::m_OwningThreadID
std::thread::id m_OwningThreadID
ID of the thread that is currently holding the CS.
Definition: CriticalSection.h:43
cCriticalSection::m_RecursionCount
int m_RecursionCount
Number of times that this CS is currently locked (levels of recursion).
Definition: CriticalSection.h:36
cCSLock::m_IsLocked
bool m_IsLocked
Definition: CriticalSection.h:59