IsInitialBlockDownload: usually avoid locking

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.
This commit is contained in:
Kaz Wesley 2016-04-26 17:21:22 -07:00 committed by Jack Grigg
parent 29aaf13b0a
commit 02eedeeb69
No known key found for this signature in database
GPG Key ID: 665DBCD284F7DAFF
1 changed files with 11 additions and 4 deletions

View File

@ -32,6 +32,7 @@
#include "wallet/asyncrpcoperation_shieldcoinbase.h"
#include <algorithm>
#include <atomic>
#include <sstream>
#include <boost/algorithm/string/replace.hpp>
@ -1727,18 +1728,24 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
bool IsInitialBlockDownload()
{
const CChainParams& chainParams = Params();
// 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;
if (fImporting || fReindex)
return true;
if (fCheckpointsEnabled && chainActive.Height() < Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints()))
return true;
static bool lockIBDState = false;
if (lockIBDState)
return false;
bool state = (chainActive.Height() < pindexBestHeader->nHeight - 24 * 6 ||
pindexBestHeader->GetBlockTime() < GetTime() - nMaxTipAge);
if (!state)
lockIBDState = true;
latchToFalse.store(true, std::memory_order_relaxed);
return state;
}