9 #ifdef DEBUG_LOCKCONTENTION 17 #include <condition_variable> 55 #ifdef DEBUG_LOCKORDER 56 template <
typename MutexType>
57 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine, MutexType*
cs,
bool fTry =
false);
59 void CheckLastCritical(
void*
cs, std::string& lockname,
const char* guardname,
const char* file,
int line);
60 std::string LocksHeld();
61 template <
typename MutexType>
63 template <
typename MutexType>
73 extern bool g_debug_lockorder_abort;
75 template <
typename MutexType>
76 inline void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine, MutexType*
cs,
bool fTry =
false) {}
78 inline void CheckLastCritical(
void*
cs, std::string& lockname,
const char* guardname,
const char* file,
int line) {}
79 template <
typename MutexType>
81 template <
typename MutexType>
91 template <
typename PARENT>
111 return PARENT::try_lock();
143 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) 148 #define AssertLockNotHeld(cs) AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs) 151 template <
typename Mutex,
typename Base =
typename Mutex::UniqueLock>
155 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
158 #ifdef DEBUG_LOCKCONTENTION 159 if (Base::try_lock())
return;
165 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
169 if (!Base::owns_lock()) {
172 return Base::owns_lock();
179 TryEnter(pszName, pszFile, nLine);
181 Enter(pszName, pszFile, nLine);
186 if (!pmutexIn)
return;
188 *
static_cast<Base*
>(
this) = Base(*pmutexIn, std::defer_lock);
190 TryEnter(pszName, pszFile, nLine);
192 Enter(pszName, pszFile, nLine);
197 if (Base::owns_lock())
203 return Base::owns_lock();
216 explicit reverse_lock(
UniqueLock& _lock,
const char* _guardname,
const char* _file,
int _line) : lock(_lock), file(_file), line(_line) {
225 EnterCritical(lockname.c_str(), file.c_str(), line, lock.mutex());
242 #define REVERSE_LOCK(g) typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_NAME(revlock)(g, #g, __FILE__, __LINE__) 244 template<
typename MutexArg>
261 #define LOCK(cs) DebugLock<decltype(cs)> UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) 262 #define LOCK2(cs1, cs2) \ 263 DebugLock<decltype(cs1)> criticalblock1(MaybeCheckNotHeld(cs1), #cs1, __FILE__, __LINE__); \ 264 DebugLock<decltype(cs2)> criticalblock2(MaybeCheckNotHeld(cs2), #cs2, __FILE__, __LINE__) 265 #define TRY_LOCK(cs, name) DebugLock<decltype(cs)> name(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__, true) 266 #define WAIT_LOCK(cs, name) DebugLock<decltype(cs)> name(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) 268 #define ENTER_CRITICAL_SECTION(cs) \ 270 EnterCritical(#cs, __FILE__, __LINE__, &cs); \ 274 #define LEAVE_CRITICAL_SECTION(cs) \ 276 std::string lockname; \ 277 CheckLastCritical((void*)(&cs), lockname, #cs, __FILE__, __LINE__); \ 305 #define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }()) 319 std::unique_lock<std::mutex> lock(
mutex);
326 std::lock_guard<std::mutex> lock(
mutex);
336 std::lock_guard<std::mutex> lock(
mutex);
397 operator bool()
const 403 #endif // BITCOIN_SYNC_H
void MoveTo(CSemaphoreGrant &grant)
void unlock() UNLOCK_FUNCTION()
#define EXCLUSIVE_LOCK_FUNCTION(...)
reverse_lock(UniqueLock &_lock, const char *_guardname, const char *_file, int _line)
void Enter(const char *pszName, const char *pszFile, int nLine)
RAII-style semaphore lock.
void lock() EXCLUSIVE_LOCK_FUNCTION()
void AssertLockNotHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) LOCKS_EXCLUDED(cs)
#define UNLOCK_FUNCTION(...)
CSemaphoreGrant(CSemaphore &sema, bool fTry=false)
void CheckLastCritical(void *cs, std::string &lockname, const char *guardname, const char *file, int line)
void DeleteLock(void *cs)
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
void EnterCritical(const char *pszName, const char *pszFile, int nLine, MutexType *cs, bool fTry=false)
UniqueLock(Mutex *pmutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
UniqueLock(Mutex &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(mutexIn)
~UniqueLock() UNLOCK_FUNCTION()
#define LOG_TIME_MICROS_WITH_CATEGORY(end_msg, log_category)
void AssertLockHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) EXCLUSIVE_LOCKS_REQUIRED(cs)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
std::condition_variable condition
Template mixin that adds -Wthread-safety locking annotations and lock order checking to a subset of t...
#define LOCKS_EXCLUDED(...)
An RAII-style reverse lock.
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
Mutex & MaybeCheckNotHeld(Mutex &cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs)
Wrapper around std::unique_lock style lock for Mutex.
void AssertLockNotHeldInline(const char *name, const char *file, int line, Mutex *cs) EXCLUSIVE_LOCKS_REQUIRED(!cs)
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
Different type to mark Mutex at global scope.