1 /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
4 * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
5 * Copyright 2007-2010 Niels Provos and Nick Mathewson
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.
29 #include "event2/event-config.h"
31 #include <sys/types.h>
32 #ifdef _EVENT_HAVE_SYS_TIME_H
35 #ifdef _EVENT_HAVE_SYS_SELECT_H
36 #include <sys/select.h>
38 #include <sys/queue.h>
46 #include "event-internal.h"
47 #include "evsignal-internal.h"
48 #include "event2/thread.h"
49 #include "evthread-internal.h"
50 #include "log-internal.h"
51 #include "evmap-internal.h"
54 #define howmany(x, y) (((x)+((y)-1))/(y))
57 #ifndef _EVENT_HAVE_FD_MASK
58 /* This type is mandatory, but Android doesn't define it. */
60 #define NFDBITS (sizeof(long)*8)
61 typedef unsigned long fd_mask;
65 int event_fds; /* Highest fd in fd set */
68 fd_set *event_readset_in;
69 fd_set *event_writeset_in;
70 fd_set *event_readset_out;
71 fd_set *event_writeset_out;
74 static void *select_init(struct event_base *);
75 static int select_add(struct event_base *, int, short old, short events, void*);
76 static int select_del(struct event_base *, int, short old, short events, void*);
77 static int select_dispatch(struct event_base *, struct timeval *);
78 static void select_dealloc(struct event_base *);
80 const struct eventop selectops = {
87 0, /* doesn't need reinit. */
92 static int select_resize(struct selectop *sop, int fdsz);
93 static void select_free_selectop(struct selectop *sop);
96 select_init(struct event_base *base)
100 if (!(sop = mm_calloc(1, sizeof(struct selectop))))
103 if (select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask))) {
104 select_free_selectop(sop);
113 #ifdef CHECK_INVARIANTS
115 check_selectop(struct selectop *sop)
117 /* nothing to be done here */
120 #define check_selectop(sop) do { (void) sop; } while (0)
124 select_dispatch(struct event_base *base, struct timeval *tv)
126 int res=0, i, j, nfds;
127 struct selectop *sop = base->evbase;
130 if (sop->resize_out_sets) {
131 fd_set *readset_out=NULL, *writeset_out=NULL;
132 size_t sz = sop->event_fdsz;
133 if (!(readset_out = mm_realloc(sop->event_readset_out, sz)))
135 sop->event_readset_out = readset_out;
136 if (!(writeset_out = mm_realloc(sop->event_writeset_out, sz))) {
137 /* We don't free readset_out here, since it was
138 * already successfully reallocated. The next time
139 * we call select_dispatch, the realloc will be a
143 sop->event_writeset_out = writeset_out;
144 sop->resize_out_sets = 0;
147 memcpy(sop->event_readset_out, sop->event_readset_in,
149 memcpy(sop->event_writeset_out, sop->event_writeset_in,
152 nfds = sop->event_fds+1;
154 EVBASE_RELEASE_LOCK(base, th_base_lock);
156 res = select(nfds, sop->event_readset_out,
157 sop->event_writeset_out, NULL, tv);
159 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
164 if (errno != EINTR) {
165 event_warn("select");
172 event_debug(("%s: select reports %d", __func__, res));
176 for (j = 0; j < nfds; ++j) {
180 if (FD_ISSET(i, sop->event_readset_out))
182 if (FD_ISSET(i, sop->event_writeset_out))
188 evmap_io_active(base, i, res);
196 select_resize(struct selectop *sop, int fdsz)
198 fd_set *readset_in = NULL;
199 fd_set *writeset_in = NULL;
201 if (sop->event_readset_in)
204 if ((readset_in = mm_realloc(sop->event_readset_in, fdsz)) == NULL)
206 sop->event_readset_in = readset_in;
207 if ((writeset_in = mm_realloc(sop->event_writeset_in, fdsz)) == NULL) {
208 /* Note that this will leave event_readset_in expanded.
209 * That's okay; we wouldn't want to free it, since that would
210 * change the semantics of select_resize from "expand the
211 * readset_in and writeset_in, or return -1" to "expand the
212 * *set_in members, or trash them and return -1."
216 sop->event_writeset_in = writeset_in;
217 sop->resize_out_sets = 1;
219 memset((char *)sop->event_readset_in + sop->event_fdsz, 0,
220 fdsz - sop->event_fdsz);
221 memset((char *)sop->event_writeset_in + sop->event_fdsz, 0,
222 fdsz - sop->event_fdsz);
224 sop->event_fdsz = fdsz;
230 event_warn("malloc");
236 select_add(struct event_base *base, int fd, short old, short events, void *p)
238 struct selectop *sop = base->evbase;
241 EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
244 * Keep track of the highest fd, so that we can calculate the size
245 * of the fd_sets for select(2)
247 if (sop->event_fds < fd) {
248 int fdsz = sop->event_fdsz;
250 if (fdsz < (int)sizeof(fd_mask))
251 fdsz = (int)sizeof(fd_mask);
253 /* In theory we should worry about overflow here. In
254 * reality, though, the highest fd on a unixy system will
255 * not overflow here. XXXX */
257 (int) (howmany(fd + 1, NFDBITS) * sizeof(fd_mask)))
260 if (fdsz != sop->event_fdsz) {
261 if (select_resize(sop, fdsz)) {
270 if (events & EV_READ)
271 FD_SET(fd, sop->event_readset_in);
272 if (events & EV_WRITE)
273 FD_SET(fd, sop->event_writeset_in);
280 * Nothing to be done here.
284 select_del(struct event_base *base, int fd, short old, short events, void *p)
286 struct selectop *sop = base->evbase;
289 EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
292 if (sop->event_fds < fd) {
297 if (events & EV_READ)
298 FD_CLR(fd, sop->event_readset_in);
300 if (events & EV_WRITE)
301 FD_CLR(fd, sop->event_writeset_in);
308 select_free_selectop(struct selectop *sop)
310 if (sop->event_readset_in)
311 mm_free(sop->event_readset_in);
312 if (sop->event_writeset_in)
313 mm_free(sop->event_writeset_in);
314 if (sop->event_readset_out)
315 mm_free(sop->event_readset_out);
316 if (sop->event_writeset_out)
317 mm_free(sop->event_writeset_out);
319 memset(sop, 0, sizeof(struct selectop));
324 select_dealloc(struct event_base *base)
328 select_free_selectop(base->evbase);