From 8edd0d31ac683378135a9839e5d4172b82f8f5b8 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Tue, 25 Jan 2022 16:10:01 +0100 Subject: refactor: reduce scope of lock `m_most_recent_block_mutex` This avoids calling the non-trivial method `CConnman::PushMessage` within the critical section. --- src/net_processing.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 349d7fd8fb..8f875e6d27 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -4759,15 +4759,16 @@ bool PeerManagerImpl::SendMessages(CNode* pto) LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__, vHeaders.front().GetHash().ToString(), pto->GetId()); - bool fGotBlockFromCache = false; + std::optional cached_cmpctblock_msg; { LOCK(m_most_recent_block_mutex); if (m_most_recent_block_hash == pBestIndex->GetBlockHash()) { - m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::CMPCTBLOCK, *m_most_recent_compact_block)); - fGotBlockFromCache = true; + cached_cmpctblock_msg = msgMaker.Make(NetMsgType::CMPCTBLOCK, *m_most_recent_compact_block); } } - if (!fGotBlockFromCache) { + if (cached_cmpctblock_msg.has_value()) { + m_connman.PushMessage(pto, std::move(cached_cmpctblock_msg.value())); + } else { CBlock block; bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams); assert(ret); -- cgit v1.2.3 From 83003ffe049a432f6fa4127e054f073127e70b90 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Mon, 2 May 2022 01:43:48 +0200 Subject: refactor: replace RecursiveMutex `m_most_recent_block_mutex` with Mutex In each of the critical sections, only the the guarded variables are accessed, without any chance that within one section another one is called. Hence, we can use an ordinary Mutex instead of RecursiveMutex. --- src/net_processing.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 8f875e6d27..166a3bebe3 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -687,7 +687,7 @@ private: // All of the following cache a recent block, and are protected by m_most_recent_block_mutex - RecursiveMutex m_most_recent_block_mutex; + Mutex m_most_recent_block_mutex; std::shared_ptr m_most_recent_block GUARDED_BY(m_most_recent_block_mutex); std::shared_ptr m_most_recent_compact_block GUARDED_BY(m_most_recent_block_mutex); uint256 m_most_recent_block_hash GUARDED_BY(m_most_recent_block_mutex); -- cgit v1.2.3