1 /* Copyright (C) 2008 Board of Trustees, Leland Stanford Jr. University.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 #include "poll-loop.h"
29 #define THIS_MODULE VLM_poll_loop
36 struct pollfd *pollfd;
40 poll_fd_func *function;
44 static struct list waiters = LIST_INITIALIZER(&waiters);
45 static size_t n_waiters;
46 static int timeout = -1;
49 static struct poll_waiter *running_cb;
52 static struct poll_waiter *
53 new_waiter(int fd, short int events)
55 struct poll_waiter *waiter = xcalloc(1, sizeof *waiter);
58 waiter->events = events;
59 list_push_back(&waiters, &waiter->node);
65 poll_fd_callback(int fd, short int events, poll_fd_func *function, void *aux)
67 struct poll_waiter *pw = new_waiter(fd, events);
68 pw->function = function;
74 poll_fd_wait(int fd, short int events, short int *revents)
76 struct poll_waiter *pw = new_waiter(fd, events);
77 pw->revents = revents;
85 poll_cancel(struct poll_waiter *pw)
88 assert(pw != running_cb);
89 list_remove(&pw->node);
95 poll_immediate_wake(void)
101 poll_timer_wait(int msec)
103 if (timeout < 0 || msec < timeout) {
104 timeout = MAX(0, msec);
111 static struct pollfd *pollfds;
112 static size_t max_pollfds;
114 struct poll_waiter *pw;
120 if (max_pollfds < n_waiters) {
121 max_pollfds = n_waiters;
122 pollfds = xrealloc(pollfds, max_pollfds * sizeof *pollfds);
126 LIST_FOR_EACH (pw, struct poll_waiter, node, &waiters) {
127 pw->pollfd = &pollfds[n_pollfds];
128 pollfds[n_pollfds].fd = pw->fd;
129 pollfds[n_pollfds].events = pw->events;
130 pollfds[n_pollfds].revents = 0;
135 retval = poll(pollfds, n_pollfds, timeout);
136 } while (retval < 0 && errno == EINTR);
138 VLOG_ERR("poll: %s", strerror(errno));
141 for (node = waiters.next; node != &waiters; ) {
142 pw = CONTAINER_OF(node, struct poll_waiter, node);
143 if (!pw->pollfd || !pw->pollfd->revents) {
153 pw->function(pw->fd, pw->pollfd->revents, pw->aux);
157 } else if (pw->revents) {
158 *pw->revents = pw->pollfd->revents;
161 node = list_remove(node);