@@ -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));
0 commit comments