poll/select: prevent busy-waiting
authorPaolo Bonzini <bonzini@gnu.org>
Mon, 21 May 2012 07:52:42 +0000 (09:52 +0200)
committerPaolo Bonzini <pbonzini@redhat.com>
Tue, 22 May 2012 08:11:38 +0000 (10:11 +0200)
2012-05-21  Paolo Bonzini  <bonzini@gnu.org>

poll/select: prevent busy-waiting.  SwitchToThread() only gives away
the rest of the current time slice to another thread in the current
process. So if the thread that feeds the file decscriptor we're
polling is not in the current process, we get busy-waiting.
* lib/poll.c: Use SleepEx(1, TRUE) instead of SwitchToThread().
Patch from Theodore Leblond.
* lib/select.c: Split polling out of the loop that sets the output
fd_sets.  Check for zero result and loop if the wait timeout is
infinite.

ChangeLog
lib/poll.c
lib/select.c

index 4b8802a..cd23cd1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2012-05-21  Paolo Bonzini  <bonzini@gnu.org>
+
+       poll/select: prevent busy-waiting.  SwitchToThread() only gives away
+       the rest of the current time slice to another thread in the current
+       process. So if the thread that feeds the file decscriptor we're
+       polling is not in the current process, we get busy-waiting.
+       * lib/poll.c: Use SleepEx(1, TRUE) instead of SwitchToThread().
+       Patch from Theodore Leblond.
+       * lib/select.c: Split polling out of the loop that sets the output
+       fd_sets.  Check for zero result and loop if the wait timeout is
+       infinite.
+
 2012-05-21  Eric Blake  <eblake@redhat.com>
 
        strdup: undeprecate, IRIX 6.5 needs it
index 3071b12..5ad9d86 100644 (file)
@@ -598,7 +598,7 @@ restart:
 
   if (!rc && timeout == INFTIM)
     {
-      SwitchToThread();
+      SleepEx (1, TRUE);
       goto restart;
     }
 
index 7cd6894..4db09a3 100644 (file)
@@ -385,6 +385,10 @@ rpl_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds,
         }
     }
 
+  /* Place a sentinel at the end of the array.  */
+  handle_array[nhandles] = NULL;
+
+restart:
   if (wait_timeout == 0 || nsock == 0)
     rc = 0;
   else
@@ -427,13 +431,44 @@ rpl_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds,
   if (rc == 0 && nsock > 0)
     rc = select (0, &handle_rfds, &handle_wfds, &handle_xfds, &tv0);
 
+  if (nhandles > 1)
+    {
+      /* Count results that are not counted in the return value of select.  */
+      nhandles = 1;
+      for (i = 0; i < nfds; i++)
+        {
+          if ((anyfds_in[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))) == 0)
+            continue;
+
+          h = (HANDLE) _get_osfhandle (i);
+          if (h == handle_array[nhandles])
+            {
+              /* Not a socket.  */
+              nhandles++;
+              windows_poll_handle (h, i, &rbits, &wbits, &xbits);
+              if (rbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))
+                  || wbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1)))
+                  || xbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1))))
+                rc++;
+            }
+        }
+
+      if (rc == 0 && wait_timeout == INFINITE)
+        {
+          /* Sleep 1 millisecond to avoid busy wait and retry with the
+             original fd_sets.  */
+          memcpy (&handle_rfds, rfds, sizeof (fd_set));
+          memcpy (&handle_wfds, wfds, sizeof (fd_set));
+          memcpy (&handle_xfds, xfds, sizeof (fd_set));
+          SleepEx (1, TRUE);
+          goto restart;
+        }
+    }
+
   /* Now fill in the results.  */
   FD_ZERO (rfds);
   FD_ZERO (wfds);
   FD_ZERO (xfds);
-
-  /* Place a sentinel at the end of the array.  */
-  handle_array[nhandles] = NULL;
   nhandles = 1;
   for (i = 0; i < nfds; i++)
     {
@@ -443,8 +478,7 @@ rpl_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds,
       h = (HANDLE) _get_osfhandle (i);
       if (h != handle_array[nhandles])
         {
-          /* Perform handle->descriptor mapping.  Don't update rc, as these
-             results are counted in the return value of Winsock's select.  */
+          /* Perform handle->descriptor mapping.  */
           WSAEventSelect ((SOCKET) h, NULL, 0);
           if (FD_ISSET (h, &handle_rfds))
             FD_SET (i, rfds);
@@ -457,22 +491,12 @@ rpl_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds,
         {
           /* Not a socket.  */
           nhandles++;
-          windows_poll_handle (h, i, &rbits, &wbits, &xbits);
           if (rbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1))))
-            {
-              rc++;
-              FD_SET (i, rfds);
-            }
+            FD_SET (i, rfds);
           if (wbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1))))
-            {
-              rc++;
-              FD_SET (i, wfds);
-            }
+            FD_SET (i, wfds);
           if (xbits.out[i / CHAR_BIT] & (1 << (i & (CHAR_BIT - 1))))
-            {
-              rc++;
-              FD_SET (i, xfds);
-            }
+            FD_SET (i, xfds);
         }
     }