Merge remote-tracking branch 'IOMultiplexer.git/master' into development
[NeonServV5.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 IODescriptor *iofd) {
35     if(iofd->type == IOTYPE_TIMER) return;
36     //add descriptor to the kevent queue
37     struct kevent changes[2];
38     int nchanges = 0;
39     int res;
40
41     EV_SET(&changes[nchanges++], iofd->fd, EVFILT_READ, EV_ADD, 0, 0, iofd);
42     if (iohandler_wants_writes(iofd))
43         EV_SET(&changes[nchanges++], iofd->fd, EVFILT_WRITE, EV_ADD, 0, 0, iofd);
44     
45     res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
46     if(res < 0)
47         iohandler_log(IOLOG_ERROR, "could not add IODescriptor %d to kevent queue. (returned: %d)", res);
48 }
49
50 static void engine_kevent_remove(struct IODescriptor *iofd) {
51     if(iofd->type == IOTYPE_TIMER) return;
52     struct kevent changes[2];
53     int nchanges = 0;
54
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);
58 }
59
60 static void engine_kevent_update(struct IODescriptor *iofd) {
61     if(iofd->type == IOTYPE_TIMER) return;
62     if(iofd->state == IO_CLOSED) {
63         engine_kevent_remove(iofd);
64         return;
65     }
66     struct kevent changes[2];
67     int nchanges = 0;
68     int res;
69
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);
72     
73     res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
74     if(res < 0) {
75         iohandler_log(IOLOG_ERROR, "could not update IODescriptor %d in kevent queue. (returned: %d)", res);
76     }
77 }
78
79 static void engine_kevent_loop(struct timeval *timeout) {
80     struct kevent events[MAX_EVENTS];
81     struct timeval now, tdiff;
82     struct timespec ts, *pts;
83     int msec;
84     int kevent_result;
85     
86     gettimeofday(&now, NULL);
87     
88     while(timer_priority) {
89         tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
90         tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
91         if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
92             if(timer_priority->constant_timeout) {
93                 tdiff.tv_sec = 0;
94                 iohandler_set_timeout(timer_priority, &tdiff);
95                 iohandler_events(timer_priority, 0, 0);
96             } else {
97                 iohandler_events(timer_priority, 0, 0);
98                 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
99             }
100             continue;
101         } else if(tdiff.tv_usec < 0) {
102             tdiff.tv_sec--;
103             tdiff.tv_usec += 1000000; //1 sec
104         }
105         if(timeval_is_smaler((&tdiff), timeout)) {
106             timeout->tv_sec = tdiff.tv_sec;
107             timeout->tv_usec = tdiff.tv_usec;
108         }
109         break;
110     }
111     
112     if (timeout) {
113         ts.tv_sec = timeout->tv_sec;
114         ts.tv_nsec = timeout->tv_usec * 1000;
115         pts = &ts;
116     } else {
117         pts = NULL;
118     }
119     
120     //select system call
121     kevent_result = kevent(kevent_fd, NULL, 0, events, MAX_EVENTS, pts);
122     
123     if (kevent_result < 0) {
124         if (errno != EINTR) {
125             iohandler_log(IOLOG_FATAL, "kevent() failed with errno %d: %s", errno, strerror(errno));
126             return;
127         }
128     } else {
129         int i;
130         for(i = 0; i < kevent_result; i++)
131             iohandler_events(events[i].udata, (events[i].filter == EVFILT_READ), (events[i].filter == EVFILT_WRITE));
132     }
133     
134     //check timers
135     while(timer_priority) {
136         tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
137         tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
138         if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
139             if(timer_priority->constant_timeout) {
140                 tdiff.tv_sec = 0;
141                 iohandler_set_timeout(timer_priority, &tdiff);
142                 iohandler_events(timer_priority, 0, 0);
143             } else {
144                 iohandler_events(timer_priority, 0, 0);
145                 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
146             }
147             continue;
148         }
149         break;
150     }
151     
152 }
153
154 static void engine_kevent_cleanup() {
155     close(kevent_fd);
156 }
157
158 struct IOEngine engine_kevent = {
159     .name = "kevent",
160     .init = engine_kevent_init,
161     .add = engine_kevent_add,
162     .remove = engine_kevent_remove,
163     .update = engine_kevent_update,
164     .loop = engine_kevent_loop,
165     .cleanup = engine_kevent_cleanup,
166 };
167
168 #else
169
170 struct IOEngine engine_kevent = {
171     .name = "kevent",
172     .init = NULL,
173     .add = NULL,
174     .remove = NULL,
175     .update = NULL,
176     .loop = NULL,
177     .cleanup = NULL,
178 };
179
180 #endif