11 #include <condition_variable> 49 #ifdef DEBUG_LOCKORDER 50 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void*
cs,
bool fTry =
false);
52 std::string LocksHeld();
62 extern bool g_debug_lockorder_abort;
64 void static inline EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void*
cs,
bool fTry =
false) {}
70 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) 71 #define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs) 77 template <
typename PARENT>
97 return PARENT::try_lock();
113 #ifdef DEBUG_LOCKCONTENTION 114 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine);
118 template <
typename Mutex,
typename Base =
typename Mutex::UniqueLock>
122 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
124 EnterCritical(pszName, pszFile, nLine, (
void*)(Base::mutex()));
125 #ifdef DEBUG_LOCKCONTENTION 126 if (!Base::try_lock()) {
127 PrintLockContention(pszName, pszFile, nLine);
130 #ifdef DEBUG_LOCKCONTENTION 135 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
137 EnterCritical(pszName, pszFile, nLine, (
void*)(Base::mutex()),
true);
139 if (!Base::owns_lock())
141 return Base::owns_lock();
148 TryEnter(pszName, pszFile, nLine);
150 Enter(pszName, pszFile, nLine);
155 if (!pmutexIn)
return;
157 *static_cast<Base*>(
this) = Base(*pmutexIn, std::defer_lock);
159 TryEnter(pszName, pszFile, nLine);
161 Enter(pszName, pszFile, nLine);
166 if (Base::owns_lock())
172 return Base::owns_lock();
176 template<
typename MutexArg>
179 #define PASTE(x, y) x ## y 180 #define PASTE2(x, y) PASTE(x, y) 182 #define LOCK(cs) DebugLock<decltype(cs)> PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__) 183 #define LOCK2(cs1, cs2) \ 184 DebugLock<decltype(cs1)> criticalblock1(cs1, #cs1, __FILE__, __LINE__); \ 185 DebugLock<decltype(cs2)> criticalblock2(cs2, #cs2, __FILE__, __LINE__); 186 #define TRY_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__, true) 187 #define WAIT_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__) 189 #define ENTER_CRITICAL_SECTION(cs) \ 191 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \ 195 #define LEAVE_CRITICAL_SECTION(cs) \ 209 #define WITH_LOCK(cs, code) [&] { LOCK(cs); code; }() 223 std::unique_lock<std::mutex> lock(
mutex);
230 std::lock_guard<std::mutex> lock(
mutex);
240 std::lock_guard<std::mutex> lock(
mutex);
301 operator bool()
const 311 template <
typename Mutex>
314 #ifdef DEBUG_LOCKORDER 321 #endif // BITCOIN_SYNC_H std::condition_variable condition
Definition: sync.h:214
static void AssertLockHeldInternal(const char *pszName, const char *pszFile, int nLine, void *cs) ASSERT_EXCLUSIVE_LOCK(cs)
Definition: sync.h:66
static void AssertLockNotHeldInternal(const char *pszName, const char *pszFile, int nLine, void *cs)
Definition: sync.h:67
~AnnotatedMixin()
Definition: sync.h:81
std::mutex mutex
Definition: sync.h:215
#define EXCLUSIVE_LOCK_FUNCTION(...)
Definition: thread_annotations.h:37
void Acquire()
Definition: sync.h:255
void Release()
Definition: sync.h:263
~CSemaphoreGrant()
Definition: sync.h:296
void MoveTo(CSemaphoreGrant &grant)
Definition: sync.h:278
bool fHaveGrant
Definition: sync.h:252
static void pool cs
Definition: mempool_eviction.cpp:13
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
Definition: thread_annotations.h:45
AnnotatedMixin< std::recursive_mutex > CCriticalSection
Definition: sync.h:108
UniqueLock(Mutex *pmutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
Definition: sync.h:153
void unlock() UNLOCK_FUNCTION()
Definition: sync.h:90
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
Definition: sync.h:95
void lock() EXCLUSIVE_LOCK_FUNCTION()
Definition: sync.h:85
CSemaphore * sem
Definition: sync.h:251
~LockAssertion() UNLOCK_FUNCTION()
Definition: sync.h:318
bool try_wait()
Definition: sync.h:228
~UniqueLock() UNLOCK_FUNCTION()
Definition: sync.h:164
bool TryAcquire()
Definition: sync.h:271
#define SCOPED_LOCKABLE
Definition: thread_annotations.h:33
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
Definition: sync.h:135
void Enter(const char *pszName, const char *pszFile, int nLine)
Definition: sync.h:122
#define AssertLockHeld(cs)
Definition: sync.h:70
UniqueLock(Mutex &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(mutexIn)
Definition: sync.h:145
CSemaphoreGrant(CSemaphore &sema, bool fTry=false)
Definition: sync.h:288
static void DeleteLock(void *cs)
Definition: sync.h:68
#define LOCKABLE
Definition: thread_annotations.h:29
void wait()
Definition: sync.h:221
static void EnterCritical(const char *pszName, const char *pszFile, int nLine, void *cs, bool fTry=false)
Definition: sync.h:64
static void LeaveCritical()
Definition: sync.h:65
CSemaphore(int init)
Definition: sync.h:219
#define UNLOCK_FUNCTION(...)
Definition: thread_annotations.h:53
CSemaphoreGrant()
Definition: sync.h:286
int value
Definition: sync.h:216
void post()
Definition: sync.h:237
AnnotatedMixin< std::mutex > Mutex
Definition: sync.h:111
LockAssertion(Mutex &mutex) EXCLUSIVE_LOCK_FUNCTION(mutex)
Definition: sync.h:312
#define ASSERT_EXCLUSIVE_LOCK(...)
Definition: threadsafety.h:54