Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
reverselock_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2015-present 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/common.h>
7
8#include <boost/test/unit_test.hpp>
9
10#include <stdexcept>
11
13
15{
16 Mutex mutex;
17 WAIT_LOCK(mutex, lock);
18
19 BOOST_CHECK(lock.owns_lock());
20 AssertLockHeld(mutex);
21 {
22 REVERSE_LOCK(lock, mutex);
23 AssertLockNotHeld(mutex);
24 BOOST_CHECK(!lock.owns_lock());
25 }
26 BOOST_CHECK(lock.owns_lock());
27}
28
30{
32 Mutex mutex;
34 WAIT_LOCK(mutex, lock);
35
36 // Make sure undoing two locks succeeds
37 {
38 REVERSE_LOCK(lock, mutex);
39 BOOST_CHECK(!lock.owns_lock());
41 BOOST_CHECK(!lock2.owns_lock());
42 }
43 BOOST_CHECK(lock.owns_lock());
44 BOOST_CHECK(lock2.owns_lock());
45}
46
48{
50 Mutex mutex;
52 WAIT_LOCK(mutex, lock);
53
54#ifdef DEBUG_LOCKORDER
57
58 // Make sure trying to reverse lock a previous lock fails
59 BOOST_CHECK_EXCEPTION(REVERSE_LOCK(lock2, mutex2), std::logic_error, HasReason{"mutex2 was not most recent critical section locked"});
60 BOOST_CHECK(lock2.owns_lock());
61
63#endif
64
65 // Make sure trying to reverse lock an unlocked lock fails
66 lock.unlock();
67
68 BOOST_CHECK(!lock.owns_lock());
69
70 BOOST_CHECK_EXCEPTION(REVERSE_LOCK(lock, mutex), std::system_error,
71 [](const std::system_error& e) { return e.code() == std::errc::operation_not_permitted; });
72
73 BOOST_CHECK(!lock.owns_lock());
74
75 // Locking the original lock after it has been taken by a reverse lock
76 // makes no sense. Ensure that the original lock no longer owns the lock
77 // after giving it to a reverse one.
78
79 lock.lock();
80 BOOST_CHECK(lock.owns_lock());
81 {
82 REVERSE_LOCK(lock, mutex);
83 BOOST_CHECK(!lock.owns_lock());
84 }
85
86 BOOST_CHECK(lock.owns_lock());
87}
88
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition common.h:38
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EXCEPTION(HTTPHeaders{}.Read(reader), std::runtime_error, HasReason{"Empty HTTP header name"})
#define BOOST_CHECK(expr)
Definition object.cpp:16
BOOST_AUTO_TEST_CASE(reverselock_basics)
#define WAIT_LOCK(cs, name)
Definition sync.h:274
#define AssertLockNotHeld(cs)
Definition sync.h:149
#define REVERSE_LOCK(g, cs)
Definition sync.h:254
#define AssertLockHeld(cs)
Definition sync.h:144
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82