From e286250ce49309bfa931b622fabfc37100246266 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 25 Nov 2016 18:03:12 -0800 Subject: [PATCH 1/5] Make fDisconnect an std::atomic --- src/net.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From e78376b4fe8f38d3f74f1185ae1d360cc1ace964 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 25 Nov 2016 21:52:44 -0800 Subject: [PATCH 2/5] Make fImporting an std::atomic --- src/main.cpp | 2 +- src/main.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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; From 5123af41d6e0c54446cc45beb39ca67015d45662 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 25 Nov 2016 18:11:25 -0800 Subject: [PATCH 3/5] Fix AddrMan locking --- src/addrman.h | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/addrman.h b/src/addrman.h index 49c589ac9..b492a5801 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; From 6da9ecda034e6890510eadb93a89bc9849504847 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 25 Nov 2016 18:13:48 -0800 Subject: [PATCH 4/5] Remove double brackets in addrman --- src/addrman.h | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/addrman.h b/src/addrman.h index b492a5801..6a7c1634a 100644 --- a/src/addrman.h +++ b/src/addrman.h @@ -517,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(); } /** @@ -567,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(); } }; From 8982265456e75efb69f74d9578a7ed21cc8d2660 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 25 Nov 2016 19:29:55 -0800 Subject: [PATCH 5/5] Fix unlocked access to vNodes.size() --- src/net.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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); }