Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

sync.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_SYNC_H
7 #define BITCOIN_SYNC_H
8 
9 #include <threadsafety.h>
10 
11 #include <condition_variable>
12 #include <thread>
13 #include <mutex>
14 
15 
17 // //
18 // THE SIMPLE DEFINITION, EXCLUDING DEBUG CODE //
19 // //
21 
22 /*
23 CCriticalSection mutex;
24  std::recursive_mutex mutex;
25 
26 LOCK(mutex);
27  std::unique_lock<std::recursive_mutex> criticalblock(mutex);
28 
29 LOCK2(mutex1, mutex2);
30  std::unique_lock<std::recursive_mutex> criticalblock1(mutex1);
31  std::unique_lock<std::recursive_mutex> criticalblock2(mutex2);
32 
33 TRY_LOCK(mutex, name);
34  std::unique_lock<std::recursive_mutex> name(mutex, std::try_to_lock_t);
35 
36 ENTER_CRITICAL_SECTION(mutex); // no RAII
37  mutex.lock();
38 
39 LEAVE_CRITICAL_SECTION(mutex); // no RAII
40  mutex.unlock();
41  */
42 
44 // //
45 // THE ACTUAL IMPLEMENTATION //
46 // //
48 
53 template <typename PARENT>
54 class LOCKABLE AnnotatedMixin : public PARENT
55 {
56 public:
58  {
59  PARENT::lock();
60  }
61 
63  {
64  PARENT::unlock();
65  }
66 
68  {
69  return PARENT::try_lock();
70  }
71 };
72 
73 #ifdef DEBUG_LOCKORDER
74 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
75 void LeaveCritical();
76 std::string LocksHeld();
77 void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
78 void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
79 void DeleteLock(void* cs);
80 #else
81 void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
82 void static inline LeaveCritical() {}
83 void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
84 void static inline AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
85 void static inline DeleteLock(void* cs) {}
86 #endif
87 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
88 #define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
89 
94 class CCriticalSection : public AnnotatedMixin<std::recursive_mutex>
95 {
96 public:
98  DeleteLock((void*)this);
99  }
100 };
101 
104 
106 typedef std::condition_variable CConditionVariable;
107 
109 typedef std::unique_lock<std::mutex> WaitableLock;
110 
111 #ifdef DEBUG_LOCKCONTENTION
112 void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
113 #endif
114 
117 {
118 private:
119  std::unique_lock<CCriticalSection> lock;
120 
121  void Enter(const char* pszName, const char* pszFile, int nLine)
122  {
123  EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
124 #ifdef DEBUG_LOCKCONTENTION
125  if (!lock.try_lock()) {
126  PrintLockContention(pszName, pszFile, nLine);
127 #endif
128  lock.lock();
129 #ifdef DEBUG_LOCKCONTENTION
130  }
131 #endif
132  }
133 
134  bool TryEnter(const char* pszName, const char* pszFile, int nLine)
135  {
136  EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
137  lock.try_lock();
138  if (!lock.owns_lock())
139  LeaveCritical();
140  return lock.owns_lock();
141  }
142 
143 public:
144  CCriticalBlock(CCriticalSection& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, std::defer_lock)
145  {
146  if (fTry)
147  TryEnter(pszName, pszFile, nLine);
148  else
149  Enter(pszName, pszFile, nLine);
150  }
151 
152  CCriticalBlock(CCriticalSection* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
153  {
154  if (!pmutexIn) return;
155 
156  lock = std::unique_lock<CCriticalSection>(*pmutexIn, std::defer_lock);
157  if (fTry)
158  TryEnter(pszName, pszFile, nLine);
159  else
160  Enter(pszName, pszFile, nLine);
161  }
162 
164  {
165  if (lock.owns_lock())
166  LeaveCritical();
167  }
168 
169  operator bool()
170  {
171  return lock.owns_lock();
172  }
173 };
174 
175 #define PASTE(x, y) x ## y
176 #define PASTE2(x, y) PASTE(x, y)
177 
178 #define LOCK(cs) CCriticalBlock PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
179 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
180 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
181 
182 #define ENTER_CRITICAL_SECTION(cs) \
183  { \
184  EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
185  (cs).lock(); \
186  }
187 
188 #define LEAVE_CRITICAL_SECTION(cs) \
189  { \
190  (cs).unlock(); \
191  LeaveCritical(); \
192  }
193 
195 {
196 private:
197  std::condition_variable condition;
198  std::mutex mutex;
199  int value;
200 
201 public:
202  explicit CSemaphore(int init) : value(init) {}
203 
204  void wait()
205  {
206  std::unique_lock<std::mutex> lock(mutex);
207  condition.wait(lock, [&]() { return value >= 1; });
208  value--;
209  }
210 
211  bool try_wait()
212  {
213  std::lock_guard<std::mutex> lock(mutex);
214  if (value < 1)
215  return false;
216  value--;
217  return true;
218  }
219 
220  void post()
221  {
222  {
223  std::lock_guard<std::mutex> lock(mutex);
224  value++;
225  }
226  condition.notify_one();
227  }
228 };
229 
232 {
233 private:
236 
237 public:
238  void Acquire()
239  {
240  if (fHaveGrant)
241  return;
242  sem->wait();
243  fHaveGrant = true;
244  }
245 
246  void Release()
247  {
248  if (!fHaveGrant)
249  return;
250  sem->post();
251  fHaveGrant = false;
252  }
253 
254  bool TryAcquire()
255  {
256  if (!fHaveGrant && sem->try_wait())
257  fHaveGrant = true;
258  return fHaveGrant;
259  }
260 
261  void MoveTo(CSemaphoreGrant& grant)
262  {
263  grant.Release();
264  grant.sem = sem;
265  grant.fHaveGrant = fHaveGrant;
266  fHaveGrant = false;
267  }
268 
270 
271  explicit CSemaphoreGrant(CSemaphore& sema, bool fTry = false) : sem(&sema), fHaveGrant(false)
272  {
273  if (fTry)
274  TryAcquire();
275  else
276  Acquire();
277  }
278 
280  {
281  Release();
282  }
283 
284  operator bool() const
285  {
286  return fHaveGrant;
287  }
288 };
289 
290 #endif // BITCOIN_SYNC_H
static void AssertLockNotHeldInternal(const char *pszName, const char *pszFile, int nLine, void *cs)
Definition: sync.h:84
void MoveTo(CSemaphoreGrant &grant)
Definition: sync.h:261
void unlock() UNLOCK_FUNCTION()
Definition: sync.h:62
std::condition_variable CConditionVariable
Just a typedef for std::condition_variable, can be wrapped later if desired.
Definition: sync.h:106
#define EXCLUSIVE_LOCK_FUNCTION(...)
Definition: threadsafety.h:43
void Enter(const char *pszName, const char *pszFile, int nLine)
Definition: sync.h:121
CCriticalBlock(CCriticalSection *pmutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
Definition: sync.h:152
RAII-style semaphore lock.
Definition: sync.h:231
bool try_wait()
Definition: sync.h:211
Definition: box.hpp:161
false true true true
Definition: bls_dkg.cpp:176
void lock() EXCLUSIVE_LOCK_FUNCTION()
Definition: sync.h:57
void Acquire()
Definition: sync.h:238
#define UNLOCK_FUNCTION(...)
Definition: threadsafety.h:47
CSemaphoreGrant(CSemaphore &sema, bool fTry=false)
Definition: sync.h:271
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
Definition: sync.h:134
~CSemaphoreGrant()
Definition: sync.h:279
false
Definition: bls_dkg.cpp:168
AnnotatedMixin< std::mutex > CWaitableCriticalSection
Wrapped mutex: supports waiting but not recursive locking.
Definition: sync.h:103
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
Definition: sync.h:67
static void DeleteLock(void *cs)
Definition: sync.h:85
static void AssertLockHeldInternal(const char *pszName, const char *pszFile, int nLine, void *cs)
Definition: sync.h:83
CSemaphore * sem
Definition: sync.h:234
int value
Definition: sync.h:199
void Release()
Definition: sync.h:246
~CCriticalBlock() UNLOCK_FUNCTION()
Definition: sync.h:163
Wrapper around std::unique_lock<CCriticalSection>
Definition: sync.h:116
static void EnterCritical(const char *pszName, const char *pszFile, int nLine, void *cs, bool fTry=false)
Definition: sync.h:81
static void LeaveCritical()
Definition: sync.h:82
CSemaphoreGrant()
Definition: sync.h:269
std::unique_lock< CCriticalSection > lock
Definition: sync.h:119
CCriticalBlock(CCriticalSection &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(mutexIn)
Definition: sync.h:144
#define LOCKABLE
Definition: threadsafety.h:35
std::condition_variable condition
Definition: sync.h:197
Template mixin that adds -Wthread-safety locking annotations to a subset of the mutex API...
Definition: sync.h:54
void wait()
Definition: sync.h:204
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
Definition: threadsafety.h:45
#define SCOPED_LOCKABLE
Definition: threadsafety.h:36
CSemaphore(int init)
Definition: sync.h:202
void post()
Definition: sync.h:220
bool TryAcquire()
Definition: sync.h:254
std::unique_lock< std::mutex > WaitableLock
Just a typedef for std::unique_lock, can be wrapped later if desired.
Definition: sync.h:109
~CCriticalSection()
Definition: sync.h:97
bool fHaveGrant
Definition: sync.h:235
Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the re...
Definition: sync.h:94
std::mutex mutex
Definition: sync.h:198
Released under the MIT license