@@ -902,14 +902,19 @@ Threads and synchronization
902902- Prefer `Mutex` type to `RecursiveMutex` one.
903903
904904- Consistently use [Clang Thread Safety Analysis](https://clang.llvm.org/docs/ThreadSafetyAnalysis.html) annotations to
905- get compile-time warnings about potential race conditions in code. Combine annotations in function declarations with
906- run-time asserts in function definitions:
905+ get compile-time warnings about potential race conditions or deadlocks in code.
907906
908907 - In functions that are declared separately from where they are defined, the
909908 thread safety annotations should be added exclusively to the function
910909 declaration. Annotations on the definition could lead to false positives
911910 (lack of compile failure) at call sites between the two.
912911
912+ - Prefer locks that are in a class rather than global, and that are
913+ internal to a class (private or protected) rather than public.
914+
915+ - Combine annotations in function declarations with run-time asserts in
916+ function definitions:
917+
913918```C++
914919// txmempool.h
915920class CTxMemPool
@@ -933,21 +938,37 @@ void CTxMemPool::UpdateTransactionsFromBlock(...)
933938
934939``` C++
935940// validation.h
936- class ChainstateManager
941+ class CChainState
937942{
943+ protected:
944+ ...
945+ Mutex m_chainstate_mutex;
946+ ...
938947public:
939948 ...
940- bool ProcessNewBlock(...) LOCKS_EXCLUDED(::cs_main);
949+ bool ActivateBestChain(
950+ BlockValidationState& state,
951+ std::shared_ptr<const CBlock > pblock = nullptr)
952+ EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
953+ LOCKS_EXCLUDED(::cs_main);
954+ ...
955+ bool PreciousBlock(BlockValidationState& state, CBlockIndex* pindex)
956+ EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
957+ LOCKS_EXCLUDED(::cs_main);
941958 ...
942959}
943960
944961// validation.cpp
945- bool ChainstateManager::ProcessNewBlock(... )
962+ bool CChainState::PreciousBlock(BlockValidationState& state, CBlockIndex * pindex )
946963{
964+ AssertLockNotHeld (m_chainstate_mutex);
947965 AssertLockNotHeld(::cs_main);
948- ...
949- LOCK(::cs_main);
950- ...
966+ {
967+ LOCK(cs_main);
968+ ...
969+ }
970+
971+ return ActivateBestChain(state, std::shared_ptr<const CBlock>());
951972}
952973```
953974
0 commit comments