poll-loop: New function poll_timer_wait_until().
authorBen Pfaff <blp@nicira.com>
Wed, 12 May 2010 19:53:07 +0000 (12:53 -0700)
committerBen Pfaff <blp@nicira.com>
Wed, 26 May 2010 18:46:59 +0000 (11:46 -0700)
Many of poll_timer_wait()'s callers actually want to wait until a specific
time, so it's convenient for them to offer a function that does this.

15 files changed:
extras/ezio/ezio-term.c
extras/ezio/ovs-switchui.c
lib/dhcp-client.c
lib/learning-switch.c
lib/mac-learning.c
lib/poll-loop.c
lib/poll-loop.h
lib/rconn.c
ofproto/fail-open.c
ofproto/in-band.c
ofproto/ofproto-sflow.c
ofproto/ofproto.c
ovsdb/trigger.c
vswitchd/bridge.c
xenserver/ovs-xenserverd.c

index cedc5c9..d96122b 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2008, 2009 Nicira Networks, Inc.
+/* Copyright (c) 2008, 2009, 2010 Nicira Networks, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -907,14 +907,7 @@ scanner_run(struct scanner *s, struct ezio *ezio)
 static void
 scanner_wait(struct scanner *s)
 {
-    long long int now = time_msec();
-    long long int expires = s->last_move + 750;
-    if (now >= expires) {
-        poll_immediate_wake();
-    } else {
-        poll_timer_wait(expires - now);
-    }
-
+    poll_timer_wait_until(s->last_move + 750);
 }
 
 static void
index 16a6903..6ebfece 100644 (file)
@@ -242,7 +242,7 @@ main(int argc, char *argv[])
             refresh();
 
             poll_fd_wait(STDIN_FILENO, POLLIN);
-            poll_timer_wait(timeout - time_msec());
+            poll_timer_wait_until(timeout);
             poll_block();
         } while (time_msec() < timeout);
         age_messages();
@@ -868,7 +868,7 @@ fetch_status(struct rconn *rconn, struct dict *dict, long long timeout)
 
         rconn_run_wait(rconn);
         rconn_recv_wait(rconn);
-        poll_timer_wait(timeout - time_msec());
+        poll_timer_wait_until(timeout);
         poll_block();
     }
 }
@@ -1714,7 +1714,7 @@ menu_show(const struct menu *menu, int start, bool select)
         refresh();
 
         if (pos < min || pos > max) {
-            poll_timer_wait(adjust - time_msec());
+            poll_timer_wait_until(adjust);
         }
         poll_fd_wait(STDIN_FILENO, POLLIN);
         poll_block();
@@ -1946,7 +1946,7 @@ static void
 block_until(long long timeout)
 {
     while (timeout > time_msec()) {
-        poll_timer_wait(timeout - time_msec());
+        poll_timer_wait_until(timeout);
         poll_block();
     }
     drain_keyboard_buffer();
index 0abf115..563a415 100644 (file)
@@ -806,13 +806,8 @@ void
 dhclient_wait(struct dhclient *cli)
 {
     if (cli->min_timeout != UINT_MAX) {
-        time_t now = time_now();
-        unsigned int wake = sat_add(cli->state_entered, cli->min_timeout);
-        if (wake <= now) {
-            poll_immediate_wake();
-        } else {
-            poll_timer_wait(sat_mul(sat_sub(wake, now), 1000));
-        }
+        long long int wake = sat_add(cli->state_entered, cli->min_timeout);
+        poll_timer_wait_until(wake * 1000);
     }
     /* Reset timeout to 1 second.  This will have no effect ordinarily, because
      * dhclient_run() will typically set it back to a higher value.  If,
index 91f8f05..64639f2 100644 (file)
@@ -220,8 +220,7 @@ lswitch_run(struct lswitch *sw, struct rconn *rconn)
 static void
 wait_timeout(long long int started)
 {
-    long long int now = time_msec();
-    poll_timer_wait(10000 - (now - started));
+    poll_timer_wait_until(started + 10000);
 }
 
 void
index a9d414d..f9859b6 100644 (file)
@@ -293,6 +293,6 @@ mac_learning_wait(struct mac_learning *ml)
 {
     if (!list_is_empty(&ml->lrus)) {
         struct mac_entry *e = mac_entry_from_lru_node(ml->lrus.next);
-        poll_timer_wait((e->expires - time_now()) * 1000);
+        poll_timer_wait_until(e->expires * 1000LL);
     }
 }
index 29931f7..91034b0 100644 (file)
@@ -100,6 +100,23 @@ poll_timer_wait(long long int msec)
                       : msec);
 }
 
+/* Causes the following call to poll_block() to wake up when the current time,
+ * as returned by time_msec(), reaches 'msec' or later.  If 'msec' is earlier
+ * than the current time, the following call to poll_block() will not block at
+ * all.
+ *
+ * The timer registration is one-shot: only the following call to poll_block()
+ * is affected.  The timer will need to be re-registered after poll_block() is
+ * called if it is to persist. */
+void
+poll_timer_wait_until(long long int msec)
+{
+    long long int now = time_msec();
+    poll_timer_wait__(msec <= now ? 0
+                      : msec < now + INT_MAX ? msec - now
+                      : INT_MAX);
+}
+
 /* Causes the following call to poll_block() to wake up immediately, without
  * blocking. */
 void
