diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2016-06-06 15:45:25 +0200 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2016-06-06 15:45:40 +0200 |
commit | 6b781df74fc2b93bc7096b5c5a4523c4e3db303c (patch) | |
tree | 8aa2f7cc5388bf3b0870215a43079db853c44985 /src/main.cpp | |
parent | 243ac0c75b1b2a9b6ed4eaa0e430c0d5d6c472f0 (diff) | |
parent | f0fdda0181e1b05b66541bf235c6702c41664170 (diff) |
Merge #8007: Minor locking improvements
f0fdda0 IsInitialBlockDownload: usually avoid locking (Kaz Wesley)
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/main.cpp b/src/main.cpp index 527d4f570e..fc8e72a7db 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,6 +37,7 @@ #include "validationinterface.h" #include "versionbits.h" +#include <atomic> #include <sstream> #include <boost/algorithm/string/replace.hpp> @@ -1577,18 +1578,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 || std::max(chainActive.Tip()->GetBlockTime(), pindexBestHeader->GetBlockTime()) < GetTime() - nMaxTipAge); if (!state) - lockIBDState = true; + latchToFalse.store(true, std::memory_order_relaxed); return state; } |