[zeromq-dev] [PATCH] zmq_poll(): Fix timeout handling for select backend.

Chia-liang Kao clkao at clkao.org
Fri Nov 12 09:48:31 CET 2010


Fix a bug that zmq_poll's select backend spins when timeout=-1, due to
ptimeout not properly recalculated after first pass.

Signed-off-by: Chia-liang Kao <clkao at clkao.org>
---
 src/zmq.cpp |   32 +++++++++++++++-----------------
 1 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/src/zmq.cpp b/src/zmq.cpp
index 82e98f6..745a338 100644
--- a/src/zmq.cpp
+++ b/src/zmq.cpp
@@ -545,24 +545,22 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
     int nevents = 0;
     fd_set inset, outset, errset;
 
-    //  Compute the timeout for the subsequent poll.
-    timeval timeout;
-    timeval *ptimeout;
-    if (first_pass) {
-        timeout.tv_sec = 0;
-        timeout.tv_usec = 0;
-        ptimeout = &timeout;
-    }
-    else if (timeout_ < 0)
-        ptimeout = NULL;
-    else {
-        timeout.tv_sec = (long) ((end - now) / 1000);
-        timeout.tv_usec = (long) ((end - now) % 1000 * 1000);
-        ptimeout = &timeout;
-    }
-
     while (true) {
-
+        //  Compute the timeout for the subsequent poll.
+        timeval timeout;
+        timeval *ptimeout;
+        if (first_pass) {
+            timeout.tv_sec = 0;
+            timeout.tv_usec = 0;
+            ptimeout = &timeout;
+        }
+        else if (timeout_ < 0)
+            ptimeout = NULL;
+        else {
+            timeout.tv_sec = (long) ((end - now) / 1000);
+            timeout.tv_usec = (long) ((end - now) % 1000 * 1000);
+            ptimeout = &timeout;
+        }
         //  Wait for events. Ignore interrupts if there's infinite timeout.
         while (true) {
             memcpy (&inset, &pollset_in, sizeof (fd_set));
-- 
1.7.0.3.254.g4503b




More information about the zeromq-dev mailing list