return 1;
}
-static void engine_kevent_add(struct IODescriptor *iofd) {
- if(iofd->type == IOTYPE_TIMER) return;
+static void engine_kevent_add(struct IOLowlevelDescriptor *iold) {
+ if(iold->fd != -1) return;
//add descriptor to the kevent queue
struct kevent changes[2];
int nchanges = 0;
int res;
- EV_SET(&changes[nchanges++], iofd->fd, EVFILT_READ, EV_ADD, 0, 0, iofd);
- if (iohandler_wants_writes(iofd))
- EV_SET(&changes[nchanges++], iofd->fd, EVFILT_WRITE, EV_ADD, 0, 0, iofd);
+ if (iold->flags & IOFLAGS_WANT_READ)
+ EV_SET(&changes[nchanges++], iold->fd, EVFILT_READ, EV_ADD, 0, 0, iold);
+ if (iold->flags & IOFLAGS_WANT_WRITE)
+ EV_SET(&changes[nchanges++], iold->fd, EVFILT_WRITE, EV_ADD, 0, 0, iold);
res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
if(res < 0)
- iohandler_log(IOLOG_ERROR, "could not add IODescriptor %d to kevent queue. (returned: %d)", res);
+ iohandler_log(IOLOG_ERROR, "could not add IOLowlevelDescriptor %d to kevent queue. (returned: %d)", res);
}
-static void engine_kevent_remove(struct IODescriptor *iofd) {
- if(iofd->type == IOTYPE_TIMER) return;
+static void engine_kevent_remove(struct IOLowlevelDescriptor *iold) {
+ if(iold->fd != -1) return;
struct kevent changes[2];
int nchanges = 0;
- EV_SET(&changes[nchanges++], iofd->fd, EVFILT_READ, EV_DELETE, 0, 0, iofd);
- EV_SET(&changes[nchanges++], iofd->fd, EVFILT_WRITE, EV_DELETE, 0, 0, iofd);
+ EV_SET(&changes[nchanges++], iold->fd, EVFILT_READ, EV_DELETE, 0, 0, iold);
+ EV_SET(&changes[nchanges++], iold->fd, EVFILT_WRITE, EV_DELETE, 0, 0, iold);
kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
}
-static void engine_kevent_update(struct IODescriptor *iofd) {
- if(iofd->type == IOTYPE_TIMER) return;
- if(iofd->state == IO_CLOSED) {
- engine_kevent_remove(iofd);
- return;
+static void engine_kevent_update(struct IOLowlevelDescriptor *iold) {
+ if(iold->fd != -1) return;
+ struct IODescriptor *iofd;
+ if((iofd = IOLOWLEVEL_GET_IOFD(iold))) {
+ if(iofd->state == IO_CLOSED) {
+ engine_kevent_remove(iold);
+ return;
+ }
}
struct kevent changes[2];
int nchanges = 0;
int res;
- EV_SET(&changes[nchanges++], iofd->fd, EVFILT_READ, EV_ADD, 0, 0, iofd);
- EV_SET(&changes[nchanges++], iofd->fd, EVFILT_WRITE, iohandler_wants_writes(iofd) ? EV_ADD : EV_DELETE, 0, 0, iofd);
+ if (iold->flags & IOFLAGS_WANT_READ)
+ EV_SET(&changes[nchanges++], iold->fd, EVFILT_READ, EV_ADD, 0, 0, iold);
+ else
+ EV_SET(&changes[nchanges++], iold->fd, EVFILT_READ, EV_DELETE, 0, 0, iold);
+ if (iold->flags & IOFLAGS_WANT_WRITE)
+ EV_SET(&changes[nchanges++], iold->fd, EVFILT_WRITE, EV_ADD, 0, 0, iold);
+ else
+ EV_SET(&changes[nchanges++], iold->fd, EVFILT_WRITE, EV_DELETE, 0, 0, iold);
res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
if(res < 0) {
- iohandler_log(IOLOG_ERROR, "could not update IODescriptor %d in kevent queue. (returned: %d)", res);
+ iohandler_log(IOLOG_ERROR, "could not update IOLowlevelDescriptor %d in kevent queue. (returned: %d)", res);
}
}
struct timespec ts, *pts;
int msec;
int kevent_result;
+ struct IOLowlevelDescriptor *iold;
gettimeofday(&now, NULL);
tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
- iohandler_events(timer_priority, 0, 0);
- iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ if(timer_priority->constant_timeout) {
+ tdiff.tv_sec = 0;
+ iohandler_set_timeout(timer_priority, &tdiff);
+ iohandler_events(timer_priority, 0, 0);
+ } else {
+ iohandler_events(timer_priority, 0, 0);
+ iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ }
continue;
} else if(tdiff.tv_usec < 0) {
tdiff.tv_sec--;
}
} else {
int i;
- for(i = 0; i < kevent_result; i++)
- iohandler_events(events[i].udata, (events[i].filter == EVFILT_READ), (events[i].filter == EVFILT_WRITE));
+ for(i = 0; i < kevent_result; i++) {
+ iold = events[i].udata;
+ if(iold->flags & IOFLAGS_HAVE_IOFD)
+ iohandler_events(iold->data.iofd, (events[i].filter == EVFILT_READ), (events[i].filter == EVFILT_WRITE));
+ else
+ iold->data.callback(iold, (events[i].filter == EVFILT_READ), (events[i].filter == EVFILT_WRITE));
+ }
}
//check timers
tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
- iohandler_events(timer_priority, 0, 0);
- iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ if(timer_priority->constant_timeout) {
+ tdiff.tv_sec = 0;
+ iohandler_set_timeout(timer_priority, &tdiff);
+ iohandler_events(timer_priority, 0, 0);
+ } else {
+ iohandler_events(timer_priority, 0, 0);
+ iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ }
continue;
}
break;