Skip to content

Commit

Permalink
net: remove unnecessary check of CNode::cs_vSend
Browse files Browse the repository at this point in the history
It is not possible to have a node in `CConnman::vNodesDisconnected` and
its reference count to be incremented - all `CNode::AddRef()` are done
either before the node is added to `CConnman::vNodes` or while holding
`CConnman::cs_vNodes` and the object being in `CConnman::vNodes`.

So, the object being in `CConnman::vNodesDisconnected` and its reference
count being zero means that it is not and will not start to be used by
other threads.

So, the lock of `CNode::cs_vSend` in `CConnman::DisconnectNodes()` will
always succeed and is not necessary.

Indeed all locks of `CNode::cs_vSend` are done either when the reference
count is >0 or under the protection of `CConnman::cs_vNodes` and the
node being in `CConnman::vNodes`.
  • Loading branch information
vasild committed Apr 22, 2021
1 parent e7776e2 commit 9096b13
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1224,19 +1224,10 @@ void CConnman::DisconnectNodes()
std::list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
for (CNode* pnode : vNodesDisconnectedCopy)
{
// wait until threads are done using it
// Destroy the object only after other threads have stopped using it.
if (pnode->GetRefCount() <= 0) {
bool fDelete = false;
{
TRY_LOCK(pnode->cs_vSend, lockSend);
if (lockSend) {
fDelete = true;
}
}
if (fDelete) {
vNodesDisconnected.remove(pnode);
DeleteNode(pnode);
}
vNodesDisconnected.remove(pnode);
DeleteNode(pnode);
}
}
}
Expand Down

0 comments on commit 9096b13

Please sign in to comment.