From 292e93a0a6a17d9dd22d5734bbb49a57d7d92695 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 6 Jan 2021 07:07:32 +0100 Subject: [PATCH] Merge #20816: net: Move RecordBytesSent() call out of cs_vSend lock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 378aedc45248cea82d9a3e6dc1038d6828008a76 [net] Add cs_vSend lock annotations (John Newbery) 673254515a2f97e53dd8c7335c836b083ba7e31a [net] Move RecordBytesSent() call out of cs_vSend lock (John Newbery) Pull request description: RecordBytesSent() does not require cs_vSend to be locked, so reduce the scope of cs_vSend. Also correctly annotate the CNode data members that are guarded by cs_vSend. This is a simpler alternative to #19673. ACKs for top commit: jnewbery: ok, reverting to commit 378aedc which has two ACKs already. Any style issues can be fixed up in future PRs. troygiorshev: ACK 378aedc45248cea82d9a3e6dc1038d6828008a76 theStack: re-ACK 378aedc45248cea82d9a3e6dc1038d6828008a76 MarcoFalke: review ACK 378aedc45248cea82d9a3e6dc1038d6828008a76 🔌 Tree-SHA512: e9cd6c472b7e1479120c1bf2d1c640cf6d18c7d589a5f9b7dfc4875e5790adaab403a7a1b945a47e79e7249a614b8583270e4549f89b22e8a9edb2e4818b0d07 --- src/net.cpp | 8 +++----- src/net.h | 8 +++++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 71843e8ded019..008f57b953630 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1879,11 +1879,9 @@ void CConnman::SocketHandler() break; } - LOCK(pnode->cs_vSend); - size_t nBytes = SocketSendData(pnode); - if (nBytes) { - RecordBytesSent(nBytes); - } + // Send data + size_t bytes_sent = WITH_LOCK(pnode->cs_vSend, return SocketSendData(pnode)); + if (bytes_sent) RecordBytesSent(bytes_sent); } ReleaseNodeVector(vErrorNodes); diff --git a/src/net.h b/src/net.h index 359c00b795a99..37ec65c801c6e 100644 --- a/src/net.h +++ b/src/net.h @@ -1030,8 +1030,10 @@ class CNode NetPermissionFlags m_permissionFlags{ PF_NONE }; std::atomic nServices{NODE_NONE}; SOCKET hSocket GUARDED_BY(cs_hSocket); - size_t nSendSize{0}; // total size of all vSendMsg entries - size_t nSendOffset{0}; // offset inside the first vSendMsg already sent + /** Total size of all vSendMsg entries */ + size_t nSendSize GUARDED_BY(cs_vSend){0}; + /** Offset inside the first vSendMsg already sent */ + size_t nSendOffset GUARDED_BY(cs_vSend){0}; uint64_t nSendBytes GUARDED_BY(cs_vSend){0}; std::list> vSendMsg GUARDED_BY(cs_vSend); std::atomic nSendMsgSize{0}; @@ -1123,7 +1125,7 @@ class CNode Network ConnectedThroughNetwork() const; protected: - mapMsgCmdSize mapSendBytesPerMsgCmd; + mapMsgCmdSize mapSendBytesPerMsgCmd GUARDED_BY(cs_vSend); mapMsgCmdSize mapRecvBytesPerMsgCmd GUARDED_BY(cs_vRecv); public: