Skip to content

Commit b1d13bf

Browse files
committed
adjust epoll wait timeout
1 parent 5519066 commit b1d13bf

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

include/slick/socket/tcp_server_unix.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,18 @@ void TCPServerBase<DerivedT>::server_loop()
283283
struct kevent events[MAX_EVENTS];
284284
std::vector<uint8_t> buffer(config_.receive_buffer_size);
285285

286-
while (running_.load())
286+
// Set timeout to 1us to allow checking running_ flag
287+
struct timespec timeout;
288+
timeout.tv_sec = 0;
289+
timeout.tv_nsec = 1000; // 1us
290+
291+
if (config_.cpu_affinity < 0)
287292
{
288-
// Set timeout to 1us to allow checking running_ flag
289-
struct timespec timeout;
290-
timeout.tv_sec = 0;
291-
timeout.tv_nsec = 1000; // 1us
293+
timeout.tv_nsec = 1000000; // 1ms
294+
}
292295

296+
while (running_.load())
297+
{
293298
int num_events = kevent(epoll_fd_, nullptr, 0, events, MAX_EVENTS, &timeout);
294299
if (num_events < 0)
295300
{
@@ -351,18 +356,20 @@ void TCPServerBase<DerivedT>::server_loop()
351356
struct epoll_event events[MAX_EVENTS];
352357
std::vector<uint8_t> buffer(config_.receive_buffer_size);
353358

359+
int timeout = 0;
360+
if (config_.cpu_affinity < 0)
361+
{
362+
// CPU isn't pinned wait for 1 ms
363+
timeout = 1;
364+
}
365+
354366
while (running_.load())
355367
{
356-
int num_events = epoll_wait(epoll_fd_, events, MAX_EVENTS, 0);
368+
int num_events = epoll_wait(epoll_fd_, events, MAX_EVENTS, timeout);
357369
if (num_events < 0)
358370
{
359371
if (errno == EINTR)
360372
{
361-
if (config_.cpu_affinity < 0)
362-
{
363-
// CPU isn't spinning
364-
std::this_thread::yield();
365-
}
366373
continue;
367374
}
368375
LOG_ERROR("epoll_wait failed: {}", std::strerror(errno));

include/slick/socket/tcp_server_win32.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,20 @@ void TCPServerBase<DrivedT>::server_loop()
271271
struct epoll_event events[MAX_EVENTS];
272272
std::vector<uint8_t> buffer(config_.receive_buffer_size);
273273

274+
int timeout = 0;
275+
if (config_.cpu_affinity < 0)
276+
{
277+
// CPU isn't pinned wait for 1 ms
278+
timeout = 1;
279+
}
280+
274281
while (running_.load(std::memory_order_relaxed))
275282
{
276-
int num_events = epoll_wait(epoll_fd_, events, MAX_EVENTS, 0);
283+
int num_events = epoll_wait(epoll_fd_, events, MAX_EVENTS, timeout);
277284
if (num_events < 0)
278285
{
279286
if (errno == EINTR)
280287
{
281-
if (config_.cpu_affinity < 0)
282-
{
283-
// CPU isn't spinning
284-
std::this_thread::yield();
285-
}
286288
continue;
287289
}
288290
LOG_ERROR("epoll_wait failed: {}", WSAGetLastError());

0 commit comments

Comments
 (0)