]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
select() and poll(): change order of events in main timing loop
authorJulian Seward <jseward@acm.org>
Wed, 26 Jun 2002 00:47:17 +0000 (00:47 +0000)
committerJulian Seward <jseward@acm.org>
Wed, 26 Jun 2002 00:47:17 +0000 (00:47 +0000)
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

coregrind/arch/x86-linux/vg_libpthread.c
coregrind/vg_libpthread.c
vg_libpthread.c

index d736558f03dd8dc636d34e7ad137de083394acb8..cca2d887c94f861ae49aed0ca094012677c7e4fa 100644 (file)
@@ -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;
+         }
+      }
+
    }
 }
 
index d736558f03dd8dc636d34e7ad137de083394acb8..cca2d887c94f861ae49aed0ca094012677c7e4fa 100644 (file)
@@ -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;
+         }
+      }
+
    }
 }
 
index d736558f03dd8dc636d34e7ad137de083394acb8..cca2d887c94f861ae49aed0ca094012677c7e4fa 100644 (file)
@@ -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;
+         }
+      }
+
    }
 }