13 #include <condition_variable>
52 #ifdef DEBUG_LOCKORDER
53 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false);
55 void CheckLastCritical(
void* cs, std::string& lockname,
const char* guardname,
const char* file,
int line);
56 std::string LocksHeld();
57 void AssertLockHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs);
58 void AssertLockNotHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs);
59 void DeleteLock(
void* cs);
66 extern bool g_debug_lockorder_abort;
68 void static inline EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false) {}
69 void static inline LeaveCritical() {}
70 void static inline CheckLastCritical(
void* cs, std::string& lockname,
const char* guardname,
const char* file,
int line) {}
71 void static inline AssertLockHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs) {}
72 void static inline AssertLockNotHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs) {}
73 void static inline DeleteLock(
void* cs) {}
75 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
76 #define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
82 template <
typename PARENT>
87 DeleteLock((
void*)
this);
102 return PARENT::try_lock();
117 #ifdef DEBUG_LOCKCONTENTION
118 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine);
122 template <
typename Mutex,
typename Base =
typename Mutex::UniqueLock>
126 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
128 EnterCritical(pszName, pszFile, nLine, (
void*)(Base::mutex()));
129 #ifdef DEBUG_LOCKCONTENTION
130 if (!Base::try_lock()) {
131 PrintLockContention(pszName, pszFile, nLine);
134 #ifdef DEBUG_LOCKCONTENTION
139 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
141 EnterCritical(pszName, pszFile, nLine, (
void*)(Base::mutex()),
true);
143 if (!Base::owns_lock())
145 return Base::owns_lock();
152 TryEnter(pszName, pszFile, nLine);
154 Enter(pszName, pszFile, nLine);
159 if (!pmutexIn)
return;
161 *
static_cast<Base*
>(
this) = Base(*pmutexIn, std::defer_lock);
163 TryEnter(pszName, pszFile, nLine);
165 Enter(pszName, pszFile, nLine);
170 if (Base::owns_lock())
176 return Base::owns_lock();
189 explicit reverse_lock(
UniqueLock& _lock,
const char* _guardname,
const char* _file,
int _line) : lock(_lock), file(_file), line(_line) {
190 CheckLastCritical((
void*)lock.mutex(), lockname, _guardname, _file, _line);
198 EnterCritical(lockname.c_str(), file.c_str(), line, (
void*)lock.mutex());
215 #define REVERSE_LOCK(g) decltype(g)::reverse_lock PASTE2(revlock, __COUNTER__)(g, #g, __FILE__, __LINE__)
217 template<
typename MutexArg>
220 #define LOCK(cs) DebugLock<decltype(cs)> PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
221 #define LOCK2(cs1, cs2) \
222 DebugLock<decltype(cs1)> criticalblock1(cs1, #cs1, __FILE__, __LINE__); \
223 DebugLock<decltype(cs2)> criticalblock2(cs2, #cs2, __FILE__, __LINE__);
224 #define TRY_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__, true)
225 #define WAIT_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__)
227 #define ENTER_CRITICAL_SECTION(cs) \
229 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
233 #define LEAVE_CRITICAL_SECTION(cs) \
247 #define WITH_LOCK(cs, code) [&] { LOCK(cs); code; }()
261 std::unique_lock<std::mutex> lock(
mutex);
268 std::lock_guard<std::mutex> lock(
mutex);
278 std::lock_guard<std::mutex> lock(
mutex);
339 operator bool()
const
Template mixin that adds -Wthread-safety locking annotations and lock order checking to a subset of t...
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
An RAII-style reverse lock.
reverse_lock(reverse_lock const &)
reverse_lock & operator=(reverse_lock const &)
reverse_lock(UniqueLock &_lock, const char *_guardname, const char *_file, int _line)
Wrapper around std::unique_lock style lock for Mutex.
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
~UniqueLock() UNLOCK_FUNCTION()
UniqueLock(Mutex &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(mutexIn)
void Enter(const char *pszName, const char *pszFile, int nLine)
UniqueLock(Mutex *pmutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
AnnotatedMixin< std::mutex > Mutex
Wrapped mutex: supports waiting but not recursive locking.
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
#define EXCLUSIVE_LOCK_FUNCTION(...)
#define UNLOCK_FUNCTION(...)