]> arthur.barton.de Git - netatalk.git/blob - libevent/select.c
Merge master
[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
94 static void *
95 select_init(struct event_base *base)
96 {
97         struct selectop *sop;
98
99         if (!(sop = mm_calloc(1, sizeof(struct selectop))))
100                 return (NULL);
101
102         select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask));
103
104         evsig_init(base);
105
106         return (sop);
107 }
108
109 #ifdef CHECK_INVARIANTS
110 static void
111 check_selectop(struct selectop *sop)
112 {
113         /* nothing to be done here */
114 }
115 #else
116 #define check_selectop(sop) do { (void) sop; } while (0)
117 #endif
118
119 static int
120 select_dispatch(struct event_base *base, struct timeval *tv)
121 {
122         int res=0, i, j, nfds;
123         struct selectop *sop = base->evbase;
124
125         check_selectop(sop);
126         if (sop->resize_out_sets) {
127                 fd_set *readset_out=NULL, *writeset_out=NULL;
128                 size_t sz = sop->event_fdsz;
129                 if (!(readset_out = mm_realloc(sop->event_readset_out, sz)))
130                         return (-1);
131                 if (!(writeset_out = mm_realloc(sop->event_writeset_out, sz))) {
132                         mm_free(readset_out);
133                         return (-1);
134                 }
135                 sop->event_readset_out = readset_out;
136                 sop->event_writeset_out = writeset_out;
137                 sop->resize_out_sets = 0;
138         }
139
140         memcpy(sop->event_readset_out, sop->event_readset_in,
141                sop->event_fdsz);
142         memcpy(sop->event_writeset_out, sop->event_writeset_in,
143                sop->event_fdsz);
144
145         nfds = sop->event_fds+1;
146
147         EVBASE_RELEASE_LOCK(base, th_base_lock);
148
149         res = select(nfds, sop->event_readset_out,
150             sop->event_writeset_out, NULL, tv);
151
152         EVBASE_ACQUIRE_LOCK(base, th_base_lock);
153
154         check_selectop(sop);
155
156         if (res == -1) {
157                 if (errno != EINTR) {
158                         event_warn("select");
159                         return (-1);
160                 }
161
162                 return (0);
163         }
164
165         event_debug(("%s: select reports %d", __func__, res));
166
167         check_selectop(sop);
168         i = random() % (nfds+1);
169         for (j = 0; j <= nfds; ++j) {
170                 if (++i >= nfds+1)
171                         i = 0;
172                 res = 0;
173                 if (FD_ISSET(i, sop->event_readset_out))
174                         res |= EV_READ;
175                 if (FD_ISSET(i, sop->event_writeset_out))
176                         res |= EV_WRITE;
177
178                 if (res == 0)
179                         continue;
180
181                 evmap_io_active(base, i, res);
182         }
183         check_selectop(sop);
184
185         return (0);
186 }
187
188
189 static int
190 select_resize(struct selectop *sop, int fdsz)
191 {
192         fd_set *readset_in = NULL;
193         fd_set *writeset_in = NULL;
194
195         if (sop->event_readset_in)
196                 check_selectop(sop);
197
198         if ((readset_in = mm_realloc(sop->event_readset_in, fdsz)) == NULL)
199                 goto error;
200         sop->event_readset_in = readset_in;
201         if ((writeset_in = mm_realloc(sop->event_writeset_in, fdsz)) == NULL)
202                 goto error;
203         sop->event_writeset_in = writeset_in;
204         sop->resize_out_sets = 1;
205
206         memset((char *)sop->event_readset_in + sop->event_fdsz, 0,
207             fdsz - sop->event_fdsz);
208         memset((char *)sop->event_writeset_in + sop->event_fdsz, 0,
209             fdsz - sop->event_fdsz);
210
211         sop->event_fdsz = fdsz;
212         check_selectop(sop);
213
214         return (0);
215
216  error:
217         event_warn("malloc");
218         return (-1);
219 }
220
221
222 static int
223 select_add(struct event_base *base, int fd, short old, short events, void *p)
224 {
225         struct selectop *sop = base->evbase;
226         (void) p;
227
228         EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
229         check_selectop(sop);
230         /*
231          * Keep track of the highest fd, so that we can calculate the size
232          * of the fd_sets for select(2)
233          */
234         if (sop->event_fds < fd) {
235                 int fdsz = sop->event_fdsz;
236
237                 if (fdsz < (int)sizeof(fd_mask))
238                         fdsz = (int)sizeof(fd_mask);
239
240                 /* In theory we should worry about overflow here.  In
241                  * reality, though, the highest fd on a unixy system will
242                  * not overflow here. XXXX */
243                 while (fdsz <
244                     (int) (howmany(fd + 1, NFDBITS) * sizeof(fd_mask)))
245                         fdsz *= 2;
246
247                 if (fdsz != sop->event_fdsz) {
248                         if (select_resize(sop, fdsz)) {
249                                 check_selectop(sop);
250                                 return (-1);
251                         }
252                 }
253
254                 sop->event_fds = fd;
255         }
256
257         if (events & EV_READ)
258                 FD_SET(fd, sop->event_readset_in);
259         if (events & EV_WRITE)
260                 FD_SET(fd, sop->event_writeset_in);
261         check_selectop(sop);
262
263         return (0);
264 }
265
266 /*
267  * Nothing to be done here.
268  */
269
270 static int
271 select_del(struct event_base *base, int fd, short old, short events, void *p)
272 {
273         struct selectop *sop = base->evbase;
274         (void)p;
275
276         EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
277         check_selectop(sop);
278
279         if (sop->event_fds < fd) {
280                 check_selectop(sop);
281                 return (0);
282         }
283
284         if (events & EV_READ)
285                 FD_CLR(fd, sop->event_readset_in);
286
287         if (events & EV_WRITE)
288                 FD_CLR(fd, sop->event_writeset_in);
289
290         check_selectop(sop);
291         return (0);
292 }
293
294 static void
295 select_dealloc(struct event_base *base)
296 {
297         struct selectop *sop = base->evbase;
298
299         evsig_dealloc(base);
300         if (sop->event_readset_in)
301                 mm_free(sop->event_readset_in);
302         if (sop->event_writeset_in)
303                 mm_free(sop->event_writeset_in);
304         if (sop->event_readset_out)
305                 mm_free(sop->event_readset_out);
306         if (sop->event_writeset_out)
307                 mm_free(sop->event_writeset_out);
308
309         memset(sop, 0, sizeof(struct selectop));
310         mm_free(sop);
311 }