mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-24 06:30:10 +01:00
Merge bitcoin/bitcoin#32465: thread-safety: fix annotations with REVERSE_LOCK
a201a99f8cthread-safety: fix annotations with REVERSE_LOCK (Cory Fields)aeea5f0ec1thread-safety: add missing lock annotation (Cory Fields)832c57a534thread-safety: modernize thread safety macros (Cory Fields) Pull request description: This is one of several PRs to cleanup/modernize our threading primitives. While replacing the old critical section locks in the mining code with a `REVERSE_LOCK`, I noticed that our thread-safety annotations weren't hooked up to it. This PR gets `REVERSE_LOCK` working properly. Firstly it modernizes the attributes as-recommended by the [clang docs](https://clang.llvm.org/docs/ThreadSafetyAnalysis.html) (ctrl+f for `USE_LOCK_STYLE_THREAD_SAFETY_ATTRIBUTES`). There's a subtle difference between the old `unlock_function` and new `release_capability`, where our `reverse_lock` only works with the latter. I believe this is an upstream bug. I've [reported and attempted a fix here](https://github.com/llvm/llvm-project/pull/139343), but either way it makes sense to me to modernize. The second adds a missing annotation pointed out by a fixed `REVERSE_LOCK`. Because clang's thread-safety annotations aren't passed through a reference to `UniqueLock` as one may assume (see [here](https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#no-alias-analysis) for more details), `cs_main` has to be listed explicitly as a requirement. The last commit actually fixes the `reverse_lock` by making it a `SCOPED_LOCK` and using the pattern [found in a clang test](https://github.com/llvm/llvm-project/blob/main/clang/test/SemaCXX/warn-thread-safety-analysis.cpp#L3126). Though the docs don't describe how to accomplish it, the functionality was added [in this commit](6a68efc959). Due to aliasing issues (see link above), in order to work correctly, the original mutex has to be passed along with the lock, so all existing `REVERSE_LOCK`s have been updated. To ensure that the mutexes actually match, a runtime assertion is added. ACKs for top commit: fjahr: re-ACKa201a99f8cdavidgumberg: reACKa201a99f8ctheuni: Ok, done. Those last pushes can be ignored. ACKs ona201a99are still fresh. ryanofsky: Code review ACKa201a99f8c. Just dropping 0065b9673db5da2994b0b07c1d50ebfb19af39d0 and fixing incorrect `reverse_lock::lockname` initialization since last review. TheCharlatan: Re-ACKa201a99f8cTree-SHA512: 2755fae0c41021976a1a633014a86d927f104ccbc8014c01c06dae89af363f92e5bc5d4276ad6d759302ac4679fe02a543758124d48318074db1c370989af7a7
This commit is contained in:
@@ -18,7 +18,7 @@ BOOST_AUTO_TEST_CASE(reverselock_basics)
|
||||
|
||||
BOOST_CHECK(lock.owns_lock());
|
||||
{
|
||||
REVERSE_LOCK(lock);
|
||||
REVERSE_LOCK(lock, mutex);
|
||||
BOOST_CHECK(!lock.owns_lock());
|
||||
}
|
||||
BOOST_CHECK(lock.owns_lock());
|
||||
@@ -33,9 +33,9 @@ BOOST_AUTO_TEST_CASE(reverselock_multiple)
|
||||
|
||||
// Make sure undoing two locks succeeds
|
||||
{
|
||||
REVERSE_LOCK(lock);
|
||||
REVERSE_LOCK(lock, mutex);
|
||||
BOOST_CHECK(!lock.owns_lock());
|
||||
REVERSE_LOCK(lock2);
|
||||
REVERSE_LOCK(lock2, mutex2);
|
||||
BOOST_CHECK(!lock2.owns_lock());
|
||||
}
|
||||
BOOST_CHECK(lock.owns_lock());
|
||||
@@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE(reverselock_errors)
|
||||
g_debug_lockorder_abort = false;
|
||||
|
||||
// Make sure trying to reverse lock a previous lock fails
|
||||
BOOST_CHECK_EXCEPTION(REVERSE_LOCK(lock2), std::logic_error, HasReason("lock2 was not most recent critical section locked"));
|
||||
BOOST_CHECK_EXCEPTION(REVERSE_LOCK(lock2, mutex2), std::logic_error, HasReason("lock2 was not most recent critical section locked"));
|
||||
BOOST_CHECK(lock2.owns_lock());
|
||||
|
||||
g_debug_lockorder_abort = prev;
|
||||
@@ -67,7 +67,7 @@ BOOST_AUTO_TEST_CASE(reverselock_errors)
|
||||
|
||||
bool failed = false;
|
||||
try {
|
||||
REVERSE_LOCK(lock);
|
||||
REVERSE_LOCK(lock, mutex);
|
||||
} catch(...) {
|
||||
failed = true;
|
||||
}
|
||||
@@ -82,7 +82,7 @@ BOOST_AUTO_TEST_CASE(reverselock_errors)
|
||||
lock.lock();
|
||||
BOOST_CHECK(lock.owns_lock());
|
||||
{
|
||||
REVERSE_LOCK(lock);
|
||||
REVERSE_LOCK(lock, mutex);
|
||||
BOOST_CHECK(!lock.owns_lock());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user