Skip to content

Commit

Permalink
unix: match kqueue and epoll code
Browse files Browse the repository at this point in the history
Match the implementation for linux.c to kqueue.c in the code around the
calls to kevent and epoll.

In linux.c the code was made more DRY by moving the nfds check up
(including a comment of why it's possible) and combining two if checks
into one.

In kqueue.c the assert to check the timeout when nfds == 0 has been
moved to be called directly after the EINTR check. Since it should
always be true regardless.

Ref: libuv#3893
Ref: nodejs/node#48490
  • Loading branch information
trevnorris committed Jul 13, 2023
1 parent d09441c commit b1e87ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 35 deletions.
13 changes: 8 additions & 5 deletions src/unix/kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ void uv__io_poll(uv_loop_t* loop, int timeout) {

if (nfds == -1)
assert(errno == EINTR);
else if (nfds == 0)
/* Unlimited timeout should only return with events or signal. */
assert(timeout != -1);

if (pset != NULL)
pthread_sigmask(SIG_UNBLOCK, pset, NULL);
Expand All @@ -285,13 +288,13 @@ void uv__io_poll(uv_loop_t* loop, int timeout) {
if (reset_timeout != 0) {
timeout = user_timeout;
reset_timeout = 0;
} else if (nfds == 0) {
/* Reached the user timeout value. */
assert(timeout != -1);
return;
}

/* Interrupted by a signal. Update timeout and poll again. */
/* If nfds == 0 then we may have been inside the system call for longer
* than |timeout| milliseconds so we need to update the timestamp to
* avoid drift.
* If nfds == -1 then it was interrupted by a signal. Update timeout and
* poll again. */
goto update_timeout;
}

Expand Down
41 changes: 11 additions & 30 deletions src/unix/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -1370,42 +1370,23 @@ void uv__io_poll(uv_loop_t* loop, int timeout) {
*/
SAVE_ERRNO(uv__update_time(loop));

if (nfds == 0) {
if (nfds == -1)
assert(errno == EINTR);
else if (nfds == 0)
/* Unlimited timeout should only return with events or signal. */
assert(timeout != -1);

if (nfds == 0 || nfds == -1) {
if (reset_timeout != 0) {
timeout = user_timeout;
reset_timeout = 0;
}

if (timeout == -1)
continue;

if (timeout == 0)
break;

/* We may have been inside the system call for longer than |timeout|
* milliseconds so we need to update the timestamp to avoid drift.
*/
goto update_timeout;
}

if (nfds == -1) {
if (errno != EINTR)
abort();

if (reset_timeout != 0) {
timeout = user_timeout;
reset_timeout = 0;
}

if (timeout == -1)
continue;

if (timeout == 0)
break;

/* Interrupted by a signal. Update timeout and poll again. */
/* If nfds == 0 then we may have been inside the system call for longer
* than |timeout| milliseconds so we need to update the timestamp to
* avoid drift.
* If nfds == -1 then it was interrupted by a signal. Update timeout and
* poll again. */
goto update_timeout;
}

Expand Down Expand Up @@ -1515,13 +1496,13 @@ void uv__io_poll(uv_loop_t* loop, int timeout) {
break;
}

update_timeout:
if (timeout == 0)
break;

if (timeout == -1)
continue;

update_timeout:
assert(timeout > 0);

real_timeout -= (loop->time - base);
Expand Down

0 comments on commit b1e87ee

Please sign in to comment.