12
12
* 3. The name of the author may not be used to endorse or promote products
13
13
* derived from this software without specific prior written permission.
15
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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) 2006 Sun Microsystems. All rights reserved.
28
* Copyright (c) 2007 Sun Microsystems. All rights reserved.
29
29
* Use is subject to license terms.
47
47
* necessary when large fd's come in. reassociate() takes care of maintaining
48
48
* the proper file-descriptor/event-port associations.
50
* As in the select(2) implementation, signals are handled by evsignal, and
51
* evport_recalc does almost nothing.
50
* As in the select(2) implementation, signals are handled by evsignal.
54
53
#ifdef HAVE_CONFIG_H
55
54
#include "config.h"
58
#ifdef HAVE_SYS_TIME_H
59
57
#include <sys/time.h>
61
#include <sys/_time.h>
63
58
#include <assert.h>
64
59
#include <sys/queue.h>
118
112
int ed_nevents; /* number of allocated fdi's */
119
113
struct fd_info *ed_fds; /* allocated fdi table */
120
114
/* fdi's that we need to reassoc */
121
struct fd_info *ed_pending[EVENTS_PER_GETN];
115
int ed_pending[EVENTS_PER_GETN]; /* fd's with pending events */
124
118
static void* evport_init (struct event_base *);
125
119
static int evport_add (void *, struct event *);
126
120
static int evport_del (void *, struct event *);
127
static int evport_recalc (struct event_base *, void *, int);
128
121
static int evport_dispatch (struct event_base *, void *, struct timeval *);
129
122
static void evport_dealloc (struct event_base *, void *);
131
124
const struct eventop evportops = {
254
247
epdp->ed_fds = tmp;
255
248
memset((char*) (epdp->ed_fds + oldsize), 0,
256
249
(newsize - oldsize)*sizeof(struct fd_info));
258
/* The ev_pending array contains pointers into the released array. */
259
for (ii = 0; ii < EVENTS_PER_GETN; ++ii) {
260
if (epdp->ed_pending[ii] != 0) {
261
int offset = epdp->ed_pending[ii] - old;
262
epdp->ed_pending[ii] = epdp->ed_fds + offset;
266
250
epdp->ed_nevents = newsize;
268
252
check_evportop(epdp);
281
265
int sysevents = FDI_TO_SYSEVENTS(fdip);
283
267
if (sysevents != 0) {
284
if ((-1 == port_associate(epdp->ed_port, PORT_SOURCE_FD,
285
fd, sysevents, NULL))) {
286
perror("port_associate");
290
if (-1 == port_dissociate(epdp->ed_port, PORT_SOURCE_FD, fd)) {
291
perror("port_dissociate");
268
if (port_associate(epdp->ed_port, PORT_SOURCE_FD,
269
fd, sysevents, NULL) == -1) {
270
event_warn("port_associate");
339
318
for (i = 0; i < EVENTS_PER_GETN; ++i) {
340
struct fd_info *fdi = epdp->ed_pending[i];
319
struct fd_info *fdi = NULL;
320
if (epdp->ed_pending[i] != -1) {
321
fdi = &(epdp->ed_fds[epdp->ed_pending[i]]);
342
324
if (fdi != NULL && FDI_HAS_EVENTS(fdi)) {
343
325
int fd = FDI_HAS_READ(fdi) ? fdi->fdi_revt->ev_fd :
344
326
fdi->fdi_wevt->ev_fd;
345
327
reassociate(epdp, fdi, fd);
346
epdp->ed_pending[i] = NULL;
328
epdp->ed_pending[i] = -1;
350
332
if ((res = port_getn(epdp->ed_port, pevtlist, EVENTS_PER_GETN,
351
333
(unsigned int *) &nevents, ts_p)) == -1) {
352
if (errno == EINTR) {
334
if (errno == EINTR || errno == EAGAIN) {
353
335
evsignal_process(base);
355
337
} else if (errno == ETIME) {
356
338
if (nevents == 0)
341
event_warn("port_getn");
362
344
} else if (base->sig.evsignal_caught) {
388
371
fdi = &(epdp->ed_fds[fd]);
391
* We now check for each of the possible events (READ or WRITE).
392
* If the event is not persistent, then we delete it. Then, we
393
* activate the event (which will cause its callback to be
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).
397
379
if ((res & EV_READ) && ((ev = fdi->fdi_revt) != NULL)) {
398
if (!(ev->ev_events & EV_PERSIST))
400
380
event_active(ev, res, 1);
403
383
if ((res & EV_WRITE) && ((ev = fdi->fdi_wevt) != NULL)) {
404
if (!(ev->ev_events & EV_PERSIST))
406
384
event_active(ev, res, 1);
410
* If there are still events (they haven't been deleted), then
411
* we must reassociate the port, since the event port interface
412
* dissociates them automatically.
414
* But we can't do it right away, because the event hasn't
415
* handled this event yet, so of course there's still data
418
if(FDI_HAS_EVENTS(fdi)) {
419
epdp->ed_pending[i] = fdi;
421
386
} /* end of all events gotten */
423
388
check_evportop(epdp);
430
* Copied from the version in select.c
434
evport_recalc(struct event_base *base, void *arg, int max)
436
struct evport_data *evpd = arg;
437
check_evportop(evpd);
443
395
* Adds the given event (so that you will be notified when it happens via
444
396
* the callback function).
512
472
if (ev->ev_events & EV_WRITE)
513
473
fdi->fdi_wevt = NULL;
515
return reassociate(evpd, fdi, ev->ev_fd);
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;