Skip to content

Commit 2b4b345

Browse files
committed
Add ability to assert a lock is not held in DEBUG_LOCKORDER
1 parent 0343676 commit 2b4b345

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/sync.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine,
155155
abort();
156156
}
157157

158+
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
159+
{
160+
for (const std::pair<void*, CLockLocation>& i : *lockstack) {
161+
if (i.first == cs) {
162+
fprintf(stderr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
163+
abort();
164+
}
165+
}
166+
}
167+
158168
void DeleteLock(void* cs)
159169
{
160170
if (!lockdata.available) {

src/sync.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,17 @@ void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs
7575
void LeaveCritical();
7676
std::string LocksHeld();
7777
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
78+
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
7879
void DeleteLock(void* cs);
7980
#else
8081
void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
8182
void static inline LeaveCritical() {}
8283
void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
84+
void static inline AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
8385
void static inline DeleteLock(void* cs) {}
8486
#endif
8587
#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
88+
#define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
8689

8790
/**
8891
* Wrapped boost mutex: supports recursive locking, but no waiting

0 commit comments

Comments
 (0)