Line data Source code
1 : // Copyright (c) 2012-2020 The Bitcoin Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #include <sync.h> 6 : #include <test/util/setup_common.h> 7 : 8 : #include <boost/test/unit_test.hpp> 9 : 10 : namespace { 11 : template <typename MutexType> 12 4 : void TestPotentialDeadLockDetected(MutexType& mutex1, MutexType& mutex2) 13 : { 14 : { 15 4 : LOCK2(mutex1, mutex2); 16 4 : } 17 4 : BOOST_CHECK(LockStackEmpty()); 18 : bool error_thrown = false; 19 : try { 20 4 : LOCK2(mutex2, mutex1); 21 4 : } catch (const std::logic_error& e) { 22 0 : BOOST_CHECK_EQUAL(e.what(), "potential deadlock detected: mutex1 -> mutex2 -> mutex1"); 23 : error_thrown = true; 24 0 : } 25 4 : BOOST_CHECK(LockStackEmpty()); 26 : #ifdef DEBUG_LOCKORDER 27 : BOOST_CHECK(error_thrown); 28 : #else 29 4 : BOOST_CHECK(!error_thrown); 30 : #endif 31 4 : } 32 : } // namespace 33 : 34 89 : BOOST_FIXTURE_TEST_SUITE(sync_tests, BasicTestingSetup) 35 : 36 95 : BOOST_AUTO_TEST_CASE(potential_deadlock_detected) 37 : { 38 : #ifdef DEBUG_LOCKORDER 39 : bool prev = g_debug_lockorder_abort; 40 : g_debug_lockorder_abort = false; 41 : #endif 42 : 43 1 : RecursiveMutex rmutex1, rmutex2; 44 1 : TestPotentialDeadLockDetected(rmutex1, rmutex2); 45 : // The second test ensures that lock tracking data have not been broken by exception. 46 1 : TestPotentialDeadLockDetected(rmutex1, rmutex2); 47 : 48 1 : Mutex mutex1, mutex2; 49 1 : TestPotentialDeadLockDetected(mutex1, mutex2); 50 : // The second test ensures that lock tracking data have not been broken by exception. 51 1 : TestPotentialDeadLockDetected(mutex1, mutex2); 52 : 53 : #ifdef DEBUG_LOCKORDER 54 : g_debug_lockorder_abort = prev; 55 : #endif 56 1 : } 57 : 58 89 : BOOST_AUTO_TEST_SUITE_END()