Move Autoconf's macro definitions into config.h.
[sliver-openvswitch.git] / 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 <config.h>
35 #include "poll-loop.h"
36 #include <assert.h>
37 #include <errno.h>
38 #include <poll.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include "list.h"
42
43 #define THIS_MODULE VLM_poll_loop
44 #include "vlog.h"
45
46 /* An event that will wake the following call to poll_block(). */
47 struct poll_waiter {
48     /* Set when the waiter is created. */
49     struct list node;           /* Element in global waiters list. */
50     int fd;                     /* File descriptor. */
51     short int events;           /* Events to wait for (POLLIN, POLLOUT). */
52     poll_fd_func *function;     /* Callback function, if any, or null. */
53     void *aux;                  /* Argument to callback function. */
54
55     /* Set only when poll_block() is called. */
56     struct pollfd *pollfd;      /* Pointer to element of the pollfds array
57                                    (null if added from a callback). */
58 };
59
60 /* All active poll waiters. */
61 static struct list waiters = LIST_INITIALIZER(&waiters);
62
63 /* Number of elements in the waiters list. */
64 static size_t n_waiters;
65
66 /* Max time to wait in next call to poll_block(), in milliseconds, or -1 to
67  * wait forever. */
68 static int timeout = -1;
69
70 /* Callback currently running, to allow verifying that poll_cancel() is not
71  * being called on a running callback. */
72 #ifndef NDEBUG
73 static struct poll_waiter *running_cb;
74 #endif
75
76 static struct poll_waiter *new_waiter(int fd, short int events);
77
78 /* Registers 'fd' as waiting for the specified 'events' (which should be POLLIN
79  * or POLLOUT or POLLIN | POLLOUT).  The following call to poll_block() will
80  * wake up when 'fd' becomes ready for one or more of the requested events.
81  *
82  * The event registration is one-shot: only the following call to poll_block()
83  * is affected.  The event will need to be re-registered after poll_block() is
84  * called if it is to persist. */
85 struct poll_waiter *
86 poll_fd_wait(int fd, short int events)
87 {
88     return new_waiter(fd, events);
89 }
90
91 /* Causes the following call to poll_block() to block for no more than 'msec'
92  * milliseconds.  If 'msec' is nonpositive, the following call to poll_block()
93  * will not block at all.
94  *
95  * The timer registration is one-shot: only the following call to poll_block()
96  * is affected.  The timer will need to be re-registered after poll_block() is
97  * called if it is to persist. */
98 void
99 poll_timer_wait(int msec)
100 {
101     if (timeout < 0 || msec < timeout) {
102         timeout = MAX(0, msec);
103     }
104 }
105
106 /* Causes the following call to poll_block() to wake up immediately, without
107  * blocking. */
108 void
109 poll_immediate_wake(void)
110 {
111     timeout = 0;
112 }
113
114 /* Blocks until one or more of the events registered with poll_fd_wait()
115  * occurs, or until the minimum duration registered with poll_timer_wait()
116  * elapses, or not at all if poll_immediate_wake() has been called.
117  *
118  * Also executes any autonomous subroutines registered with poll_fd_callback(),
119  * if their file descriptor have become ready. */
120 void
121 poll_block(void)
122 {
123     static struct pollfd *pollfds;
124     static size_t max_pollfds;
125
126     struct poll_waiter *pw;
127     struct list *node;
128     int n_pollfds;
129     int retval;
130
131     assert(!running_cb);
132     if (max_pollfds < n_waiters) {
133         max_pollfds = n_waiters;
134         pollfds = xrealloc(pollfds, max_pollfds * sizeof *pollfds);
135     }
136
137     n_pollfds = 0;
138     LIST_FOR_EACH (pw, struct poll_waiter, node, &waiters) {
139         pw->pollfd = &pollfds[n_pollfds];
140         pollfds[n_pollfds].fd = pw->fd;
141         pollfds[n_pollfds].events = pw->events;
142         pollfds[n_pollfds].revents = 0;
143         n_pollfds++;
144     }
145
146     do {
147         retval = poll(pollfds, n_pollfds, timeout);
148     } while (retval < 0 && errno == EINTR);
149     if (retval < 0) {
150         VLOG_ERR("poll: %s", strerror(errno));
151     }
152
153     for (node = waiters.next; node != &waiters; ) {
154         pw = CONTAINER_OF(node, struct poll_waiter, node);
155         if (!pw->pollfd || !pw->pollfd->revents) {
156             if (pw->function) {
157                 node = node->next;
158                 continue;
159             }
160         } else if (pw->function) {
161 #ifndef NDEBUG
162             running_cb = pw;
163 #endif
164             pw->function(pw->fd, pw->pollfd->revents, pw->aux);
165 #ifndef NDEBUG
166             running_cb = NULL;
167 #endif
168         }
169         node = node->next;
170         poll_cancel(pw);
171     }
172
173     timeout = -1;
174 }
175
176 /* Registers 'function' to be called with argument 'aux' by poll_block() when
177  * 'fd' becomes ready for one of the events in 'events', which should be POLLIN
178  * or POLLOUT or POLLIN | POLLOUT.
179  *
180  * The callback registration persists until the event actually occurs.  At that
181  * point, it is automatically de-registered.  The callback function must
182  * re-register the event by calling poll_fd_callback() again within the
183  * callback, if it wants to be called back again later. */
184 struct poll_waiter *
185 poll_fd_callback(int fd, short int events, poll_fd_func *function, void *aux)
186 {
187     struct poll_waiter *pw = new_waiter(fd, events);
188     pw->function = function;
189     pw->aux = aux;
190     return pw;
191 }
192
193 /* Cancels the file descriptor event registered with poll_fd_wait() or
194  * poll_fd_callback().  'pw' must be the struct poll_waiter returned by one of
195  * those functions.
196  *
197  * An event registered with poll_fd_wait() may be canceled from its time of
198  * registration until the next call to poll_block().  At that point, the event
199  * is automatically canceled by the system and its poll_waiter is freed.
200  *
201  * An event registered with poll_fd_callback() may be canceled from its time of
202  * registration until its callback is actually called.  At that point, the
203  * event is automatically canceled by the system and its poll_waiter is
204  * freed. */
205 void
206 poll_cancel(struct poll_waiter *pw)
207 {
208     if (pw) {
209         assert(pw != running_cb);
210         list_remove(&pw->node);
211         free(pw);
212         n_waiters--;
213     }
214 }
215 \f
216 /* Creates and returns a new poll_waiter for 'fd' and 'events'. */
217 static struct poll_waiter *
218 new_waiter(int fd, short int events)
219 {
220     struct poll_waiter *waiter = xcalloc(1, sizeof *waiter);
221     assert(fd >= 0);
222     waiter->fd = fd;
223     waiter->events = events;
224     list_push_back(&waiters, &waiter->node);
225     n_waiters++;
226     return waiter;
227 }