Auto merge of #4967 - LarryRuane:upstream-lockfixes, r=nuttycom

cherry-pick upstream locking fixes

Cherry-pick https://github.com/bitcoin/bitcoin/pull/9225
This commit is contained in:
Homu 2021-01-28 14:13:34 +00:00
commit b9504dbb3f
5 changed files with 32 additions and 36 deletions

View File

@ -470,6 +470,7 @@ public:
//! Return the number of (unique) addresses in all tables. //! Return the number of (unique) addresses in all tables.
size_t size() const size_t size() const
{ {
LOCK(cs); // TODO: Cache this in an atomic to avoid this overhead
return vRandom.size(); return vRandom.size();
} }
@ -488,14 +489,12 @@ public:
//! Add a single address. //! Add a single address.
bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0) bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0)
{
bool fRet = false;
{ {
LOCK(cs); LOCK(cs);
bool fRet = false;
Check(); Check();
fRet |= Add_(addr, source, nTimePenalty); fRet |= Add_(addr, source, nTimePenalty);
Check(); Check();
}
if (fRet) if (fRet)
LogPrint("addrman", "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew); LogPrint("addrman", "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew);
return fRet; return fRet;
@ -503,15 +502,13 @@ public:
//! Add multiple addresses. //! Add multiple addresses.
bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0) bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
{
int nAdd = 0;
{ {
LOCK(cs); LOCK(cs);
int nAdd = 0;
Check(); Check();
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++) for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0; nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
Check(); Check();
}
if (nAdd) if (nAdd)
LogPrint("addrman", "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew); LogPrint("addrman", "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
return nAdd > 0; return nAdd > 0;
@ -519,25 +516,21 @@ public:
//! Mark an entry as accessible. //! Mark an entry as accessible.
void Good(const CService &addr, int64_t nTime = GetTime()) void Good(const CService &addr, int64_t nTime = GetTime())
{
{ {
LOCK(cs); LOCK(cs);
Check(); Check();
Good_(addr, nTime); Good_(addr, nTime);
Check(); Check();
} }
}
//! Mark an entry as connection attempted to. //! Mark an entry as connection attempted to.
void Attempt(const CService &addr, int64_t nTime = GetTime()) void Attempt(const CService &addr, int64_t nTime = GetTime())
{
{ {
LOCK(cs); LOCK(cs);
Check(); Check();
Attempt_(addr, nTime); Attempt_(addr, nTime);
Check(); Check();
} }
}
/** /**
* Choose an address to connect to. * Choose an address to connect to.
@ -569,14 +562,12 @@ public:
//! Mark an entry as currently-connected-to. //! Mark an entry as currently-connected-to.
void Connected(const CService &addr, int64_t nTime = GetTime()) void Connected(const CService &addr, int64_t nTime = GetTime())
{
{ {
LOCK(cs); LOCK(cs);
Check(); Check();
Connected_(addr, nTime); Connected_(addr, nTime);
Check(); Check();
} }
}
}; };

View File

@ -68,7 +68,7 @@ static int64_t nTimeBestReceived = 0;
CWaitableCriticalSection csBestBlock; CWaitableCriticalSection csBestBlock;
CConditionVariable cvBlockChange; CConditionVariable cvBlockChange;
int nScriptCheckThreads = 0; int nScriptCheckThreads = 0;
bool fImporting = false; std::atomic_bool fImporting(false);
std::atomic_bool fReindex(false); std::atomic_bool fReindex(false);
bool fTxIndex = false; bool fTxIndex = false;
bool fAddressIndex = false; // insightexplorer || lightwalletd bool fAddressIndex = false; // insightexplorer || lightwalletd

View File

@ -145,7 +145,7 @@ extern uint64_t nLastBlockSize;
extern const std::string strMessageMagic; extern const std::string strMessageMagic;
extern CWaitableCriticalSection csBestBlock; extern CWaitableCriticalSection csBestBlock;
extern CConditionVariable cvBlockChange; extern CConditionVariable cvBlockChange;
extern bool fImporting; extern std::atomic_bool fImporting;
extern std::atomic_bool fReindex; extern std::atomic_bool fReindex;
extern int nScriptCheckThreads; extern int nScriptCheckThreads;
extern bool fTxIndex; extern bool fTxIndex;

View File

@ -1042,8 +1042,13 @@ void ThreadSocketHandler()
} }
} }
} }
if(vNodes.size() != nPrevNodeCount) { size_t vNodesSize;
nPrevNodeCount = vNodes.size(); {
LOCK(cs_vNodes);
vNodesSize = vNodes.size();
}
if (vNodesSize != nPrevNodeCount) {
nPrevNodeCount = vNodesSize;
uiInterface.NotifyNumConnectionsChanged(nPrevNodeCount); uiInterface.NotifyNumConnectionsChanged(nPrevNodeCount);
} }

View File

@ -287,7 +287,7 @@ public:
bool fInbound; bool fInbound;
bool fNetworkNode; bool fNetworkNode;
bool fSuccessfullyConnected; bool fSuccessfullyConnected;
bool fDisconnect; std::atomic_bool fDisconnect;
// We use fRelayTxes for two purposes - // We use fRelayTxes for two purposes -
// a) it allows us to not relay tx invs before receiving the peer's version message // 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 // b) the peer may tell us in its version message that we should not relay tx invs