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>
27 static int engine_kevent_init() {
34 static void engine_kevent_add(struct IOLowlevelDescriptor *iold) {
35 if(iold->fd != -1) return;
36 //add descriptor to the kevent queue
37 struct kevent changes[2];
41 if (iold->flags & IOFLAGS_WANT_READ)
42 EV_SET(&changes[nchanges++], iold->fd, EVFILT_READ, EV_ADD, 0, 0, iold);
43 if (iold->flags & IOFLAGS_WANT_WRITE)
44 EV_SET(&changes[nchanges++], iold->fd, EVFILT_WRITE, EV_ADD, 0, 0, iold);
46 res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
48 iohandler_log(IOLOG_ERROR, "could not add IOLowlevelDescriptor %d to kevent queue. (returned: %d)", res);
51 static void engine_kevent_remove(struct IOLowlevelDescriptor *iold) {
52 if(iold->fd != -1) return;
53 struct kevent changes[2];
56 EV_SET(&changes[nchanges++], iold->fd, EVFILT_READ, EV_DELETE, 0, 0, iold);
57 EV_SET(&changes[nchanges++], iold->fd, EVFILT_WRITE, EV_DELETE, 0, 0, iold);
58 kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
61 static void engine_kevent_update(struct IOLowlevelDescriptor *iold) {
62 if(iold->fd != -1) return;
63 struct IODescriptor *iofd;
64 if((iofd = IOLOWLEVEL_GET_IOFD(iold))) {
65 if(iofd->state == IO_CLOSED) {
66 engine_kevent_remove(iold);
70 struct kevent changes[2];
74 if (iold->flags & IOFLAGS_WANT_READ)
75 EV_SET(&changes[nchanges++], iold->fd, EVFILT_READ, EV_ADD, 0, 0, iold);
77 EV_SET(&changes[nchanges++], iold->fd, EVFILT_READ, EV_DELETE, 0, 0, iold);
78 if (iold->flags & IOFLAGS_WANT_WRITE)
79 EV_SET(&changes[nchanges++], iold->fd, EVFILT_WRITE, EV_ADD, 0, 0, iold);
81 EV_SET(&changes[nchanges++], iold->fd, EVFILT_WRITE, EV_DELETE, 0, 0, iold);
83 res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
85 iohandler_log(IOLOG_ERROR, "could not update IOLowlevelDescriptor %d in kevent queue. (returned: %d)", res);
89 static void engine_kevent_loop(struct timeval *timeout) {
90 struct kevent events[MAX_EVENTS];
91 struct timeval now, tdiff;
92 struct timespec ts, *pts;
95 struct IOLowlevelDescriptor *iold;
97 gettimeofday(&now, NULL);
99 while(timer_priority) {
100 tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
101 tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
102 if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
103 if(timer_priority->constant_timeout) {
105 iohandler_set_timeout(timer_priority, &tdiff);
106 iohandler_events(timer_priority, 0, 0);
108 iohandler_events(timer_priority, 0, 0);
109 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
112 } else if(tdiff.tv_usec < 0) {
114 tdiff.tv_usec += 1000000; //1 sec
116 if(timeval_is_smaler((&tdiff), timeout)) {
117 timeout->tv_sec = tdiff.tv_sec;
118 timeout->tv_usec = tdiff.tv_usec;
124 ts.tv_sec = timeout->tv_sec;
125 ts.tv_nsec = timeout->tv_usec * 1000;
132 kevent_result = kevent(kevent_fd, NULL, 0, events, MAX_EVENTS, pts);
134 if (kevent_result < 0) {
135 if (errno != EINTR) {
136 iohandler_log(IOLOG_FATAL, "kevent() failed with errno %d: %s", errno, strerror(errno));
141 for(i = 0; i < kevent_result; i++) {
142 iold = events[i].udata;
143 if(iold->flags & IOFLAGS_HAVE_IOFD)
144 iohandler_events(iold->data.iofd, (events[i].filter == EVFILT_READ), (events[i].filter == EVFILT_WRITE));
146 iold->data.callback(iold, (events[i].filter == EVFILT_READ), (events[i].filter == EVFILT_WRITE));
151 while(timer_priority) {
152 tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
153 tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
154 if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
155 if(timer_priority->constant_timeout) {
157 iohandler_set_timeout(timer_priority, &tdiff);
158 iohandler_events(timer_priority, 0, 0);
160 iohandler_events(timer_priority, 0, 0);
161 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
170 static void engine_kevent_cleanup() {
174 struct IOEngine engine_kevent = {
176 .init = engine_kevent_init,
177 .add = engine_kevent_add,
178 .remove = engine_kevent_remove,
179 .update = engine_kevent_update,
180 .loop = engine_kevent_loop,
181 .cleanup = engine_kevent_cleanup,
186 struct IOEngine engine_kevent = {