From: Bruno Jesus <00cpxxx@gmail.com> Subject: ws2_32: Simplify select() to make its code more reusable Message-Id: Date: Thu, 21 May 2015 23:55:03 -0300 Will be used in the WSAPoll implementation to reduce code duplication. diff --git a/dlls/ws2_32/socket.c b/dlls/ws2_32/socket.c index de391c9..ac4a8cf 100644 --- a/dlls/ws2_32/socket.c +++ b/dlls/ws2_32/socket.c @@ -4789,6 +4789,42 @@ static void release_poll_fds( const WS_fd_set *readfds, const WS_fd_set *writefd } } +static int do_poll(struct pollfd *pollfds, int count, int timeout) +{ + struct timeval tv1, tv2; + int torig, ret; + + if (timeout > 0) + { + gettimeofday( &tv1, 0 ); + torig = timeout; + } + + while ((ret = poll( pollfds, count, timeout )) < 0) + { + if (errno != EINTR) break; + if (timeout < 0) continue; + + gettimeofday( &tv2, 0 ); + + tv2.tv_sec -= tv1.tv_sec; + tv2.tv_usec -= tv1.tv_usec; + if (tv2.tv_usec < 0) + { + tv2.tv_usec += 1000000; + tv2.tv_sec -= 1; + } + + timeout = torig - (tv2.tv_sec * 1000) - (tv2.tv_usec + 999) / 1000; + if (timeout <= 0) + { + ret = 0; + break; + } + } + return ret; +} + /* map the poll results back into the Windows fd sets */ static int get_poll_results( WS_fd_set *readfds, WS_fd_set *writefds, WS_fd_set *exceptfds, const struct pollfd *fds ) @@ -4830,7 +4866,6 @@ static int get_poll_results( WS_fd_set *readfds, WS_fd_set *writefds, WS_fd_set return total; } - /*********************************************************************** * select (WS2_32.18) */ @@ -4839,8 +4874,6 @@ int WINAPI WS_select(int nfds, WS_fd_set *ws_readfds, const struct WS_timeval* ws_timeout) { struct pollfd *pollfds; - struct timeval tv1, tv2; - int torig = 0; int count, ret, timeout = -1; TRACE("read %p, write %p, excp %p timeout %p\n", @@ -4850,35 +4883,9 @@ int WINAPI WS_select(int nfds, WS_fd_set *ws_readfds, return SOCKET_ERROR; if (ws_timeout) - { - torig = (ws_timeout->tv_sec * 1000) + (ws_timeout->tv_usec + 999) / 1000; - timeout = torig; - gettimeofday( &tv1, 0 ); - } + timeout = (ws_timeout->tv_sec * 1000) + (ws_timeout->tv_usec + 999) / 1000; - while ((ret = poll( pollfds, count, timeout )) < 0) - { - if (errno == EINTR) - { - if (!ws_timeout) continue; - gettimeofday( &tv2, 0 ); - - tv2.tv_sec -= tv1.tv_sec; - tv2.tv_usec -= tv1.tv_usec; - if (tv2.tv_usec < 0) - { - tv2.tv_usec += 1000000; - tv2.tv_sec -= 1; - } - - timeout = torig - (tv2.tv_sec * 1000) - (tv2.tv_usec + 999) / 1000; - if (timeout <= 0) - { - ret = 0; - break; - } - } else break; - } + ret = do_poll(pollfds, count, timeout); release_poll_fds( ws_readfds, ws_writefds, ws_exceptfds, pollfds ); if (ret == -1) SetLastError(wsaErrno());