14#include <condition_variable>
53template <
typename MutexType>
54void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine, MutexType*
cs,
bool fTry =
false);
56void CheckLastCritical(
void*
cs, std::string& lockname,
const char* guardname,
const char* file,
int line);
57std::string LocksHeld();
58template <
typename MutexType>
60template <
typename MutexType>
70extern bool g_debug_lockorder_abort;
72template <
typename MutexType>
73inline void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine, MutexType*
cs,
bool fTry =
false) {}
75inline void CheckLastCritical(
void*
cs, std::string& lockname,
const char* guardname,
const char* file,
int line) {}
76template <
typename MutexType>
78template <
typename MutexType>
83#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
84#define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
90template <
typename PARENT>
110 return PARENT::try_lock();
132template <
typename Mutex,
typename Base =
typename Mutex::UniqueLock>
136 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
139 if (Base::try_lock())
return;
144 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
148 if (!Base::owns_lock()) {
151 return Base::owns_lock();
158 TryEnter(pszName, pszFile, nLine);
160 Enter(pszName, pszFile, nLine);
165 if (!pmutexIn)
return;
167 *
static_cast<Base*
>(
this) = Base(*pmutexIn, std::defer_lock);
169 TryEnter(pszName, pszFile, nLine);
171 Enter(pszName, pszFile, nLine);
176 if (Base::owns_lock())
182 return Base::owns_lock();
195 explicit reverse_lock(
UniqueLock& _lock,
const char* _guardname,
const char* _file,
int _line) : lock(_lock), file(_file), line(_line) {
204 EnterCritical(lockname.c_str(), file.c_str(), line, lock.mutex());
221#define REVERSE_LOCK(g) typename std::decay<decltype(g)>::type::reverse_lock PASTE2(revlock, __COUNTER__)(g, #g, __FILE__, __LINE__)
223template<
typename MutexArg>
226#define LOCK(cs) DebugLock<decltype(cs)> PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
227#define LOCK2(cs1, cs2) \
228 DebugLock<decltype(cs1)> criticalblock1(cs1, #cs1, __FILE__, __LINE__); \
229 DebugLock<decltype(cs2)> criticalblock2(cs2, #cs2, __FILE__, __LINE__);
230#define TRY_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__, true)
231#define WAIT_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__)
233#define ENTER_CRITICAL_SECTION(cs) \
235 EnterCritical(#cs, __FILE__, __LINE__, &cs); \
239#define LEAVE_CRITICAL_SECTION(cs) \
241 std::string lockname; \
242 CheckLastCritical((void*)(&cs), lockname, #cs, __FILE__, __LINE__); \
270#define WITH_LOCK(cs, code) [&]() -> decltype(auto) { LOCK(cs); code; }()
284 std::unique_lock<std::mutex> lock(
mutex);
291 std::lock_guard<std::mutex> lock(
mutex);
301 std::lock_guard<std::mutex> lock(
mutex);
362 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)
void AssertLockHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) EXCLUSIVE_LOCKS_REQUIRED(cs)
void EnterCritical(const char *pszName, const char *pszFile, int nLine, MutexType *cs, bool fTry=false)
void DeleteLock(void *cs)
AnnotatedMixin< std::mutex > Mutex
Wrapped mutex: supports waiting but not recursive locking.
void CheckLastCritical(void *cs, std::string &lockname, const char *guardname, const char *file, int line)
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)