[IOMultiplexer] initial commit
[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
22 #define MAX_EVENTS 32
23
24 static int kevent_fd;
25
26 static int engine_kevent_init() {
27     kevent_fd = kqueue();
28     if (kevent_fd < 0)
29         return 0;
30     return 1;
31 }
32
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];
37     int nchanges = 0;
38     int res;
39
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);
43     
44     res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
45     if(res < 0)
46         iohandler_log(IOLOG_ERROR, "could not add IODescriptor %d to kevent queue. (returned: %d)", res);
47     }
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     struct kevent changes[2];
63     int nchanges = 0;
64     int res;
65
66     EV_SET(&changes[nchanges++], iofd->fd, EVFILT_READ, EV_ADD, 0, 0, iofd);
67     EV_SET(&changes[nchanges++], iofd->fd, EVFILT_WRITE, iohandler_wants_writes(iofd) ? EV_ADD : EV_DELETE, 0, 0, iofd);
68     
69     res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
70     if(res < 0) {
71         iohandler_log(IOLOG_ERROR, "could not update IODescriptor %d in kevent queue. (returned: %d)", res);
72     }
73 }
74
75 static void engine_kevent_loop(struct timeval *timeout) {
76     struct kevent evts[MAX_EVENTS];
77     struct timeval now, tdiff;
78     struct timespec ts, *ptr
79     int msec;
80     int events;
81     int kevent_result;
82     
83     gettimeofday(&now, NULL);
84     
85     while(timer_priority) {
86         tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
87         tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
88         if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
89             iohandler_events(timer_priority, 0, 0);
90             iohandler_close(timer_priority); //also sets timer_priority to the next timed element
91             continue;
92         } else if(tdiff.tv_usec < 0) {
93             tdiff.tv_sec--;
94             tdiff.tv_usec += 1000000; //1 sec
95         }
96         if(timeval_is_smaler((&tdiff), timeout)) {
97             timeout->tv_sec = tdiff.tv_sec;
98             timeout->tv_usec = tdiff.tv_usec;
99         }
100         break;
101     }
102     
103     if (timeout) {
104         ts.tv_sec = timeout->tv_sec;
105         ts.tv_nsec = timeout->tv_usec * 1000;
106         pts = &ts;
107     } else {
108         pts = NULL;
109     }
110     
111     //select system call
112     kevent_result = kevent(kq_fd, NULL, 0, events, MAX_EVENTS, pts);
113     
114     if (kevent_result < 0) {
115         if (errno != EINTR) {
116             iohandler_log(IOLOG_FATAL, "kevent() failed with errno %d: %s", errno, strerror(errno));
117             return;
118         }
119     } else {
120         int i;
121         for(i = 0; i < kevent_result; i++)
122             iohandler_events(evts[i].udata, (evts[i].filter == EVFILT_READ), (evts[i].filter == EVFILT_WRITE));
123     }
124     
125     //check timers
126     while(timer_priority) {
127         tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
128         tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
129         if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
130             iohandler_events(timer_priority, 0, 0);
131             iohandler_close(timer_priority); //also sets timer_priority to the next timed element
132             continue;
133         }
134         break;
135     }
136     
137 }
138
139 static void engine_kevent_cleanup() {
140     close(kevent_fd);
141 }
142
143 struct IOEngine engine_kevent = {
144     .name = "kevent",
145     .init = engine_kevent_init,
146     .add = engine_kevent_add,
147     .remove = engine_kevent_remove,
148     .update = engine_kevent_update,
149     .loop = engine_kevent_loop,
150     .cleanup = engine_kevent_cleanup,
151 };
152
153 #else
154
155 struct IOEngine engine_kevent = {
156     .name = "kevent",
157     .init = NULL,
158     .add = NULL,
159     .remove = NULL,
160     .update = NULL,
161     .loop = NULL,
162     .cleanup = NULL,
163 };
164
165 #endif