2015-09-03 18:53:00 +02:00
|
|
|
// Copyright (c) 2015 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2020-03-06 20:47:49 +01:00
|
|
|
#include <sync.h>
|
2022-02-25 18:19:25 +01:00
|
|
|
#include <test/util/setup_common.h>
|
2015-09-03 18:53:00 +02:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(reverselock_tests, BasicTestingSetup)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(reverselock_basics)
|
|
|
|
{
|
2020-03-06 20:47:49 +01:00
|
|
|
Mutex mutex;
|
|
|
|
WAIT_LOCK(mutex, lock);
|
2015-09-03 18:53:00 +02:00
|
|
|
|
|
|
|
BOOST_CHECK(lock.owns_lock());
|
|
|
|
{
|
2020-03-06 20:47:49 +01:00
|
|
|
REVERSE_LOCK(lock);
|
2015-09-03 18:53:00 +02:00
|
|
|
BOOST_CHECK(!lock.owns_lock());
|
|
|
|
}
|
|
|
|
BOOST_CHECK(lock.owns_lock());
|
|
|
|
}
|
|
|
|
|
2020-03-06 20:47:49 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(reverselock_multiple)
|
|
|
|
{
|
|
|
|
Mutex mutex2;
|
|
|
|
Mutex mutex;
|
|
|
|
WAIT_LOCK(mutex2, lock2);
|
|
|
|
WAIT_LOCK(mutex, lock);
|
|
|
|
|
|
|
|
// Make sure undoing two locks succeeds
|
|
|
|
{
|
|
|
|
REVERSE_LOCK(lock);
|
|
|
|
BOOST_CHECK(!lock.owns_lock());
|
|
|
|
REVERSE_LOCK(lock2);
|
|
|
|
BOOST_CHECK(!lock2.owns_lock());
|
|
|
|
}
|
|
|
|
BOOST_CHECK(lock.owns_lock());
|
|
|
|
BOOST_CHECK(lock2.owns_lock());
|
|
|
|
}
|
|
|
|
|
2015-09-03 18:53:00 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(reverselock_errors)
|
|
|
|
{
|
2020-03-06 20:47:49 +01:00
|
|
|
Mutex mutex2;
|
|
|
|
Mutex mutex;
|
|
|
|
WAIT_LOCK(mutex2, lock2);
|
|
|
|
WAIT_LOCK(mutex, lock);
|
|
|
|
|
|
|
|
#ifdef DEBUG_LOCKORDER
|
2020-12-11 01:54:57 +01:00
|
|
|
bool prev = g_debug_lockorder_abort;
|
|
|
|
g_debug_lockorder_abort = false;
|
|
|
|
|
2020-03-06 20:47:49 +01:00
|
|
|
// Make sure trying to reverse lock a previous lock fails
|
2020-12-11 01:54:57 +01:00
|
|
|
BOOST_CHECK_EXCEPTION(REVERSE_LOCK(lock2), std::logic_error, HasReason("lock2 was not most recent critical section locked"));
|
2020-03-06 20:47:49 +01:00
|
|
|
BOOST_CHECK(lock2.owns_lock());
|
2020-12-11 01:54:57 +01:00
|
|
|
|
|
|
|
g_debug_lockorder_abort = prev;
|
2020-03-06 20:47:49 +01:00
|
|
|
#endif
|
2015-09-03 18:53:00 +02:00
|
|
|
|
|
|
|
// Make sure trying to reverse lock an unlocked lock fails
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
BOOST_CHECK(!lock.owns_lock());
|
|
|
|
|
|
|
|
bool failed = false;
|
|
|
|
try {
|
2020-03-06 20:47:49 +01:00
|
|
|
REVERSE_LOCK(lock);
|
2015-09-03 18:53:00 +02:00
|
|
|
} catch(...) {
|
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_CHECK(failed);
|
|
|
|
BOOST_CHECK(!lock.owns_lock());
|
|
|
|
|
2017-04-11 12:55:41 +02:00
|
|
|
// Locking the original lock after it has been taken by a reverse lock
|
|
|
|
// makes no sense. Ensure that the original lock no longer owns the lock
|
|
|
|
// after giving it to a reverse one.
|
2015-09-03 18:53:00 +02:00
|
|
|
|
|
|
|
lock.lock();
|
|
|
|
BOOST_CHECK(lock.owns_lock());
|
2017-04-11 12:55:41 +02:00
|
|
|
{
|
2020-03-06 20:47:49 +01:00
|
|
|
REVERSE_LOCK(lock);
|
2017-04-11 12:55:41 +02:00
|
|
|
BOOST_CHECK(!lock.owns_lock());
|
2015-09-03 18:53:00 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 12:55:41 +02:00
|
|
|
BOOST_CHECK(failed);
|
2015-09-03 18:53:00 +02:00
|
|
|
BOOST_CHECK(lock.owns_lock());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|