]> arthur.barton.de Git - netatalk.git/blob - libevent/evport.c
Add libevent
[netatalk.git] / libevent / evport.c
1 /*
2  * Submitted by David Pacheco (dp.spambait@gmail.com)
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY SUN MICROSYSTEMS, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /*
28  * Copyright (c) 2007 Sun Microsystems. All rights reserved.
29  * Use is subject to license terms.
30  */
31
32 /*
33  * evport.c: event backend using Solaris 10 event ports. See port_create(3C).
34  * This implementation is loosely modeled after the one used for select(2) (in
35  * select.c).
36  *
37  * The outstanding events are tracked in a data structure called evport_data.
38  * Each entry in the ed_fds array corresponds to a file descriptor, and contains
39  * pointers to the read and write events that correspond to that fd. (That is,
40  * when the file is readable, the "read" event should handle it, etc.)
41  *
42  * evport_add and evport_del update this data structure. evport_dispatch uses it
43  * to determine where to callback when an event occurs (which it gets from
44  * port_getn).
45  *
46  * Helper functions are used: grow() grows the file descriptor array as
47  * necessary when large fd's come in. reassociate() takes care of maintaining
48  * the proper file-descriptor/event-port associations.
49  *
50  * As in the select(2) implementation, signals are handled by evsignal.
51  */
52
53 #include "event2/event-config.h"
54
55 #include <sys/time.h>
56 #include <sys/queue.h>
57 #include <errno.h>
58 #include <poll.h>
59 #include <port.h>
60 #include <signal.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <time.h>
65 #include <unistd.h>
66
67 #include "event2/thread.h"
68
69 #include "evthread-internal.h"
70 #include "event-internal.h"
71 #include "log-internal.h"
72 #include "evsignal-internal.h"
73 #include "evmap-internal.h"
74
75 /*
76  * Default value for ed_nevents, which is the maximum file descriptor number we
77  * can handle. If an event comes in for a file descriptor F > nevents, we will
78  * grow the array of file descriptors, doubling its size.
79  */
80 #define DEFAULT_NFDS    16
81
82
83 /*
84  * EVENTS_PER_GETN is the maximum number of events to retrieve from port_getn on
85  * any particular call. You can speed things up by increasing this, but it will
86  * (obviously) require more memory.
87  */
88 #define EVENTS_PER_GETN 8
89
90 /*
91  * Per-file-descriptor information about what events we're subscribed to. These
92  * fields are NULL if no event is subscribed to either of them.
93  */
94
95 struct fd_info {
96         short fdi_what;         /* combinations of EV_READ and EV_WRITE */
97 };
98
99 #define FDI_HAS_READ(fdi)  ((fdi)->fdi_what & EV_READ)
100 #define FDI_HAS_WRITE(fdi) ((fdi)->fdi_what & EV_WRITE)
101 #define FDI_HAS_EVENTS(fdi) (FDI_HAS_READ(fdi) || FDI_HAS_WRITE(fdi))
102 #define FDI_TO_SYSEVENTS(fdi) (FDI_HAS_READ(fdi) ? POLLIN : 0) | \
103     (FDI_HAS_WRITE(fdi) ? POLLOUT : 0)
104
105 struct evport_data {
106         int             ed_port;        /* event port for system events  */
107         int             ed_nevents;     /* number of allocated fdi's     */
108         struct fd_info *ed_fds;         /* allocated fdi table           */
109         /* fdi's that we need to reassoc */
110         int ed_pending[EVENTS_PER_GETN]; /* fd's with pending events */
111 };
112
113 static void*    evport_init(struct event_base *);
114 static int evport_add(struct event_base *, int fd, short old, short events, void *);
115 static int evport_del(struct event_base *, int fd, short old, short events, void *);
116 static int      evport_dispatch(struct event_base *, struct timeval *);
117 static void     evport_dealloc(struct event_base *);
118
119 const struct eventop evportops = {
120         "evport",
121         evport_init,
122         evport_add,
123         evport_del,
124         evport_dispatch,
125         evport_dealloc,
126         1, /* need reinit */
127         0, /* features */
128         0, /* fdinfo length */
129 };
130
131 /*
132  * Initialize the event port implementation.
133  */
134
135 static void*
136 evport_init(struct event_base *base)
137 {
138         struct evport_data *evpd;
139         int i;
140
141         if (!(evpd = mm_calloc(1, sizeof(struct evport_data))))
142                 return (NULL);
143
144         if ((evpd->ed_port = port_create()) == -1) {
145                 mm_free(evpd);
146                 return (NULL);
147         }
148
149         /*
150          * Initialize file descriptor structure
151          */
152         evpd->ed_fds = mm_calloc(DEFAULT_NFDS, sizeof(struct fd_info));
153         if (evpd->ed_fds == NULL) {
154                 close(evpd->ed_port);
155                 mm_free(evpd);
156                 return (NULL);
157         }
158         evpd->ed_nevents = DEFAULT_NFDS;
159         for (i = 0; i < EVENTS_PER_GETN; i++)
160                 evpd->ed_pending[i] = -1;
161
162         evsig_init(base);
163
164         return (evpd);
165 }
166
167 #ifdef CHECK_INVARIANTS
168 /*
169  * Checks some basic properties about the evport_data structure. Because it
170  * checks all file descriptors, this function can be expensive when the maximum
171  * file descriptor ever used is rather large.
172  */
173
174 static void
175 check_evportop(struct evport_data *evpd)
176 {
177         EVUTIL_ASSERT(evpd);
178         EVUTIL_ASSERT(evpd->ed_nevents > 0);
179         EVUTIL_ASSERT(evpd->ed_port > 0);
180         EVUTIL_ASSERT(evpd->ed_fds > 0);
181 }
182
183 /*
184  * Verifies very basic integrity of a given port_event.
185  */
186 static void
187 check_event(port_event_t* pevt)
188 {
189         /*
190          * We've only registered for PORT_SOURCE_FD events. The only
191          * other thing we can legitimately receive is PORT_SOURCE_ALERT,
192          * but since we're not using port_alert either, we can assume
193          * PORT_SOURCE_FD.
194          */
195         EVUTIL_ASSERT(pevt->portev_source == PORT_SOURCE_FD);
196         EVUTIL_ASSERT(pevt->portev_user == NULL);
197 }
198
199 #else
200 #define check_evportop(epop)
201 #define check_event(pevt)
202 #endif /* CHECK_INVARIANTS */
203
204 /*
205  * Doubles the size of the allocated file descriptor array.
206  */
207 static int
208 grow(struct evport_data *epdp, int factor)
209 {
210         struct fd_info *tmp;
211         int oldsize = epdp->ed_nevents;
212         int newsize = factor * oldsize;
213         EVUTIL_ASSERT(factor > 1);
214
215         check_evportop(epdp);
216
217         tmp = mm_realloc(epdp->ed_fds, sizeof(struct fd_info) * newsize);
218         if (NULL == tmp)
219                 return -1;
220         epdp->ed_fds = tmp;
221         memset((char*) (epdp->ed_fds + oldsize), 0,
222             (newsize - oldsize)*sizeof(struct fd_info));
223         epdp->ed_nevents = newsize;
224
225         check_evportop(epdp);
226
227         return 0;
228 }
229
230
231 /*
232  * (Re)associates the given file descriptor with the event port. The OS events
233  * are specified (implicitly) from the fd_info struct.
234  */
235 static int
236 reassociate(struct evport_data *epdp, struct fd_info *fdip, int fd)
237 {
238         int sysevents = FDI_TO_SYSEVENTS(fdip);
239
240         if (sysevents != 0) {
241                 if (port_associate(epdp->ed_port, PORT_SOURCE_FD,
242                                    fd, sysevents, NULL) == -1) {
243                         event_warn("port_associate");
244                         return (-1);
245                 }
246         }
247
248         check_evportop(epdp);
249
250         return (0);
251 }
252
253 /*
254  * Main event loop - polls port_getn for some number of events, and processes
255  * them.
256  */
257
258 static int
259 evport_dispatch(struct event_base *base, struct timeval *tv)
260 {
261         int i, res;
262         struct evport_data *epdp = base->evbase;
263         port_event_t pevtlist[EVENTS_PER_GETN];
264
265         /*
266          * port_getn will block until it has at least nevents events. It will
267          * also return how many it's given us (which may be more than we asked
268          * for, as long as it's less than our maximum (EVENTS_PER_GETN)) in
269          * nevents.
270          */
271         int nevents = 1;
272
273         /*
274          * We have to convert a struct timeval to a struct timespec
275          * (only difference is nanoseconds vs. microseconds). If no time-based
276          * events are active, we should wait for I/O (and tv == NULL).
277          */
278         struct timespec ts;
279         struct timespec *ts_p = NULL;
280         if (tv != NULL) {
281                 ts.tv_sec = tv->tv_sec;
282                 ts.tv_nsec = tv->tv_usec * 1000;
283                 ts_p = &ts;
284         }
285
286         /*
287          * Before doing anything else, we need to reassociate the events we hit
288          * last time which need reassociation. See comment at the end of the
289          * loop below.
290          */
291         for (i = 0; i < EVENTS_PER_GETN; ++i) {
292                 struct fd_info *fdi = NULL;
293                 if (epdp->ed_pending[i] != -1) {
294                         fdi = &(epdp->ed_fds[epdp->ed_pending[i]]);
295                 }
296
297                 if (fdi != NULL && FDI_HAS_EVENTS(fdi)) {
298                         int fd = epdp->ed_pending[i];
299                         reassociate(epdp, fdi, fd);
300                         epdp->ed_pending[i] = -1;
301                 }
302         }
303
304         EVBASE_RELEASE_LOCK(base, th_base_lock);
305
306         res = port_getn(epdp->ed_port, pevtlist, EVENTS_PER_GETN,
307             (unsigned int *) &nevents, ts_p);
308
309         EVBASE_ACQUIRE_LOCK(base, th_base_lock);
310
311         if (res == -1) {
312                 if (errno == EINTR || errno == EAGAIN) {
313                         return (0);
314                 } else if (errno == ETIME) {
315                         if (nevents == 0)
316                                 return (0);
317                 } else {
318                         event_warn("port_getn");
319                         return (-1);
320                 }
321         }
322
323         event_debug(("%s: port_getn reports %d events", __func__, nevents));
324
325         for (i = 0; i < nevents; ++i) {
326                 struct fd_info *fdi;
327                 port_event_t *pevt = &pevtlist[i];
328                 int fd = (int) pevt->portev_object;
329
330                 check_evportop(epdp);
331                 check_event(pevt);
332                 epdp->ed_pending[i] = fd;
333
334                 /*
335                  * Figure out what kind of event it was
336                  * (because we have to pass this to the callback)
337                  */
338                 res = 0;
339                 if (pevt->portev_events & POLLIN)
340                         res |= EV_READ;
341                 if (pevt->portev_events & POLLOUT)
342                         res |= EV_WRITE;
343
344                 EVUTIL_ASSERT(epdp->ed_nevents > fd);
345                 fdi = &(epdp->ed_fds[fd]);
346
347                 evmap_io_active(base, fd, res);
348         } /* end of all events gotten */
349
350         check_evportop(epdp);
351
352         return (0);
353 }
354
355
356 /*
357  * Adds the given event (so that you will be notified when it happens via
358  * the callback function).
359  */
360
361 static int
362 evport_add(struct event_base *base, int fd, short old, short events, void *p)
363 {
364         struct evport_data *evpd = base->evbase;
365         struct fd_info *fdi;
366         int factor;
367         (void)p;
368
369         check_evportop(evpd);
370
371         /*
372          * If necessary, grow the file descriptor info table
373          */
374
375         factor = 1;
376         while (fd >= factor * evpd->ed_nevents)
377                 factor *= 2;
378
379         if (factor > 1) {
380                 if (-1 == grow(evpd, factor)) {
381                         return (-1);
382                 }
383         }
384
385         fdi = &evpd->ed_fds[fd];
386         fdi->fdi_what |= events;
387
388         return reassociate(evpd, fdi, fd);
389 }
390
391 /*
392  * Removes the given event from the list of events to wait for.
393  */
394
395 static int
396 evport_del(struct event_base *base, int fd, short old, short events, void *p)
397 {
398         struct evport_data *evpd = base->evbase;
399         struct fd_info *fdi;
400         int i;
401         int associated = 1;
402         (void)p;
403
404         check_evportop(evpd);
405
406         if (evpd->ed_nevents < fd) {
407                 return (-1);
408         }
409
410         for (i = 0; i < EVENTS_PER_GETN; ++i) {
411                 if (evpd->ed_pending[i] == fd) {
412                         associated = 0;
413                         break;
414                 }
415         }
416
417         fdi = &evpd->ed_fds[fd];
418         if (events & EV_READ)
419                 fdi->fdi_what &= ~EV_READ;
420         if (events & EV_WRITE)
421                 fdi->fdi_what &= ~EV_WRITE;
422
423         if (associated) {
424                 if (!FDI_HAS_EVENTS(fdi) &&
425                     port_dissociate(evpd->ed_port, PORT_SOURCE_FD, fd) == -1) {
426                         /*
427                          * Ignore EBADFD error the fd could have been closed
428                          * before event_del() was called.
429                          */
430                         if (errno != EBADFD) {
431                                 event_warn("port_dissociate");
432                                 return (-1);
433                         }
434                 } else {
435                         if (FDI_HAS_EVENTS(fdi)) {
436                                 return (reassociate(evpd, fdi, fd));
437                         }
438                 }
439         } else {
440                 if ((fdi->fdi_what & (EV_READ|EV_WRITE)) == 0) {
441                         evpd->ed_pending[i] = -1;
442                 }
443         }
444         return 0;
445 }
446
447
448 static void
449 evport_dealloc(struct event_base *base)
450 {
451         struct evport_data *evpd = base->evbase;
452
453         evsig_dealloc(base);
454
455         close(evpd->ed_port);
456
457         if (evpd->ed_fds)
458                 mm_free(evpd->ed_fds);
459         mm_free(evpd);
460 }