diff --git a/src/blockgeneration/miner.cpp b/src/blockgeneration/miner.cpp index 51ee594d..be138b3a 100644 --- a/src/blockgeneration/miner.cpp +++ b/src/blockgeneration/miner.cpp @@ -150,7 +150,7 @@ std::unique_ptr CreateNewPoWBlock(CWallet *pwallet, const CScrip // Collect memory pool transactions into the block { LOCK(cs_main); - READLOCK(mempool.cs); + READLOCK(mempool.cs_txmempool); CBlockIndex *_pindexPrev = pnetMan->getChainActive()->chainActive.Tip(); const int nHeight = _pindexPrev->nHeight + 1; pblock->nTime = GetAdjustedTime(); @@ -518,9 +518,9 @@ void EccMiner(CWallet *pwallet) nHashCounter += nHashesDone; if (GetTimeMillis() - nHPSTimerStart > 4000) { - static CCriticalSection cs; + static CCriticalSection cs_miner; { - LOCK(cs); + LOCK(cs_miner); if (GetTimeMillis() - nHPSTimerStart > 4000) { dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart); diff --git a/src/blockgeneration/minter.cpp b/src/blockgeneration/minter.cpp index 81dd2ba4..3dbaf4ae 100644 --- a/src/blockgeneration/minter.cpp +++ b/src/blockgeneration/minter.cpp @@ -145,7 +145,7 @@ std::unique_ptr CreateNewPoSBlock(CWallet *pwallet, const CScrip // Collect memory pool transactions into the block { LOCK(cs_main); - READLOCK(mempool.cs); + READLOCK(mempool.cs_txmempool); CBlockIndex *_pindexPrev = pnetMan->getChainActive()->chainActive.Tip(); const int nHeight = _pindexPrev->nHeight + 1; pblock->nTime = GetAdjustedTime(); diff --git a/src/httpserver.cpp b/src/httpserver.cpp index dd6c2ab4..c39a81a8 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -69,7 +69,7 @@ class WorkQueue { private: /** Mutex protects entire object */ - CWaitableCriticalSection cs; + CWaitableCriticalSection cs_workqueue; CConditionVariable cond; /* XXX in C++11 we can use std::unique_ptr here and avoid manual cleanup */ std::deque queue; @@ -84,12 +84,12 @@ class WorkQueue WorkQueue &wq; ThreadCounter(WorkQueue &w) : wq(w) { - std::lock_guard lock(wq.cs); + std::lock_guard lock(wq.cs_workqueue); wq.numThreads += 1; } ~ThreadCounter() { - std::lock_guard lock(wq.cs); + std::lock_guard lock(wq.cs_workqueue); wq.numThreads -= 1; wq.cond.notify_all(); } @@ -111,7 +111,7 @@ class WorkQueue /** Enqueue a work item */ bool Enqueue(WorkItem *item) { - std::unique_lock lock(cs); + std::unique_lock lock(cs_workqueue); if (queue.size() >= maxDepth) { return false; @@ -128,7 +128,7 @@ class WorkQueue { WorkItem *i = 0; { - std::unique_lock lock(cs); + std::unique_lock lock(cs_workqueue); while (running && queue.empty()) cond.wait(lock); if (!running) @@ -143,14 +143,14 @@ class WorkQueue /** Interrupt and exit loops */ void Interrupt() { - std::unique_lock lock(cs); + std::unique_lock lock(cs_workqueue); running = false; cond.notify_all(); } /** Wait for worker threads to exit */ void WaitExit() { - std::unique_lock lock(cs); + std::unique_lock lock(cs_workqueue); while (numThreads > 0) cond.wait(lock); } @@ -158,7 +158,7 @@ class WorkQueue /** Return current depth of queue */ size_t Depth() { - std::unique_lock lock(cs); + std::unique_lock lock(cs_workqueue); return queue.size(); } }; diff --git a/src/main.cpp b/src/main.cpp index 11c17cba..6c62239e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -191,7 +191,7 @@ bool TestLockPointValidity(const LockPoints *lp) bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints *lp, bool useExistingLockPoints) { AssertLockHeld(cs_main); - AssertLockHeld(mempool.cs); + AssertLockHeld(mempool.cs_txmempool); CBlockIndex *tip = pnetMan->getChainActive()->chainActive.Tip(); CBlockIndex index; @@ -341,7 +341,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool &pool, // Check for conflicts with in-memory transactions { - READLOCK(pool.cs); // protect pool.mapNextTx + READLOCK(pool.cs_txmempool); // protect pool.mapNextTx for (auto const &txin : tx.vin) { auto itConflicting = pool.mapNextTx.find(txin.prevout); @@ -360,7 +360,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool &pool, CAmount nValueIn = 0; LockPoints lp; { - WRITELOCK(pool.cs); + WRITELOCK(pool.cs_txmempool); CCoinsViewMemPool viewMemPool(pcoinsTip.get(), pool); view.SetBackend(viewMemPool); @@ -406,7 +406,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool &pool, // Only accept BIP68 sequence locked transactions that can be mined in the next // block; we don't want our mempool filled up with transactions that can't // be mined yet. - // Must keep pool.cs for this unless we change CheckSequenceLocks to take a + // Must keep pool.cs_txmempool for this unless we change CheckSequenceLocks to take a // CoinsViewCache instead of create its own if (!CheckSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, &lp)) { @@ -595,7 +595,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool &pool, __func__, hash.ToString(), FormatStateMessage(state)); } { - WRITELOCK(pool.cs); + WRITELOCK(pool.cs_txmempool); if (!pool._CalculateMemPoolAncestors(entry, setAncestors, nLimitAncestors, nLimitAncestorSize, nLimitDescendants, nLimitDescendantSize, errString)) { @@ -604,7 +604,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool &pool, } { - WRITELOCK(pool.cs); + WRITELOCK(pool.cs_txmempool); // Store transaction in memory pool.addUnchecked(hash, entry, setAncestors, !pnetMan->getChainActive()->IsInitialBlockDownload()); } diff --git a/src/net/addrman.h b/src/net/addrman.h index ae4c0705..5ffcbd6c 100644 --- a/src/net/addrman.h +++ b/src/net/addrman.h @@ -189,7 +189,7 @@ class CAddrMan { private: //! critical section to protect the inner data structures - mutable CCriticalSection cs; + mutable CCriticalSection cs_addrman; //! last used nId int nIdCount; @@ -311,7 +311,7 @@ class CAddrMan template void Serialize(Stream &s) const { - LOCK(cs); + LOCK(cs_addrman); uint8_t nVersion = 1; s << nVersion; @@ -371,7 +371,7 @@ class CAddrMan template void Unserialize(Stream &s) { - LOCK(cs); + LOCK(cs_addrman); Clear(); @@ -530,7 +530,7 @@ class CAddrMan size_t size() const { // TODO: Cache this in an atomic to avoid this overhead - LOCK(cs); + LOCK(cs_addrman); return vRandom.size(); } @@ -539,7 +539,7 @@ class CAddrMan { #ifdef DEBUG_ADDRMAN { - LOCK(cs); + LOCK(cs_addrman); int err; if ((err = Check_())) LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err); @@ -550,7 +550,7 @@ class CAddrMan //! Add a single address. bool Add(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty = 0) { - LOCK(cs); + LOCK(cs_addrman); bool fRet = false; Check(); fRet |= Add_(addr, source, nTimePenalty); @@ -564,7 +564,7 @@ class CAddrMan //! Add multiple addresses. bool Add(const std::vector &vAddr, const CNetAddr &source, int64_t nTimePenalty = 0) { - LOCK(cs); + LOCK(cs_addrman); int nAdd = 0; Check(); for (std::vector::const_iterator it = vAddr.begin(); it != vAddr.end(); it++) @@ -579,7 +579,7 @@ class CAddrMan //! Mark an entry as accessible. void Good(const CService &addr, int64_t nTime = GetAdjustedTime()) { - LOCK(cs); + LOCK(cs_addrman); Check(); Good_(addr, nTime); Check(); @@ -588,7 +588,7 @@ class CAddrMan //! Mark an entry as connection attempted to. void Attempt(const CService &addr, bool fCountFailure, int64_t nTime = GetAdjustedTime()) { - LOCK(cs); + LOCK(cs_addrman); Check(); Attempt_(addr, fCountFailure, nTime); Check(); @@ -601,7 +601,7 @@ class CAddrMan { CAddrInfo addrRet; { - LOCK(cs); + LOCK(cs_addrman); Check(); addrRet = Select_(newOnly); Check(); @@ -615,7 +615,7 @@ class CAddrMan Check(); std::vector vAddr; { - LOCK(cs); + LOCK(cs_addrman); GetAddr_(vAddr); } Check(); @@ -625,7 +625,7 @@ class CAddrMan //! Mark an entry as currently-connected-to. void Connected(const CService &addr, int64_t nTime = GetAdjustedTime()) { - LOCK(cs); + LOCK(cs_addrman); Check(); Connected_(addr, nTime); Check(); @@ -633,7 +633,7 @@ class CAddrMan void SetServices(const CService &addr, ServiceFlags nServices) { - LOCK(cs); + LOCK(cs_addrman); Check(); SetServices_(addr, nServices); Check(); diff --git a/src/net/nodestate.cpp b/src/net/nodestate.cpp index 792a7100..6c2c7f3c 100644 --- a/src/net/nodestate.cpp +++ b/src/net/nodestate.cpp @@ -20,13 +20,13 @@ CNodeState *CNodesStateManager::_GetNodeState(const NodeId id) void CNodesStateManager::InitializeNodeState(const CNode *pnode) { - LOCK(cs); + LOCK(cs_nodestateman); mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, std::forward_as_tuple(pnode->GetId()), std::forward_as_tuple(pnode->addr, pnode->GetAddrName())); } void CNodesStateManager::RemoveNodeState(const NodeId id) { - LOCK(cs); + LOCK(cs_nodestateman); mapNodeState.erase(id); } diff --git a/src/net/nodestate.h b/src/net/nodestate.h index a093f48a..526f1379 100644 --- a/src/net/nodestate.h +++ b/src/net/nodestate.h @@ -91,7 +91,7 @@ struct CNodeState class CNodesStateManager { protected: - CCriticalSection cs; + CCriticalSection cs_nodestateman; std::map mapNodeState; friend class CNodeStateAccessor; @@ -107,14 +107,14 @@ class CNodesStateManager /** Clear the entire nodestate map */ void Clear() { - LOCK(cs); + LOCK(cs_nodestateman); mapNodeState.clear(); } /** Is mapNodestate empty */ bool Empty() { - LOCK(cs); + LOCK(cs_nodestateman); return mapNodeState.empty(); } }; @@ -132,7 +132,7 @@ class CNodeStateAccessor } CNodeStateAccessor(CNodesStateManager &ns, const NodeId id) { - cs = &ns.cs; + cs = &ns.cs_nodestateman; EnterCritical("CNodeStateAccessor.cs", __FILE__, __LINE__, (void *)(&cs), LockType::RECURSIVE_MUTEX, OwnershipType::EXCLUSIVE); obj = ns._GetNodeState(id); diff --git a/src/rpc/rpcblockchain.cpp b/src/rpc/rpcblockchain.cpp index 7f4b6520..a388e32b 100644 --- a/src/rpc/rpcblockchain.cpp +++ b/src/rpc/rpcblockchain.cpp @@ -188,7 +188,7 @@ UniValue mempoolToJSON(bool fVerbose = false) { if (fVerbose) { - READLOCK(mempool.cs); + READLOCK(mempool.cs_txmempool); UniValue o(UniValue::VOBJ); for (auto const &e : mempool.mapTx) { @@ -584,7 +584,7 @@ UniValue gettxout(const UniValue ¶ms, bool fHelp) Coin coin; if (fMempool) { - READLOCK(mempool.cs); + READLOCK(mempool.cs_txmempool); CCoinsViewMemPool view(pcoinsTip.get(), mempool); if (!view.GetCoin(out, coin) || mempool.isSpent(out)) { diff --git a/src/rpc/rpcrawtransaction.cpp b/src/rpc/rpcrawtransaction.cpp index aebfd840..e4c69725 100644 --- a/src/rpc/rpcrawtransaction.cpp +++ b/src/rpc/rpcrawtransaction.cpp @@ -670,7 +670,7 @@ UniValue signrawtransaction(const UniValue ¶ms, bool fHelp) CCoinsView viewDummy; CCoinsViewCache view(&viewDummy); { - READLOCK(mempool.cs); + READLOCK(mempool.cs_txmempool); CCoinsViewCache &viewChain = *pcoinsTip; CCoinsViewMemPool viewMempool(&viewChain, mempool); view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view diff --git a/src/sync.h b/src/sync.h index b25aa3e9..be9f4735 100644 --- a/src/sync.h +++ b/src/sync.h @@ -298,6 +298,10 @@ class SCOPED_LOCKABLE CMutexLock bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, std::defer_lock) { + assert(pszName != nullptr); + // we no longer allow naming critical sections cs, please name it something more meaningful + assert(std::string(pszName) != "cs"); + if (fTry) TryEnter(pszName, pszFile, nLine, type); else @@ -314,6 +318,9 @@ class SCOPED_LOCKABLE CMutexLock if (!pmutexIn) return; + assert(pszName != nullptr); + // we no longer allow naming critical sections cs, please name it something more meaningful + assert(std::string(pszName) != "cs"); lock = std::unique_lock(*pmutexIn, std::defer_lock); if (fTry) TryEnter(pszName, pszFile, nLine, type); @@ -402,6 +409,10 @@ class SCOPED_LOCKABLE CMutexReadLock bool fTry = false) SHARED_LOCK_FUNCTION(mutexIn) : lock(mutexIn, std::defer_lock) { + assert(pszName != nullptr); + // we no longer allow naming critical sections cs, please name it something more meaningful + assert(std::string(pszName) != "cs"); + if (fTry) TryEnter(pszName, pszFile, nLine, type); else @@ -418,6 +429,9 @@ class SCOPED_LOCKABLE CMutexReadLock if (!pmutexIn) return; + assert(pszName != nullptr); + // we no longer allow naming critical sections cs, please name it something more meaningful + assert(std::string(pszName) != "cs"); lock = boost::shared_lock(*pmutexIn, boost::defer_lock); if (fTry) TryEnter(pszName, pszFile, nLine, type); diff --git a/src/test/mempool_tests.cpp b/src/test/mempool_tests.cpp index c5b68cbd..ac0545d8 100644 --- a/src/test/mempool_tests.cpp +++ b/src/test/mempool_tests.cpp @@ -211,7 +211,7 @@ BOOST_AUTO_TEST_CASE(MempoolIndexingTest) std::vector snapshotOrder; { CTxMemPool::setEntries setAncestorsCalculated; - WRITELOCK(pool.cs); + WRITELOCK(pool.cs_txmempool); BOOST_CHECK_EQUAL(pool._CalculateMemPoolAncestors(entry.Fee(2000000LL).FromTx(tx7), setAncestorsCalculated, 100, 1000000, 1000, 1000000, dummy), true); diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp index fae8a0ea..8f232769 100644 --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -59,7 +59,7 @@ CBlockIndex CreateBlockIndex(int nHeight) bool TestSequenceLocks(const CTransaction &tx, int flags) { - READLOCK(mempool.cs); + READLOCK(mempool.cs_txmempool); return CheckSequenceLocks(tx, flags); } diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 6a7ce1e9..cebf024c 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -24,11 +24,11 @@ BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(util_criticalsection) { - CCriticalSection cs; + CCriticalSection cs_utiltest; do { - LOCK(cs); + LOCK(cs_utiltest); break; BOOST_ERROR("break was swallowed!"); @@ -36,7 +36,7 @@ BOOST_AUTO_TEST_CASE(util_criticalsection) do { - TRY_LOCK(cs, lockTest); + TRY_LOCK(cs_utiltest, lockTest); if (lockTest) break; @@ -60,11 +60,11 @@ void ThreadSharedCritTest(CSharedCriticalSection *cs) BOOST_AUTO_TEST_CASE(util_sharedcriticalsection) { - CSharedCriticalSection cs; + CSharedCriticalSection cs_shared_util_test; do { - READLOCK(cs); + READLOCK(cs_shared_util_test); break; BOOST_ERROR("break was swallowed!"); @@ -72,7 +72,7 @@ BOOST_AUTO_TEST_CASE(util_sharedcriticalsection) do { - WRITELOCK(cs); + WRITELOCK(cs_shared_util_test); break; BOOST_ERROR("break was swallowed!"); @@ -80,8 +80,8 @@ BOOST_AUTO_TEST_CASE(util_sharedcriticalsection) { // If the read lock does not allow simultaneous locking, this code will hang in the join_all thread_group thrds; - READLOCK(cs); - thrds.create_thread(boost::bind(ThreadSharedCritTest, &cs)); + READLOCK(cs_shared_util_test); + thrds.create_thread(boost::bind(ThreadSharedCritTest, &cs_shared_util_test)); thrds.join_all(); } @@ -92,8 +92,8 @@ BOOST_AUTO_TEST_CASE(util_sharedcriticalsection) critVal = 1; thread_group thrds; { - WRITELOCK(cs); - thrds.create_thread(boost::bind(ThreadSharedCritTest, &cs)); + WRITELOCK(cs_shared_util_test); + thrds.create_thread(boost::bind(ThreadSharedCritTest, &cs_shared_util_test)); MilliSleep(250); // give thread a chance to run. BOOST_CHECK(threadStarted == true); BOOST_CHECK(threadExited == false); diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 81fc15ac..dcaac592 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -92,7 +92,7 @@ bool CTxMemPool::UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendants, const std::set &setExclude) { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); // Track the number of entries (outside setExclude) that we'd need to visit // (will bail out if it exceeds maxDescendantsToVisit) int nChildrenToVisit = 0; @@ -168,7 +168,7 @@ bool CTxMemPool::UpdateForDescendants(txiter updateIt, // add fee/size information for such descendants to the parent. void CTxMemPool::UpdateTransactionsFromBlock(const std::vector &vHashesToUpdate) { - WRITELOCK(cs); + WRITELOCK(cs_txmempool); // For each entry in vHashesToUpdate, store the set of in-mempool, but not // in-vHashesToUpdate transactions, so that we don't have to recalculate // descendants when we come across a previously seen entry. @@ -225,7 +225,7 @@ bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, std::string &errString, bool fSearchForParents /* = true */) { - READLOCK(cs); + READLOCK(cs_txmempool); setEntries setAncestors; return _CalculateMemPoolAncestors(entry, setAncestors, limitAncestorCount, limitAncestorSize, limitDescendantCount, limitDescendantSize, errString, fSearchForParents); @@ -241,7 +241,7 @@ bool CTxMemPool::_CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, std::string &errString, bool fSearchForParents /* = true */) { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); setEntries parentHashes; const CTransaction &tx = entry.GetTx(); @@ -332,7 +332,7 @@ bool CTxMemPool::ValidateMemPoolAncestors(const std::vector &txIn, uint64_t limitDescendantSize, std::string &errString) { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); setEntries parentHashes; setEntries setAncestors; int mySizeEstimate = 0; // we don't know our own tx size yet: entry.GetTxSize(); @@ -408,7 +408,7 @@ bool CTxMemPool::ValidateMemPoolAncestors(const std::vector &txIn, void CTxMemPool::_UpdateAncestorsOf(bool add, txiter it, setEntries &setAncestors) { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); setEntries parentIters = GetMemPoolParents(it); // add or remove this tx as a child of each parent BOOST_FOREACH (txiter piter, parentIters) @@ -426,7 +426,7 @@ void CTxMemPool::_UpdateAncestorsOf(bool add, txiter it, setEntries &setAncestor void CTxMemPool::UpdateChildrenForRemoval(txiter it) { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); const setEntries &setMemPoolChildren = GetMemPoolChildren(it); BOOST_FOREACH (txiter updateIt, setMemPoolChildren) { @@ -436,7 +436,7 @@ void CTxMemPool::UpdateChildrenForRemoval(txiter it) void CTxMemPool::_UpdateForRemoveFromMempool(const setEntries &entriesToRemove) { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); // For each entry, walk back all ancestors and decrement size associated with this // transaction const uint64_t nNoLimit = std::numeric_limits::max(); @@ -514,19 +514,19 @@ CTxMemPool::CTxMemPool(const CFeeRate &_minReasonableRelayFee) : nTransactionsUp CTxMemPool::~CTxMemPool() { delete minerPolicyEstimator; } bool CTxMemPool::isSpent(const COutPoint &outpoint) { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); return mapNextTx.count(outpoint); } unsigned int CTxMemPool::GetTransactionsUpdated() const { - READLOCK(cs); + READLOCK(cs_txmempool); return nTransactionsUpdated; } void CTxMemPool::AddTransactionsUpdated(unsigned int n) { - WRITELOCK(cs); + WRITELOCK(cs_txmempool); nTransactionsUpdated += n; } @@ -538,7 +538,7 @@ bool CTxMemPool::addUnchecked(const uint256 &hash, // Add to memory pool without checking anything. // Used by main.cpp AcceptToMemoryPool(), which DOES do // all the appropriate checks. - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); if (mapTx.find(hash) != mapTx.end()) // already inserted { // LogPrintf("WARNING: transaction already in mempool\n"); @@ -599,7 +599,7 @@ bool CTxMemPool::addUnchecked(const uint256 &hash, void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason) { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); NotifyEntryRemoved(it->GetSharedTx(), reason); const uint256 hash = it->GetTx().GetHash(); BOOST_FOREACH (const CTxIn &txin, it->GetTx().vin) @@ -622,7 +622,7 @@ void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason) // can save time by not iterating over those entries. void CTxMemPool::_CalculateDescendants(txiter entryit, setEntries &setDescendants) { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); setEntries stage; if (setDescendants.count(entryit) == 0) { @@ -650,13 +650,13 @@ void CTxMemPool::_CalculateDescendants(txiter entryit, setEntries &setDescendant void CTxMemPool::remove(const CTransaction &origTx, std::list &removed, bool fRecursive) { - WRITELOCK(cs); + WRITELOCK(cs_txmempool); _remove(origTx, removed, fRecursive); } void CTxMemPool::_remove(const CTransaction &origTx, std::list &removed, bool fRecursive) { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); // Remove transaction from memory pool setEntries txToRemove; txiter origit = mapTx.find(origTx.GetHash()); @@ -702,7 +702,7 @@ void CTxMemPool::_remove(const CTransaction &origTx, std::list void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags) { // Remove transactions spending a coinbase which are now immature and no-longer-final transactions - WRITELOCK(cs); + WRITELOCK(cs_txmempool); list transactionsToRemove; for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { @@ -747,13 +747,13 @@ void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMem void CTxMemPool::removeConflicts(const CTransaction &tx, std::list &removed) { - WRITELOCK(cs); + WRITELOCK(cs_txmempool); _removeConflicts(tx, removed); } void CTxMemPool::_removeConflicts(const CTransaction &tx, std::list &removed) { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); // Remove transactions which depend on inputs of tx, recursively for (const CTxIn &txin : tx.vin) { @@ -778,7 +778,7 @@ void CTxMemPool::removeForBlock(const std::vector &vtx, std::list &conflicts, bool fCurrentEstimate) { - WRITELOCK(cs); + WRITELOCK(cs_txmempool); std::vector entries; for (const auto &tx : vtx) { @@ -816,7 +816,7 @@ void CTxMemPool::_clear() void CTxMemPool::clear() { - WRITELOCK(cs); + WRITELOCK(cs_txmempool); _clear(); } @@ -831,7 +831,7 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins) const uint64_t checkTotal = 0; uint64_t innerUsage = 0; - READLOCK(cs); + READLOCK(cs_txmempool); // LogPrintf("MEMPOOL", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), // (unsigned int)mapNextTx.size()); @@ -946,14 +946,14 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins) const void CTxMemPool::queryHashes(vector &vtxid) const { - READLOCK(mempool.cs); + READLOCK(mempool.cs_txmempool); _queryHashes(vtxid); } void CTxMemPool::_queryHashes(vector &vtxid) const { vtxid.clear(); - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); vtxid.reserve(mapTx.size()); for (indexed_transaction_set::const_iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi) vtxid.push_back(mi->GetTx().GetHash()); @@ -962,7 +962,7 @@ void CTxMemPool::_queryHashes(vector &vtxid) const bool CTxMemPool::_lookup(const uint256 &hash, CTxMemPoolEntry &result) const { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); indexed_transaction_set::const_iterator i = mapTx.find(hash); if (i == mapTx.end()) return false; @@ -972,20 +972,20 @@ bool CTxMemPool::_lookup(const uint256 &hash, CTxMemPoolEntry &result) const bool CTxMemPool::lookup(const uint256 &hash, CTxMemPoolEntry &result) const { - READLOCK(cs); + READLOCK(cs_txmempool); return _lookup(hash, result); } bool CTxMemPool::lookup(const uint256 &hash, CTransaction &result) const { - READLOCK(cs); + READLOCK(cs_txmempool); return _lookup(hash, result); } bool CTxMemPool::_lookup(const uint256 &hash, CTransaction &result) const { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); indexed_transaction_set::const_iterator i = mapTx.find(hash); if (i == mapTx.end()) return false; @@ -995,7 +995,7 @@ bool CTxMemPool::_lookup(const uint256 &hash, CTransaction &result) const CFeeRate CTxMemPool::estimateFee(int nBlocks) const { - READLOCK(cs); + READLOCK(cs_txmempool); return minerPolicyEstimator->estimateFee(nBlocks); } @@ -1003,7 +1003,7 @@ bool CTxMemPool::WriteFeeEstimates(CAutoFile &fileout) const { try { - READLOCK(cs); + READLOCK(cs_txmempool); fileout << 109900; // version required to read: 0.10.99 or later fileout << CLIENT_VERSION; // version that wrote the file minerPolicyEstimator->Write(fileout); @@ -1025,7 +1025,7 @@ bool CTxMemPool::ReadFeeEstimates(CAutoFile &filein) if (nVersionRequired > CLIENT_VERSION) return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired); - WRITELOCK(cs); + WRITELOCK(cs_txmempool); minerPolicyEstimator->Read(filein); } catch (const std::exception &) @@ -1038,7 +1038,7 @@ bool CTxMemPool::ReadFeeEstimates(CAutoFile &filein) CTransactionRef CTxMemPool::_get(const uint256 &hash) const { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); indexed_transaction_set::const_iterator i = mapTx.find(hash); if (i == mapTx.end()) return nullptr; @@ -1047,7 +1047,7 @@ CTransactionRef CTxMemPool::_get(const uint256 &hash) const CTransactionRef CTxMemPool::get(const uint256 &hash) const { - READLOCK(cs); + READLOCK(cs_txmempool); return _get(hash); } @@ -1057,7 +1057,7 @@ void CTxMemPool::PrioritiseTransaction(const uint256 hash, const CAmount &nFeeDelta) { { - WRITELOCK(cs); + WRITELOCK(cs_txmempool); std::pair &deltas = mapDeltas[hash]; deltas.first += dPriorityDelta; deltas.second += nFeeDelta; @@ -1081,13 +1081,13 @@ void CTxMemPool::PrioritiseTransaction(const uint256 hash, void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta) const { - READLOCK(cs); + READLOCK(cs_txmempool); _ApplyDeltas(hash, dPriorityDelta, nFeeDelta); } void CTxMemPool::_ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta) const { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); std::map >::const_iterator pos = mapDeltas.find(hash); if (pos == mapDeltas.end()) return; @@ -1098,7 +1098,7 @@ void CTxMemPool::_ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmoun void CTxMemPool::ClearPrioritisation(const uint256 hash) { - WRITELOCK(cs); + WRITELOCK(cs_txmempool); mapDeltas.erase(hash); } void CTxMemPool::_ClearPrioritisation(const uint256 hash) { mapDeltas.erase(hash); } @@ -1143,7 +1143,7 @@ bool CCoinsViewMemPool::HaveCoin(const COutPoint &outpoint) const size_t CTxMemPool::DynamicMemoryUsage() const { - READLOCK(cs); + READLOCK(cs_txmempool); // Estimate the overhead of mapTx to be 12 pointers + an allocation, as no exact formula for // boost::multi_index_contained is implemented. return _DynamicMemoryUsage(); @@ -1151,7 +1151,7 @@ size_t CTxMemPool::DynamicMemoryUsage() const size_t CTxMemPool::_DynamicMemoryUsage() const { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); // Estimate the overhead of mapTx to be 12 pointers + an allocation, as no exact formula for // boost::multi_index_contained is implemented. return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 12 * sizeof(void *)) * mapTx.size() + @@ -1161,7 +1161,7 @@ size_t CTxMemPool::_DynamicMemoryUsage() const void CTxMemPool::_RemoveStaged(setEntries &stage) { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); _UpdateForRemoveFromMempool(stage); BOOST_FOREACH (const txiter &it, stage) { @@ -1171,7 +1171,7 @@ void CTxMemPool::_RemoveStaged(setEntries &stage) int CTxMemPool::Expire(int64_t time, std::vector &vCoinsToUncache) { - WRITELOCK(cs); + WRITELOCK(cs_txmempool); indexed_transaction_set::index::type::iterator it = mapTx.get().begin(); setEntries toremove; while (it != mapTx.get().end() && it->GetTime() < time) @@ -1193,7 +1193,7 @@ int CTxMemPool::Expire(int64_t time, std::vector &vCoinsToUncache) bool CTxMemPool::addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate) { NotifyEntryAdded(entry.GetSharedTx()); - WRITELOCK(cs); + WRITELOCK(cs_txmempool); setEntries setAncestors; uint64_t nNoLimit = std::numeric_limits::max(); std::string dummy; @@ -1204,7 +1204,7 @@ bool CTxMemPool::addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry, void CTxMemPool::_UpdateChild(txiter entry, txiter child, bool add) { setEntries s; - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); if (add && mapLinks[entry].children.insert(child).second) { cachedInnerUsage += memusage::IncrementalDynamicUsage(s); @@ -1218,7 +1218,7 @@ void CTxMemPool::_UpdateChild(txiter entry, txiter child, bool add) void CTxMemPool::_UpdateParent(txiter entry, txiter parent, bool add) { setEntries s; - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); if (add && mapLinks[entry].parents.insert(parent).second) { cachedInnerUsage += memusage::IncrementalDynamicUsage(s); @@ -1231,7 +1231,7 @@ void CTxMemPool::_UpdateParent(txiter entry, txiter parent, bool add) const CTxMemPool::setEntries &CTxMemPool::GetMemPoolParents(txiter entry) const { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); assert(entry != mapTx.end()); txlinksMap::const_iterator it = mapLinks.find(entry); assert(it != mapLinks.end()); @@ -1240,7 +1240,7 @@ const CTxMemPool::setEntries &CTxMemPool::GetMemPoolParents(txiter entry) const const CTxMemPool::setEntries &CTxMemPool::GetMemPoolChildren(txiter entry) const { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); assert(entry != mapTx.end()); txlinksMap::const_iterator it = mapLinks.find(entry); assert(it != mapLinks.end()); @@ -1249,13 +1249,13 @@ const CTxMemPool::setEntries &CTxMemPool::GetMemPoolChildren(txiter entry) const CFeeRate CTxMemPool::GetMinFee(size_t sizelimit) const { - READLOCK(cs); + READLOCK(cs_txmempool); return _GetMinFee(sizelimit); } CFeeRate CTxMemPool::_GetMinFee(size_t sizelimit) const { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); if (!blockSinceLastRollingFeeBump || rollingMinimumFeeRate == 0) return CFeeRate(rollingMinimumFeeRate); @@ -1283,7 +1283,7 @@ CFeeRate CTxMemPool::_GetMinFee(size_t sizelimit) const void CTxMemPool::trackPackageRemoved(const CFeeRate &rate) { - AssertLockHeld(cs); + AssertLockHeld(cs_txmempool); if (rate.GetFeePerK() > rollingMinimumFeeRate) { rollingMinimumFeeRate = rate.GetFeePerK(); @@ -1293,7 +1293,7 @@ void CTxMemPool::trackPackageRemoved(const CFeeRate &rate) void CTxMemPool::TrimToSize(size_t sizelimit, std::vector *pvNoSpendsRemaining) { - WRITELOCK(cs); + WRITELOCK(cs_txmempool); unsigned nTxnRemoved = 0; CFeeRate maxFeeRateRemoved(0); while (_DynamicMemoryUsage() > sizelimit) diff --git a/src/txmempool.h b/src/txmempool.h index 6ff08e32..8700de2e 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -475,7 +475,7 @@ class CTxMemPool CompareTxMemPoolEntryByScore> > > indexed_transaction_set; - mutable CSharedCriticalSection cs; + mutable CSharedCriticalSection cs_txmempool; indexed_transaction_set mapTx; typedef indexed_transaction_set::nth_index<0>::type::iterator txiter; struct CompareIteratorByHash @@ -591,7 +591,7 @@ class CTxMemPool * fSearchForParents = whether to search a tx's vin for in-mempool parents, or * look up parents from mapLinks. Must be true for entries not in the mempool * - * If you actually want the ancestor set returned, you must LOCK(this->cs) for the duration of your + * If you actually want the ancestor set returned, you must LOCK(this->cs_txmempool) for the duration of your * use of the returned setEntries. Therefore only the lockless version returns the ancestor set. */ bool CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, @@ -644,19 +644,19 @@ class CTxMemPool unsigned long size() { - READLOCK(cs); + READLOCK(cs_txmempool); return mapTx.size(); } unsigned long _size() { return mapTx.size(); } uint64_t GetTotalTxSize() { - READLOCK(cs); + READLOCK(cs_txmempool); return totalTxSize; } bool exists(uint256 hash) const { - READLOCK(cs); + READLOCK(cs_txmempool); return (mapTx.count(hash) != 0); } bool _exists(uint256 hash) const { return (mapTx.count(hash) != 0); } @@ -668,7 +668,7 @@ class CTxMemPool bool exists(const COutPoint &outpoint) const { - READLOCK(cs); + READLOCK(cs_txmempool); auto it = mapTx.find(outpoint.hash); return (it != mapTx.end() && outpoint.n < it->GetTx().vout.size()); }