15#include <condition_variable>
48template <
typename MutexType>
49void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine, MutexType*
cs,
bool fTry =
false);
51void CheckLastCritical(
void*
cs, std::string& lockname,
const char* guardname,
const char* file,
int line);
52template <
typename MutexType>
54template <
typename MutexType>
64extern bool g_debug_lockorder_abort;
66template <
typename MutexType>
67inline void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine, MutexType*
cs,
bool fTry =
false) {}
69inline void CheckLastCritical(
void*
cs, std::string& lockname,
const char* guardname,
const char* file,
int line) {}
70template <
typename MutexType>
72template <
typename MutexType>
82#ifdef DEBUG_LOCKCONTENTION
84template <
typename LockType>
85void ContendedLock(std::string_view
name, std::string_view file,
int nLine, LockType& lock);
92template <
typename PARENT>
112 return PARENT::try_lock();
144#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
149#define AssertLockNotHeld(cs) AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs)
152template <
typename MutexType>
156 using Base =
typename MutexType::unique_lock;
158 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
161#ifdef DEBUG_LOCKCONTENTION
162 if (!Base::try_lock()) {
163 ContendedLock(pszName, pszFile, nLine,
static_cast<Base&
>(*
this));
170 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
173 if (Base::try_lock()) {
184 TryEnter(pszName, pszFile, nLine);
186 Enter(pszName, pszFile, nLine);
191 if (!pmutexIn)
return;
193 *
static_cast<Base*
>(
this) =
Base(*pmutexIn, std::defer_lock);
195 TryEnter(pszName, pszFile, nLine);
197 Enter(pszName, pszFile, nLine);
202 if (Base::owns_lock())
208 return Base::owns_lock();
223 assert(std::addressof(mutex) == lock.mutex());
233 EnterCritical(lockname.c_str(), file.c_str(), line, lock.mutex());
254#define REVERSE_LOCK(g, cs) typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_NAME(revlock)(g, cs, #cs, __FILE__, __LINE__)
263template <
typename MutexType>
265template <
typename MutexType>
268#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
269#define LOCK2(cs1, cs2) \
270 UniqueLock criticalblock1(MaybeCheckNotHeld(cs1), #cs1, __FILE__, __LINE__); \
271 UniqueLock criticalblock2(MaybeCheckNotHeld(cs2), #cs2, __FILE__, __LINE__)
272#define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__
273#define TRY_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs), true)
274#define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs))
299#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
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()
(Un)serialize a number as raw byte or 2 hexadecimal chars.
Different type to mark Mutex at global scope.
An RAII-style reverse lock.
~reverse_lock() UNLOCK_FUNCTION()
reverse_lock(UniqueLock &_lock, const MutexType &mutex, const char *_guardname, const char *_file, int _line) UNLOCK_FUNCTION(mutex)
reverse_lock(reverse_lock const &)
reverse_lock & operator=(reverse_lock const &)
Wrapper around std::unique_lock style lock for MutexType.
~UniqueLock() UNLOCK_FUNCTION()
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)
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(...)