@@ -902,14 +902,19 @@ Threads and synchronization
902
902
- Prefer `Mutex` type to `RecursiveMutex` one.
903
903
904
904
- 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.
907
906
908
907
- In functions that are declared separately from where they are defined, the
909
908
thread safety annotations should be added exclusively to the function
910
909
declaration. Annotations on the definition could lead to false positives
911
910
(lack of compile failure) at call sites between the two.
912
911
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
+
913
918
```C++
914
919
// txmempool.h
915
920
class CTxMemPool
@@ -933,21 +938,37 @@ void CTxMemPool::UpdateTransactionsFromBlock(...)
933
938
934
939
``` C++
935
940
// validation.h
936
- class ChainstateManager
941
+ class CChainState
937
942
{
943
+ protected:
944
+ ...
945
+ Mutex m_chainstate_mutex;
946
+ ...
938
947
public:
939
948
...
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);
941
958
...
942
959
}
943
960
944
961
// validation.cpp
945
- bool ChainstateManager::ProcessNewBlock(... )
962
+ bool CChainState::PreciousBlock(BlockValidationState& state, CBlockIndex * pindex )
946
963
{
964
+ AssertLockNotHeld (m_chainstate_mutex);
947
965
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>());
951
972
}
952
973
```
953
974
0 commit comments