Skip to content

Commit 802a933

Browse files
codablockpanleone
authored andcommitted
Don't wake up select if it was already woken up (dashpay#2863)
This avoids calling WakeupSelect() for each node instead of just once.
1 parent 436300d commit 802a933

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/net.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ bool CConnman::GenerateSelectSet(std::set<SOCKET>& recv_set, std::set<SOCKET>& s
12951295
}
12961296
}
12971297

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

1342-
isInSelect = true;
1343-
if (poll(vpollfds.data(), vpollfds.size(), SELECT_TIMEOUT_MILLISECONDS) < 0) return;
1344-
isInSelect = false;
1342+
wakeupSelectNeeded = true;
1343+
int r = poll(vpollfds.data(), vpollfds.size(), SELECT_TIMEOUT_MILLISECONDS);
1344+
wakeupSelectNeeded = false;
1345+
if (r < 0) {
1346+
return;
1347+
}
13451348

13461349
if (interruptNet) return;
13471350

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

1393-
isInSelect = true;
1396+
wakeupSelectNeeded = true;
13941397
int nSelect = select(hSocketMax + 1, &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
1395-
isInSelect = false;
1398+
wakeupSelectNeeded = false;
13961399

13971400
if (interruptNet)
13981401
return;
@@ -1433,7 +1436,7 @@ void CConnman::SocketHandler()
14331436
std::set<SOCKET> recv_set, send_set, error_set;
14341437
SocketEvents(recv_set, send_set, error_set);
14351438

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

15641567
void CConnman::WakeSelect()
15651568
{
1566-
#ifndef WIN32
1569+
#ifdef USE_WAKEUP_PIPE
15671570
if (wakeupPipe[1] == -1) {
15681571
return;
15691572
}
@@ -1575,6 +1578,8 @@ void CConnman::WakeSelect()
15751578
LogPrint(BCLog::NET, "write to wakeupPipe failed\n");
15761579
}
15771580
#endif
1581+
1582+
wakeupSelectNeeded = false;
15781583
}
15791584

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

2287-
#ifndef WIN32
2292+
#ifdef USE_WAKEUP_PIPE
22882293
if (pipe(wakeupPipe) != 0) {
22892294
wakeupPipe[0] = wakeupPipe[1] = -1;
22902295
LogPrint(BCLog::NET, "pipe() for wakeupPipe failed\n");
@@ -2429,7 +2434,7 @@ void CConnman::Stop()
24292434
vhListenSocket.clear();
24302435
semOutbound.reset();
24312436
semAddnode.reset();
2432-
#ifndef WIN32
2437+
#ifdef USE_WAKEUP_PIPE
24332438
if (wakeupPipe[0] != -1) close(wakeupPipe[0]);
24342439
if (wakeupPipe[1] != -1) close(wakeupPipe[1]);
24352440
wakeupPipe[0] = wakeupPipe[1] = -1;
@@ -2780,7 +2785,7 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg, bool allowOpti
27802785
if (optimisticSend == true)
27812786
nBytesSent = SocketSendData(pnode);
27822787
// wake up select() call in case there was no pending data before (so it was not selecting this socket for sending)
2783-
else if (!hasPendingData && isInSelect)
2788+
else if (!hasPendingData && wakeupSelectNeeded)
27842789
WakeSelect();
27852790
}
27862791
if (nBytesSent)

src/net.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#define DEFAULT_ALLOW_OPTIMISTIC_SEND true
4747
#else
4848
#define DEFAULT_ALLOW_OPTIMISTIC_SEND false
49+
#define USE_WAKEUP_PIPE
4950
#endif
5051

5152
class CAddrMan;
@@ -495,11 +496,11 @@ class CConnman
495496

496497
CThreadInterrupt interruptNet;
497498

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

504505
std::thread threadDNSAddressSeed;
505506
std::thread threadSocketHandler;

0 commit comments

Comments
 (0)