X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fpoll-loop.c;h=de080b4c9e5d28c49fa4997fc92f5f1cf68f426a;hb=36ce148c6d2105dc2e485ec169bcc9998ffebf9d;hp=4e192f3ae0eaaacd863ce545247cf698de322850;hpb=959ec62e324d88e47930379d3f0d196119595de9;p=openvswitch diff --git a/lib/poll-loop.c b/lib/poll-loop.c index 4e192f3a..de080b4c 100644 --- a/lib/poll-loop.c +++ b/lib/poll-loop.c @@ -55,9 +55,6 @@ struct poll_waiter { /* All active poll waiters. */ static struct list waiters = LIST_INITIALIZER(&waiters); -/* Number of elements in the waiters list. */ -static size_t n_waiters; - /* Max time to wait in next call to poll_block(), in milliseconds, or -1 to * wait forever. */ static int timeout = -1; @@ -65,6 +62,15 @@ static int timeout = -1; /* Location where waiter created. */ static const char *timeout_where; +/* Array of file descriptors from last run of poll_block(). */ +static struct pollfd *pollfds; + +/* Allocated size of pollfds. */ +static size_t max_pollfds; + +/* Current number of elements in pollfds. */ +static int n_pollfds; + static struct poll_waiter *new_waiter(int fd, short int events, const char *where); @@ -151,10 +157,10 @@ poll_immediate_wake(const char *where) * arguments have two possible interpretations: * * - If 'pollfd' is nonnull then it should be the "struct pollfd" that caused - * the wakeup. In this case, 'timeout' is ignored. + * the wakeup. 'timeout' is ignored. * - * - If 'pollfd' is nonnull then 'timeout' is the number of milliseconds - * after which the poll loop woke up. + * - If 'pollfd' is NULL then 'timeout' is the number of milliseconds after + * which the poll loop woke up. */ static void log_wakeup(const char *where, const struct pollfd *pollfd, int timeout) @@ -213,17 +219,15 @@ log_wakeup(const char *where, const struct pollfd *pollfd, int timeout) void poll_block(void) { - static struct pollfd *pollfds; - static size_t max_pollfds; - struct poll_waiter *pw, *next; - int n_pollfds; + int n_waiters; int retval; /* Register fatal signal events before actually doing any real work for * poll_block. */ fatal_signal_wait(); + n_waiters = list_size(&waiters); if (max_pollfds < n_waiters) { max_pollfds = n_waiters; pollfds = xrealloc(pollfds, max_pollfds * sizeof *pollfds); @@ -275,9 +279,27 @@ poll_cancel(struct poll_waiter *pw) if (pw) { list_remove(&pw->node); free(pw); - n_waiters--; } } + +/* Checks whether the given file descriptor caused the poll loop to wake up + * in the previous iteration. If it did, returns a bitmask of the events + * that caused the wakeup. Otherwise returns 0; + */ +short int +poll_fd_woke(int fd) +{ + int i; + short int events = 0; + + for (i = 0; i < n_pollfds; i++) { + if (pollfds[i].fd == fd) { + events |= pollfds[i].revents; + } + } + + return events; +} /* Creates and returns a new poll_waiter for 'fd' and 'events'. */ static struct poll_waiter * @@ -289,6 +311,5 @@ new_waiter(int fd, short int events, const char *where) waiter->events = events; waiter->where = where; list_push_back(&waiters, &waiter->node); - n_waiters++; return waiter; }