8#include <boost/test/unit_test.hpp>
14template <
typename MutexType>
15void TestPotentialDeadLockDetected(MutexType& mutex1, MutexType& mutex2)
18 LOCK2(mutex1, mutex2);
21 bool error_thrown =
false;
23 LOCK2(mutex2, mutex1);
24 }
catch (
const std::logic_error& e) {
25 BOOST_CHECK_EQUAL(e.what(),
"potential deadlock detected: mutex1 -> mutex2 -> mutex1");
29 #ifdef DEBUG_LOCKORDER
37template <
typename MutexType>
38void TestDoubleLock2(MutexType& m)
43template <
typename MutexType>
44void TestDoubleLock(
bool should_throw)
46 const bool prev = g_debug_lockorder_abort;
47 g_debug_lockorder_abort =
false;
53 BOOST_CHECK_EXCEPTION(TestDoubleLock2(m), std::logic_error,
61 g_debug_lockorder_abort = prev;
65template <
typename MutexType>
66void TestInconsistentLockOrderDetected(MutexType& mutex1, MutexType& mutex2)
72 BOOST_CHECK_EXCEPTION(
REVERSE_LOCK(lock1, mutex1), std::logic_error,
HasReason(
"mutex1 was not most recent critical section locked"));
79BOOST_AUTO_TEST_SUITE(sync_tests)
83 #ifdef DEBUG_LOCKORDER
84 bool prev = g_debug_lockorder_abort;
85 g_debug_lockorder_abort =
false;
89 TestPotentialDeadLockDetected(rmutex1, rmutex2);
91 TestPotentialDeadLockDetected(rmutex1, rmutex2);
94 TestPotentialDeadLockDetected(mutex1, mutex2);
96 TestPotentialDeadLockDetected(mutex1, mutex2);
98 #ifdef DEBUG_LOCKORDER
99 g_debug_lockorder_abort = prev;
106#ifdef DEBUG_LOCKORDER
109 TestDoubleLock<Mutex>(
true);
114 TestDoubleLock<RecursiveMutex>(
false);
120#ifdef DEBUG_LOCKORDER
121 bool prev = g_debug_lockorder_abort;
122 g_debug_lockorder_abort =
false;
126 TestInconsistentLockOrderDetected(rmutex1, rmutex2);
129 TestInconsistentLockOrderDetected(rmutex1, rmutex2);
131 Mutex mutex1, mutex2;
132 TestInconsistentLockOrderDetected(mutex1, mutex2);
135 TestInconsistentLockOrderDetected(mutex1, mutex2);
137#ifdef DEBUG_LOCKORDER
138 g_debug_lockorder_abort = prev;
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK_NO_THROW(stmt)
#define BOOST_CHECK(expr)
#define WAIT_LOCK(cs, name)
#define REVERSE_LOCK(g, cs)
BOOST_AUTO_TEST_CASE(potential_deadlock_detected)