556521ab91882c2b69266919c56d9ede1b699ceb
[openvswitch] / lib / poll-loop.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include "poll-loop.h"
35 #include <assert.h>
36 #include <errno.h>
37 #include <poll.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include "list.h"
41
42 #define THIS_MODULE VLM_poll_loop
43 #include "vlog.h"
44
45 /* An event that will wake the following call to poll_block(). */
46 struct poll_waiter {
47     /* Set when the waiter is created. */
48     struct list node;           /* Element in global waiters list. */
49     int fd;                     /* File descriptor. */
50     short int events;           /* Events to wait for (POLLIN, POLLOUT). */
51     poll_fd_func *function;     /* Callback function, if any, or null. */
52     void *aux;                  /* Argument to callback function. */
53
54     /* Set only when poll_block() is called. */
55     struct pollfd *pollfd;      /* Pointer to element of the pollfds array
56                                    (null if added from a callback). */
57 };
58
59 /* All active poll waiters. */
60 static struct list waiters = LIST_INITIALIZER(&waiters);
61
62 /* Number of elements in the waiters list. */
63 static size_t n_waiters;
64
65 /* Max time to wait in next call to poll_block(), in milliseconds, or -1 to
66  * wait forever. */
67 static int timeout = -1;
68
69 /* Callback currently running, to allow verifying that poll_cancel() is not
70  * being called on a running callback. */
71 #ifndef NDEBUG
72 static struct poll_waiter *running_cb;
73 #endif
74
75 static struct poll_waiter *new_waiter(int fd, short int events);
76
77 /* Registers 'fd' as waiting for the specified 'events' (which should be POLLIN
78  * or POLLOUT or POLLIN | POLLOUT).  The following call to poll_block() will
79  * wake up when 'fd' becomes ready for one or more of the requested events.
80  *
81  * The event registration is one-shot: only the following call to poll_block()
82  * is affected.  The event will need to be re-registered after poll_block() is
83  * called if it is to persist. */
84 struct poll_waiter *
85 poll_fd_wait(int fd, short int events)
86 {
87     return new_waiter(fd, events);
88 }
89
90 /* Causes the following call to poll_block() to block for no more than 'msec'
91  * milliseconds.  If 'msec' is nonpositive, the following call to poll_block()
92  * will not block at all.
93  *
94  * The timer registration is one-shot: only the following call to poll_block()
95  * is affected.  The timer will need to be re-registered after poll_block() is
96  * called if it is to persist. */
97 void
98 poll_timer_wait(int msec)
99 {
100     if (timeout < 0 || msec < timeout) {
101         timeout = MAX(0, msec);
102     }
103 }
104
105 /* Causes the following call to poll_block() to wake up immediately, without
106  * blocking. */
107 void
108 poll_immediate_wake(void)
109 {
110     timeout = 0;
111 }
112
113 /* Blocks until one or more of the events registered with poll_fd_wait()
114  * occurs, or until the minimum duration registered with poll_timer_wait()
115  * elapses, or not at all if poll_immediate_wake() has been called.
116  *
117  * Also executes any autonomous subroutines registered with poll_fd_callback(),
118  * if their file descriptor have become ready. */
119 void
120 poll_block(void)
121 {
122     static struct pollfd *pollfds;
123     static size_t max_pollfds;
124
125     struct poll_waiter *pw;
126     struct list *node;
127     int n_pollfds;
128     int retval;
129
130     assert(!running_cb);
131     if (max_pollfds < n_waiters) {
132         max_pollfds = n_waiters;
133         pollfds = xrealloc(pollfds, max_pollfds * sizeof *pollfds);
134     }
135
136     n_pollfds = 0;
137     LIST_FOR_EACH (pw, struct poll_waiter, node, &waiters) {
138         pw->pollfd = &pollfds[n_pollfds];
139         pollfds[n_pollfds].fd = pw->fd;
140         pollfds[n_pollfds].events = pw->events;
141         pollfds[n_pollfds].revents = 0;
142         n_pollfds++;
143     }
144
145     do {
146         retval = poll(pollfds, n_pollfds, timeout);
147     } while (retval < 0 && errno == EINTR);
148     if (retval < 0) {
149         VLOG_ERR("poll: %s", strerror(errno));
150     }
151
152     for (node = waiters.next; node != &waiters; ) {
153         pw = CONTAINER_OF(node, struct poll_waiter, node);
154         if (!pw->pollfd || !pw->pollfd->revents) {
155             if (pw->function) {
156                 node = node->next;
157                 continue;
158             }
159         } else if (pw->function) {
160 #ifndef NDEBUG
161             running_cb = pw;
162 #endif
163             pw->function(pw->fd, pw->pollfd->revents, pw->aux);
164 #ifndef NDEBUG
165             running_cb = NULL;
166 #endif
167         }
168         node = node->next;
169         poll_cancel(pw);
170     }
171
172     timeout = -1;
173 }
174
175 /* Registers 'function' to be called with argument 'aux' by poll_block() when
176  * 'fd' becomes ready for one of the events in 'events', which should be POLLIN
177  * or POLLOUT or POLLIN | POLLOUT.
178  *
179  * The callback registration persists until the event actually occurs.  At that
180  * point, it is automatically de-registered.  The callback function must
181  * re-register the event by calling poll_fd_callback() again within the
182  * callback, if it wants to be called back again later. */
183 struct poll_waiter *
184 poll_fd_callback(int fd, short int events, poll_fd_func *function, void *aux)
185 {
186     struct poll_waiter *pw = new_waiter(fd, events);
187     pw->function = function;
188     pw->aux = aux;
189     return pw;
190 }
191
192 /* Cancels the file descriptor event registered with poll_fd_wait() or
193  * poll_fd_callback().  'pw' must be the struct poll_waiter returned by one of
194  * those functions.
195  *
196  * An event registered with poll_fd_wait() may be canceled from its time of
197  * registration until the next call to poll_block().  At that point, the event
198  * is automatically canceled by the system and its poll_waiter is freed.
199  *
200  * An event registered with poll_fd_callback() may be canceled from its time of
201  * registration until its callback is actually called.  At that point, the
202  * event is automatically canceled by the system and its poll_waiter is
203  * freed. */
204 void
205 poll_cancel(struct poll_waiter *pw)
206 {
207     if (pw) {
208         assert(pw != running_cb);
209         list_remove(&pw->node);
210         free(pw);
211         n_waiters--;
212     }
213 }
214 \f
215 /* Creates and returns a new poll_waiter for 'fd' and 'events'. */
216 static struct poll_waiter *
217 new_waiter(int fd, short int events)
218 {
219     struct poll_waiter *waiter = xcalloc(1, sizeof *waiter);
220     assert(fd >= 0);
221     waiter->fd = fd;
222     waiter->events = events;
223     list_push_back(&waiters, &waiter->node);
224     n_waiters++;
225     return waiter;
226 }