[IOMultiplexer] Added asynchronous DNS Lookups
[IOMultiplexer.git] / src / IOEngine_kevent.c
1 /* IOengine_kevent.c - IOMultiplexer
2  * Copyright (C) 2012  Philipp Kreil (pk910)
3  * 
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.
8  * 
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.
13  * 
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/>. 
16  */
17 #include "IOEngine.h"
18
19 #ifdef HAVE_SYS_EVENT_H
20 #include <sys/event.h>
21 #include <errno.h>
22
23 #define MAX_EVENTS 32
24
25 static int kevent_fd;
26
27 static int engine_kevent_init() {
28     kevent_fd = kqueue();
29     if (kevent_fd < 0)
30         return 0;
31     return 1;
32 }
33
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];
38     int nchanges = 0;
39     int res;
40
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);
45     
46     res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
47     if(res < 0)
48         iohandler_log(IOLOG_ERROR, "could not add IOLowlevelDescriptor %d to kevent queue. (returned: %d)", res);
49 }
50
51 static void engine_kevent_remove(struct IOLowlevelDescriptor *iold) {
52     if(iold->fd != -1) return;
53     struct kevent changes[2];
54     int nchanges = 0;
55
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);
59 }
60
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);
67             return;
68         }
69     }
70     struct kevent changes[2];
71     int nchanges = 0;
72     int res;
73
74     if (iold->flags & IOFLAGS_WANT_READ)
75         EV_SET(&changes[nchanges++], iold->fd, EVFILT_READ, EV_ADD, 0, 0, iold);
76     else
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);
80     else
81         EV_SET(&changes[nchanges++], iold->fd, EVFILT_WRITE, EV_DELETE, 0, 0, iold);
82     
83     res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
84     if(res < 0) {
85         iohandler_log(IOLOG_ERROR, "could not update IOLowlevelDescriptor %d in kevent queue. (returned: %d)", res);
86     }
87 }
88
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;
93     int msec;
94     int kevent_result;
95     struct IOLowlevelDescriptor *iold;
96     
97     gettimeofday(&now, NULL);
98     
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) {
104                 tdiff.tv_sec = 0;
105                 iohandler_set_timeout(timer_priority, &tdiff);
106                 iohandler_events(timer_priority, 0, 0);
107             } else {
108                 iohandler_events(timer_priority, 0, 0);
109                 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
110             }
111             continue;
112         } else if(tdiff.tv_usec < 0) {
113             tdiff.tv_sec--;
114             tdiff.tv_usec += 1000000; //1 sec
115         }
116         if(timeval_is_smaler((&tdiff), timeout)) {
117             timeout->tv_sec = tdiff.tv_sec;
118             timeout->tv_usec = tdiff.tv_usec;
119         }
120         break;
121     }
122     
123     if (timeout) {
124         ts.tv_sec = timeout->tv_sec;
125         ts.tv_nsec = timeout->tv_usec * 1000;
126         pts = &ts;
127     } else {
128         pts = NULL;
129     }
130     
131     //select system call
132     kevent_result = kevent(kevent_fd, NULL, 0, events, MAX_EVENTS, pts);
133     
134     if (kevent_result < 0) {
135         if (errno != EINTR) {
136             iohandler_log(IOLOG_FATAL, "kevent() failed with errno %d: %s", errno, strerror(errno));
137             return;
138         }
139     } else {
140         int i;
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));
145             else
146                 iold->data.callback(iold, (events[i].filter == EVFILT_READ), (events[i].filter == EVFILT_WRITE));
147         }
148     }
149     
150     //check timers
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) {
156                 tdiff.tv_sec = 0;
157                 iohandler_set_timeout(timer_priority, &tdiff);
158                 iohandler_events(timer_priority, 0, 0);
159             } else {
160                 iohandler_events(timer_priority, 0, 0);
161                 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
162             }
163             continue;
164         }
165         break;
166     }
167     
168 }
169
170 static void engine_kevent_cleanup() {
171     close(kevent_fd);
172 }
173
174 struct IOEngine engine_kevent = {
175     .name = "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,
182 };
183
184 #else
185
186 struct IOEngine engine_kevent = {
187     .name = "kevent",
188     .init = NULL,
189     .add = NULL,
190     .remove = NULL,
191     .update = NULL,
192     .loop = NULL,
193     .cleanup = NULL,
194 };
195
196 #endif