2017-11-08 21:28:35 +01:00
|
|
|
// Copyright (c) 2012-2017 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <sync.h>
|
2021-10-25 17:58:37 +02:00
|
|
|
#include <test/setup_common.h>
|
2017-11-08 21:28:35 +01:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
template <typename MutexType>
|
|
|
|
void TestPotentialDeadLockDetected(MutexType& mutex1, MutexType& mutex2)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LOCK2(mutex1, mutex2);
|
|
|
|
}
|
2020-06-22 20:12:14 +02:00
|
|
|
BOOST_CHECK(LockStackEmpty());
|
2017-11-08 21:28:35 +01:00
|
|
|
bool error_thrown = false;
|
|
|
|
try {
|
|
|
|
LOCK2(mutex2, mutex1);
|
|
|
|
} catch (const std::logic_error& e) {
|
2020-06-22 17:12:26 +02:00
|
|
|
BOOST_CHECK_EQUAL(e.what(), "potential deadlock detected: mutex1 -> mutex2 -> mutex1");
|
2017-11-08 21:28:35 +01:00
|
|
|
error_thrown = true;
|
|
|
|
}
|
2020-06-22 20:12:14 +02:00
|
|
|
BOOST_CHECK(LockStackEmpty());
|
2017-11-08 21:28:35 +01:00
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
|
|
BOOST_CHECK(error_thrown);
|
|
|
|
#else
|
|
|
|
BOOST_CHECK(!error_thrown);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(sync_tests, BasicTestingSetup)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(potential_deadlock_detected)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
|
|
bool prev = g_debug_lockorder_abort;
|
|
|
|
g_debug_lockorder_abort = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CCriticalSection rmutex1, rmutex2;
|
|
|
|
TestPotentialDeadLockDetected(rmutex1, rmutex2);
|
2020-06-22 20:12:14 +02:00
|
|
|
// The second test ensures that lock tracking data have not been broken by exception.
|
|
|
|
TestPotentialDeadLockDetected(rmutex1, rmutex2);
|
2017-11-08 21:28:35 +01:00
|
|
|
|
2021-06-04 21:26:33 +02:00
|
|
|
Mutex mutex1, mutex2;
|
2017-11-08 21:28:35 +01:00
|
|
|
TestPotentialDeadLockDetected(mutex1, mutex2);
|
2020-06-22 20:12:14 +02:00
|
|
|
// The second test ensures that lock tracking data have not been broken by exception.
|
|
|
|
TestPotentialDeadLockDetected(mutex1, mutex2);
|
2017-11-08 21:28:35 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
|
|
g_debug_lockorder_abort = prev;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|