11 #include <condition_variable> 49 #ifdef DEBUG_LOCKORDER 50 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false);
52 std::string LocksHeld();
53 void AssertLockHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs)
ASSERT_EXCLUSIVE_LOCK(cs);
54 void AssertLockNotHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs);
55 void DeleteLock(
void* cs);
62 extern bool g_debug_lockorder_abort;
64 void static inline EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false) {}
65 void static inline LeaveCritical() {}
66 void static inline AssertLockHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs)
ASSERT_EXCLUSIVE_LOCK(cs) {}
67 void static inline AssertLockNotHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs) {}
68 void static inline DeleteLock(
void* cs) {}
70 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) 71 #define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs) 77 template <
typename PARENT>
82 DeleteLock((
void*)
this);
97 return PARENT::try_lock();
112 #ifdef DEBUG_LOCKCONTENTION 113 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine);
117 template <
typename Mutex,
typename Base =
typename Mutex::UniqueLock>
121 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
123 EnterCritical(pszName, pszFile, nLine, (
void*)(Base::mutex()));
124 #ifdef DEBUG_LOCKCONTENTION 125 if (!Base::try_lock()) {
126 PrintLockContention(pszName, pszFile, nLine);
129 #ifdef DEBUG_LOCKCONTENTION 134 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
136 EnterCritical(pszName, pszFile, nLine, (
void*)(Base::mutex()),
true);
138 if (!Base::owns_lock())
140 return Base::owns_lock();
147 TryEnter(pszName, pszFile, nLine);
149 Enter(pszName, pszFile, nLine);
154 if (!pmutexIn)
return;
156 *
static_cast<Base*
>(
this) = Base(*pmutexIn, std::defer_lock);
158 TryEnter(pszName, pszFile, nLine);
160 Enter(pszName, pszFile, nLine);
165 if (Base::owns_lock())
171 return Base::owns_lock();
175 template<
typename MutexArg>
178 #define PASTE(x, y) x ## y 179 #define PASTE2(x, y) PASTE(x, y) 181 #define LOCK(cs) DebugLock<decltype(cs)> PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__) 182 #define LOCK2(cs1, cs2) \ 183 DebugLock<decltype(cs1)> criticalblock1(cs1, #cs1, __FILE__, __LINE__); \ 184 DebugLock<decltype(cs2)> criticalblock2(cs2, #cs2, __FILE__, __LINE__); 185 #define TRY_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__, true) 186 #define WAIT_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__) 188 #define ENTER_CRITICAL_SECTION(cs) \ 190 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \ 194 #define LEAVE_CRITICAL_SECTION(cs) \ 212 std::unique_lock<std::mutex> lock(
mutex);
219 std::lock_guard<std::mutex> lock(
mutex);
229 std::lock_guard<std::mutex> lock(
mutex);
290 operator bool()
const 296 #endif // BITCOIN_SYNC_H void MoveTo(CSemaphoreGrant &grant)
void unlock() UNLOCK_FUNCTION()
#define EXCLUSIVE_LOCK_FUNCTION(...)
void Enter(const char *pszName, const char *pszFile, int nLine)
AnnotatedMixin< std::recursive_mutex > CCriticalSection
Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the re...
RAII-style semaphore lock.
void lock() EXCLUSIVE_LOCK_FUNCTION()
#define UNLOCK_FUNCTION(...)
CSemaphoreGrant(CSemaphore &sema, bool fTry=false)
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()
std::condition_variable condition
Template mixin that adds -Wthread-safety locking annotations and lock order checking to a subset of t...
#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)
#define ASSERT_EXCLUSIVE_LOCK(...)