mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-03 04:16:11 +02: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:
16
src/sync.h
16
src/sync.h
@@ -14,6 +14,7 @@
|
||||
#include <threadsafety.h> // IWYU pragma: export
|
||||
#include <util/macros.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
@@ -212,16 +213,19 @@ public:
|
||||
/**
|
||||
* An RAII-style reverse lock. Unlocks on construction and locks on destruction.
|
||||
*/
|
||||
class reverse_lock {
|
||||
class SCOPED_LOCKABLE reverse_lock {
|
||||
public:
|
||||
explicit reverse_lock(UniqueLock& _lock, const char* _guardname, const char* _file, int _line) : lock(_lock), file(_file), line(_line) {
|
||||
explicit reverse_lock(UniqueLock& _lock, const MutexType& mutex, const char* _guardname, const char* _file, int _line) UNLOCK_FUNCTION(mutex) : lock(_lock), file(_file), line(_line) {
|
||||
// Ensure that mutex passed back for thread-safety analysis is indeed the original
|
||||
assert(std::addressof(mutex) == lock.mutex());
|
||||
|
||||
CheckLastCritical((void*)lock.mutex(), lockname, _guardname, _file, _line);
|
||||
lock.unlock();
|
||||
LeaveCritical();
|
||||
lock.swap(templock);
|
||||
}
|
||||
|
||||
~reverse_lock() {
|
||||
~reverse_lock() UNLOCK_FUNCTION() {
|
||||
templock.swap(lock);
|
||||
EnterCritical(lockname.c_str(), file.c_str(), line, lock.mutex());
|
||||
lock.lock();
|
||||
@@ -240,7 +244,11 @@ public:
|
||||
friend class reverse_lock;
|
||||
};
|
||||
|
||||
#define REVERSE_LOCK(g) typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_NAME(revlock)(g, #g, __FILE__, __LINE__)
|
||||
// clang's thread-safety analyzer is unable to deal with aliases of mutexes, so
|
||||
// it is not possible to use the lock's copy of the mutex for that purpose.
|
||||
// Instead, the original mutex needs to be passed back to the reverse_lock for
|
||||
// the sake of thread-safety analysis, but it is not actually used otherwise.
|
||||
#define REVERSE_LOCK(g, cs) typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_NAME(revlock)(g, cs, #g, __FILE__, __LINE__)
|
||||
|
||||
// When locking a Mutex, require negative capability to ensure the lock
|
||||
// is not already held
|
||||
|
||||
Reference in New Issue
Block a user