Skip to content

Commit 63e9e40

Browse files
committed
test: Add LockStackEmpty()
1 parent 42b2a95 commit 63e9e40

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/sync.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,17 @@ void DeleteLock(void* cs)
264264
}
265265
}
266266

267+
bool LockStackEmpty()
268+
{
269+
LockData& lockdata = GetLockData();
270+
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
271+
const auto it = lockdata.m_lock_stacks.find(std::this_thread::get_id());
272+
if (it == lockdata.m_lock_stacks.end()) {
273+
return true;
274+
}
275+
return it->second.empty();
276+
}
277+
267278
bool g_debug_lockorder_abort = true;
268279

269280
#endif /* DEBUG_LOCKORDER */

src/sync.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ template <typename MutexType>
5656
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs) ASSERT_EXCLUSIVE_LOCK(cs);
5757
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
5858
void DeleteLock(void* cs);
59+
bool LockStackEmpty();
5960

6061
/**
6162
* Call abort() if a potential lock order deadlock bug is detected, instead of
@@ -64,13 +65,14 @@ void DeleteLock(void* cs);
6465
*/
6566
extern bool g_debug_lockorder_abort;
6667
#else
67-
void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
68-
void static inline LeaveCritical() {}
69-
void static inline CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line) {}
68+
inline void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
69+
inline void LeaveCritical() {}
70+
inline void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line) {}
7071
template <typename MutexType>
71-
void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs) ASSERT_EXCLUSIVE_LOCK(cs) {}
72-
void static inline AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
73-
void static inline DeleteLock(void* cs) {}
72+
inline void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs) ASSERT_EXCLUSIVE_LOCK(cs) {}
73+
inline void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
74+
inline void DeleteLock(void* cs) {}
75+
inline bool LockStackEmpty() { return true; }
7476
#endif
7577
#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
7678
#define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)

src/test/sync_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ void TestPotentialDeadLockDetected(MutexType& mutex1, MutexType& mutex2)
1414
{
1515
LOCK2(mutex1, mutex2);
1616
}
17+
BOOST_CHECK(LockStackEmpty());
1718
bool error_thrown = false;
1819
try {
1920
LOCK2(mutex2, mutex1);
2021
} catch (const std::logic_error& e) {
2122
BOOST_CHECK_EQUAL(e.what(), "potential deadlock detected: mutex1 -> mutex2 -> mutex1");
2223
error_thrown = true;
2324
}
25+
BOOST_CHECK(LockStackEmpty());
2426
#ifdef DEBUG_LOCKORDER
2527
BOOST_CHECK(error_thrown);
2628
#else

0 commit comments

Comments
 (0)