Skip to content

Commit

Permalink
IsInitialBlockDownload: usually avoid locking
Browse files Browse the repository at this point in the history
Optimistically test the latch bool before taking the lock.
For all IsInitialBlockDownload calls after the first to return false,
this avoids the need to lock cs_main.

Coming from btc@f0fdda0181e1b05b66541bf235c6702c41664170
  • Loading branch information
furszy committed Jul 4, 2020
1 parent c178aab commit 9e378e4
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1530,17 +1530,22 @@ int64_t GetMasternodePayment()

bool IsInitialBlockDownload()
{
// Once this function has returned false, it must remain false.
static std::atomic<bool> latchToFalse{false};
// Optimization: pre-test latch before taking the lock.
if (latchToFalse.load(std::memory_order_relaxed))
return false;

LOCK(cs_main);
if (latchToFalse.load(std::memory_order_relaxed))
return false;
const int chainHeight = chainActive.Height();
if (fImporting || fReindex || fVerifyingBlocks || chainHeight < Checkpoints::GetTotalBlocksEstimate())
return true;
static bool lockIBDState = false;
if (lockIBDState)
return false;
bool state = (chainHeight < pindexBestHeader->nHeight - 24 * 6 ||
pindexBestHeader->GetBlockTime() < GetTime() - nMaxTipAge);
if (!state)
lockIBDState = true;
latchToFalse.store(true, std::memory_order_relaxed);
return state;
}

Expand Down

0 comments on commit 9e378e4

Please sign in to comment.