1 /* IOengine_kevent.c - IOMultiplexer
2 * Copyright (C) 2012 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifdef HAVE_SYS_EVENT_H
20 #include <sys/event.h>
26 static int engine_kevent_init() {
33 static void engine_kevent_add(struct IODescriptor *iofd) {
34 if(iofd->type == IOTYPE_TIMER) return;
35 //add descriptor to the kevent queue
36 struct kevent changes[2];
40 EV_SET(&changes[nchanges++], iofd->fd, EVFILT_READ, EV_ADD, 0, 0, iofd);
41 if (iohandler_wants_writes(iofd))
42 EV_SET(&changes[nchanges++], iofd->fd, EVFILT_WRITE, EV_ADD, 0, 0, iofd);
44 res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
46 iohandler_log(IOLOG_ERROR, "could not add IODescriptor %d to kevent queue. (returned: %d)", res);
50 static void engine_kevent_remove(struct IODescriptor *iofd) {
51 if(iofd->type == IOTYPE_TIMER) return;
52 struct kevent changes[2];
55 EV_SET(&changes[nchanges++], iofd->fd, EVFILT_READ, EV_DELETE, 0, 0, iofd);
56 EV_SET(&changes[nchanges++], iofd->fd, EVFILT_WRITE, EV_DELETE, 0, 0, iofd);
57 kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
60 static void engine_kevent_update(struct IODescriptor *iofd) {
61 if(iofd->type == IOTYPE_TIMER) return;
62 if(iofd->state == IO_CLOSED) {
63 engine_epoll_remove(iofd);
66 struct kevent changes[2];
70 EV_SET(&changes[nchanges++], iofd->fd, EVFILT_READ, EV_ADD, 0, 0, iofd);
71 EV_SET(&changes[nchanges++], iofd->fd, EVFILT_WRITE, iohandler_wants_writes(iofd) ? EV_ADD : EV_DELETE, 0, 0, iofd);
73 res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
75 iohandler_log(IOLOG_ERROR, "could not update IODescriptor %d in kevent queue. (returned: %d)", res);
79 static void engine_kevent_loop(struct timeval *timeout) {
80 struct kevent evts[MAX_EVENTS];
81 struct timeval now, tdiff;
82 struct timespec ts, *ptr
87 gettimeofday(&now, NULL);
89 while(timer_priority) {
90 tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
91 tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
92 if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
93 iohandler_events(timer_priority, 0, 0);
94 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
96 } else if(tdiff.tv_usec < 0) {
98 tdiff.tv_usec += 1000000; //1 sec
100 if(timeval_is_smaler((&tdiff), timeout)) {
101 timeout->tv_sec = tdiff.tv_sec;
102 timeout->tv_usec = tdiff.tv_usec;
108 ts.tv_sec = timeout->tv_sec;
109 ts.tv_nsec = timeout->tv_usec * 1000;
116 kevent_result = kevent(kq_fd, NULL, 0, events, MAX_EVENTS, pts);
118 if (kevent_result < 0) {
119 if (errno != EINTR) {
120 iohandler_log(IOLOG_FATAL, "kevent() failed with errno %d: %s", errno, strerror(errno));
125 for(i = 0; i < kevent_result; i++)
126 iohandler_events(evts[i].udata, (evts[i].filter == EVFILT_READ), (evts[i].filter == EVFILT_WRITE));
130 while(timer_priority) {
131 tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
132 tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
133 if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
134 iohandler_events(timer_priority, 0, 0);
135 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
143 static void engine_kevent_cleanup() {
147 struct IOEngine engine_kevent = {
149 .init = engine_kevent_init,
150 .add = engine_kevent_add,
151 .remove = engine_kevent_remove,
152 .update = engine_kevent_update,
153 .loop = engine_kevent_loop,
154 .cleanup = engine_kevent_cleanup,
159 struct IOEngine engine_kevent = {