Skip to content

Commit 6b781df

Browse files
committed
Merge #8007: Minor locking improvements
f0fdda0 IsInitialBlockDownload: usually avoid locking (Kaz Wesley)
2 parents 243ac0c + f0fdda0 commit 6b781df

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/main.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "validationinterface.h"
3838
#include "versionbits.h"
3939

40+
#include <atomic>
4041
#include <sstream>
4142

4243
#include <boost/algorithm/string/replace.hpp>
@@ -1577,18 +1578,24 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
15771578
bool IsInitialBlockDownload()
15781579
{
15791580
const CChainParams& chainParams = Params();
1581+
1582+
// Once this function has returned false, it must remain false.
1583+
static std::atomic<bool> latchToFalse{false};
1584+
// Optimization: pre-test latch before taking the lock.
1585+
if (latchToFalse.load(std::memory_order_relaxed))
1586+
return false;
1587+
15801588
LOCK(cs_main);
1589+
if (latchToFalse.load(std::memory_order_relaxed))
1590+
return false;
15811591
if (fImporting || fReindex)
15821592
return true;
15831593
if (fCheckpointsEnabled && chainActive.Height() < Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints()))
15841594
return true;
1585-
static bool lockIBDState = false;
1586-
if (lockIBDState)
1587-
return false;
15881595
bool state = (chainActive.Height() < pindexBestHeader->nHeight - 24 * 6 ||
15891596
std::max(chainActive.Tip()->GetBlockTime(), pindexBestHeader->GetBlockTime()) < GetTime() - nMaxTipAge);
15901597
if (!state)
1591-
lockIBDState = true;
1598+
latchToFalse.store(true, std::memory_order_relaxed);
15921599
return state;
15931600
}
15941601

0 commit comments

Comments
 (0)