Skip to content

Commit 99a8eb6

Browse files
committed
Merge #19854: Avoid locking CTxMemPool::cs recursively in simple cases
020f051 refactor: CTxMemPool::IsUnbroadcastTx() requires CTxMemPool::cs lock (Hennadii Stepanov) 7c4bd03 refactor: CTxMemPool::GetTotalTxSize() requires CTxMemPool::cs lock (Hennadii Stepanov) fa5fcb0 refactor: CTxMemPool::ClearPrioritisation() requires CTxMemPool::cs lock (Hennadii Stepanov) 7140b31 refactor: CTxMemPool::ApplyDelta() requires CTxMemPool::cs lock (Hennadii Stepanov) 66e47e5 refactor: CTxMemPool::UpdateChild() requires CTxMemPool::cs lock (Hennadii Stepanov) 9398077 refactor: CTxMemPool::UpdateParent() requires CTxMemPool::cs lock (Hennadii Stepanov) Pull request description: This is another step to transit `CTxMemPool::cs` from `RecursiveMutex` to `Mutex`. Split out from #19306. Only trivial thread safety annotations and lock assertions added. No new locks. No behavior change. Refactoring `const uint256` to `const uint256&` was [requested](bitcoin/bitcoin#19647 (comment)) by **promag**. Please note that now, since #19668 has been merged, it is safe to apply `AssertLockHeld()` macros as they do not swallow compile time Thread Safety Analysis warnings. ACKs for top commit: promag: Core review ACK 020f051. jnewbery: Code review ACK 020f051 vasild: ACK 020f051 Tree-SHA512: a31e389142d5a19b25fef0aaf1072a337278564528b5cc9209df88ae548a31440e1b8dd9bae0169fd7aa59ea06e22fe5e0413955386512b83ef1f3e7d941e890
2 parents a0a422c + 020f051 commit 99a8eb6

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/txmempool.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -852,19 +852,19 @@ void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeD
852852
LogPrintf("PrioritiseTransaction: %s feerate += %s\n", hash.ToString(), FormatMoney(nFeeDelta));
853853
}
854854

855-
void CTxMemPool::ApplyDelta(const uint256 hash, CAmount &nFeeDelta) const
855+
void CTxMemPool::ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const
856856
{
857-
LOCK(cs);
857+
AssertLockHeld(cs);
858858
std::map<uint256, CAmount>::const_iterator pos = mapDeltas.find(hash);
859859
if (pos == mapDeltas.end())
860860
return;
861861
const CAmount &delta = pos->second;
862862
nFeeDelta += delta;
863863
}
864864

865-
void CTxMemPool::ClearPrioritisation(const uint256 hash)
865+
void CTxMemPool::ClearPrioritisation(const uint256& hash)
866866
{
867-
LOCK(cs);
867+
AssertLockHeld(cs);
868868
mapDeltas.erase(hash);
869869
}
870870

@@ -968,6 +968,7 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimat
968968

969969
void CTxMemPool::UpdateChild(txiter entry, txiter child, bool add)
970970
{
971+
AssertLockHeld(cs);
971972
setEntries s;
972973
if (add && mapLinks[entry].children.insert(child).second) {
973974
cachedInnerUsage += memusage::IncrementalDynamicUsage(s);
@@ -978,6 +979,7 @@ void CTxMemPool::UpdateChild(txiter entry, txiter child, bool add)
978979

979980
void CTxMemPool::UpdateParent(txiter entry, txiter parent, bool add)
980981
{
982+
AssertLockHeld(cs);
981983
setEntries s;
982984
if (add && mapLinks[entry].parents.insert(parent).second) {
983985
cachedInnerUsage += memusage::IncrementalDynamicUsage(s);

src/txmempool.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,8 @@ class CTxMemPool
568568
typedef std::map<txiter, TxLinks, CompareIteratorByHash> txlinksMap;
569569
txlinksMap mapLinks;
570570

571-
void UpdateParent(txiter entry, txiter parent, bool add);
572-
void UpdateChild(txiter entry, txiter child, bool add);
571+
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
572+
void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
573573

574574
std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
575575

@@ -626,8 +626,8 @@ class CTxMemPool
626626

627627
/** Affect CreateNewBlock prioritisation of transactions */
628628
void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
629-
void ApplyDelta(const uint256 hash, CAmount &nFeeDelta) const;
630-
void ClearPrioritisation(const uint256 hash);
629+
void ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
630+
void ClearPrioritisation(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs);
631631

632632
/** Get the transaction in the pool that spends the same prevout */
633633
const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
@@ -710,9 +710,9 @@ class CTxMemPool
710710
return mapTx.size();
711711
}
712712

713-
uint64_t GetTotalTxSize() const
713+
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
714714
{
715-
LOCK(cs);
715+
AssertLockHeld(cs);
716716
return totalTxSize;
717717
}
718718

@@ -757,9 +757,10 @@ class CTxMemPool
757757
}
758758

759759
/** Returns whether a txid is in the unbroadcast set */
760-
bool IsUnbroadcastTx(const uint256& txid) const {
761-
LOCK(cs);
762-
return (m_unbroadcast_txids.count(txid) != 0);
760+
bool IsUnbroadcastTx(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
761+
{
762+
AssertLockHeld(cs);
763+
return m_unbroadcast_txids.count(txid) != 0;
763764
}
764765

765766
private:

0 commit comments

Comments
 (0)