 |
Bitcoin Core
23.99.0
P2P Digital Currency
|
Go to the documentation of this file.
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();
132 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
136 #define AssertLockNotHeld(cs) AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs)
139 template <
typename Mutex,
typename Base =
typename Mutex::UniqueLock>
143 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
146 #ifdef DEBUG_LOCKCONTENTION
147 if (Base::try_lock())
return;
153 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
157 if (!Base::owns_lock()) {
160 return Base::owns_lock();
167 TryEnter(pszName, pszFile, nLine);
169 Enter(pszName, pszFile, nLine);
174 if (!pmutexIn)
return;
176 *
static_cast<Base*
>(
this) = Base(*pmutexIn, std::defer_lock);
178 TryEnter(pszName, pszFile, nLine);
180 Enter(pszName, pszFile, nLine);
185 if (Base::owns_lock())
191 return Base::owns_lock();
204 explicit reverse_lock(
UniqueLock& _lock,
const char* _guardname,
const char* _file,
int _line) : lock(_lock), file(_file), line(_line) {
213 EnterCritical(lockname.c_str(), file.c_str(), line, lock.mutex());
230 #define REVERSE_LOCK(g) typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_NAME(revlock)(g, #g, __FILE__, __LINE__)
232 template<
typename MutexArg>
235 #define LOCK(cs) DebugLock<decltype(cs)> UNIQUE_NAME(criticalblock)(cs, #cs, __FILE__, __LINE__)
236 #define LOCK2(cs1, cs2) \
237 DebugLock<decltype(cs1)> criticalblock1(cs1, #cs1, __FILE__, __LINE__); \
238 DebugLock<decltype(cs2)> criticalblock2(cs2, #cs2, __FILE__, __LINE__);
239 #define TRY_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__, true)
240 #define WAIT_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__)
242 #define ENTER_CRITICAL_SECTION(cs) \
244 EnterCritical(#cs, __FILE__, __LINE__, &cs); \
248 #define LEAVE_CRITICAL_SECTION(cs) \
250 std::string lockname; \
251 CheckLastCritical((void*)(&cs), lockname, #cs, __FILE__, __LINE__); \
279 #define WITH_LOCK(cs, code) [&]() -> decltype(auto) { LOCK(cs); code; }()
293 std::unique_lock<std::mutex> lock(
mutex);
300 std::lock_guard<std::mutex> lock(
mutex);
310 std::lock_guard<std::mutex> lock(
mutex);
371 operator bool()
const
377 #endif // BITCOIN_SYNC_H
void Enter(const char *pszName, const char *pszFile, int nLine)
#define EXCLUSIVE_LOCK_FUNCTION(...)
#define UNLOCK_FUNCTION(...)
reverse_lock(UniqueLock &_lock, const char *_guardname, const char *_file, int _line)
void MoveTo(CSemaphoreGrant &grant)
~UniqueLock() UNLOCK_FUNCTION()
Template mixin that adds -Wthread-safety locking annotations and lock order checking to a subset of t...
void CheckLastCritical(void *cs, std::string &lockname, const char *guardname, const char *file, int line)
void DeleteLock(void *cs)
std::condition_variable condition
RAII-style semaphore lock.
void unlock() UNLOCK_FUNCTION()
void AssertLockNotHeldInline(const char *name, const char *file, int line, Mutex *cs) EXCLUSIVE_LOCKS_REQUIRED(!cs)
void AssertLockHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) EXCLUSIVE_LOCKS_REQUIRED(cs)
#define LOG_TIME_MICROS_WITH_CATEGORY(end_msg, log_category)
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
Wrapper around std::unique_lock style lock for Mutex.
#define LOCKS_EXCLUDED(...)
void EnterCritical(const char *pszName, const char *pszFile, int nLine, MutexType *cs, bool fTry=false)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
UniqueLock(Mutex *pmutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
void lock() EXCLUSIVE_LOCK_FUNCTION()
CSemaphoreGrant(CSemaphore &sema, bool fTry=false)
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
void AssertLockNotHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) LOCKS_EXCLUDED(cs)
UniqueLock(Mutex &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(mutexIn)
An RAII-style reverse lock.