Bitcoin Core  0.19.99
P2P Digital Currency
sync.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 #include <util/macros.h>
11 
12 #include <condition_variable>
13 #include <thread>
14 #include <mutex>
15 
16 
18 // //
19 // THE SIMPLE DEFINITION, EXCLUDING DEBUG CODE //
20 // //
22 
23 /*
24 RecursiveMutex mutex;
25  std::recursive_mutex mutex;
26 
27 LOCK(mutex);
28  std::unique_lock<std::recursive_mutex> criticalblock(mutex);
29 
30 LOCK2(mutex1, mutex2);
31  std::unique_lock<std::recursive_mutex> criticalblock1(mutex1);
32  std::unique_lock<std::recursive_mutex> criticalblock2(mutex2);
33 
34 TRY_LOCK(mutex, name);
35  std::unique_lock<std::recursive_mutex> name(mutex, std::try_to_lock_t);
36 
37 ENTER_CRITICAL_SECTION(mutex); // no RAII
38  mutex.lock();
39 
40 LEAVE_CRITICAL_SECTION(mutex); // no RAII
41  mutex.unlock();
42  */
43 
45 // //
46 // THE ACTUAL IMPLEMENTATION //
47 // //
49 
50 #ifdef DEBUG_LOCKORDER
51 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
52 void LeaveCritical();
53 std::string LocksHeld();
54 void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) ASSERT_EXCLUSIVE_LOCK(cs);
55 void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
56 void DeleteLock(void* cs);
57 
63 extern bool g_debug_lockorder_abort;
64 #else
65 void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
66 void static inline LeaveCritical() {}
67 void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) ASSERT_EXCLUSIVE_LOCK(cs) {}
68 void static inline AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
69 void static inline DeleteLock(void* cs) {}
70 #endif
71 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
72 #define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
73 
78 template <typename PARENT>
79 class LOCKABLE AnnotatedMixin : public PARENT
80 {
81 public:
83  DeleteLock((void*)this);
84  }
85 
87  {
88  PARENT::lock();
89  }
90 
92  {
93  PARENT::unlock();
94  }
95 
97  {
98  return PARENT::try_lock();
99  }
100 
101  using UniqueLock = std::unique_lock<PARENT>;
102 };
103 
109 
112 
113 #ifdef DEBUG_LOCKCONTENTION
114 void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
115 #endif
116 
118 template <typename Mutex, typename Base = typename Mutex::UniqueLock>
119 class SCOPED_LOCKABLE UniqueLock : public Base
120 {
121 private:
122  void Enter(const char* pszName, const char* pszFile, int nLine)
123  {
124  EnterCritical(pszName, pszFile, nLine, (void*)(Base::mutex()));
125 #ifdef DEBUG_LOCKCONTENTION
126  if (!Base::try_lock()) {
127  PrintLockContention(pszName, pszFile, nLine);
128 #endif
129  Base::lock();
130 #ifdef DEBUG_LOCKCONTENTION
131  }
132 #endif
133  }
134 
135  bool TryEnter(const char* pszName, const char* pszFile, int nLine)
136  {
137  EnterCritical(pszName, pszFile, nLine, (void*)(Base::mutex()), true);
138  Base::try_lock();
139  if (!Base::owns_lock())
140  LeaveCritical();
141  return Base::owns_lock();
142  }
143 
144 public:
145  UniqueLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : Base(mutexIn, std::defer_lock)
146  {
147  if (fTry)
148  TryEnter(pszName, pszFile, nLine);
149  else
150  Enter(pszName, pszFile, nLine);
151  }
152 
153  UniqueLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
154  {
155  if (!pmutexIn) return;
156 
157  *static_cast<Base*>(this) = Base(*pmutexIn, std::defer_lock);
158  if (fTry)
159  TryEnter(pszName, pszFile, nLine);
160  else
161  Enter(pszName, pszFile, nLine);
162  }
163 
165  {
166  if (Base::owns_lock())
167  LeaveCritical();
168  }
169 
170  operator bool()
171  {
172  return Base::owns_lock();
173  }
174 };
175 
176 template<typename MutexArg>
178 
179 #define LOCK(cs) DebugLock<decltype(cs)> PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
180 #define LOCK2(cs1, cs2) \
181  DebugLock<decltype(cs1)> criticalblock1(cs1, #cs1, __FILE__, __LINE__); \
182  DebugLock<decltype(cs2)> criticalblock2(cs2, #cs2, __FILE__, __LINE__);
183 #define TRY_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__, true)
184 #define WAIT_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__)
185 
186 #define ENTER_CRITICAL_SECTION(cs) \
187  { \
188  EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
189  (cs).lock(); \
190  }
191 
192 #define LEAVE_CRITICAL_SECTION(cs) \
193  { \
194  (cs).unlock(); \
195  LeaveCritical(); \
196  }
197 
206 #define WITH_LOCK(cs, code) [&] { LOCK(cs); code; }()
207 
209 {
210 private:
211  std::condition_variable condition;
212  std::mutex mutex;
213  int value;
214 
215 public:
216  explicit CSemaphore(int init) : value(init) {}
217 
218  void wait()
219  {
220  std::unique_lock<std::mutex> lock(mutex);
221  condition.wait(lock, [&]() { return value >= 1; });
222  value--;
223  }
224 
225  bool try_wait()
226  {
227  std::lock_guard<std::mutex> lock(mutex);
228  if (value < 1)
229  return false;
230  value--;
231  return true;
232  }
233 
234  void post()
235  {
236  {
237  std::lock_guard<std::mutex> lock(mutex);
238  value++;
239  }
240  condition.notify_one();
241  }
242 };
243 
246 {
247 private:
250 
251 public:
252  void Acquire()
253  {
254  if (fHaveGrant)
255  return;
256  sem->wait();
257  fHaveGrant = true;
258  }
259 
260  void Release()
261  {
262  if (!fHaveGrant)
263  return;
264  sem->post();
265  fHaveGrant = false;
266  }
267 
268  bool TryAcquire()
269  {
270  if (!fHaveGrant && sem->try_wait())
271  fHaveGrant = true;
272  return fHaveGrant;
273  }
274 
275  void MoveTo(CSemaphoreGrant& grant)
276  {
277  grant.Release();
278  grant.sem = sem;
279  grant.fHaveGrant = fHaveGrant;
280  fHaveGrant = false;
281  }
282 
283  CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {}
284 
285  explicit CSemaphoreGrant(CSemaphore& sema, bool fTry = false) : sem(&sema), fHaveGrant(false)
286  {
287  if (fTry)
288  TryAcquire();
289  else
290  Acquire();
291  }
292 
294  {
295  Release();
296  }
297 
298  operator bool() const
299  {
300  return fHaveGrant;
301  }
302 };
303 
304 // Utility class for indicating to compiler thread analysis that a mutex is
305 // locked (when it couldn't be determined otherwise).
307 {
308  template <typename Mutex>
310  {
311 #ifdef DEBUG_LOCKORDER
312  AssertLockHeld(mutex);
313 #endif
314  }
316 };
317 
318 #endif // BITCOIN_SYNC_H
static void AssertLockHeldInternal(const char *pszName, const char *pszFile, int nLine, void *cs) ASSERT_EXCLUSIVE_LOCK(cs)
Definition: sync.h:67
static void AssertLockNotHeldInternal(const char *pszName, const char *pszFile, int nLine, void *cs)
Definition: sync.h:68
void MoveTo(CSemaphoreGrant &grant)
Definition: sync.h:275
void unlock() UNLOCK_FUNCTION()
Definition: sync.h:91
#define EXCLUSIVE_LOCK_FUNCTION(...)
Definition: threadsafety.h:44
void Enter(const char *pszName, const char *pszFile, int nLine)
Definition: sync.h:122
static void pool cs
RAII-style semaphore lock.
Definition: sync.h:245
bool try_wait()
Definition: sync.h:225
~AnnotatedMixin()
Definition: sync.h:82
void lock() EXCLUSIVE_LOCK_FUNCTION()
Definition: sync.h:86
void Acquire()
Definition: sync.h:252
#define UNLOCK_FUNCTION(...)
Definition: threadsafety.h:48
CSemaphoreGrant(CSemaphore &sema, bool fTry=false)
Definition: sync.h:285
#define AssertLockHeld(cs)
Definition: sync.h:71
~CSemaphoreGrant()
Definition: sync.h:293
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
Definition: sync.h:96
static void DeleteLock(void *cs)
Definition: sync.h:69
UniqueLock(Mutex *pmutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
Definition: sync.h:153
CSemaphore * sem
Definition: sync.h:248
UniqueLock(Mutex &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(mutexIn)
Definition: sync.h:145
int value
Definition: sync.h:213
~UniqueLock() UNLOCK_FUNCTION()
Definition: sync.h:164
void Release()
Definition: sync.h:260
static void EnterCritical(const char *pszName, const char *pszFile, int nLine, void *cs, bool fTry=false)
Definition: sync.h:65
static void LeaveCritical()
Definition: sync.h:66
CSemaphoreGrant()
Definition: sync.h:283
#define LOCKABLE
Definition: threadsafety.h:36
std::condition_variable condition
Definition: sync.h:211
Template mixin that adds -Wthread-safety locking annotations and lock order checking to a subset of t...
Definition: sync.h:79
~LockAssertion() UNLOCK_FUNCTION()
Definition: sync.h:315
void wait()
Definition: sync.h:218
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
Definition: threadsafety.h:46
#define SCOPED_LOCKABLE
Definition: threadsafety.h:37
CSemaphore(int init)
Definition: sync.h:216
LockAssertion(Mutex &mutex) EXCLUSIVE_LOCK_FUNCTION(mutex)
Definition: sync.h:309
Wrapper around std::unique_lock style lock for Mutex.
Definition: sync.h:119
AnnotatedMixin< std::mutex > Mutex
Wrapped mutex: supports waiting but not recursive locking.
Definition: sync.h:111
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
Definition: sync.h:135
void post()
Definition: sync.h:234
bool TryAcquire()
Definition: sync.h:268
bool fHaveGrant
Definition: sync.h:249
#define ASSERT_EXCLUSIVE_LOCK(...)
Definition: threadsafety.h:54
std::mutex mutex
Definition: sync.h:212