1 /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
4 * Copyright 2000-2002 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>
47 #ifdef CHECK_INVARIANTS
52 #include "event-internal.h"
57 #define howmany(x, y) (((x)+((y)-1))/(y))
60 extern volatile sig_atomic_t evsignal_caught;
63 int event_fds; /* Highest fd in fd set */
65 fd_set *event_readset_in;
66 fd_set *event_writeset_in;
67 fd_set *event_readset_out;
68 fd_set *event_writeset_out;
69 struct event **event_r_by_fd;
70 struct event **event_w_by_fd;
73 void *select_init (void);
74 int select_add (void *, struct event *);
75 int select_del (void *, struct event *);
76 int select_recalc (struct event_base *, void *, int);
77 int select_dispatch (struct event_base *, void *, struct timeval *);
78 void select_dealloc (void *);
80 const struct eventop selectops = {
90 static int select_resize(struct selectop *sop, int fdsz);
97 /* Disable select when this environment variable is set */
98 if (getenv("EVENT_NOSELECT"))
101 if (!(sop = calloc(1, sizeof(struct selectop))))
104 select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask));
111 #ifdef CHECK_INVARIANTS
113 check_selectop(struct selectop *sop)
116 for (i=0;i<=sop->event_fds;++i) {
117 if (FD_ISSET(i, sop->event_readset_in)) {
118 assert(sop->event_r_by_fd[i]);
119 assert(sop->event_r_by_fd[i]->ev_events & EV_READ);
120 assert(sop->event_r_by_fd[i]->ev_fd == i);
122 assert(! sop->event_r_by_fd[i]);
124 if (FD_ISSET(i, sop->event_writeset_in)) {
125 assert(sop->event_w_by_fd[i]);
126 assert(sop->event_w_by_fd[i]->ev_events & EV_WRITE);
127 assert(sop->event_w_by_fd[i]->ev_fd == i);
129 assert(! sop->event_w_by_fd[i]);
135 #define check_selectop(sop) do { (void) sop; } while (0)
139 * Called with the highest fd that we know about. If it is 0, completely
140 * recalculate everything.
144 select_recalc(struct event_base *base, void *arg, int max)
146 struct selectop *sop = arg;
154 select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
157 struct selectop *sop = arg;
161 memcpy(sop->event_readset_out, sop->event_readset_in,
163 memcpy(sop->event_writeset_out, sop->event_writeset_in,
166 res = select(sop->event_fds + 1, sop->event_readset_out,
167 sop->event_writeset_out, NULL, tv);
172 if (errno != EINTR) {
173 event_warn("select");
179 } else if (evsignal_caught)
182 event_debug(("%s: select reports %d", __func__, res));
185 for (i = 0; i <= sop->event_fds; ++i) {
186 struct event *r_ev = NULL, *w_ev = NULL;
188 if (FD_ISSET(i, sop->event_readset_out)) {
189 r_ev = sop->event_r_by_fd[i];
192 if (FD_ISSET(i, sop->event_writeset_out)) {
193 w_ev = sop->event_w_by_fd[i];
196 if (r_ev && (res & r_ev->ev_events)) {
197 if (!(r_ev->ev_events & EV_PERSIST))
199 event_active(r_ev, res & r_ev->ev_events, 1);
201 if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) {
202 if (!(w_ev->ev_events & EV_PERSIST))
204 event_active(w_ev, res & w_ev->ev_events, 1);
214 select_resize(struct selectop *sop, int fdsz)
216 int n_events, n_events_old;
218 fd_set *readset_in = NULL;
219 fd_set *writeset_in = NULL;
220 fd_set *readset_out = NULL;
221 fd_set *writeset_out = NULL;
222 struct event **r_by_fd = NULL;
223 struct event **w_by_fd = NULL;
225 n_events = (fdsz/sizeof(fd_mask)) * NFDBITS;
226 n_events_old = (sop->event_fdsz/sizeof(fd_mask)) * NFDBITS;
228 if (sop->event_readset_in)
231 if ((readset_in = realloc(sop->event_readset_in, fdsz)) == NULL)
233 sop->event_readset_in = readset_in;
234 if ((readset_out = realloc(sop->event_readset_out, fdsz)) == NULL)
236 sop->event_readset_out = readset_out;
237 if ((writeset_in = realloc(sop->event_writeset_in, fdsz)) == NULL)
239 sop->event_writeset_in = writeset_in;
240 if ((writeset_out = realloc(sop->event_writeset_out, fdsz)) == NULL)
242 sop->event_writeset_out = writeset_out;
243 if ((r_by_fd = realloc(sop->event_r_by_fd,
244 n_events*sizeof(struct event*))) == NULL)
246 sop->event_r_by_fd = r_by_fd;
247 if ((w_by_fd = realloc(sop->event_w_by_fd,
248 n_events * sizeof(struct event*))) == NULL)
250 sop->event_w_by_fd = w_by_fd;
252 memset((char *)sop->event_readset_in + sop->event_fdsz, 0,
253 fdsz - sop->event_fdsz);
254 memset((char *)sop->event_writeset_in + sop->event_fdsz, 0,
255 fdsz - sop->event_fdsz);
256 memset(sop->event_r_by_fd + n_events_old, 0,
257 (n_events-n_events_old) * sizeof(struct event*));
258 memset(sop->event_w_by_fd + n_events_old, 0,
259 (n_events-n_events_old) * sizeof(struct event*));
261 sop->event_fdsz = fdsz;
267 event_warn("malloc");
273 select_add(void *arg, struct event *ev)
275 struct selectop *sop = arg;
277 if (ev->ev_events & EV_SIGNAL)
278 return (evsignal_add(ev));
282 * Keep track of the highest fd, so that we can calculate the size
283 * of the fd_sets for select(2)
285 if (sop->event_fds < ev->ev_fd) {
286 int fdsz = sop->event_fdsz;
288 if (fdsz < sizeof(fd_mask))
289 fdsz = sizeof(fd_mask);
292 (howmany(ev->ev_fd + 1, NFDBITS) * sizeof(fd_mask)))
295 if (fdsz != sop->event_fdsz) {
296 if (select_resize(sop, fdsz)) {
302 sop->event_fds = ev->ev_fd;
305 if (ev->ev_events & EV_READ) {
306 FD_SET(ev->ev_fd, sop->event_readset_in);
307 sop->event_r_by_fd[ev->ev_fd] = ev;
309 if (ev->ev_events & EV_WRITE) {
310 FD_SET(ev->ev_fd, sop->event_writeset_in);
311 sop->event_w_by_fd[ev->ev_fd] = ev;
319 * Nothing to be done here.
323 select_del(void *arg, struct event *ev)
325 struct selectop *sop = arg;
328 if (ev->ev_events & EV_SIGNAL)
329 return (evsignal_del(ev));
331 if (sop->event_fds < ev->ev_fd) {
336 if (ev->ev_events & EV_READ) {
337 FD_CLR(ev->ev_fd, sop->event_readset_in);
338 sop->event_r_by_fd[ev->ev_fd] = NULL;
341 if (ev->ev_events & EV_WRITE) {
342 FD_CLR(ev->ev_fd, sop->event_writeset_in);
343 sop->event_w_by_fd[ev->ev_fd] = NULL;
351 select_dealloc(void *arg)
353 struct selectop *sop = arg;
355 if (sop->event_readset_in)
356 free(sop->event_readset_in);
357 if (sop->event_writeset_in)
358 free(sop->event_writeset_in);
359 if (sop->event_readset_out)
360 free(sop->event_readset_out);
361 if (sop->event_writeset_out)
362 free(sop->event_writeset_out);
363 if (sop->event_r_by_fd)
364 free(sop->event_r_by_fd);
365 if (sop->event_w_by_fd)
366 free(sop->event_w_by_fd);
368 memset(sop, 0, sizeof(struct selectop));