9#ifdef DEBUG_LOCKCONTENTION
17#include <condition_variable>
56template <
typename MutexType>
57void EnterCritical(
const char *pszName,
const char *pszFile,
int nLine,
58 MutexType *
cs,
bool fTry =
false);
61 const char *file,
int line);
62std::string LocksHeld();
63template <
typename MutexType>
66template <
typename MutexType>
77extern bool g_debug_lockorder_abort;
79template <
typename MutexType>
80inline void EnterCritical(
const char *pszName,
const char *pszFile,
int nLine,
81 MutexType *
cs,
bool fTry =
false) {}
84 const char *guardname,
const char *file,
86template <
typename MutexType>
88 int nLine, MutexType *
cs)
90template <
typename MutexType>
112 return PARENT::try_lock();
146#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
163#define AssertLockNotHeld(cs) \
164 AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs)
167template <
typename MutexType>
170 using Base =
typename MutexType::unique_lock;
172 void Enter(
const char *pszName,
const char *pszFile,
int nLine) {
174#ifdef DEBUG_LOCKCONTENTION
175 if (Base::try_lock()) {
179 strprintf(
"lock contention %s, %s:%d", pszName, pszFile, nLine),
185 bool TryEnter(
const char *pszName,
const char *pszFile,
int nLine) {
187 if (Base::try_lock()) {
195 UniqueLock(MutexType &mutexIn,
const char *pszName,
const char *pszFile,
197 :
Base(mutexIn,
std::defer_lock) {
199 TryEnter(pszName, pszFile, nLine);
201 Enter(pszName, pszFile, nLine);
205 UniqueLock(MutexType *pmutexIn,
const char *pszName,
const char *pszFile,
211 *
static_cast<Base *
>(
this) =
Base(*pmutexIn, std::defer_lock);
213 TryEnter(pszName, pszFile, nLine);
215 Enter(pszName, pszFile, nLine);
220 if (Base::owns_lock()) {
225 operator bool() {
return Base::owns_lock(); }
239 const char *_file,
int _line)
240 : lock(_lock), file(_file), line(_line) {
250 EnterCritical(lockname.c_str(), file.c_str(), line, lock.mutex());
267#define REVERSE_LOCK(g) \
268 typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_LOG_NAME( \
269 revlock)(g, #g, __FILE__, __LINE__)
284template <
typename MutexType>
289template <
typename MutexType>
296 UniqueLock UNIQUE_LOG_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, \
298#define LOCK2(cs1, cs2) \
299 UniqueLock criticalblock1(MaybeCheckNotHeld(cs1), #cs1, __FILE__, \
301 UniqueLock criticalblock2(MaybeCheckNotHeld(cs2), #cs2, __FILE__, __LINE__)
302#define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__
303#define TRY_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs), true)
304#define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs))
306#define ENTER_CRITICAL_SECTION(cs) \
308 EnterCritical(#cs, __FILE__, __LINE__, &cs); \
312#define LEAVE_CRITICAL_SECTION(cs) \
314 std::string lockname; \
315 CheckLastCritical((void *)(&cs), lockname, #cs, __FILE__, __LINE__); \
343#define WITH_LOCK(cs, code) \
344 (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { \
359 std::unique_lock<std::mutex> lock(
mutex);
365 std::lock_guard<std::mutex> lock(
mutex);
375 std::lock_guard<std::mutex> lock(
mutex);
Template mixin that adds -Wthread-safety locking annotations and lock order checking to a subset of t...
std::unique_lock< PARENT > unique_lock
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
void unlock() UNLOCK_FUNCTION()
void lock() EXCLUSIVE_LOCK_FUNCTION()
RAII-style semaphore lock.
CSemaphoreGrant(CSemaphore &sema, bool fTry=false)
void MoveTo(CSemaphoreGrant &grant)
std::condition_variable condition
Different type to mark Mutex at global scope.
An RAII-style reverse lock.
reverse_lock(reverse_lock const &)
reverse_lock(UniqueLock &_lock, const char *_guardname, const char *_file, int _line)
reverse_lock & operator=(reverse_lock const &)
Wrapper around std::unique_lock style lock for MutexType.
~UniqueLock() UNLOCK_FUNCTION()
typename MutexType::unique_lock Base
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
void Enter(const char *pszName, const char *pszFile, int nLine)
UniqueLock(MutexType &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(mutexIn)
UniqueLock(MutexType *pmutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
Implement std::hash so RCUPtr can be used as a key for maps or sets.
void AssertLockHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) EXCLUSIVE_LOCKS_REQUIRED(cs)
void AssertLockNotHeldInline(const char *name, const char *file, int line, Mutex *cs) EXCLUSIVE_LOCKS_REQUIRED(!cs)
void EnterCritical(const char *pszName, const char *pszFile, int nLine, MutexType *cs, bool fTry=false)
void DeleteLock(void *cs)
void CheckLastCritical(void *cs, std::string &lockname, const char *guardname, const char *file, int line)
Mutex & MaybeCheckNotHeld(Mutex &cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs)
void AssertLockNotHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) LOCKS_EXCLUDED(cs)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
#define LOCKS_EXCLUDED(...)
#define EXCLUSIVE_LOCK_FUNCTION(...)
#define UNLOCK_FUNCTION(...)
#define LOG_TIME_MICROS_WITH_CATEGORY(end_msg, log_category)