mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 20:42:59 +01:00
50daf4dce9
9f85e9cb3d scripted-diff: Rename LockAnnotation to LockAssertion (practicalswift) de9b5dbca3 Make sure the compile-time locking promises given via LockAnnotation:s hold also in practice at runtime (ifdef DEBUG_LOCKORDER) (practicalswift) 3a809446b3 Move LockAnnotation to make it reflect the truth (practicalswift) cc2588579c Move LockAnnotation from threadsafety.h (imported code) to sync.h (our code) (practicalswift) Pull request description: `LockAnnotation lock(mutex);` is a guarantee to the compiler thread-analysis that `mutex` is locked (when it couldn't be determined otherwise). Before this PR it was possible to make the mistake of adding a `LockAnnotation` where the correct mutex is _not_ held. This in turn makes the thread-analysis reasoning being based on incorrect premises. This PR adds an assertion in the `LockAnnotation` ctor which checks that the guarantees given by us at compile-time are held also in practice (`ifdef DEBUG_LOCKORDER`). Issues like the one described in #16028 will be discovered immediately with this PR merged. Changes in this PR: * Move `LockAnnotation` from `threadsafety.h` (imported code) to `sync.h` (our code) * Move `LockAnnotation` in `wallet_tests` to make it reflect the truth * Make sure the compile-time locking promises given via `LockAnnotation`:s hold also in practice at runtime (`ifdef DEBUG_LOCKORDER`) * Rename `LockAnnotation` to `LockAssertion` ACKs for commit 9f85e9: ryanofsky: utACK 9f85e9cb3d687862128ddf464d2bc2462b8627f0. No changes at all since last review except clean rebase after base PR #16033 was merged Tree-SHA512: fb80e78fe362adfd6ea8405bcb142c09b99f834fe8be4397282b223ca2c3a2bb9719a074a47a043b44757f840b239a6fcd2f98d14771f8729204834ecf608c3a
78 lines
3.3 KiB
C++
78 lines
3.3 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#ifndef BITCOIN_THREADSAFETY_H
|
|
#define BITCOIN_THREADSAFETY_H
|
|
|
|
#include <mutex>
|
|
|
|
#ifdef __clang__
|
|
// TL;DR Add GUARDED_BY(mutex) to member variables. The others are
|
|
// rarely necessary. Ex: int nFoo GUARDED_BY(cs_foo);
|
|
//
|
|
// See https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
|
|
// for documentation. The clang compiler can do advanced static analysis
|
|
// of locking when given the -Wthread-safety option.
|
|
#define LOCKABLE __attribute__((lockable))
|
|
#define SCOPED_LOCKABLE __attribute__((scoped_lockable))
|
|
#define GUARDED_BY(x) __attribute__((guarded_by(x)))
|
|
#define PT_GUARDED_BY(x) __attribute__((pt_guarded_by(x)))
|
|
#define ACQUIRED_AFTER(...) __attribute__((acquired_after(__VA_ARGS__)))
|
|
#define ACQUIRED_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__)))
|
|
#define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__((exclusive_lock_function(__VA_ARGS__)))
|
|
#define SHARED_LOCK_FUNCTION(...) __attribute__((shared_lock_function(__VA_ARGS__)))
|
|
#define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__((exclusive_trylock_function(__VA_ARGS__)))
|
|
#define SHARED_TRYLOCK_FUNCTION(...) __attribute__((shared_trylock_function(__VA_ARGS__)))
|
|
#define UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__)))
|
|
#define LOCK_RETURNED(x) __attribute__((lock_returned(x)))
|
|
#define LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__)))
|
|
#define EXCLUSIVE_LOCKS_REQUIRED(...) __attribute__((exclusive_locks_required(__VA_ARGS__)))
|
|
#define SHARED_LOCKS_REQUIRED(...) __attribute__((shared_locks_required(__VA_ARGS__)))
|
|
#define NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
|
|
#define ASSERT_EXCLUSIVE_LOCK(...) __attribute__((assert_exclusive_lock(__VA_ARGS__)))
|
|
#else
|
|
#define LOCKABLE
|
|
#define SCOPED_LOCKABLE
|
|
#define GUARDED_BY(x)
|
|
#define PT_GUARDED_BY(x)
|
|
#define ACQUIRED_AFTER(...)
|
|
#define ACQUIRED_BEFORE(...)
|
|
#define EXCLUSIVE_LOCK_FUNCTION(...)
|
|
#define SHARED_LOCK_FUNCTION(...)
|
|
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
|
|
#define SHARED_TRYLOCK_FUNCTION(...)
|
|
#define UNLOCK_FUNCTION(...)
|
|
#define LOCK_RETURNED(x)
|
|
#define LOCKS_EXCLUDED(...)
|
|
#define EXCLUSIVE_LOCKS_REQUIRED(...)
|
|
#define SHARED_LOCKS_REQUIRED(...)
|
|
#define NO_THREAD_SAFETY_ANALYSIS
|
|
#define ASSERT_EXCLUSIVE_LOCK(...)
|
|
#endif // __GNUC__
|
|
|
|
// StdMutex provides an annotated version of std::mutex for us,
|
|
// and should only be used when sync.h Mutex/LOCK/etc are not usable.
|
|
class LOCKABLE StdMutex : public std::mutex
|
|
{
|
|
public:
|
|
#ifdef __clang__
|
|
//! For negative capabilities in the Clang Thread Safety Analysis.
|
|
//! A negative requirement uses the EXCLUSIVE_LOCKS_REQUIRED attribute, in conjunction
|
|
//! with the ! operator, to indicate that a mutex should not be held.
|
|
const StdMutex& operator!() const { return *this; }
|
|
#endif // __clang__
|
|
};
|
|
|
|
// StdLockGuard provides an annotated version of std::lock_guard for us,
|
|
// and should only be used when sync.h Mutex/LOCK/etc are not usable.
|
|
class SCOPED_LOCKABLE StdLockGuard : public std::lock_guard<StdMutex>
|
|
{
|
|
public:
|
|
explicit StdLockGuard(StdMutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard<StdMutex>(cs) {}
|
|
~StdLockGuard() UNLOCK_FUNCTION() {}
|
|
};
|
|
|
|
#endif // BITCOIN_THREADSAFETY_H
|