]> arthur.barton.de Git - netatalk.git/blob - libevent/select.c
Update libevent to 2.0.12
[netatalk.git] / libevent / select.c
1 /*      $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $       */
2
3 /*
4  * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
5  * Copyright 2007-2010 Niels Provos and Nick Mathewson
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 #include "event2/event-config.h"
30
31 #include <sys/types.h>
32 #ifdef _EVENT_HAVE_SYS_TIME_H
33 #include <sys/time.h>
34 #endif
35 #ifdef _EVENT_HAVE_SYS_SELECT_H
36 #include <sys/select.h>
37 #endif
38 #include <sys/queue.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <errno.h>
45
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"
52
53 #ifndef howmany
54 #define howmany(x, y)   (((x)+((y)-1))/(y))
55 #endif
56
57 #ifndef _EVENT_HAVE_FD_MASK
58 /* This type is mandatory, but Android doesn't define it. */
59 #undef NFDBITS
60 #define NFDBITS (sizeof(long)*8)
61 typedef unsigned long fd_mask;
62 #endif
63
64 struct selectop {
65         int event_fds;          /* Highest fd in fd set */
66         int event_fdsz;
67         int resize_out_sets;
68         fd_set *event_readset_in;
69         fd_set *event_writeset_in;
70         fd_set *event_readset_out;
71         fd_set *event_writeset_out;
72 };
73
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 *);
79
80 const struct eventop selectops = {
81         "select",
82         select_init,
83         select_add,
84         select_del,
85         select_dispatch,
86         select_dealloc,
87         0, /* doesn't need reinit. */
88         EV_FEATURE_FDS,
89         0,
90 };
91
92 static int select_resize(struct selectop *sop, int fdsz);
93 static void select_free_selectop(struct selectop *sop);
94
95 static void *
96 select_init(struct event_base *base)
97 {
98         struct selectop *sop;
99
100         if (!(sop = mm_calloc(1, sizeof(struct selectop))))
101                 return (NULL);
102
103         if (select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask))) {
104                 select_free_selectop(sop);
105                 return (NULL);
106         }
107
108         evsig_init(base);
109
110         return (sop);
111 }
112
113 #ifdef CHECK_INVARIANTS
114 static void
115 check_selectop(struct selectop *sop)
116 {
117         /* nothing to be done here */
118 }
119 #else
120 #define check_selectop(sop) do { (void) sop; } while (0)
121 #endif
122
123 static int
124 select_dispatch(struct event_base *base, struct timeval *tv)
125 {
126         int res=0, i, j, nfds;
127         struct selectop *sop = base->evbase;
128
129         check_selectop(sop);
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)))
134                         return (-1);
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
140                          * no-op. */
141                         return (-1);
142                 }
143                 sop->event_writeset_out = writeset_out;
144                 sop->resize_out_sets = 0;
145         }
146
147         memcpy(sop->event_readset_out, sop->event_readset_in,
148                sop->event_fdsz);
149         memcpy(sop->event_writeset_out, sop->event_writeset_in,
150                sop->event_fdsz);
151
152         nfds = sop->event_fds+1;
153
154         EVBASE_RELEASE_LOCK(base, th_base_lock);
155
156         res = select(nfds, sop->event_readset_out,
157             sop->event_writeset_out, NULL, tv);
158
159         EVBASE_ACQUIRE_LOCK(base, th_base_lock);
160
161         check_selectop(sop);
162
163         if (res == -1) {
164                 if (errno != EINTR) {
165                         event_warn("select");
166                         return (-1);
167                 }
168
169                 return (0);
170         }
171
172         event_debug(("%s: select reports %d", __func__, res));
173
174         check_selectop(sop);
175         i = random() % nfds;
176         for (j = 0; j < nfds; ++j) {
177                 if (++i >= nfds)
178                         i = 0;
179                 res = 0;
180                 if (FD_ISSET(i, sop->event_readset_out))
181                         res |= EV_READ;
182                 if (FD_ISSET(i, sop->event_writeset_out))
183                         res |= EV_WRITE;
184
185                 if (res == 0)
186                         continue;
187
188                 evmap_io_active(base, i, res);
189         }
190         check_selectop(sop);
191
192         return (0);
193 }
194
195 static int
196 select_resize(struct selectop *sop, int fdsz)
197 {
198         fd_set *readset_in = NULL;
199         fd_set *writeset_in = NULL;
200
201         if (sop->event_readset_in)
202                 check_selectop(sop);
203
204         if ((readset_in = mm_realloc(sop->event_readset_in, fdsz)) == NULL)
205                 goto error;
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."
213                  */
214                 goto error;
215         }
216         sop->event_writeset_in = writeset_in;
217         sop->resize_out_sets = 1;
218
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);
223
224         sop->event_fdsz = fdsz;
225         check_selectop(sop);
226
227         return (0);
228
229  error:
230         event_warn("malloc");
231         return (-1);
232 }
233
234
235 static int
236 select_add(struct event_base *base, int fd, short old, short events, void *p)
237 {
238         struct selectop *sop = base->evbase;
239         (void) p;
240
241         EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
242         check_selectop(sop);
243         /*
244          * Keep track of the highest fd, so that we can calculate the size
245          * of the fd_sets for select(2)
246          */
247         if (sop->event_fds < fd) {
248                 int fdsz = sop->event_fdsz;
249
250                 if (fdsz < (int)sizeof(fd_mask))
251                         fdsz = (int)sizeof(fd_mask);
252
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 */
256                 while (fdsz <
257                     (int) (howmany(fd + 1, NFDBITS) * sizeof(fd_mask)))
258                         fdsz *= 2;
259
260                 if (fdsz != sop->event_fdsz) {
261                         if (select_resize(sop, fdsz)) {
262                                 check_selectop(sop);
263                                 return (-1);
264                         }
265                 }
266
267                 sop->event_fds = fd;
268         }
269
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);
274         check_selectop(sop);
275
276         return (0);
277 }
278
279 /*
280  * Nothing to be done here.
281  */
282
283 static int
284 select_del(struct event_base *base, int fd, short old, short events, void *p)
285 {
286         struct selectop *sop = base->evbase;
287         (void)p;
288
289         EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
290         check_selectop(sop);
291
292         if (sop->event_fds < fd) {
293                 check_selectop(sop);
294                 return (0);
295         }
296
297         if (events & EV_READ)
298                 FD_CLR(fd, sop->event_readset_in);
299
300         if (events & EV_WRITE)
301                 FD_CLR(fd, sop->event_writeset_in);
302
303         check_selectop(sop);
304         return (0);
305 }
306
307 static void
308 select_free_selectop(struct selectop *sop)
309 {
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);
318
319         memset(sop, 0, sizeof(struct selectop));
320         mm_free(sop);
321 }
322
323 static void
324 select_dealloc(struct event_base *base)
325 {
326         evsig_dealloc(base);
327
328         select_free_selectop(base->evbase);
329 }