Import libevent-1.3e.
[dragonfly.git] / contrib / libevent / poll.c
1 /*      $OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
2
3 /*
4  * Copyright 2000-2003 Niels Provos <provos@citi.umich.edu>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
17  *
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.
28  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_TIME_H
35 #include <sys/time.h>
36 #else
37 #include <sys/_time.h>
38 #endif
39 #include <sys/queue.h>
40 #include <sys/tree.h>
41 #include <poll.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <errno.h>
48 #ifdef CHECK_INVARIANTS
49 #include <assert.h>
50 #endif
51
52 #include "event.h"
53 #include "event-internal.h"
54 #include "evsignal.h"
55 #include "log.h"
56
57 struct pollop {
58         int event_count;                /* Highest number alloc */
59         int nfds;                       /* Size of event_* */
60         int fd_count;                   /* Size of idxplus1_by_fd */
61         struct pollfd *event_set;
62         struct event **event_r_back;
63         struct event **event_w_back;
64         int *idxplus1_by_fd; /* Index into event_set by fd; we add 1 so
65                               * that 0 (which is easy to memset) can mean
66                               * "no entry." */
67 };
68
69 void *poll_init (struct event_base *);
70 int poll_add            (void *, struct event *);
71 int poll_del            (void *, struct event *);
72 int poll_recalc         (struct event_base *, void *, int);
73 int poll_dispatch       (struct event_base *, void *, struct timeval *);
74 void poll_dealloc       (struct event_base *, void *);
75
76 const struct eventop pollops = {
77         "poll",
78         poll_init,
79         poll_add,
80         poll_del,
81         poll_recalc,
82         poll_dispatch,
83         poll_dealloc
84 };
85
86 void *
87 poll_init(struct event_base *base)
88 {
89         struct pollop *pollop;
90
91         /* Disable poll when this environment variable is set */
92         if (getenv("EVENT_NOPOLL"))
93                 return (NULL);
94
95         if (!(pollop = calloc(1, sizeof(struct pollop))))
96                 return (NULL);
97
98         evsignal_init(base);
99
100         return (pollop);
101 }
102
103 /*
104  * Called with the highest fd that we know about.  If it is 0, completely
105  * recalculate everything.
106  */
107
108 int
109 poll_recalc(struct event_base *base, void *arg, int max)
110 {
111         return (0);
112 }
113
114 #ifdef CHECK_INVARIANTS
115 static void
116 poll_check_ok(struct pollop *pop)
117 {
118         int i, idx;
119         struct event *ev;
120
121         for (i = 0; i < pop->fd_count; ++i) {
122                 idx = pop->idxplus1_by_fd[i]-1;
123                 if (idx < 0)
124                         continue;
125                 assert(pop->event_set[idx].fd == i);
126                 if (pop->event_set[idx].events & POLLIN) {
127                         ev = pop->event_r_back[idx];
128                         assert(ev);
129                         assert(ev->ev_events & EV_READ);
130                         assert(ev->ev_fd == i);
131                 }
132                 if (pop->event_set[idx].events & POLLOUT) {
133                         ev = pop->event_w_back[idx];
134                         assert(ev);
135                         assert(ev->ev_events & EV_WRITE);
136                         assert(ev->ev_fd == i);
137                 }
138         }
139         for (i = 0; i < pop->nfds; ++i) {
140                 struct pollfd *pfd = &pop->event_set[i];
141                 assert(pop->idxplus1_by_fd[pfd->fd] == i+1);
142         }
143 }
144 #else
145 #define poll_check_ok(pop)
146 #endif
147
148 int
149 poll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
150 {
151         int res, i, msec = -1, nfds;
152         struct pollop *pop = arg;
153
154         poll_check_ok(pop);
155
156         if (tv != NULL)
157                 msec = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
158
159         nfds = pop->nfds;
160         res = poll(pop->event_set, nfds, msec);
161
162         if (res == -1) {
163                 if (errno != EINTR) {
164                         event_warn("poll");
165                         return (-1);
166                 }
167
168                 evsignal_process(base);
169                 return (0);
170         } else if (base->sig.evsignal_caught) {
171                 evsignal_process(base);
172         }
173
174         event_debug(("%s: poll reports %d", __func__, res));
175
176         if (res == 0)
177                 return (0);
178
179         for (i = 0; i < nfds; i++) {
180                 int what = pop->event_set[i].revents;
181                 struct event *r_ev = NULL, *w_ev = NULL;
182                 if (!what)
183                         continue;
184
185                 res = 0;
186
187                 /* If the file gets closed notify */
188                 if (what & (POLLHUP|POLLERR))
189                         what |= POLLIN|POLLOUT;
190                 if (what & POLLIN) {
191                         res |= EV_READ;
192                         r_ev = pop->event_r_back[i];
193                 }
194                 if (what & POLLOUT) {
195                         res |= EV_WRITE;
196                         w_ev = pop->event_w_back[i];
197                 }
198                 if (res == 0)
199                         continue;
200
201                 if (r_ev && (res & r_ev->ev_events)) {
202                         if (!(r_ev->ev_events & EV_PERSIST))
203                                 event_del(r_ev);
204                         event_active(r_ev, res & r_ev->ev_events, 1);
205                 }
206                 if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) {
207                         if (!(w_ev->ev_events & EV_PERSIST))
208                                 event_del(w_ev);
209                         event_active(w_ev, res & w_ev->ev_events, 1);
210                 }
211         }
212
213         return (0);
214 }
215
216 int
217 poll_add(void *arg, struct event *ev)
218 {
219         struct pollop *pop = arg;
220         struct pollfd *pfd = NULL;
221         int i;
222
223         if (ev->ev_events & EV_SIGNAL)
224                 return (evsignal_add(ev));
225         if (!(ev->ev_events & (EV_READ|EV_WRITE)))
226                 return (0);
227
228         poll_check_ok(pop);
229         if (pop->nfds + 1 >= pop->event_count) {
230                 struct pollfd *tmp_event_set;
231                 struct event **tmp_event_r_back;
232                 struct event **tmp_event_w_back;
233                 int tmp_event_count;
234
235                 if (pop->event_count < 32)
236                         tmp_event_count = 32;
237                 else
238                         tmp_event_count = pop->event_count * 2;
239
240                 /* We need more file descriptors */
241                 tmp_event_set = realloc(pop->event_set,
242                                  tmp_event_count * sizeof(struct pollfd));
243                 if (tmp_event_set == NULL) {
244                         event_warn("realloc");
245                         return (-1);
246                 }
247                 pop->event_set = tmp_event_set;
248
249                 tmp_event_r_back = realloc(pop->event_r_back,
250                             tmp_event_count * sizeof(struct event *));
251                 if (tmp_event_r_back == NULL) {
252                         /* event_set overallocated; that's okay. */
253                         event_warn("realloc");
254                         return (-1);
255                 }
256                 pop->event_r_back = tmp_event_r_back;
257
258                 tmp_event_w_back = realloc(pop->event_w_back,
259                             tmp_event_count * sizeof(struct event *));
260                 if (tmp_event_w_back == NULL) {
261                         /* event_set and event_r_back overallocated; that's
262                          * okay. */
263                         event_warn("realloc");
264                         return (-1);
265                 }
266                 pop->event_w_back = tmp_event_w_back;
267
268                 pop->event_count = tmp_event_count;
269         }
270         if (ev->ev_fd >= pop->fd_count) {
271                 int *tmp_idxplus1_by_fd;
272                 int new_count;
273                 if (pop->fd_count < 32)
274                         new_count = 32;
275                 else
276                         new_count = pop->fd_count * 2;
277                 while (new_count <= ev->ev_fd)
278                         new_count *= 2;
279                 tmp_idxplus1_by_fd =
280                         realloc(pop->idxplus1_by_fd, new_count * sizeof(int));
281                 if (tmp_idxplus1_by_fd == NULL) {
282                         event_warn("realloc");
283                         return (-1);
284                 }
285                 pop->idxplus1_by_fd = tmp_idxplus1_by_fd;
286                 memset(pop->idxplus1_by_fd + pop->fd_count,
287                        0, sizeof(int)*(new_count - pop->fd_count));
288                 pop->fd_count = new_count;
289         }
290
291         i = pop->idxplus1_by_fd[ev->ev_fd] - 1;
292         if (i >= 0) {
293                 pfd = &pop->event_set[i];
294         } else {
295                 i = pop->nfds++;
296                 pfd = &pop->event_set[i];
297                 pfd->events = 0;
298                 pfd->fd = ev->ev_fd;
299                 pop->event_w_back[i] = pop->event_r_back[i] = NULL;
300                 pop->idxplus1_by_fd[ev->ev_fd] = i + 1;
301         }
302
303         pfd->revents = 0;
304         if (ev->ev_events & EV_WRITE) {
305                 pfd->events |= POLLOUT;
306                 pop->event_w_back[i] = ev;
307         }
308         if (ev->ev_events & EV_READ) {
309                 pfd->events |= POLLIN;
310                 pop->event_r_back[i] = ev;
311         }
312         poll_check_ok(pop);
313
314         return (0);
315 }
316
317 /*
318  * Nothing to be done here.
319  */
320
321 int
322 poll_del(void *arg, struct event *ev)
323 {
324         struct pollop *pop = arg;
325         struct pollfd *pfd = NULL;
326         int i;
327
328         if (ev->ev_events & EV_SIGNAL)
329                 return (evsignal_del(ev));
330
331         if (!(ev->ev_events & (EV_READ|EV_WRITE)))
332                 return (0);
333
334         poll_check_ok(pop);
335         i = pop->idxplus1_by_fd[ev->ev_fd] - 1;
336         if (i < 0)
337                 return (-1);
338
339         /* Do we still want to read or write? */
340         pfd = &pop->event_set[i];
341         if (ev->ev_events & EV_READ) {
342                 pfd->events &= ~POLLIN;
343                 pop->event_r_back[i] = NULL;
344         }
345         if (ev->ev_events & EV_WRITE) {
346                 pfd->events &= ~POLLOUT;
347                 pop->event_w_back[i] = NULL;
348         }
349         poll_check_ok(pop);
350         if (pfd->events)
351                 /* Another event cares about that fd. */
352                 return (0);
353
354         /* Okay, so we aren't interested in that fd anymore. */
355         pop->idxplus1_by_fd[ev->ev_fd] = 0;
356
357         --pop->nfds;
358         if (i != pop->nfds) {
359                 /* 
360                  * Shift the last pollfd down into the now-unoccupied
361                  * position.
362                  */
363                 memcpy(&pop->event_set[i], &pop->event_set[pop->nfds],
364                        sizeof(struct pollfd));
365                 pop->event_r_back[i] = pop->event_r_back[pop->nfds];
366                 pop->event_w_back[i] = pop->event_w_back[pop->nfds];
367                 pop->idxplus1_by_fd[pop->event_set[i].fd] = i + 1;
368         }
369
370         poll_check_ok(pop);
371         return (0);
372 }
373
374 void
375 poll_dealloc(struct event_base *base, void *arg)
376 {
377         struct pollop *pop = arg;
378
379         evsignal_dealloc(base);
380         if (pop->event_set)
381                 free(pop->event_set);
382         if (pop->event_r_back)
383                 free(pop->event_r_back);
384         if (pop->event_w_back)
385                 free(pop->event_w_back);
386         if (pop->idxplus1_by_fd)
387                 free(pop->idxplus1_by_fd);
388
389         memset(pop, 0, sizeof(struct pollop));
390         free(pop);
391 }