12 #include <condition_variable>
50 #ifdef DEBUG_LOCKORDER
51 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void*
cs,
bool fTry =
false);
53 void CheckLastCritical(
void*
cs, std::string& lockname,
const char* guardname,
const char* file,
int line);
54 std::string LocksHeld();
55 template <
typename MutexType>
57 template <
typename MutexType>
67 extern bool g_debug_lockorder_abort;
69 inline void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void*
cs,
bool fTry =
false) {}
71 inline void CheckLastCritical(
void*
cs, std::string& lockname,
const char* guardname,
const char* file,
int line) {}
72 template <
typename MutexType>
74 template <
typename MutexType>
79 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
80 #define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
86 template <
typename PARENT>
106 return PARENT::try_lock();
127 #ifdef DEBUG_LOCKCONTENTION
128 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine);
132 template <
typename Mutex,
typename Base =
typename Mutex::UniqueLock>
136 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
138 EnterCritical(pszName, pszFile, nLine, (
void*)(Base::mutex()));
139 #ifdef DEBUG_LOCKCONTENTION
140 if (!Base::try_lock()) {
141 PrintLockContention(pszName, pszFile, nLine);
144 #ifdef DEBUG_LOCKCONTENTION
149 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
151 EnterCritical(pszName, pszFile, nLine, (
void*)(Base::mutex()),
true);
153 if (!Base::owns_lock())
155 return Base::owns_lock();
162 TryEnter(pszName, pszFile, nLine);
164 Enter(pszName, pszFile, nLine);
169 if (!pmutexIn)
return;
171 *
static_cast<Base*
>(
this) = Base(*pmutexIn, std::defer_lock);
173 TryEnter(pszName, pszFile, nLine);
175 Enter(pszName, pszFile, nLine);
180 if (Base::owns_lock())
186 return Base::owns_lock();
199 explicit reverse_lock(
UniqueLock& _lock,
const char* _guardname,
const char* _file,
int _line) : lock(_lock), file(_file), line(_line) {
208 EnterCritical(lockname.c_str(), file.c_str(), line, (
void*)lock.mutex());
225 #define REVERSE_LOCK(g) typename std::decay<decltype(g)>::type::reverse_lock PASTE2(revlock, __COUNTER__)(g, #g, __FILE__, __LINE__)
227 template<
typename MutexArg>
230 #define LOCK(cs) DebugLock<decltype(cs)> PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
231 #define LOCK2(cs1, cs2) \
232 DebugLock<decltype(cs1)> criticalblock1(cs1, #cs1, __FILE__, __LINE__); \
233 DebugLock<decltype(cs2)> criticalblock2(cs2, #cs2, __FILE__, __LINE__);
234 #define TRY_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__, true)
235 #define WAIT_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__)
237 #define ENTER_CRITICAL_SECTION(cs) \
239 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
243 #define LEAVE_CRITICAL_SECTION(cs) \
257 #define WITH_LOCK(cs, code) [&] { LOCK(cs); code; }()
271 std::unique_lock<std::mutex> lock(mutex);
272 condition.wait(lock, [&]() {
return value >= 1; });
278 std::lock_guard<std::mutex> lock(mutex);
288 std::lock_guard<std::mutex> lock(mutex);
291 condition.notify_one();
349 operator bool()
const
355 #endif // BITCOIN_SYNC_H
void MoveTo(CSemaphoreGrant &grant)
void unlock() UNLOCK_FUNCTION()
#define EXCLUSIVE_LOCK_FUNCTION(...)
reverse_lock(UniqueLock &_lock, const char *_guardname, const char *_file, int _line)
void AssertLockNotHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) EXCLUSIVE_LOCKS_REQUIRED(!cs)
void Enter(const char *pszName, const char *pszFile, int nLine)
RAII-style semaphore lock.
void lock() EXCLUSIVE_LOCK_FUNCTION()
#define UNLOCK_FUNCTION(...)
CSemaphoreGrant(CSemaphore &sema, bool fTry=false)
void CheckLastCritical(void *cs, std::string &lockname, const char *guardname, const char *file, int line)
void DeleteLock(void *cs)
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
UniqueLock(Mutex *pmutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
UniqueLock(Mutex &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(mutexIn)
~UniqueLock() UNLOCK_FUNCTION()
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, void *cs, bool fTry=false)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
std::condition_variable condition
Template mixin that adds -Wthread-safety locking annotations and lock order checking to a subset of t...
An RAII-style reverse lock.
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
Wrapper around std::unique_lock style lock for Mutex.
AnnotatedMixin< std::mutex > Mutex
Wrapped mutex: supports waiting but not recursive locking.
bool TryEnter(const char *pszName, const char *pszFile, int nLine)