1 /* $OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
4 * Copyright 2000-2003 Niels Provos <provos@citi.umich.edu>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_TIME_H
37 #include <sys/_time.h>
39 #include <sys/queue.h>
48 #ifdef CHECK_INVARIANTS
53 #include "event-internal.h"
57 extern volatile sig_atomic_t evsignal_caught;
60 int event_count; /* Highest number alloc */
61 int nfds; /* Size of event_* */
62 int fd_count; /* Size of idxplus1_by_fd */
63 struct pollfd *event_set;
64 struct event **event_r_back;
65 struct event **event_w_back;
66 int *idxplus1_by_fd; /* Index into event_set by fd; we add 1 so
67 * that 0 (which is easy to memset) can mean
71 void *poll_init (void);
72 int poll_add (void *, struct event *);
73 int poll_del (void *, struct event *);
74 int poll_recalc (struct event_base *, void *, int);
75 int poll_dispatch (struct event_base *, void *, struct timeval *);
76 void poll_dealloc (void *);
78 const struct eventop pollops = {
91 struct pollop *pollop;
93 /* Disable poll when this environment variable is set */
94 if (getenv("EVENT_NOPOLL"))
97 if (!(pollop = calloc(1, sizeof(struct pollop))))
106 * Called with the highest fd that we know about. If it is 0, completely
107 * recalculate everything.
111 poll_recalc(struct event_base *base, void *arg, int max)
116 #ifdef CHECK_INVARIANTS
118 poll_check_ok(struct pollop *pop)
123 for (i = 0; i < pop->fd_count; ++i) {
124 idx = pop->idxplus1_by_fd[i]-1;
127 assert(pop->event_set[idx].fd == i);
128 if (pop->event_set[idx].events & POLLIN) {
129 ev = pop->event_r_back[idx];
131 assert(ev->ev_events & EV_READ);
132 assert(ev->ev_fd == i);
134 if (pop->event_set[idx].events & POLLOUT) {
135 ev = pop->event_w_back[idx];
137 assert(ev->ev_events & EV_WRITE);
138 assert(ev->ev_fd == i);
141 for (i = 0; i < pop->nfds; ++i) {
142 struct pollfd *pfd = &pop->event_set[i];
143 assert(pop->idxplus1_by_fd[pfd->fd] == i+1);
147 #define poll_check_ok(pop)
151 poll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
153 int res, i, sec, nfds;
154 struct pollop *pop = arg;
157 sec = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
159 res = poll(pop->event_set, nfds, sec);
162 if (errno != EINTR) {
169 } else if (evsignal_caught)
172 event_debug(("%s: poll reports %d", __func__, res));
177 for (i = 0; i < nfds; i++) {
178 int what = pop->event_set[i].revents;
179 struct event *r_ev = NULL, *w_ev = NULL;
185 /* If the file gets closed notify */
186 if (what & (POLLHUP|POLLERR))
187 what |= POLLIN|POLLOUT;
190 r_ev = pop->event_r_back[i];
192 if (what & POLLOUT) {
194 w_ev = pop->event_w_back[i];
199 if (r_ev && (res & r_ev->ev_events)) {
200 if (!(r_ev->ev_events & EV_PERSIST))
202 event_active(r_ev, res & r_ev->ev_events, 1);
204 if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) {
205 if (!(w_ev->ev_events & EV_PERSIST))
207 event_active(w_ev, res & w_ev->ev_events, 1);
215 poll_add(void *arg, struct event *ev)
217 struct pollop *pop = arg;
218 struct pollfd *pfd = NULL;
221 if (ev->ev_events & EV_SIGNAL)
222 return (evsignal_add(ev));
223 if (!(ev->ev_events & (EV_READ|EV_WRITE)))
227 if (pop->nfds + 1 >= pop->event_count) {
228 struct pollfd *tmp_event_set;
229 struct event **tmp_event_r_back;
230 struct event **tmp_event_w_back;
233 if (pop->event_count < 32)
234 tmp_event_count = 32;
236 tmp_event_count = pop->event_count * 2;
238 /* We need more file descriptors */
239 tmp_event_set = realloc(pop->event_set,
240 tmp_event_count * sizeof(struct pollfd));
241 if (tmp_event_set == NULL) {
242 event_warn("realloc");
245 pop->event_set = tmp_event_set;
247 tmp_event_r_back = realloc(pop->event_r_back,
248 tmp_event_count * sizeof(struct event *));
249 if (tmp_event_r_back == NULL) {
250 /* event_set overallocated; that's okay. */
251 event_warn("realloc");
254 pop->event_r_back = tmp_event_r_back;
256 tmp_event_w_back = realloc(pop->event_w_back,
257 tmp_event_count * sizeof(struct event *));
258 if (tmp_event_w_back == NULL) {
259 /* event_set and event_r_back overallocated; that's
261 event_warn("realloc");
264 pop->event_w_back = tmp_event_w_back;
266 pop->event_count = tmp_event_count;
268 if (ev->ev_fd >= pop->fd_count) {
269 int *tmp_idxplus1_by_fd;
271 if (pop->fd_count < 32)
274 new_count = pop->fd_count * 2;
275 while (new_count <= ev->ev_fd)
278 realloc(pop->idxplus1_by_fd, new_count * sizeof(int));
279 if (tmp_idxplus1_by_fd == NULL) {
280 event_warn("realloc");
283 pop->idxplus1_by_fd = tmp_idxplus1_by_fd;
284 memset(pop->idxplus1_by_fd + pop->fd_count,
285 0, sizeof(int)*(new_count - pop->fd_count));
286 pop->fd_count = new_count;
289 i = pop->idxplus1_by_fd[ev->ev_fd] - 1;
291 pfd = &pop->event_set[i];
294 pfd = &pop->event_set[i];
297 pop->event_w_back[i] = pop->event_r_back[i] = NULL;
298 pop->idxplus1_by_fd[ev->ev_fd] = i + 1;
302 if (ev->ev_events & EV_WRITE) {
303 pfd->events |= POLLOUT;
304 pop->event_w_back[i] = ev;
306 if (ev->ev_events & EV_READ) {
307 pfd->events |= POLLIN;
308 pop->event_r_back[i] = ev;
316 * Nothing to be done here.
320 poll_del(void *arg, struct event *ev)
322 struct pollop *pop = arg;
323 struct pollfd *pfd = NULL;
326 if (ev->ev_events & EV_SIGNAL)
327 return (evsignal_del(ev));
329 if (!(ev->ev_events & (EV_READ|EV_WRITE)))
333 i = pop->idxplus1_by_fd[ev->ev_fd] - 1;
337 /* Do we still want to read or write? */
338 pfd = &pop->event_set[i];
339 if (ev->ev_events & EV_READ) {
340 pfd->events &= ~POLLIN;
341 pop->event_r_back[i] = NULL;
343 if (ev->ev_events & EV_WRITE) {
344 pfd->events &= ~POLLOUT;
345 pop->event_w_back[i] = NULL;
349 /* Another event cares about that fd. */
352 /* Okay, so we aren't interested in that fd anymore. */
353 pop->idxplus1_by_fd[ev->ev_fd] = 0;
356 if (i != pop->nfds) {
358 * Shift the last pollfd down into the now-unoccupied
361 memcpy(&pop->event_set[i], &pop->event_set[pop->nfds],
362 sizeof(struct pollfd));
363 pop->event_r_back[i] = pop->event_r_back[pop->nfds];
364 pop->event_w_back[i] = pop->event_w_back[pop->nfds];
365 pop->idxplus1_by_fd[pop->event_set[i].fd] = i + 1;
373 poll_dealloc(void *arg)
375 struct pollop *pop = arg;
378 free(pop->event_set);
379 if (pop->event_r_back)
380 free(pop->event_r_back);
381 if (pop->event_w_back)
382 free(pop->event_w_back);
383 if (pop->idxplus1_by_fd)
384 free(pop->idxplus1_by_fd);
386 memset(pop, 0, sizeof(struct pollop));