Bitcoin Core 31.99.0
P2P Digital Currency
sync.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-present 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> // IWYU pragma: export
10#include <util/macros.h>
11
12#include <cassert>
13#include <condition_variable>
14#include <mutex>
15#include <string>
16#include <thread>
17
19// //
20// THE SIMPLE DEFINITION, EXCLUDING DEBUG CODE //
21// //
23
24/*
25RecursiveMutex mutex;
26 std::recursive_mutex mutex;
27
28LOCK(mutex);
29 std::unique_lock<std::recursive_mutex> criticalblock(mutex);
30
31LOCK2(mutex1, mutex2);
32 std::unique_lock<std::recursive_mutex> criticalblock1(mutex1);
33 std::unique_lock<std::recursive_mutex> criticalblock2(mutex2);
34
35TRY_LOCK(mutex, name);
36 std::unique_lock<std::recursive_mutex> name(mutex, std::try_to_lock_t);
37 */
38
40// //
41// THE ACTUAL IMPLEMENTATION //
42// //
44
45#ifdef DEBUG_LOCKORDER
46template <typename MutexType>
47void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry = false);
48void LeaveCritical();
49void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line);
50template <typename MutexType>
51void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs) EXCLUSIVE_LOCKS_REQUIRED(cs);
52template <typename MutexType>
53void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs) LOCKS_EXCLUDED(cs);
54void DeleteLock(void* cs);
55bool LockStackEmpty();
56
62extern bool g_debug_lockorder_abort;
63#else
64template <typename MutexType>
65inline void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry = false) {}
66inline void LeaveCritical() {}
67inline void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line) {}
68template <typename MutexType>
69inline void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs) EXCLUSIVE_LOCKS_REQUIRED(cs) {}
70template <typename MutexType>
71void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs) LOCKS_EXCLUDED(cs) {}
72inline void DeleteLock(void* cs) {}
73inline bool LockStackEmpty() { return true; }
74#endif
75
76/*
77 * Called when a mutex fails to lock immediately because it is held by another
78 * thread, or spuriously. Responsible for locking the lock before returning.
79 */
80#ifdef DEBUG_LOCKCONTENTION
81
82template <typename LockType>
83void ContendedLock(std::string_view name, std::string_view file, int nLine, LockType& lock);
84#endif
85
90template <typename PARENT>
91class LOCKABLE AnnotatedMixin : public PARENT
92{
93public:
95 DeleteLock((void*)this);
96 }
97
99 {
100 PARENT::lock();
101 }
102
104 {
105 PARENT::unlock();
106 }
107
109 {
110 return PARENT::try_lock();
111 }
112
113 using unique_lock = std::unique_lock<PARENT>;
114#ifdef __clang__
118 const AnnotatedMixin& operator!() const { return *this; }
119#endif // __clang__
120};
121
127
130
140class GlobalMutex : public Mutex { };
141
142#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
143
144inline void AssertLockNotHeldInline(const char* name, const char* file, int line, Mutex* cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) { AssertLockNotHeldInternal(name, file, line, cs); }
145inline void AssertLockNotHeldInline(const char* name, const char* file, int line, RecursiveMutex* cs) LOCKS_EXCLUDED(cs) { AssertLockNotHeldInternal(name, file, line, cs); }
146inline void AssertLockNotHeldInline(const char* name, const char* file, int line, GlobalMutex* cs) LOCKS_EXCLUDED(cs) { AssertLockNotHeldInternal(name, file, line, cs); }
147#define AssertLockNotHeld(cs) AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs)
148
150template <typename MutexType>
151class SCOPED_LOCKABLE UniqueLock : public MutexType::unique_lock
152{
153private:
154 using Base = typename MutexType::unique_lock;
155
156 void Enter(const char* pszName, const char* pszFile, int nLine)
157 {
158 EnterCritical(pszName, pszFile, nLine, Base::mutex());
159#ifdef DEBUG_LOCKCONTENTION
160 if (!Base::try_lock()) {
161 ContendedLock(pszName, pszFile, nLine, static_cast<Base&>(*this));
162 }
163#else
164 Base::lock();
165#endif
166 }
167
168 bool TryEnter(const char* pszName, const char* pszFile, int nLine)
169 {
170 EnterCritical(pszName, pszFile, nLine, Base::mutex(), true);
171 if (Base::try_lock()) {
172 return true;
173 }
175 return false;
176 }
177
178public:
179 UniqueLock(MutexType& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : Base(mutexIn, std::defer_lock)
180 {
181 if (fTry)
182 TryEnter(pszName, pszFile, nLine);
183 else
184 Enter(pszName, pszFile, nLine);
185 }
186
187 UniqueLock(MutexType* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
188 {
189 if (!pmutexIn) return;
190
191 *static_cast<Base*>(this) = Base(*pmutexIn, std::defer_lock);
192 if (fTry)
193 TryEnter(pszName, pszFile, nLine);
194 else
195 Enter(pszName, pszFile, nLine);
196 }
197
199 {
200 if (Base::owns_lock())
202 }
203
204 operator bool()
205 {
206 return Base::owns_lock();
207 }
208
209protected:
210 // needed for reverse_lock
211 UniqueLock() = default;
212
213public:
218 public:
219 explicit reverse_lock(UniqueLock& _lock, const MutexType& mutex, const char* _guardname, const char* _file, int _line) UNLOCK_FUNCTION(mutex) : lock(_lock), file(_file), line(_line) {
220 // Ensure that mutex passed back for thread-safety analysis is indeed the original
221 assert(std::addressof(mutex) == lock.mutex());
222
223 CheckLastCritical((void*)lock.mutex(), lockname, _guardname, _file, _line);
224 lock.unlock();
226 lock.swap(templock);
227 }
228
230 templock.swap(lock);
231 EnterCritical(lockname.c_str(), file.c_str(), line, lock.mutex());
232 lock.lock();
233 }
234
235 private:
238
241 std::string lockname;
242 const std::string file;
243 const int line;
244 };
245 friend class reverse_lock;
246};
247
248// clang's thread-safety analyzer is unable to deal with aliases of mutexes, so
249// it is not possible to use the lock's copy of the mutex for that purpose.
250// Instead, the original mutex needs to be passed back to the reverse_lock for
251// the sake of thread-safety analysis, but it is not actually used otherwise.
252#define REVERSE_LOCK(g, cs) typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_NAME(revlock)(g, cs, #cs, __FILE__, __LINE__)
253
254// When locking a Mutex, require negative capability to ensure the lock
255// is not already held
258
259// When locking a GlobalMutex or RecursiveMutex, just check it is not
260// locked in the surrounding scope.
261template <typename MutexType>
262inline MutexType& MaybeCheckNotHeld(MutexType& m) LOCKS_EXCLUDED(m) LOCK_RETURNED(m) { return m; }
263template <typename MutexType>
264inline MutexType* MaybeCheckNotHeld(MutexType* m) LOCKS_EXCLUDED(m) LOCK_RETURNED(m) { return m; }
265
266#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
267#define LOCK2(cs1, cs2) \
268 UniqueLock criticalblock1(MaybeCheckNotHeld(cs1), #cs1, __FILE__, __LINE__); \
269 UniqueLock criticalblock2(MaybeCheckNotHeld(cs2), #cs2, __FILE__, __LINE__)
270#define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__
271#define TRY_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs), true)
272#define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs))
273
297#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
298
299#endif // BITCOIN_SYNC_H
static void pool cs
Template mixin that adds -Wthread-safety locking annotations and lock order checking to a subset of t...
Definition: sync.h:92
~AnnotatedMixin()
Definition: sync.h:94
std::unique_lock< PARENT > unique_lock
Definition: sync.h:113
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
Definition: sync.h:108
void unlock() UNLOCK_FUNCTION()
Definition: sync.h:103
void lock() EXCLUSIVE_LOCK_FUNCTION()
Definition: sync.h:98
(Un)serialize a number as raw byte or 2 hexadecimal chars.
Different type to mark Mutex at global scope.
Definition: sync.h:140
An RAII-style reverse lock.
Definition: sync.h:217
UniqueLock & lock
Definition: sync.h:239
const std::string file
Definition: sync.h:242
~reverse_lock() UNLOCK_FUNCTION()
Definition: sync.h:229
reverse_lock(UniqueLock &_lock, const MutexType &mutex, const char *_guardname, const char *_file, int _line) UNLOCK_FUNCTION(mutex)
Definition: sync.h:219
std::string lockname
Definition: sync.h:241
reverse_lock(reverse_lock const &)
reverse_lock & operator=(reverse_lock const &)
UniqueLock templock
Definition: sync.h:240
Wrapper around std::unique_lock style lock for MutexType.
Definition: sync.h:152
~UniqueLock() UNLOCK_FUNCTION()
Definition: sync.h:198
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
Definition: sync.h:168
void Enter(const char *pszName, const char *pszFile, int nLine)
Definition: sync.h:156
UniqueLock()=default
UniqueLock(MutexType &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(mutexIn)
Definition: sync.h:179
UniqueLock(MutexType *pmutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
Definition: sync.h:187
Definition: common.h:29
const char * name
Definition: rest.cpp:49
void AssertLockHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: sync.h:69
void AssertLockNotHeldInline(const char *name, const char *file, int line, Mutex *cs) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: sync.h:144
void EnterCritical(const char *pszName, const char *pszFile, int nLine, MutexType *cs, bool fTry=false)
Definition: sync.h:65
void DeleteLock(void *cs)
Definition: sync.h:72
void CheckLastCritical(void *cs, std::string &lockname, const char *guardname, const char *file, int line)
Definition: sync.h:67
void LeaveCritical()
Definition: sync.h:66
bool LockStackEmpty()
Definition: sync.h:73
Mutex & MaybeCheckNotHeld(Mutex &cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs)
Definition: sync.h:256
void AssertLockNotHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) LOCKS_EXCLUDED(cs)
Definition: sync.h:71
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
Definition: threadsafety.h:45
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:50
#define EXCLUSIVE_LOCK_FUNCTION(...)
Definition: threadsafety.h:43
#define SCOPED_LOCKABLE
Definition: threadsafety.h:38
#define LOCKABLE
Definition: threadsafety.h:37
#define LOCK_RETURNED(x)
Definition: threadsafety.h:49
#define UNLOCK_FUNCTION(...)
Definition: threadsafety.h:47
assert(!tx.IsCoinBase())