Skip to content

Commit ad05e68

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#24103: Replace RecursiveMutex m_cs_chainstate with Mutex, and rename it
020acea refactor: replace RecursiveMutex m_chainstate_mutex with Mutex (w0xlt) ddeefee refactor: add negative TS annotations for `m_chainstate_mutex` (w0xlt) 1dfd31b scripted-diff: rename m_cs_chainstate -> m_chainstate_mutex (w0xlt) Pull request description: This PR is related to #19303 and gets rid of the `RecursiveMutex m_cs_chainstate`. `m_cs_chainstate` is only held in `ActivateBestChain()` and `InvalidateBlock()`. So apparently there is no recursion involved, so the `m_cs_chainstate` can be a non-recursive mutex. ACKs for top commit: hebasto: ACK 020acea, I have reviewed the code and it looks OK, I agree it can be merged. theStack: Code-review ACK 020acea 🌴 shaavan: reACK 020acea Tree-SHA512: c7c16e727e326df3410514915ce753a2a5e1da78857ef965ef683e36251e1b73c9cced4cd5231b04dbe2be0ea14084f6731b4d7a4d9a8e086e982b985e37e4b4
2 parents 5f4c07b + 020acea commit ad05e68

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/validation.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,6 +2847,8 @@ static void LimitValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main) {
28472847

28482848
bool CChainState::ActivateBestChain(BlockValidationState& state, std::shared_ptr<const CBlock> pblock)
28492849
{
2850+
AssertLockNotHeld(m_chainstate_mutex);
2851+
28502852
// Note that while we're often called here from ProcessNewBlock, this is
28512853
// far from a guarantee. Things in the P2P/RPC will often end up calling
28522854
// us in the middle of ProcessNewBlock - do not assume pblock is set
@@ -2856,8 +2858,8 @@ bool CChainState::ActivateBestChain(BlockValidationState& state, std::shared_ptr
28562858
// ABC maintains a fair degree of expensive-to-calculate internal state
28572859
// because this function periodically releases cs_main so that it does not lock up other threads for too long
28582860
// during large connects - and to allow for e.g. the callback queue to drain
2859-
// we use m_cs_chainstate to enforce mutual exclusion so that only one caller may execute this function at a time
2860-
LOCK(m_cs_chainstate);
2861+
// we use m_chainstate_mutex to enforce mutual exclusion so that only one caller may execute this function at a time
2862+
LOCK(m_chainstate_mutex);
28612863

28622864
CBlockIndex *pindexMostWork = nullptr;
28632865
CBlockIndex *pindexNewTip = nullptr;
@@ -2976,6 +2978,8 @@ bool CChainState::PreciousBlock(BlockValidationState& state, CBlockIndex* pindex
29762978

29772979
bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pindex)
29782980
{
2981+
AssertLockNotHeld(m_chainstate_mutex);
2982+
29792983
// Genesis block can't be invalidated
29802984
assert(pindex);
29812985
if (pindex->nHeight == 0) return false;
@@ -2987,7 +2991,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
29872991
// We do not allow ActivateBestChain() to run while InvalidateBlock() is
29882992
// running, as that could cause the tip to change while we disconnect
29892993
// blocks.
2990-
LOCK(m_cs_chainstate);
2994+
LOCK(m_chainstate_mutex);
29912995

29922996
// We'll be acquiring and releasing cs_main below, to allow the validation
29932997
// callbacks to run. However, we should keep the block index in a

src/validation.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,11 @@ class CChainState
471471
arith_uint256 nLastPreciousChainwork = 0;
472472

473473
/**
474-
* the ChainState CriticalSection
475-
* A lock that must be held when modifying this ChainState - held in ActivateBestChain()
474+
* The ChainState Mutex
475+
* A lock that must be held when modifying this ChainState - held in ActivateBestChain() and
476+
* InvalidateBlock()
476477
*/
477-
RecursiveMutex m_cs_chainstate;
478+
Mutex m_chainstate_mutex;
478479

479480
/**
480481
* Whether this chainstate is undergoing initial block download.
@@ -638,7 +639,7 @@ class CChainState
638639
*/
639640
bool ActivateBestChain(
640641
BlockValidationState& state,
641-
std::shared_ptr<const CBlock> pblock = nullptr) LOCKS_EXCLUDED(cs_main);
642+
std::shared_ptr<const CBlock> pblock = nullptr) LOCKS_EXCLUDED(m_chainstate_mutex, cs_main);
642643

643644
bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, BlockValidationState& state, CBlockIndex** ppindex, bool fRequested, const FlatFilePos* dbp, bool* fNewBlock) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
644645

@@ -658,7 +659,7 @@ class CChainState
658659
*/
659660
bool PreciousBlock(BlockValidationState& state, CBlockIndex* pindex) LOCKS_EXCLUDED(cs_main);
660661
/** Mark a block as invalid. */
661-
bool InvalidateBlock(BlockValidationState& state, CBlockIndex* pindex) LOCKS_EXCLUDED(cs_main);
662+
bool InvalidateBlock(BlockValidationState& state, CBlockIndex* pindex) LOCKS_EXCLUDED(m_chainstate_mutex, cs_main);
662663
/** Remove invalidity status from a block and its descendants. */
663664
void ResetBlockFailureFlags(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
664665

0 commit comments

Comments
 (0)