8 #include <boost/test/unit_test.hpp> 14 template <
typename MutexType>
15 void 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 36 #ifdef DEBUG_LOCKORDER 37 template <
typename MutexType>
38 void TestDoubleLock2(MutexType& m)
43 template <
typename MutexType>
44 void 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;
65 template <
typename MutexType>
66 void TestInconsistentLockOrderDetected(MutexType& mutex1, MutexType& mutex2)
71 #ifdef DEBUG_LOCKORDER 72 BOOST_CHECK_EXCEPTION(
REVERSE_LOCK(lock1, mutex1), std::logic_error,
HasReason(
"mutex1 was not most recent critical section locked"));
73 #endif // DEBUG_LOCKORDER 79 BOOST_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;
123 #endif // DEBUG_LOCKORDER 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;
139 #endif // DEBUG_LOCKORDER BOOST_AUTO_TEST_CASE(potential_deadlock_detected)
#define REVERSE_LOCK(g, cs)
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
#define WAIT_LOCK(cs, name)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK_NO_THROW(stmt)
#define BOOST_CHECK(expr)