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 
int m_RecursionCount
Number of times that this CS is currently locked (levels of recursion).
std::recursive_mutex m_Mutex
std::thread::id m_OwningThreadID
ID of the thread that is currently holding the CS.
bool IsLockedByCurrentThread(void)
Returns true if the CS is currently locked by the thread calling this function.
bool IsLocked(void)
Returns true if the CS is currently locked.
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.
cCSLock(cCriticalSection *a_CS)
DISALLOW_COPY_AND_ASSIGN(cCSLock)
void Lock(void)
cCriticalSection * m_CS
void Unlock(void)
bool m_IsLocked
Temporary RAII unlock for a cCSLock.
cCSLock & m_Lock
cCSUnlock(cCSLock &a_Lock)
DISALLOW_COPY_AND_ASSIGN(cCSUnlock)