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-2012 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"
30 #include "evconfig-private.h"
32 #ifdef EVENT__HAVE_SELECT
35 /* Apple wants us to define this if we might ever pass more than
36 * FD_SETSIZE bits to select(). */
37 #define _DARWIN_UNLIMITED_SELECT
40 #include <sys/types.h>
41 #ifdef EVENT__HAVE_SYS_TIME_H
44 #ifdef EVENT__HAVE_SYS_SELECT_H
45 #include <sys/select.h>
47 #include <sys/queue.h>
55 #include "event-internal.h"
56 #include "evsignal-internal.h"
57 #include "event2/thread.h"
58 #include "evthread-internal.h"
59 #include "log-internal.h"
60 #include "evmap-internal.h"
62 #ifndef EVENT__HAVE_FD_MASK
63 /* This type is mandatory, but Android doesn't define it. */
64 typedef unsigned long fd_mask;
68 #define NFDBITS (sizeof(fd_mask)*8)
71 /* Divide positive x by y, rounding up. */
72 #define DIV_ROUNDUP(x, y) (((x)+((y)-1))/(y))
74 /* How many bytes to allocate for N fds? */
75 #define SELECT_ALLOC_SIZE(n) \
76 (DIV_ROUNDUP(n, NFDBITS) * sizeof(fd_mask))
79 int event_fds; /* Highest fd in fd set */
82 fd_set *event_readset_in;
83 fd_set *event_writeset_in;
84 fd_set *event_readset_out;
85 fd_set *event_writeset_out;
88 static void *select_init(struct event_base *);
89 static int select_add(struct event_base *, int, short old, short events, void*);
90 static int select_del(struct event_base *, int, short old, short events, void*);
91 static int select_dispatch(struct event_base *, struct timeval *);
92 static void select_dealloc(struct event_base *);
94 const struct eventop selectops = {
101 0, /* doesn't need reinit. */
106 static int select_resize(struct selectop *sop, int fdsz);
107 static void select_free_selectop(struct selectop *sop);
110 select_init(struct event_base *base)
112 struct selectop *sop;
114 if (!(sop = mm_calloc(1, sizeof(struct selectop))))
117 if (select_resize(sop, SELECT_ALLOC_SIZE(32 + 1))) {
118 select_free_selectop(sop);
124 evutil_weakrand_seed_(&base->weakrand_seed, 0);
129 #ifdef CHECK_INVARIANTS
131 check_selectop(struct selectop *sop)
133 /* nothing to be done here */
136 #define check_selectop(sop) do { (void) sop; } while (0)
140 select_dispatch(struct event_base *base, struct timeval *tv)
142 int res=0, i, j, nfds;
143 struct selectop *sop = base->evbase;
146 if (sop->resize_out_sets) {
147 fd_set *readset_out=NULL, *writeset_out=NULL;
148 size_t sz = sop->event_fdsz;
149 if (!(readset_out = mm_realloc(sop->event_readset_out, sz)))
151 sop->event_readset_out = readset_out;
152 if (!(writeset_out = mm_realloc(sop->event_writeset_out, sz))) {
153 /* We don't free readset_out here, since it was
154 * already successfully reallocated. The next time
155 * we call select_dispatch, the realloc will be a
159 sop->event_writeset_out = writeset_out;
160 sop->resize_out_sets = 0;
163 memcpy(sop->event_readset_out, sop->event_readset_in,
165 memcpy(sop->event_writeset_out, sop->event_writeset_in,
168 nfds = sop->event_fds+1;
170 EVBASE_RELEASE_LOCK(base, th_base_lock);
172 res = select(nfds, sop->event_readset_out,
173 sop->event_writeset_out, NULL, tv);
175 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
180 if (errno != EINTR) {
181 event_warn("select");
188 event_debug(("%s: select reports %d", __func__, res));
191 i = evutil_weakrand_range_(&base->weakrand_seed, nfds);
192 for (j = 0; j < nfds; ++j) {
196 if (FD_ISSET(i, sop->event_readset_out))
198 if (FD_ISSET(i, sop->event_writeset_out))
204 evmap_io_active_(base, i, res);
212 select_resize(struct selectop *sop, int fdsz)
214 fd_set *readset_in = NULL;
215 fd_set *writeset_in = NULL;
217 if (sop->event_readset_in)
220 if ((readset_in = mm_realloc(sop->event_readset_in, fdsz)) == NULL)
222 sop->event_readset_in = readset_in;
223 if ((writeset_in = mm_realloc(sop->event_writeset_in, fdsz)) == NULL) {
224 /* Note that this will leave event_readset_in expanded.
225 * That's okay; we wouldn't want to free it, since that would
226 * change the semantics of select_resize from "expand the
227 * readset_in and writeset_in, or return -1" to "expand the
228 * *set_in members, or trash them and return -1."
232 sop->event_writeset_in = writeset_in;
233 sop->resize_out_sets = 1;
235 memset((char *)sop->event_readset_in + sop->event_fdsz, 0,
236 fdsz - sop->event_fdsz);
237 memset((char *)sop->event_writeset_in + sop->event_fdsz, 0,
238 fdsz - sop->event_fdsz);
240 sop->event_fdsz = fdsz;
246 event_warn("malloc");
252 select_add(struct event_base *base, int fd, short old, short events, void *p)
254 struct selectop *sop = base->evbase;
257 EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
260 * Keep track of the highest fd, so that we can calculate the size
261 * of the fd_sets for select(2)
263 if (sop->event_fds < fd) {
264 int fdsz = sop->event_fdsz;
266 if (fdsz < (int)sizeof(fd_mask))
267 fdsz = (int)sizeof(fd_mask);
269 /* In theory we should worry about overflow here. In
270 * reality, though, the highest fd on a unixy system will
271 * not overflow here. XXXX */
272 while (fdsz < (int) SELECT_ALLOC_SIZE(fd + 1))
275 if (fdsz != sop->event_fdsz) {
276 if (select_resize(sop, fdsz)) {
285 if (events & EV_READ)
286 FD_SET(fd, sop->event_readset_in);
287 if (events & EV_WRITE)
288 FD_SET(fd, sop->event_writeset_in);
295 * Nothing to be done here.
299 select_del(struct event_base *base, int fd, short old, short events, void *p)
301 struct selectop *sop = base->evbase;
304 EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
307 if (sop->event_fds < fd) {
312 if (events & EV_READ)
313 FD_CLR(fd, sop->event_readset_in);
315 if (events & EV_WRITE)
316 FD_CLR(fd, sop->event_writeset_in);
323 select_free_selectop(struct selectop *sop)
325 if (sop->event_readset_in)
326 mm_free(sop->event_readset_in);
327 if (sop->event_writeset_in)
328 mm_free(sop->event_writeset_in);
329 if (sop->event_readset_out)
330 mm_free(sop->event_readset_out);
331 if (sop->event_writeset_out)
332 mm_free(sop->event_writeset_out);
334 memset(sop, 0, sizeof(struct selectop));
339 select_dealloc(struct event_base *base)
341 evsig_dealloc_(base);
343 select_free_selectop(base->evbase);
346 #endif /* EVENT__HAVE_SELECT */