/* Some platforms apparently define the udata field of struct kevent as *intptr_t,whereasothersdefineitasvoid*.Theredoesn'tseemtobean
* easy way to tell them apart via autoconf, so we need to use OS macros. */ #ifdefined(__NetBSD__) #define PTR_TO_UDATA(x) ((typeof(((struct kevent *)0)->udata))(x)) #define INT_TO_UDATA(x) ((typeof(((struct kevent *)0)->udata))(intptr_t)(x)) #elifdefined(EVENT__HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__) && !defined(__CloudABI__) #define PTR_TO_UDATA(x) ((intptr_t)(x)) #define INT_TO_UDATA(x) ((intptr_t)(x)) #else #define PTR_TO_UDATA(x) (x) #define INT_TO_UDATA(x) ((void*)(x)) #endif
if (change & EV_CHANGE_ADD) {
out->flags = EV_ADD; /* We set a magic number here so that we can tell 'add'
* errors from 'del' errors. */
out->udata = INT_TO_UDATA(ADD_UDATA); if (change & EV_ET)
out->flags |= EV_CLEAR; #ifdef NOTE_EOF /* Make it behave like select() and poll() */ if (filter == EVFILT_READ)
out->fflags = NOTE_EOF; #endif
} else {
EVUTIL_ASSERT(change & EV_CHANGE_DEL);
out->flags = EV_DELETE;
}
}
staticint
kq_build_changes_list(conststruct event_changelist *changelist, struct kqop *kqop)
{ int i; int n_changes = 0;
for (i = 0; i < changelist->n_changes; ++i) { struct event_change *in_ch = &changelist->changes[i]; struct kevent *out_ch; if (n_changes >= kqop->changes_size - 1) { int newsize; struct kevent *newchanges;
/* steal the changes array in case some broken code tries to call
* dispatch twice at once. */
changes = kqop->changes;
kqop->changes = NULL;
/* Make sure that 'events' is at least as long as the list of changes: *otherwiseerrorsinthechangescangetreportedasa-1return *valuefromkevent()ratherthanasEV_ERROReventsintheevents *array. * *(Wecouldinsteadhandle-1returnvaluesfromkevent()by *retryingwithasmallerchangesarrayoralargereventsarray, *butthisapproachseemslessriskyfornow.)
*/ if (kqop->events_size < n_changes) { int new_size = kqop->events_size; do {
new_size *= 2;
} while (new_size < n_changes);
if (events[i].flags & EV_ERROR) { switch (events[i].data) {
/* Can occur on delete if we are not currently *watchinganyeventsonthisfd.Thatcan *happenwhenthefdwasclosedandanother
* file was opened with that fd. */ case ENOENT: /* Can occur for reasons not fully understood
* on FreeBSD. */ case EINVAL: continue; #ifdefined(__FreeBSD__) /* *ThiscurrentlyoccursifanFDisclosed *beforetheEV_DELETEmakesitoutviakevent(). *TheFreeBSDcapabilitiescodeseestheblank *capabilitysetandrejectstherequestto *modifyanevent. * *Tobestrictlycorrect-whenanFDisclosed, *alltheregisteredeventsarealsoremoved. *QueuingEV_DELETEtoaclosedFDiswrong. *Theevent(s)shouldjustbedeletedfrom *thependingchangelist.
*/ case ENOTCAPABLE: continue; #endif
/* Can occur on a delete if the fd is closed. */ case EBADF: /* XXXX On NetBSD, we can also get EBADF if we *trytoaddthewritesideofapipe,but *thereadsidehasalreadybeenclosed. *OtherBSDscallthissituation'EPIPE'.It *wouldbegoodifwehadawaytoreport
* this situation. */ continue; /* These two can occur on an add if the fd was one side
* of a pipe, and the other side was closed. */ case EPERM: case EPIPE: /* Report read events, if we're listening for *them,sothattheusercanlearnaboutany *adderrors.(Iftheoperationwasa
* delete, then udata should be cleared.) */ if (events[i].udata) { /* The operation was an add:
* report the error as a read. */
which |= EV_READ; break;
} else { /* The operation was a del:
* report nothing. */ continue;
}
if (events[i].filter == EVFILT_SIGNAL) {
evmap_signal_active_(base, events[i].ident, 1);
} else {
evmap_io_active_(base, events[i].ident, which | EV_ET);
}
}
if (res == kqop->events_size) { /* We used all the events space that we have. Maybe we should
make it bigger. */
kq_grow_events(kqop, kqop->events_size * 2);
}
return (0);
}
staticvoid
kqop_free(struct kqop *kqop)
{ if (kqop->changes)
mm_free(kqop->changes); if (kqop->events)
mm_free(kqop->events); if (kqop->kq >= 0 && kqop->pid == getpid())
close(kqop->kq);
memset(kqop, 0, sizeof(struct kqop));
mm_free(kqop);
}
/* Be ready for the signal if it is sent any *timebetweennowandthenextcallto
* kq_dispatch. */ if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) return (-1);
/* We can set the handler for most signals to SIG_IGN and *stillhavethemreportedtousinthequeue.However, *ifthehandlerforSIGCHLDisSIG_IGN,thesystemreaps *zombieprocessesforus,andwedon'tgetanynotification.
* This appears to be the only signal with this quirk. */ if (evsig_set_handler_(base, nsignal,
nsignal == SIGCHLD ? SIG_DFL : SIG_IGN) == -1) return (-1);
return (0);
}
staticint
kq_sig_del(struct event_base *base, int nsignal, short old, short events, void *p)
{ struct kqop *kqop = base->evbase; struct kevent kev;
/* Because we insert signal events *immediately,weneedtodeletethem
* immediately, too */ if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) return (-1);
if (evsig_restore_handler_(base, nsignal) == -1) return (-1);
return (0);
}
/* OSX 10.6 and FreeBSD 8.1 add support for EVFILT_USER, which we can use
* to wake up the event loop from another thread. */
/* Magic number we use for our filter ID. */ #define NOTIFY_IDENT 42
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.