Skip to content

Commit

Permalink
Don't wake up select if it was already woken up (PIVX-Project#2863)
Browse files Browse the repository at this point in the history
This avoids calling WakeupSelect() for each node instead of just once.
  • Loading branch information
codablock authored and panleone committed Nov 10, 2024
1 parent 896df7c commit 4da73ff
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
27 changes: 16 additions & 11 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ bool CConnman::GenerateSelectSet(std::set<SOCKET>& recv_set, std::set<SOCKET>& s
}
}

#ifndef WIN32
#ifdef USE_WAKEUP_PIPE
// We add a pipe to the read set so that the select() call can be woken up from the outside
// This is done when data is added to send buffers (vSendMsg) or when new peers are added
// This is currently only implemented for POSIX compliant systems. This means that Windows will fall back to
Expand Down Expand Up @@ -1339,9 +1339,12 @@ void CConnman::SocketEvents(std::set<SOCKET>& recv_set, std::set<SOCKET>& send_s
vpollfds.push_back(std::move(it.second));
}

isInSelect = true;
if (poll(vpollfds.data(), vpollfds.size(), SELECT_TIMEOUT_MILLISECONDS) < 0) return;
isInSelect = false;
wakeupSelectNeeded = true;
int r = poll(vpollfds.data(), vpollfds.size(), SELECT_TIMEOUT_MILLISECONDS);
wakeupSelectNeeded = false;
if (r < 0) {
return;
}

if (interruptNet) return;

Expand Down Expand Up @@ -1390,9 +1393,9 @@ void CConnman::SocketEvents(std::set<SOCKET> &recv_set, std::set<SOCKET> &send_s
hSocketMax = std::max(hSocketMax, hSocket);
}

isInSelect = true;
wakeupSelectNeeded = true;
int nSelect = select(hSocketMax + 1, &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
isInSelect = false;
wakeupSelectNeeded = false;

if (interruptNet)
return;
Expand Down Expand Up @@ -1433,7 +1436,7 @@ void CConnman::SocketHandler()
std::set<SOCKET> recv_set, send_set, error_set;
SocketEvents(recv_set, send_set, error_set);

#ifndef WIN32
#ifdef USE_WAKEUP_PIPE
// drain the wakeup pipe
if (recv_set.count(wakeupPipe[0])) {
LogPrint(BCLog::NET, "woke up select()\n");
Expand Down Expand Up @@ -1563,7 +1566,7 @@ void CConnman::WakeMessageHandler()

void CConnman::WakeSelect()
{
#ifndef WIN32
#ifdef USE_WAKEUP_PIPE
if (wakeupPipe[1] == -1) {
return;
}
Expand All @@ -1575,6 +1578,8 @@ void CConnman::WakeSelect()
LogPrint(BCLog::NET, "write to wakeupPipe failed\n");
}
#endif

wakeupSelectNeeded = false;
}

static std::string GetDNSHost(const CDNSSeedData& data, ServiceFlags* requiredServiceBits)
Expand Down Expand Up @@ -2284,7 +2289,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
fMsgProcWake = false;
}

#ifndef WIN32
#ifdef USE_WAKEUP_PIPE
if (pipe(wakeupPipe) != 0) {
wakeupPipe[0] = wakeupPipe[1] = -1;
LogPrint(BCLog::NET, "pipe() for wakeupPipe failed\n");
Expand Down Expand Up @@ -2429,7 +2434,7 @@ void CConnman::Stop()
vhListenSocket.clear();
semOutbound.reset();
semAddnode.reset();
#ifndef WIN32
#ifdef USE_WAKEUP_PIPE
if (wakeupPipe[0] != -1) close(wakeupPipe[0]);
if (wakeupPipe[1] != -1) close(wakeupPipe[1]);
wakeupPipe[0] = wakeupPipe[1] = -1;
Expand Down Expand Up @@ -2780,7 +2785,7 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg, bool allowOpti
if (optimisticSend == true)
nBytesSent = SocketSendData(pnode);
// wake up select() call in case there was no pending data before (so it was not selecting this socket for sending)
else if (!hasPendingData && isInSelect)
else if (!hasPendingData && wakeupSelectNeeded)
WakeSelect();
}
if (nBytesSent)
Expand Down
5 changes: 3 additions & 2 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#define DEFAULT_ALLOW_OPTIMISTIC_SEND true
#else
#define DEFAULT_ALLOW_OPTIMISTIC_SEND false
#define USE_WAKEUP_PIPE
#endif

class CAddrMan;
Expand Down Expand Up @@ -495,11 +496,11 @@ class CConnman

CThreadInterrupt interruptNet;

#ifndef WIN32
#ifdef USE_WAKEUP_PIPE
/** a pipe which is added to select() calls to wakeup before the timeout */
int wakeupPipe[2]{-1, -1};
#endif
std::atomic<bool> isInSelect{false};
std::atomic<bool> wakeupSelectNeeded{false};

std::thread threadDNSAddressSeed;
std::thread threadSocketHandler;
Expand Down

0 comments on commit 4da73ff

Please sign in to comment.