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.
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<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
{
LOCK(cs);
int nAdd = 0;
{
LOCK(cs);
Check();
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
Check();
}
Check();
for (std::vector<CAddress>::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();
}
};

View File

@ -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

View File

@ -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;

View File

@ -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);
}

View File

@ -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