index eaeca2b..a9038ca 100644 (file)
@@ -43,6 +43,7 @@ struct poll_waiter;
 /* Schedule events to wake up the following poll_block(). */
 struct poll_waiter *poll_fd_wait(int fd, short int events);
 void poll_timer_wait(long long int msec);
+void poll_timer_wait_until(long long int msec);
 void poll_immediate_wake(void);
 
 /* Wait until an event occurs. */
index ea45134..71198ea 100644 (file)
@@ -500,9 +500,8 @@ rconn_run_wait(struct rconn *rc)
 
     timeo = timeout(rc);
     if (timeo != UINT_MAX) {
-        unsigned int expires = sat_add(rc->state_entered, timeo);
-        unsigned int remaining = sat_sub(expires, time_now());
-        poll_timer_wait(sat_mul(remaining, 1000));
+        long long int expires = sat_add(rc->state_entered, timeo);
+        poll_timer_wait_until(expires * 1000);
     }
 
     if ((rc->state & (S_ACTIVE | S_IDLE)) && rc->txq.n) {
index a79c5b2..b028493 100644 (file)
@@ -271,7 +271,7 @@ void
 fail_open_wait(struct fail_open *fo)
 {
     if (fo->next_bogus_packet_in != LLONG_MAX) {
-        poll_timer_wait(fo->next_bogus_packet_in - time_msec());
+        poll_timer_wait_until(fo->next_bogus_packet_in);
     }
 }
 
index bf90273..e52a0a0 100644 (file)
@@ -745,14 +745,9 @@ in_band_run(struct in_band *ib)
 void
 in_band_wait(struct in_band *in_band)
 {
-    time_t now = time_now();
-    time_t wakeup 
+    long long int wakeup
             = MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
-    if (wakeup > now) {
-        poll_timer_wait((wakeup - now) * 1000);
-    } else {
-        poll_immediate_wake();
-    }
+    poll_timer_wait_until(wakeup * 1000);
 }
 
 /* ofproto has flushed all flows from the flow table and it is calling us back
index 60baf0e..37c1bb7 100644 (file)
@@ -607,6 +607,6 @@ void
 ofproto_sflow_wait(struct ofproto_sflow *os)
 {
     if (ofproto_sflow_is_enabled(os)) {
-        poll_timer_wait(os->next_tick * 1000 - time_msec());
+        poll_timer_wait_until(os->next_tick * 1000LL);
     }
 }
index 4197787..1010948 100644 (file)
@@ -1155,7 +1155,7 @@ ofproto_wait(struct ofproto *p)
         ofconn_wait(ofconn);
     }
     if (p->in_band) {
-        poll_timer_wait(p->next_in_band_update - time_msec());
+        poll_timer_wait_until(p->next_in_band_update);
         in_band_wait(p->in_band);
     }
     if (p->fail_open) {
@@ -1172,7 +1172,7 @@ ofproto_wait(struct ofproto *p)
         VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
         poll_immediate_wake();
     } else if (p->next_expiration != LLONG_MAX) {
-        poll_timer_wait(p->next_expiration - time_msec());
+        poll_timer_wait_until(p->next_expiration);
     }
     for (i = 0; i < p->n_listeners; i++) {
         pvconn_wait(p->listeners[i]);
index 8f18291..4771969 100644 (file)
@@ -102,7 +102,7 @@ ovsdb_trigger_wait(struct ovsdb *db, long long int now)
         }
 
         if (deadline < LLONG_MAX) {
-            poll_timer_wait(deadline - now);
+            poll_timer_wait_until(deadline);
         }
     }
 }
index 354d4d8..04898f8 100644 (file)
@@ -1986,11 +1986,11 @@ bond_wait(struct bridge *br)
         for (j = 0; j < port->n_ifaces; j++) {
             struct iface *iface = port->ifaces[j];
             if (iface->delay_expires != LLONG_MAX) {
-                poll_timer_wait(iface->delay_expires - time_msec());
+                poll_timer_wait_until(iface->delay_expires);
             }
         }
         if (port->bond_fake_iface) {
-            poll_timer_wait(port->bond_next_fake_iface_update - time_msec());
+            poll_timer_wait_until(port->bond_next_fake_iface_update);
         }
     }
 }
index a69ca8a..1598b01 100644 (file)
@@ -240,7 +240,7 @@ network_uuid_refresh_wait(void)
             poll_timer_wait(1000);
         }
         if (next_refresh != LLONG_MAX) {
-            poll_timer_wait(next_refresh - time_msec());
+            poll_timer_wait_until(next_refresh);
         }
     }
 }