diff --git a/src/addrman.h b/src/addrman.h index 49c589ac9..6a7c1634a 100644 --- a/src/addrman.h +++ b/src/addrman.h @@ -470,6 +470,7 @@ public: //! Return the number of (unique) addresses in all tables. size_t size() const { + LOCK(cs); // TODO: Cache this in an atomic to avoid this overhead return vRandom.size(); } @@ -489,13 +490,11 @@ public: //! Add a single address. bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0) { + LOCK(cs); bool fRet = false; - { - LOCK(cs); - Check(); - fRet |= Add_(addr, source, nTimePenalty); - Check(); - } + Check(); + fRet |= Add_(addr, source, nTimePenalty); + Check(); if (fRet) LogPrint("addrman", "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew); return fRet; @@ -504,14 +503,12 @@ public: //! Add multiple addresses. bool Add(const std::vector &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0) { + LOCK(cs); int nAdd = 0; - { - LOCK(cs); - Check(); - for (std::vector::const_iterator it = vAddr.begin(); it != vAddr.end(); it++) - nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0; - Check(); - } + Check(); + for (std::vector::const_iterator it = vAddr.begin(); it != vAddr.end(); it++) + nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0; + Check(); if (nAdd) LogPrint("addrman", "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew); return nAdd > 0; @@ -520,23 +517,19 @@ public: //! Mark an entry as accessible. void Good(const CService &addr, int64_t nTime = GetTime()) { - { - LOCK(cs); - Check(); - Good_(addr, nTime); - Check(); - } + LOCK(cs); + Check(); + Good_(addr, nTime); + Check(); } //! Mark an entry as connection attempted to. void Attempt(const CService &addr, int64_t nTime = GetTime()) { - { - LOCK(cs); - Check(); - Attempt_(addr, nTime); - Check(); - } + LOCK(cs); + Check(); + Attempt_(addr, nTime); + Check(); } /** @@ -570,12 +563,10 @@ public: //! Mark an entry as currently-connected-to. void Connected(const CService &addr, int64_t nTime = GetTime()) { - { - LOCK(cs); - Check(); - Connected_(addr, nTime); - Check(); - } + LOCK(cs); + Check(); + Connected_(addr, nTime); + Check(); } }; diff --git a/src/main.cpp b/src/main.cpp index 1d123c446..e720f8c90 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,7 +68,7 @@ static int64_t nTimeBestReceived = 0; CWaitableCriticalSection csBestBlock; CConditionVariable cvBlockChange; int nScriptCheckThreads = 0; -bool fImporting = false; +std::atomic_bool fImporting(false); std::atomic_bool fReindex(false); bool fTxIndex = false; bool fAddressIndex = false; // insightexplorer || lightwalletd diff --git a/src/main.h b/src/main.h index bfb8cb3b5..bca161193 100644 --- a/src/main.h +++ b/src/main.h @@ -145,7 +145,7 @@ extern uint64_t nLastBlockSize; extern const std::string strMessageMagic; extern CWaitableCriticalSection csBestBlock; extern CConditionVariable cvBlockChange; -extern bool fImporting; +extern std::atomic_bool fImporting; extern std::atomic_bool fReindex; extern int nScriptCheckThreads; extern bool fTxIndex; diff --git a/src/net.cpp b/src/net.cpp index 51908b341..3b96008d3 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1042,8 +1042,13 @@ void ThreadSocketHandler() } } } - if(vNodes.size() != nPrevNodeCount) { - nPrevNodeCount = vNodes.size(); + size_t vNodesSize; + { + LOCK(cs_vNodes); + vNodesSize = vNodes.size(); + } + if (vNodesSize != nPrevNodeCount) { + nPrevNodeCount = vNodesSize; uiInterface.NotifyNumConnectionsChanged(nPrevNodeCount); } diff --git a/src/net.h b/src/net.h index bd38caffd..bb977801d 100644 --- a/src/net.h +++ b/src/net.h @@ -287,7 +287,7 @@ public: bool fInbound; bool fNetworkNode; bool fSuccessfullyConnected; - bool fDisconnect; + std::atomic_bool fDisconnect; // We use fRelayTxes for two purposes - // a) it allows us to not relay tx invs before receiving the peer's version message // b) the peer may tell us in its version message that we should not relay tx invs