Skip to content

Commit fa21068

Browse files
author
MarcoFalke
committed
net: Move SocketSendData lock annotation to header
Also, add lock annotation to SendMessages Can be reviewed with "--word-diff-regex=."
1 parent fa0a717 commit fa21068

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

src/net.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -790,30 +790,30 @@ void V1TransportSerializer::prepareForTransport(CSerializedNetMsg& msg, std::vec
790790
CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, header, 0, hdr};
791791
}
792792

793-
size_t CConnman::SocketSendData(CNode *pnode) const EXCLUSIVE_LOCKS_REQUIRED(pnode->cs_vSend)
793+
size_t CConnman::SocketSendData(CNode& node) const
794794
{
795-
auto it = pnode->vSendMsg.begin();
795+
auto it = node.vSendMsg.begin();
796796
size_t nSentSize = 0;
797797

798-
while (it != pnode->vSendMsg.end()) {
799-
const auto &data = *it;
800-
assert(data.size() > pnode->nSendOffset);
798+
while (it != node.vSendMsg.end()) {
799+
const auto& data = *it;
800+
assert(data.size() > node.nSendOffset);
801801
int nBytes = 0;
802802
{
803-
LOCK(pnode->cs_hSocket);
804-
if (pnode->hSocket == INVALID_SOCKET)
803+
LOCK(node.cs_hSocket);
804+
if (node.hSocket == INVALID_SOCKET)
805805
break;
806-
nBytes = send(pnode->hSocket, reinterpret_cast<const char*>(data.data()) + pnode->nSendOffset, data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
806+
nBytes = send(node.hSocket, reinterpret_cast<const char*>(data.data()) + node.nSendOffset, data.size() - node.nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
807807
}
808808
if (nBytes > 0) {
809-
pnode->nLastSend = GetSystemTimeInSeconds();
810-
pnode->nSendBytes += nBytes;
811-
pnode->nSendOffset += nBytes;
809+
node.nLastSend = GetSystemTimeInSeconds();
810+
node.nSendBytes += nBytes;
811+
node.nSendOffset += nBytes;
812812
nSentSize += nBytes;
813-
if (pnode->nSendOffset == data.size()) {
814-
pnode->nSendOffset = 0;
815-
pnode->nSendSize -= data.size();
816-
pnode->fPauseSend = pnode->nSendSize > nSendBufferMaxSize;
813+
if (node.nSendOffset == data.size()) {
814+
node.nSendOffset = 0;
815+
node.nSendSize -= data.size();
816+
node.fPauseSend = node.nSendSize > nSendBufferMaxSize;
817817
it++;
818818
} else {
819819
// could not send full message; stop sending more
@@ -823,22 +823,21 @@ size_t CConnman::SocketSendData(CNode *pnode) const EXCLUSIVE_LOCKS_REQUIRED(pno
823823
if (nBytes < 0) {
824824
// error
825825
int nErr = WSAGetLastError();
826-
if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
827-
{
826+
if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS) {
828827
LogPrintf("socket send error %s\n", NetworkErrorString(nErr));
829-
pnode->CloseSocketDisconnect();
828+
node.CloseSocketDisconnect();
830829
}
831830
}
832831
// couldn't send anything at all
833832
break;
834833
}
835834
}
836835

837-
if (it == pnode->vSendMsg.end()) {
838-
assert(pnode->nSendOffset == 0);
839-
assert(pnode->nSendSize == 0);
836+
if (it == node.vSendMsg.end()) {
837+
assert(node.nSendOffset == 0);
838+
assert(node.nSendSize == 0);
840839
}
841-
pnode->vSendMsg.erase(pnode->vSendMsg.begin(), it);
840+
node.vSendMsg.erase(node.vSendMsg.begin(), it);
842841
return nSentSize;
843842
}
844843

@@ -1508,7 +1507,7 @@ void CConnman::SocketHandler()
15081507

15091508
if (sendSet) {
15101509
// Send data
1511-
size_t bytes_sent = WITH_LOCK(pnode->cs_vSend, return SocketSendData(pnode));
1510+
size_t bytes_sent = WITH_LOCK(pnode->cs_vSend, return SocketSendData(*pnode));
15121511
if (bytes_sent) RecordBytesSent(bytes_sent);
15131512
}
15141513

@@ -2992,7 +2991,7 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
29922991

29932992
// If write queue empty, attempt "optimistic write"
29942993
if (optimisticSend == true)
2995-
nBytesSent = SocketSendData(pnode);
2994+
nBytesSent = SocketSendData(*pnode);
29962995
}
29972996
if (nBytesSent)
29982997
RecordBytesSent(nBytesSent);

src/net.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ class NetEventsInterface
778778
{
779779
public:
780780
virtual bool ProcessMessages(CNode* pnode, std::atomic<bool>& interrupt) = 0;
781-
virtual bool SendMessages(CNode* pnode) = 0;
781+
virtual bool SendMessages(CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(pnode->cs_sendProcessing) = 0;
782782
virtual void InitializeNode(CNode* pnode) = 0;
783783
virtual void FinalizeNode(const CNode& node, bool& update_connection_time) = 0;
784784

@@ -1057,7 +1057,7 @@ class CConnman
10571057

10581058
NodeId GetNewNodeId();
10591059

1060-
size_t SocketSendData(CNode *pnode) const;
1060+
size_t SocketSendData(CNode& node) const EXCLUSIVE_LOCKS_REQUIRED(node.cs_vSend);
10611061
void DumpAddresses();
10621062

10631063
// Network stats

0 commit comments

Comments
 (0)