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.
59 #include <sys/queue.h>
69 #ifdef CHECK_INVARIANTS
74 #include "event-internal.h"
80 * Default value for ed_nevents, which is the maximum file descriptor number we
81 * can handle. If an event comes in for a file descriptor F > nevents, we will
82 * grow the array of file descriptors, doubling its size.
84 #define DEFAULT_NFDS 16
88 * EVENTS_PER_GETN is the maximum number of events to retrieve from port_getn on
89 * any particular call. You can speed things up by increasing this, but it will
90 * (obviously) require more memory.
92 #define EVENTS_PER_GETN 8
95 * Per-file-descriptor information about what events we're subscribed to. These
96 * fields are NULL if no event is subscribed to either of them.
100 struct event
* fdi_revt
; /* the event responsible for the "read" */
101 struct event
* fdi_wevt
; /* the event responsible for the "write" */
104 #define FDI_HAS_READ(fdi) ((fdi)->fdi_revt != NULL)
105 #define FDI_HAS_WRITE(fdi) ((fdi)->fdi_wevt != NULL)
106 #define FDI_HAS_EVENTS(fdi) (FDI_HAS_READ(fdi) || FDI_HAS_WRITE(fdi))
107 #define FDI_TO_SYSEVENTS(fdi) (FDI_HAS_READ(fdi) ? POLLIN : 0) | \
108 (FDI_HAS_WRITE(fdi) ? POLLOUT : 0)
111 int ed_port
; /* event port for system events */
112 int ed_nevents
; /* number of allocated fdi's */
113 struct fd_info
*ed_fds
; /* allocated fdi table */
114 /* fdi's that we need to reassoc */
115 int ed_pending
[EVENTS_PER_GETN
]; /* fd's with pending events */
118 static void* evport_init (struct event_base
*);
119 static int evport_add (void *, struct event
*);
120 static int evport_del (void *, struct event
*);
121 static int evport_dispatch (struct event_base
*, void *, struct timeval
*);
122 static void evport_dealloc (struct event_base
*, void *);
124 const struct eventop evportops
= {
135 * Initialize the event port implementation.
139 evport_init(struct event_base
*base
)
141 struct evport_data
*evpd
;
144 * Disable event ports when this environment variable is set
146 if (evutil_getenv("EVENT_NOEVPORT"))
149 if (!(evpd
= calloc(1, sizeof(struct evport_data
))))
152 if ((evpd
->ed_port
= port_create()) == -1) {
158 * Initialize file descriptor structure
160 evpd
->ed_fds
= calloc(DEFAULT_NFDS
, sizeof(struct fd_info
));
161 if (evpd
->ed_fds
== NULL
) {
162 close(evpd
->ed_port
);
166 evpd
->ed_nevents
= DEFAULT_NFDS
;
167 for (i
= 0; i
< EVENTS_PER_GETN
; i
++)
168 evpd
->ed_pending
[i
] = -1;
175 #ifdef CHECK_INVARIANTS
177 * Checks some basic properties about the evport_data structure. Because it
178 * checks all file descriptors, this function can be expensive when the maximum
179 * file descriptor ever used is rather large.
183 check_evportop(struct evport_data
*evpd
)
186 assert(evpd
->ed_nevents
> 0);
187 assert(evpd
->ed_port
> 0);
188 assert(evpd
->ed_fds
> 0);
191 * Verify the integrity of the fd_info struct as well as the events to
192 * which it points (at least, that they're valid references and correct
193 * for their position in the structure).
196 for (i
= 0; i
< evpd
->ed_nevents
; ++i
) {
200 fdi
= &evpd
->ed_fds
[i
];
201 if ((ev
= fdi
->fdi_revt
) != NULL
) {
202 assert(ev
->ev_fd
== i
);
204 if ((ev
= fdi
->fdi_wevt
) != NULL
) {
205 assert(ev
->ev_fd
== i
);
211 * Verifies very basic integrity of a given port_event.
214 check_event(port_event_t
* pevt
)
217 * We've only registered for PORT_SOURCE_FD events. The only
218 * other thing we can legitimately receive is PORT_SOURCE_ALERT,
219 * but since we're not using port_alert either, we can assume
222 assert(pevt
->portev_source
== PORT_SOURCE_FD
);
223 assert(pevt
->portev_user
== NULL
);
227 #define check_evportop(epop)
228 #define check_event(pevt)
229 #endif /* CHECK_INVARIANTS */
232 * Doubles the size of the allocated file descriptor array.
235 grow(struct evport_data
*epdp
, int factor
)
238 int oldsize
= epdp
->ed_nevents
;
239 int newsize
= factor
* oldsize
;
242 check_evportop(epdp
);
244 tmp
= realloc(epdp
->ed_fds
, sizeof(struct fd_info
) * newsize
);
248 memset((char*) (epdp
->ed_fds
+ oldsize
), 0,
249 (newsize
- oldsize
)*sizeof(struct fd_info
));
250 epdp
->ed_nevents
= newsize
;
252 check_evportop(epdp
);
259 * (Re)associates the given file descriptor with the event port. The OS events
260 * are specified (implicitly) from the fd_info struct.
263 reassociate(struct evport_data
*epdp
, struct fd_info
*fdip
, int fd
)
265 int sysevents
= FDI_TO_SYSEVENTS(fdip
);
267 if (sysevents
!= 0) {
268 if (port_associate(epdp
->ed_port
, PORT_SOURCE_FD
,
269 fd
, sysevents
, NULL
) == -1) {
270 event_warn("port_associate");
275 check_evportop(epdp
);
281 * Main event loop - polls port_getn for some number of events, and processes
286 evport_dispatch(struct event_base
*base
, void *arg
, struct timeval
*tv
)
289 struct evport_data
*epdp
= arg
;
290 port_event_t pevtlist
[EVENTS_PER_GETN
];
293 * port_getn will block until it has at least nevents events. It will
294 * also return how many it's given us (which may be more than we asked
295 * for, as long as it's less than our maximum (EVENTS_PER_GETN)) in
301 * We have to convert a struct timeval to a struct timespec
302 * (only difference is nanoseconds vs. microseconds). If no time-based
303 * events are active, we should wait for I/O (and tv == NULL).
306 struct timespec
*ts_p
= NULL
;
308 ts
.tv_sec
= tv
->tv_sec
;
309 ts
.tv_nsec
= tv
->tv_usec
* 1000;
314 * Before doing anything else, we need to reassociate the events we hit
315 * last time which need reassociation. See comment at the end of the
318 for (i
= 0; i
< EVENTS_PER_GETN
; ++i
) {
319 struct fd_info
*fdi
= NULL
;
320 if (epdp
->ed_pending
[i
] != -1) {
321 fdi
= &(epdp
->ed_fds
[epdp
->ed_pending
[i
]]);
324 if (fdi
!= NULL
&& FDI_HAS_EVENTS(fdi
)) {
325 int fd
= FDI_HAS_READ(fdi
) ? fdi
->fdi_revt
->ev_fd
:
326 fdi
->fdi_wevt
->ev_fd
;
327 reassociate(epdp
, fdi
, fd
);
328 epdp
->ed_pending
[i
] = -1;
332 if ((res
= port_getn(epdp
->ed_port
, pevtlist
, EVENTS_PER_GETN
,
333 (unsigned int *) &nevents
, ts_p
)) == -1) {
334 if (errno
== EINTR
|| errno
== EAGAIN
) {
335 evsignal_process(base
);
337 } else if (errno
== ETIME
) {
341 event_warn("port_getn");
344 } else if (base
->sig
.evsignal_caught
) {
345 evsignal_process(base
);
348 event_debug(("%s: port_getn reports %d events", __func__
, nevents
));
350 for (i
= 0; i
< nevents
; ++i
) {
353 port_event_t
*pevt
= &pevtlist
[i
];
354 int fd
= (int) pevt
->portev_object
;
356 check_evportop(epdp
);
358 epdp
->ed_pending
[i
] = fd
;
361 * Figure out what kind of event it was
362 * (because we have to pass this to the callback)
365 if (pevt
->portev_events
& POLLIN
)
367 if (pevt
->portev_events
& POLLOUT
)
370 assert(epdp
->ed_nevents
> fd
);
371 fdi
= &(epdp
->ed_fds
[fd
]);
374 * We now check for each of the possible events (READ
375 * or WRITE). Then, we activate the event (which will
376 * cause its callback to be executed).
379 if ((res
& EV_READ
) && ((ev
= fdi
->fdi_revt
) != NULL
)) {
380 event_active(ev
, res
, 1);
383 if ((res
& EV_WRITE
) && ((ev
= fdi
->fdi_wevt
) != NULL
)) {
384 event_active(ev
, res
, 1);
386 } /* end of all events gotten */
388 check_evportop(epdp
);
395 * Adds the given event (so that you will be notified when it happens via
396 * the callback function).
400 evport_add(void *arg
, struct event
*ev
)
402 struct evport_data
*evpd
= arg
;
406 check_evportop(evpd
);
409 * Delegate, if it's not ours to handle.
411 if (ev
->ev_events
& EV_SIGNAL
)
412 return (evsignal_add(ev
));
415 * If necessary, grow the file descriptor info table
419 while (ev
->ev_fd
>= factor
* evpd
->ed_nevents
)
423 if (-1 == grow(evpd
, factor
)) {
428 fdi
= &evpd
->ed_fds
[ev
->ev_fd
];
429 if (ev
->ev_events
& EV_READ
)
431 if (ev
->ev_events
& EV_WRITE
)
434 return reassociate(evpd
, fdi
, ev
->ev_fd
);
438 * Removes the given event from the list of events to wait for.
442 evport_del(void *arg
, struct event
*ev
)
444 struct evport_data
*evpd
= arg
;
449 check_evportop(evpd
);
452 * Delegate, if it's not ours to handle
454 if (ev
->ev_events
& EV_SIGNAL
) {
455 return (evsignal_del(ev
));
458 if (evpd
->ed_nevents
< ev
->ev_fd
) {
462 for (i
= 0; i
< EVENTS_PER_GETN
; ++i
) {
463 if (evpd
->ed_pending
[i
] == ev
->ev_fd
) {
469 fdi
= &evpd
->ed_fds
[ev
->ev_fd
];
470 if (ev
->ev_events
& EV_READ
)
471 fdi
->fdi_revt
= NULL
;
472 if (ev
->ev_events
& EV_WRITE
)
473 fdi
->fdi_wevt
= NULL
;
476 if (!FDI_HAS_EVENTS(fdi
) &&
477 port_dissociate(evpd
->ed_port
, PORT_SOURCE_FD
,
480 * Ignre EBADFD error the fd could have been closed
481 * before event_del() was called.
483 if (errno
!= EBADFD
) {
484 event_warn("port_dissociate");
488 if (FDI_HAS_EVENTS(fdi
)) {
489 return (reassociate(evpd
, fdi
, ev
->ev_fd
));
493 if (fdi
->fdi_revt
== NULL
&& fdi
->fdi_wevt
== NULL
) {
494 evpd
->ed_pending
[i
] = -1;
502 evport_dealloc(struct event_base
*base
, void *arg
)
504 struct evport_data
*evpd
= arg
;
506 evsignal_dealloc(base
);
508 close(evpd
->ed_port
);