2 * Submitted by David Pacheco (dp.spambait@gmail.com)
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 * Copyright (c) 2007 Sun Microsystems. All rights reserved.
29 * Use is subject to license terms.
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
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.)
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
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.
50 * As in the select(2) implementation, signals are handled by evsignal.
53 #include "event2/event-config.h"
56 #include <sys/queue.h>
67 #include "event2/thread.h"
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"
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.
80 #define DEFAULT_NFDS 16
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.
88 #define EVENTS_PER_GETN 8
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.
96 short fdi_what; /* combinations of EV_READ and EV_WRITE */
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)
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 */
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 *);
119 const struct eventop evportops = {
128 0, /* fdinfo length */
132 * Initialize the event port implementation.
136 evport_init(struct event_base *base)
138 struct evport_data *evpd;
141 if (!(evpd = mm_calloc(1, sizeof(struct evport_data))))
144 if ((evpd->ed_port = port_create()) == -1) {
150 * Initialize file descriptor structure
152 evpd->ed_fds = mm_calloc(DEFAULT_NFDS, sizeof(struct fd_info));
153 if (evpd->ed_fds == NULL) {
154 close(evpd->ed_port);
158 evpd->ed_nevents = DEFAULT_NFDS;
159 for (i = 0; i < EVENTS_PER_GETN; i++)
160 evpd->ed_pending[i] = -1;
167 #ifdef CHECK_INVARIANTS
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.
175 check_evportop(struct evport_data *evpd)
178 EVUTIL_ASSERT(evpd->ed_nevents > 0);
179 EVUTIL_ASSERT(evpd->ed_port > 0);
180 EVUTIL_ASSERT(evpd->ed_fds > 0);
184 * Verifies very basic integrity of a given port_event.
187 check_event(port_event_t* pevt)
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
195 EVUTIL_ASSERT(pevt->portev_source == PORT_SOURCE_FD);
196 EVUTIL_ASSERT(pevt->portev_user == NULL);
200 #define check_evportop(epop)
201 #define check_event(pevt)
202 #endif /* CHECK_INVARIANTS */
205 * Doubles the size of the allocated file descriptor array.
208 grow(struct evport_data *epdp, int factor)
211 int oldsize = epdp->ed_nevents;
212 int newsize = factor * oldsize;
213 EVUTIL_ASSERT(factor > 1);
215 check_evportop(epdp);
217 tmp = mm_realloc(epdp->ed_fds, sizeof(struct fd_info) * newsize);
221 memset((char*) (epdp->ed_fds + oldsize), 0,
222 (newsize - oldsize)*sizeof(struct fd_info));
223 epdp->ed_nevents = newsize;
225 check_evportop(epdp);
232 * (Re)associates the given file descriptor with the event port. The OS events
233 * are specified (implicitly) from the fd_info struct.
236 reassociate(struct evport_data *epdp, struct fd_info *fdip, int fd)
238 int sysevents = FDI_TO_SYSEVENTS(fdip);
240 if (sysevents != 0) {
241 if (port_associate(epdp->ed_port, PORT_SOURCE_FD,
242 fd, sysevents, NULL) == -1) {
243 event_warn("port_associate");
248 check_evportop(epdp);
254 * Main event loop - polls port_getn for some number of events, and processes
259 evport_dispatch(struct event_base *base, struct timeval *tv)
262 struct evport_data *epdp = base->evbase;
263 port_event_t pevtlist[EVENTS_PER_GETN];
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
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).
279 struct timespec *ts_p = NULL;
281 ts.tv_sec = tv->tv_sec;
282 ts.tv_nsec = tv->tv_usec * 1000;
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
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]]);
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;
304 EVBASE_RELEASE_LOCK(base, th_base_lock);
306 res = port_getn(epdp->ed_port, pevtlist, EVENTS_PER_GETN,
307 (unsigned int *) &nevents, ts_p);
309 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
312 if (errno == EINTR || errno == EAGAIN) {
314 } else if (errno == ETIME) {
318 event_warn("port_getn");
323 event_debug(("%s: port_getn reports %d events", __func__, nevents));
325 for (i = 0; i < nevents; ++i) {
327 port_event_t *pevt = &pevtlist[i];
328 int fd = (int) pevt->portev_object;
330 check_evportop(epdp);
332 epdp->ed_pending[i] = fd;
335 * Figure out what kind of event it was
336 * (because we have to pass this to the callback)
339 if (pevt->portev_events & POLLIN)
341 if (pevt->portev_events & POLLOUT)
344 EVUTIL_ASSERT(epdp->ed_nevents > fd);
345 fdi = &(epdp->ed_fds[fd]);
347 evmap_io_active(base, fd, res);
348 } /* end of all events gotten */
350 check_evportop(epdp);
357 * Adds the given event (so that you will be notified when it happens via
358 * the callback function).
362 evport_add(struct event_base *base, int fd, short old, short events, void *p)
364 struct evport_data *evpd = base->evbase;
369 check_evportop(evpd);
372 * If necessary, grow the file descriptor info table
376 while (fd >= factor * evpd->ed_nevents)
380 if (-1 == grow(evpd, factor)) {
385 fdi = &evpd->ed_fds[fd];
386 fdi->fdi_what |= events;
388 return reassociate(evpd, fdi, fd);
392 * Removes the given event from the list of events to wait for.
396 evport_del(struct event_base *base, int fd, short old, short events, void *p)
398 struct evport_data *evpd = base->evbase;
404 check_evportop(evpd);
406 if (evpd->ed_nevents < fd) {
410 for (i = 0; i < EVENTS_PER_GETN; ++i) {
411 if (evpd->ed_pending[i] == fd) {
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;
424 if (!FDI_HAS_EVENTS(fdi) &&
425 port_dissociate(evpd->ed_port, PORT_SOURCE_FD, fd) == -1) {
427 * Ignore EBADFD error the fd could have been closed
428 * before event_del() was called.
430 if (errno != EBADFD) {
431 event_warn("port_dissociate");
435 if (FDI_HAS_EVENTS(fdi)) {
436 return (reassociate(evpd, fdi, fd));
440 if ((fdi->fdi_what & (EV_READ|EV_WRITE)) == 0) {
441 evpd->ed_pending[i] = -1;
449 evport_dealloc(struct event_base *base)
451 struct evport_data *evpd = base->evbase;
455 close(evpd->ed_port);
458 mm_free(evpd->ed_fds);