select() fixes around timeout expiry return value

This commit is contained in:
manojampalam 2016-03-07 22:46:21 -08:00
parent 682fac1587
commit ae2873e8f1
2 changed files with 63 additions and 72 deletions

View File

@ -15,8 +15,8 @@
* - any signals (errno = EINTR ) - TODO * - any signals (errno = EINTR ) - TODO
* - any of the supplied events set * - any of the supplied events set
* - any APCs caused by IO completions * - any APCs caused by IO completions
* - time out (errno = ETIMEOUT) * - time out
* - Returns 0 on IO completion and -1 on rest * - Returns 0 on IO completion, timeout -1 on rest
* if milli_seconds is 0, this function returns 0, its called with 0 * if milli_seconds is 0, this function returns 0, its called with 0
* to execute any scheduled APCs * to execute any scheduled APCs
*/ */
@ -35,11 +35,7 @@ wait_for_any_event(HANDLE* events, int num_events, DWORD milli_seconds)
return 0; return 0;
} }
else if (ret == WAIT_TIMEOUT) { else if (ret == WAIT_TIMEOUT) {
if (milli_seconds == 0) return 0;
return 0;
errno = ETIMEDOUT;
debug("ERROR: wait timed out");
return -1;
} }
/* some other error*/ /* some other error*/
else { else {
@ -54,11 +50,7 @@ wait_for_any_event(HANDLE* events, int num_events, DWORD milli_seconds)
return 0; return 0;
} }
else if (ret == 0) { else if (ret == 0) {
if (milli_seconds == 0) return 0;
return 0;
errno = ETIMEDOUT;
debug("ERROR: wait timed out");
return -1;
} }
else { //some other error else { //some other error
errno = EOTHER; errno = EOTHER;

View File

@ -91,8 +91,8 @@ fd_table_clear(int index)
void void
w32posix_initialize() { w32posix_initialize() {
if ( (fd_table_initialize() != 0) if ((fd_table_initialize() != 0)
|| (socketio_initialize() != 0)) || (socketio_initialize() != 0))
abort(); abort();
} }
@ -381,7 +381,7 @@ w32_close(int fd) {
pio = fd_table.w32_ios[fd]; pio = fd_table.w32_ios[fd];
debug("io:%p, type:%d, fd:%d, table_index:%d", pio, pio->type, fd, debug("io:%p, type:%d, fd:%d, table_index:%d", pio, pio->type, fd,
pio->table_index); pio->table_index);
fd_table_clear(pio->table_index); fd_table_clear(pio->table_index);
if ((pio->type == SOCK_FD)) if ((pio->type == SOCK_FD))
return socketio_close(pio); return socketio_close(pio);
@ -418,7 +418,7 @@ w32_fcntl(int fd, int cmd, ... /* arg */) {
#define SELECT_EVENT_LIMIT 32 #define SELECT_EVENT_LIMIT 32
int int
w32_select(int fds, w32_fd_set* readfds, w32_fd_set* writefds, w32_fd_set* exceptfds, w32_select(int fds, w32_fd_set* readfds, w32_fd_set* writefds, w32_fd_set* exceptfds,
const struct timeval *timeout) { const struct timeval *timeout) {
ULONGLONG ticks_start = GetTickCount64(), ticks_spent; ULONGLONG ticks_start = GetTickCount64(), ticks_spent;
w32_fd_set read_ready_fds, write_ready_fds; w32_fd_set read_ready_fds, write_ready_fds;
HANDLE events[SELECT_EVENT_LIMIT]; HANDLE events[SELECT_EVENT_LIMIT];
@ -487,7 +487,7 @@ w32_select(int fds, w32_fd_set* readfds, w32_fd_set* writefds, w32_fd_set* excep
if (w32_io_on_select(fd_table.w32_ios[i], TRUE) == -1) if (w32_io_on_select(fd_table.w32_ios[i], TRUE) == -1)
return -1; return -1;
if ((fd_table.w32_ios[i]->type == SOCK_FD) if ((fd_table.w32_ios[i]->type == SOCK_FD)
&& (fd_table.w32_ios[i]->internal.state == SOCK_LISTENING)) { && (fd_table.w32_ios[i]->internal.state == SOCK_LISTENING)) {
if (num_events == SELECT_EVENT_LIMIT) { if (num_events == SELECT_EVENT_LIMIT) {
debug("ERROR: max #events reached for select"); debug("ERROR: max #events reached for select");
errno = ENOMEM; errno = ENOMEM;
@ -501,7 +501,7 @@ w32_select(int fds, w32_fd_set* readfds, w32_fd_set* writefds, w32_fd_set* excep
if (w32_io_on_select(fd_table.w32_ios[i], FALSE) == -1) if (w32_io_on_select(fd_table.w32_ios[i], FALSE) == -1)
return -1; return -1;
if ((fd_table.w32_ios[i]->type == SOCK_FD) if ((fd_table.w32_ios[i]->type == SOCK_FD)
&& (fd_table.w32_ios[i]->internal.state == SOCK_CONNECTING)) { && (fd_table.w32_ios[i]->internal.state == SOCK_CONNECTING)) {
if (num_events == SELECT_EVENT_LIMIT) { if (num_events == SELECT_EVENT_LIMIT) {
debug("ERROR: max #events reached for select"); debug("ERROR: max #events reached for select");
errno = ENOMEM; errno = ENOMEM;
@ -534,55 +534,54 @@ w32_select(int fds, w32_fd_set* readfds, w32_fd_set* writefds, w32_fd_set* excep
} }
} }
/* wait for io if none is already ready */
while (out_ready_fds == 0) {
ticks_spent = GetTickCount64() - ticks_start;
time_rem = 0;
if (timeout != NULL) { /* timeout specified and both fields are 0 - polling mode*/
if (timeout_ms < ticks_spent) { /* proceed with further wait if not in polling mode*/
errno = ETIMEDOUT; if ((timeout == NULL) || (timeout_ms != 0))
debug("select timing out"); /* wait for io if none is already ready */
return -1; while (out_ready_fds == 0) {
ticks_spent = GetTickCount64() - ticks_start;
time_rem = 0;
if (timeout != NULL) {
if (timeout_ms < ticks_spent) {
debug("select timing out");
break;
}
time_rem = timeout_ms - (ticks_spent & 0xffffffff);
} }
time_rem = timeout_ms - (ticks_spent & 0xffffffff); else
} time_rem = INFINITE;
if (0 != wait_for_any_event(events, num_events, time_rem)) if (0 != wait_for_any_event(events, num_events, time_rem))
return -1; return -1;
/* check on fd status */ /* check on fd status */
out_ready_fds = 0; out_ready_fds = 0;
for (int i = 0; i < fds; i++) { for (int i = 0; i < fds; i++) {
if (readfds && FD_ISSET(i, readfds)) { if (readfds && FD_ISSET(i, readfds)) {
in_set_fds++; in_set_fds++;
if (w32_io_is_io_available(fd_table.w32_ios[i], TRUE)) { if (w32_io_is_io_available(fd_table.w32_ios[i], TRUE)) {
FD_SET(i, &read_ready_fds); FD_SET(i, &read_ready_fds);
out_ready_fds++; out_ready_fds++;
}
}
if (writefds && FD_ISSET(i, writefds)) {
in_set_fds++;
if (w32_io_is_io_available(fd_table.w32_ios[i], FALSE)) {
FD_SET(i, &write_ready_fds);
out_ready_fds++;
}
} }
} }
if (writefds && FD_ISSET(i, writefds)) { if (out_ready_fds == 0)
in_set_fds++; debug2("wait ended without any IO completion, looping again");
if (w32_io_is_io_available(fd_table.w32_ios[i], FALSE)) {
FD_SET(i, &write_ready_fds);
out_ready_fds++;
}
}
}
if (out_ready_fds == 0) {
if (timeout == 0) {
/* not timeout specified, return EAGAIN if none of fds are ready */
errno = EAGAIN;
return -1;
}
debug2("wait ended without any IO completion, looping again");
} }
};
/* clear out fds that are not ready yet */ /* clear out fds that are not ready yet */
if (readfds) if (readfds)
for (i = 0; i < fds; i++) for (i = 0; i < fds; i++)
@ -676,7 +675,7 @@ w32_temp_DelChildToWatch(HANDLE processtowatch) {
} }
int w32_temp_AddChildToWatch(HANDLE processtowatch) { int w32_temp_AddChildToWatch(HANDLE processtowatch) {
return 0; return 0;
} }
HANDLE HANDLE