Skip to content

Commit 0be1dc1

Browse files
author
MacroFake
committed
Merge bitcoin#24062: refactor: replace RecursiveMutex m_most_recent_block_mutex with Mutex
83003ff refactor: replace RecursiveMutex `m_most_recent_block_mutex` with Mutex (Sebastian Falbesoner) 8edd0d3 refactor: reduce scope of lock `m_most_recent_block_mutex` (Sebastian Falbesoner) Pull request description: This PR is related to bitcoin#19303 and gets rid of the RecursiveMutex `m_most_recent_block_mutex`. All of the critical sections (5 in total) only directly access the guarded elements, i.e. it is not possible that within one section another one is called, and we can use a regular Mutex: https://github.com/bitcoin/bitcoin/blob/b019cdc036343a437fd7ced85467bd95f48d84c4/src/net_processing.cpp#L1650-L1655 https://github.com/bitcoin/bitcoin/blob/b019cdc036343a437fd7ced85467bd95f48d84c4/src/net_processing.cpp#L1861-L1865 https://github.com/bitcoin/bitcoin/blob/b019cdc036343a437fd7ced85467bd95f48d84c4/src/net_processing.cpp#L3149-L3152 https://github.com/bitcoin/bitcoin/blob/b019cdc036343a437fd7ced85467bd95f48d84c4/src/net_processing.cpp#L3201-L3206 https://github.com/bitcoin/bitcoin/blob/b019cdc036343a437fd7ced85467bd95f48d84c4/src/net_processing.cpp#L4763-L4769 The scope of the last critical section is reduced in the first commit, in order to avoid calling the non-trivial method `CConnman::PushMessage` while the lock is held. ACKs for top commit: furszy: Code ACK 83003ff with a small comment. hebasto: ACK 83003ff w0xlt: ACK bitcoin@83003ff Tree-SHA512: 3df290cafd2f6c4d40afb9f14e822a77d9c1828e66f5e2233f3ac1deccc2b0a8290bc5fb8eb992f49d39e887b50bc0e9aad63e05db2d870791a8d409fb95695f
2 parents 8270740 + 83003ff commit 0be1dc1

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/net_processing.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ class PeerManagerImpl final : public PeerManager
687687

688688

689689
// All of the following cache a recent block, and are protected by m_most_recent_block_mutex
690-
RecursiveMutex m_most_recent_block_mutex;
690+
Mutex m_most_recent_block_mutex;
691691
std::shared_ptr<const CBlock> m_most_recent_block GUARDED_BY(m_most_recent_block_mutex);
692692
std::shared_ptr<const CBlockHeaderAndShortTxIDs> m_most_recent_compact_block GUARDED_BY(m_most_recent_block_mutex);
693693
uint256 m_most_recent_block_hash GUARDED_BY(m_most_recent_block_mutex);
@@ -4759,15 +4759,16 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
47594759
LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__,
47604760
vHeaders.front().GetHash().ToString(), pto->GetId());
47614761

4762-
bool fGotBlockFromCache = false;
4762+
std::optional<CSerializedNetMsg> cached_cmpctblock_msg;
47634763
{
47644764
LOCK(m_most_recent_block_mutex);
47654765
if (m_most_recent_block_hash == pBestIndex->GetBlockHash()) {
4766-
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::CMPCTBLOCK, *m_most_recent_compact_block));
4767-
fGotBlockFromCache = true;
4766+
cached_cmpctblock_msg = msgMaker.Make(NetMsgType::CMPCTBLOCK, *m_most_recent_compact_block);
47684767
}
47694768
}
4770-
if (!fGotBlockFromCache) {
4769+
if (cached_cmpctblock_msg.has_value()) {
4770+
m_connman.PushMessage(pto, std::move(cached_cmpctblock_msg.value()));
4771+
} else {
47714772
CBlock block;
47724773
bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
47734774
assert(ret);

0 commit comments

Comments
 (0)