mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 05:57:59 +01:00
Merge #19337: sync: detect double lock from the same thread
95975dd08dsync: detect double lock from the same thread (Vasil Dimov)4df6567e4csync: make EnterCritical() & push_lock() type safe (Vasil Dimov) Pull request description: Double lock of the same (non-recursive) mutex from the same thread would produce an undefined behavior. Detect this from `DEBUG_LOCKORDER` and react similarly to the deadlock detection. This came up during discussion in another, related PR: https://github.com/bitcoin/bitcoin/pull/19238#discussion_r442394521. ACKs for top commit: laanwj: code review ACK95975dd08dhebasto: re-ACK95975dd08dTree-SHA512: 375c62db7819e348bfaecc3bd82a7907fcd8f5af24f7d637ac82f3f16789da9fc127dbd0e37158a08e0dcbba01a55c6635caf1d8e9e827cf5a3747f7690a498e
This commit is contained in:
14
src/sync.h
14
src/sync.h
@@ -48,7 +48,8 @@ LEAVE_CRITICAL_SECTION(mutex); // no RAII
|
||||
///////////////////////////////
|
||||
|
||||
#ifdef DEBUG_LOCKORDER
|
||||
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
|
||||
template <typename MutexType>
|
||||
void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry = false);
|
||||
void LeaveCritical();
|
||||
void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line);
|
||||
std::string LocksHeld();
|
||||
@@ -66,7 +67,8 @@ bool LockStackEmpty();
|
||||
*/
|
||||
extern bool g_debug_lockorder_abort;
|
||||
#else
|
||||
inline void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
|
||||
template <typename MutexType>
|
||||
inline void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry = false) {}
|
||||
inline void LeaveCritical() {}
|
||||
inline void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line) {}
|
||||
template <typename MutexType>
|
||||
@@ -135,7 +137,7 @@ class SCOPED_LOCKABLE UniqueLock : public Base
|
||||
private:
|
||||
void Enter(const char* pszName, const char* pszFile, int nLine)
|
||||
{
|
||||
EnterCritical(pszName, pszFile, nLine, (void*)(Base::mutex()));
|
||||
EnterCritical(pszName, pszFile, nLine, Base::mutex());
|
||||
#ifdef DEBUG_LOCKCONTENTION
|
||||
if (!Base::try_lock()) {
|
||||
PrintLockContention(pszName, pszFile, nLine);
|
||||
@@ -148,7 +150,7 @@ private:
|
||||
|
||||
bool TryEnter(const char* pszName, const char* pszFile, int nLine)
|
||||
{
|
||||
EnterCritical(pszName, pszFile, nLine, (void*)(Base::mutex()), true);
|
||||
EnterCritical(pszName, pszFile, nLine, Base::mutex(), true);
|
||||
Base::try_lock();
|
||||
if (!Base::owns_lock())
|
||||
LeaveCritical();
|
||||
@@ -205,7 +207,7 @@ public:
|
||||
|
||||
~reverse_lock() {
|
||||
templock.swap(lock);
|
||||
EnterCritical(lockname.c_str(), file.c_str(), line, (void*)lock.mutex());
|
||||
EnterCritical(lockname.c_str(), file.c_str(), line, lock.mutex());
|
||||
lock.lock();
|
||||
}
|
||||
|
||||
@@ -236,7 +238,7 @@ using DebugLock = UniqueLock<typename std::remove_reference<typename std::remove
|
||||
|
||||
#define ENTER_CRITICAL_SECTION(cs) \
|
||||
{ \
|
||||
EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
|
||||
EnterCritical(#cs, __FILE__, __LINE__, &cs); \
|
||||
(cs).lock(); \
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user