From: Julian Seward Date: Wed, 26 Jun 2002 00:47:17 +0000 (+0000) Subject: select() and poll(): change order of events in main timing loop X-Git-Tag: svn/VALGRIND_1_0_3~34 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=827b3d25658010548b30d39ed400373dc24f171c;p=thirdparty%2Fvalgrind.git select() and poll(): change order of events in main timing loop so that very short selects/polls don't just cause a timeout with no real test. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@466 --- diff --git a/coregrind/arch/x86-linux/vg_libpthread.c b/coregrind/arch/x86-linux/vg_libpthread.c index d736558f03..cca2d887c9 100644 --- a/coregrind/arch/x86-linux/vg_libpthread.c +++ b/coregrind/arch/x86-linux/vg_libpthread.c @@ -1941,20 +1941,10 @@ int select ( int n, /* Either timeout == NULL, meaning wait indefinitely, or timeout != NULL, in which case ms_end holds the end time. */ + while (1) { - if (timeout) { - VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, - VG_USERREQ__READ_MILLISECOND_TIMER, - 0, 0, 0, 0); - my_assert(ms_now != 0xFFFFFFFF); - if (ms_now >= ms_end) { - /* timeout; nothing interesting happened. */ - if (rfds) FD_ZERO(rfds); - if (wfds) FD_ZERO(wfds); - if (xfds) FD_ZERO(xfds); - return 0; - } - } + + /* First, do a return-immediately select(). */ /* These could be trashed each time round the loop, so restore them each time. */ @@ -1983,6 +1973,10 @@ int select ( int n, if (xfds) *xfds = xfds_copy; return res; } + + /* Nothing interesting happened, so we go to sleep for a + while. */ + /* fprintf(stderr, "MY_SELECT: nanosleep\n"); */ /* nanosleep and go round again */ nanosleep_interval.tv_sec = 0; @@ -1997,6 +1991,23 @@ int select ( int n, * (__errno_location()) = EINTR; return -1; } + + /* Sleeping finished. If a finite timeout, check to see if it + has expired yet. */ + if (timeout) { + VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, + VG_USERREQ__READ_MILLISECOND_TIMER, + 0, 0, 0, 0); + my_assert(ms_now != 0xFFFFFFFF); + if (ms_now >= ms_end) { + /* timeout; nothing interesting happened. */ + if (rfds) FD_ZERO(rfds); + if (wfds) FD_ZERO(wfds); + if (xfds) FD_ZERO(xfds); + return 0; + } + } + } } @@ -2055,23 +2066,13 @@ int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) /* Either timeout < 0, meaning wait indefinitely, or timeout > 0, in which case t_end holds the end time. */ + my_assert(__timeout != 0); while (1) { - if (__timeout > 0) { - VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, - VG_USERREQ__READ_MILLISECOND_TIMER, - 0, 0, 0, 0); - my_assert(ms_now != 0xFFFFFFFF); - if (ms_now >= ms_end) { - /* timeout; nothing interesting happened. */ - for (i = 0; i < __nfds; i++) - __fds[i].revents = 0; - return 0; - } - } /* Do a return-immediately poll. */ + res = my_do_syscall3(__NR_poll, (int)__fds, __nfds, 0 ); if (is_kerror(res)) { /* Some kind of error. Set errno and return. */ @@ -2082,6 +2083,10 @@ int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) /* One or more fds is ready. Return now. */ return res; } + + /* Nothing interesting happened, so we go to sleep for a + while. */ + /* fprintf(stderr, "MY_POLL: nanosleep\n"); */ /* nanosleep and go round again */ nanosleep_interval.tv_sec = 0; @@ -2090,6 +2095,22 @@ int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) is nonblocking. */ (void)my_do_syscall2(__NR_nanosleep, (int)(&nanosleep_interval), (int)NULL); + + /* Sleeping finished. If a finite timeout, check to see if it + has expired yet. */ + if (__timeout > 0) { + VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, + VG_USERREQ__READ_MILLISECOND_TIMER, + 0, 0, 0, 0); + my_assert(ms_now != 0xFFFFFFFF); + if (ms_now >= ms_end) { + /* timeout; nothing interesting happened. */ + for (i = 0; i < __nfds; i++) + __fds[i].revents = 0; + return 0; + } + } + } } diff --git a/coregrind/vg_libpthread.c b/coregrind/vg_libpthread.c index d736558f03..cca2d887c9 100644 --- a/coregrind/vg_libpthread.c +++ b/coregrind/vg_libpthread.c @@ -1941,20 +1941,10 @@ int select ( int n, /* Either timeout == NULL, meaning wait indefinitely, or timeout != NULL, in which case ms_end holds the end time. */ + while (1) { - if (timeout) { - VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, - VG_USERREQ__READ_MILLISECOND_TIMER, - 0, 0, 0, 0); - my_assert(ms_now != 0xFFFFFFFF); - if (ms_now >= ms_end) { - /* timeout; nothing interesting happened. */ - if (rfds) FD_ZERO(rfds); - if (wfds) FD_ZERO(wfds); - if (xfds) FD_ZERO(xfds); - return 0; - } - } + + /* First, do a return-immediately select(). */ /* These could be trashed each time round the loop, so restore them each time. */ @@ -1983,6 +1973,10 @@ int select ( int n, if (xfds) *xfds = xfds_copy; return res; } + + /* Nothing interesting happened, so we go to sleep for a + while. */ + /* fprintf(stderr, "MY_SELECT: nanosleep\n"); */ /* nanosleep and go round again */ nanosleep_interval.tv_sec = 0; @@ -1997,6 +1991,23 @@ int select ( int n, * (__errno_location()) = EINTR; return -1; } + + /* Sleeping finished. If a finite timeout, check to see if it + has expired yet. */ + if (timeout) { + VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, + VG_USERREQ__READ_MILLISECOND_TIMER, + 0, 0, 0, 0); + my_assert(ms_now != 0xFFFFFFFF); + if (ms_now >= ms_end) { + /* timeout; nothing interesting happened. */ + if (rfds) FD_ZERO(rfds); + if (wfds) FD_ZERO(wfds); + if (xfds) FD_ZERO(xfds); + return 0; + } + } + } } @@ -2055,23 +2066,13 @@ int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) /* Either timeout < 0, meaning wait indefinitely, or timeout > 0, in which case t_end holds the end time. */ + my_assert(__timeout != 0); while (1) { - if (__timeout > 0) { - VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, - VG_USERREQ__READ_MILLISECOND_TIMER, - 0, 0, 0, 0); - my_assert(ms_now != 0xFFFFFFFF); - if (ms_now >= ms_end) { - /* timeout; nothing interesting happened. */ - for (i = 0; i < __nfds; i++) - __fds[i].revents = 0; - return 0; - } - } /* Do a return-immediately poll. */ + res = my_do_syscall3(__NR_poll, (int)__fds, __nfds, 0 ); if (is_kerror(res)) { /* Some kind of error. Set errno and return. */ @@ -2082,6 +2083,10 @@ int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) /* One or more fds is ready. Return now. */ return res; } + + /* Nothing interesting happened, so we go to sleep for a + while. */ + /* fprintf(stderr, "MY_POLL: nanosleep\n"); */ /* nanosleep and go round again */ nanosleep_interval.tv_sec = 0; @@ -2090,6 +2095,22 @@ int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) is nonblocking. */ (void)my_do_syscall2(__NR_nanosleep, (int)(&nanosleep_interval), (int)NULL); + + /* Sleeping finished. If a finite timeout, check to see if it + has expired yet. */ + if (__timeout > 0) { + VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, + VG_USERREQ__READ_MILLISECOND_TIMER, + 0, 0, 0, 0); + my_assert(ms_now != 0xFFFFFFFF); + if (ms_now >= ms_end) { + /* timeout; nothing interesting happened. */ + for (i = 0; i < __nfds; i++) + __fds[i].revents = 0; + return 0; + } + } + } } diff --git a/vg_libpthread.c b/vg_libpthread.c index d736558f03..cca2d887c9 100644 --- a/vg_libpthread.c +++ b/vg_libpthread.c @@ -1941,20 +1941,10 @@ int select ( int n, /* Either timeout == NULL, meaning wait indefinitely, or timeout != NULL, in which case ms_end holds the end time. */ + while (1) { - if (timeout) { - VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, - VG_USERREQ__READ_MILLISECOND_TIMER, - 0, 0, 0, 0); - my_assert(ms_now != 0xFFFFFFFF); - if (ms_now >= ms_end) { - /* timeout; nothing interesting happened. */ - if (rfds) FD_ZERO(rfds); - if (wfds) FD_ZERO(wfds); - if (xfds) FD_ZERO(xfds); - return 0; - } - } + + /* First, do a return-immediately select(). */ /* These could be trashed each time round the loop, so restore them each time. */ @@ -1983,6 +1973,10 @@ int select ( int n, if (xfds) *xfds = xfds_copy; return res; } + + /* Nothing interesting happened, so we go to sleep for a + while. */ + /* fprintf(stderr, "MY_SELECT: nanosleep\n"); */ /* nanosleep and go round again */ nanosleep_interval.tv_sec = 0; @@ -1997,6 +1991,23 @@ int select ( int n, * (__errno_location()) = EINTR; return -1; } + + /* Sleeping finished. If a finite timeout, check to see if it + has expired yet. */ + if (timeout) { + VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, + VG_USERREQ__READ_MILLISECOND_TIMER, + 0, 0, 0, 0); + my_assert(ms_now != 0xFFFFFFFF); + if (ms_now >= ms_end) { + /* timeout; nothing interesting happened. */ + if (rfds) FD_ZERO(rfds); + if (wfds) FD_ZERO(wfds); + if (xfds) FD_ZERO(xfds); + return 0; + } + } + } } @@ -2055,23 +2066,13 @@ int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) /* Either timeout < 0, meaning wait indefinitely, or timeout > 0, in which case t_end holds the end time. */ + my_assert(__timeout != 0); while (1) { - if (__timeout > 0) { - VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, - VG_USERREQ__READ_MILLISECOND_TIMER, - 0, 0, 0, 0); - my_assert(ms_now != 0xFFFFFFFF); - if (ms_now >= ms_end) { - /* timeout; nothing interesting happened. */ - for (i = 0; i < __nfds; i++) - __fds[i].revents = 0; - return 0; - } - } /* Do a return-immediately poll. */ + res = my_do_syscall3(__NR_poll, (int)__fds, __nfds, 0 ); if (is_kerror(res)) { /* Some kind of error. Set errno and return. */ @@ -2082,6 +2083,10 @@ int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) /* One or more fds is ready. Return now. */ return res; } + + /* Nothing interesting happened, so we go to sleep for a + while. */ + /* fprintf(stderr, "MY_POLL: nanosleep\n"); */ /* nanosleep and go round again */ nanosleep_interval.tv_sec = 0; @@ -2090,6 +2095,22 @@ int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) is nonblocking. */ (void)my_do_syscall2(__NR_nanosleep, (int)(&nanosleep_interval), (int)NULL); + + /* Sleeping finished. If a finite timeout, check to see if it + has expired yet. */ + if (__timeout > 0) { + VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, + VG_USERREQ__READ_MILLISECOND_TIMER, + 0, 0, 0, 0); + my_assert(ms_now != 0xFFFFFFFF); + if (ms_now >= ms_end) { + /* timeout; nothing interesting happened. */ + for (i = 0; i < __nfds; i++) + __fds[i].revents = 0; + return 0; + } + } + } }