mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-04 17:52:25 +01:00
Merge bitcoin/bitcoin#24931: Strengthen thread safety assertions
ce893c0497doc: Update developer notes (Anthony Towns)d2852917eesync.h: Imply negative assertions when calling LOCK (Anthony Towns)bba87c0553scripted-diff: Convert global Mutexes to GlobalMutexes (Anthony Towns)a559509a0bsync.h: Add GlobalMutex type (Anthony Towns)be6aa72f9fqt/clientmodel: thread safety annotation for m_cached_tip_mutex (Anthony Towns)f24bd45b37net_processing: thread safety annotation for m_tx_relay_mutex (Anthony Towns) Pull request description: This changes `LOCK(mutex)` for non-global, non-recursive mutexes to be annotated with the negative capability for the mutex it refers to, to prevent . clang applies negative capabilities recursively, so this helps avoid forgetting to annotate functions. This can't reasonably be used for globals, because clang would require every function to be annotated with `EXCLUSIVE_LOCKS_REQUIRED(!g_mutex)` for each global mutex; so this introduces a trivial `GlobalMutex` subclass of `Mutex`, and reduces the annotations for both `GlobalMutex` to `LOCKS_EXCLUDED` which only catches trivial errors (eg (`LOCK(x); LOCK(x);`). ACKs for top commit: MarcoFalke: review ACKce893c0497🐦 hebasto: ACKce893c0497Tree-SHA512: 5c35e8c7677ce3d994a7e3774f4344adad496223a51b3a1d1d3b5f20684b2e1d5cff688eb3fbc8d33e1b9940dfa76e515f9434e21de6f3ce3c935e29a319f529
This commit is contained in:
@@ -921,14 +921,19 @@ Threads and synchronization
|
||||
- Prefer `Mutex` type to `RecursiveMutex` one.
|
||||
|
||||
- Consistently use [Clang Thread Safety Analysis](https://clang.llvm.org/docs/ThreadSafetyAnalysis.html) annotations to
|
||||
get compile-time warnings about potential race conditions in code. Combine annotations in function declarations with
|
||||
run-time asserts in function definitions:
|
||||
get compile-time warnings about potential race conditions or deadlocks in code.
|
||||
|
||||
- In functions that are declared separately from where they are defined, the
|
||||
thread safety annotations should be added exclusively to the function
|
||||
declaration. Annotations on the definition could lead to false positives
|
||||
(lack of compile failure) at call sites between the two.
|
||||
|
||||
- Prefer locks that are in a class rather than global, and that are
|
||||
internal to a class (private or protected) rather than public.
|
||||
|
||||
- Combine annotations in function declarations with run-time asserts in
|
||||
function definitions:
|
||||
|
||||
```C++
|
||||
// txmempool.h
|
||||
class CTxMemPool
|
||||
@@ -952,21 +957,37 @@ void CTxMemPool::UpdateTransactionsFromBlock(...)
|
||||
|
||||
```C++
|
||||
// validation.h
|
||||
class ChainstateManager
|
||||
class CChainState
|
||||
{
|
||||
protected:
|
||||
...
|
||||
Mutex m_chainstate_mutex;
|
||||
...
|
||||
public:
|
||||
...
|
||||
bool ProcessNewBlock(...) LOCKS_EXCLUDED(::cs_main);
|
||||
bool ActivateBestChain(
|
||||
BlockValidationState& state,
|
||||
std::shared_ptr<const CBlock> pblock = nullptr)
|
||||
EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
|
||||
LOCKS_EXCLUDED(::cs_main);
|
||||
...
|
||||
bool PreciousBlock(BlockValidationState& state, CBlockIndex* pindex)
|
||||
EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
|
||||
LOCKS_EXCLUDED(::cs_main);
|
||||
...
|
||||
}
|
||||
|
||||
// validation.cpp
|
||||
bool ChainstateManager::ProcessNewBlock(...)
|
||||
bool CChainState::PreciousBlock(BlockValidationState& state, CBlockIndex* pindex)
|
||||
{
|
||||
AssertLockNotHeld(m_chainstate_mutex);
|
||||
AssertLockNotHeld(::cs_main);
|
||||
...
|
||||
LOCK(::cs_main);
|
||||
...
|
||||
{
|
||||
LOCK(cs_main);
|
||||
...
|
||||
}
|
||||
|
||||
return ActivateBestChain(state, std::shared_ptr<const CBlock>());
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user