Skip to content

Commit 7cf8b26

Browse files
committed
poll-loop: New function poll_timer_wait_until().
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.
1 parent 8bf4bbe commit 7cf8b26

15 files changed

Lines changed: 40 additions & 41 deletions

File tree

extras/ezio/ezio-term.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2008, 2009 Nicira Networks, Inc.
1+
/* Copyright (c) 2008, 2009, 2010 Nicira Networks, Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -907,14 +907,7 @@ scanner_run(struct scanner *s, struct ezio *ezio)
907907
static void
908908
scanner_wait(struct scanner *s)
909909
{
910-
long long int now = time_msec();
911-
long long int expires = s->last_move + 750;
912-
if (now >= expires) {
913-
poll_immediate_wake();
914-
} else {
915-
poll_timer_wait(expires - now);
916-
}
917-
910+
poll_timer_wait_until(s->last_move + 750);
918911
}
919912

920913
static void

extras/ezio/ovs-switchui.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ main(int argc, char *argv[])
242242
refresh();
243243

244244
poll_fd_wait(STDIN_FILENO, POLLIN);
245-
poll_timer_wait(timeout - time_msec());
245+
poll_timer_wait_until(timeout);
246246
poll_block();
247247
} while (time_msec() < timeout);
248248
age_messages();
@@ -868,7 +868,7 @@ fetch_status(struct rconn *rconn, struct dict *dict, long long timeout)
868868

869869
rconn_run_wait(rconn);
870870
rconn_recv_wait(rconn);
871-
poll_timer_wait(timeout - time_msec());
871+
poll_timer_wait_until(timeout);
872872
poll_block();
873873
}
874874
}
@@ -1714,7 +1714,7 @@ menu_show(const struct menu *menu, int start, bool select)
17141714
refresh();
17151715

17161716
if (pos < min || pos > max) {
1717-
poll_timer_wait(adjust - time_msec());
1717+
poll_timer_wait_until(adjust);
17181718
}
17191719
poll_fd_wait(STDIN_FILENO, POLLIN);
17201720
poll_block();
@@ -1946,7 +1946,7 @@ static void
19461946
block_until(long long timeout)
19471947
{
19481948
while (timeout > time_msec()) {
1949-
poll_timer_wait(timeout - time_msec());
1949+
poll_timer_wait_until(timeout);
19501950
poll_block();
19511951
}
19521952
drain_keyboard_buffer();

lib/dhcp-client.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -806,13 +806,8 @@ void
806806
dhclient_wait(struct dhclient *cli)
807807
{
808808
if (cli->min_timeout != UINT_MAX) {
809-
time_t now = time_now();
810-
unsigned int wake = sat_add(cli->state_entered, cli->min_timeout);
811-
if (wake <= now) {
812-
poll_immediate_wake();
813-
} else {
814-
poll_timer_wait(sat_mul(sat_sub(wake, now), 1000));
815-
}
809+
long long int wake = sat_add(cli->state_entered, cli->min_timeout);
810+
poll_timer_wait_until(wake * 1000);
816811
}
817812
/* Reset timeout to 1 second. This will have no effect ordinarily, because
818813
* dhclient_run() will typically set it back to a higher value. If,

lib/learning-switch.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ lswitch_run(struct lswitch *sw, struct rconn *rconn)
220220
static void
221221
wait_timeout(long long int started)
222222
{
223-
long long int now = time_msec();
224-
poll_timer_wait(10000 - (now - started));
223+
poll_timer_wait_until(started + 10000);
225224
}
226225

227226
void

lib/mac-learning.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,6 @@ mac_learning_wait(struct mac_learning *ml)
293293
{
294294
if (!list_is_empty(&ml->lrus)) {
295295
struct mac_entry *e = mac_entry_from_lru_node(ml->lrus.next);
296-
poll_timer_wait((e->expires - time_now()) * 1000);
296+
poll_timer_wait_until(e->expires * 1000LL);
297297
}
298298
}

lib/poll-loop.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,23 @@ poll_timer_wait(long long int msec)
100100
: msec);
101101
}
102102

103+
/* Causes the following call to poll_block() to wake up when the current time,
104+
* as returned by time_msec(), reaches 'msec' or later. If 'msec' is earlier
105+
* than the current time, the following call to poll_block() will not block at
106+
* all.
107+
*
108+
* The timer registration is one-shot: only the following call to poll_block()
109+
* is affected. The timer will need to be re-registered after poll_block() is
110+
* called if it is to persist. */
111+
void
112+
poll_timer_wait_until(long long int msec)
113+
{
114+
long long int now = time_msec();
115+
poll_timer_wait__(msec <= now ? 0
116+
: msec < now + INT_MAX ? msec - now
117+
: INT_MAX);
118+
}
119+
103120
/* Causes the following call to poll_block() to wake up immediately, without
104121
* blocking. */
105122
void

lib/poll-loop.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct poll_waiter;
4343
/* Schedule events to wake up the following poll_block(). */
4444
struct poll_waiter *poll_fd_wait(int fd, short int events);
4545
void poll_timer_wait(long long int msec);
46+
void poll_timer_wait_until(long long int msec);
4647
void poll_immediate_wake(void);
4748

4849
/* Wait until an event occurs. */

lib/rconn.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,8 @@ rconn_run_wait(struct rconn *rc)
500500

501501
timeo = timeout(rc);
502502
if (timeo != UINT_MAX) {
503-
unsigned int expires = sat_add(rc->state_entered, timeo);
504-
unsigned int remaining = sat_sub(expires, time_now());
505-
poll_timer_wait(sat_mul(remaining, 1000));
503+
long long int expires = sat_add(rc->state_entered, timeo);
504+
poll_timer_wait_until(expires * 1000);
506505
}
507506

508507
if ((rc->state & (S_ACTIVE | S_IDLE)) && rc->txq.n) {

ofproto/fail-open.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ void
271271
fail_open_wait(struct fail_open *fo)
272272
{
273273
if (fo->next_bogus_packet_in != LLONG_MAX) {
274-
poll_timer_wait(fo->next_bogus_packet_in - time_msec());
274+
poll_timer_wait_until(fo->next_bogus_packet_in);
275275
}
276276
}
277277

ofproto/in-band.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -745,14 +745,9 @@ in_band_run(struct in_band *ib)
745745
void
746746
in_band_wait(struct in_band *in_band)
747747
{
748-
time_t now = time_now();
749-
time_t wakeup
748+
long long int wakeup
750749
= MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
751-
if (wakeup > now) {
752-
poll_timer_wait((wakeup - now) * 1000);
753-
} else {
754-
poll_immediate_wake();
755-
}
750+
poll_timer_wait_until(wakeup * 1000);
756751
}
757752

758753
/* ofproto has flushed all flows from the flow table and it is calling us back

0 commit comments

Comments
 (0)