[IOMultiplexerV2] alpha
[NextIRCd.git] / src / IOHandler / IOEngine_kevent.c
1 /* IOengine_kevent.c - IOMultiplexer
2  * Copyright (C) 2014  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 #define _IOHandler_internals
18 #include "IOInternal.h"
19 #include "IOHandler.h"
20 #include "IOLog.h"
21 #include "IOSockets.h"
22
23 #ifdef HAVE_SYS_EVENT_H
24 #include <sys/event.h>
25 #include <errno.h>
26
27 #define MAX_EVENTS 32
28
29 static int kevent_fd;
30
31 static int engine_kevent_init() {
32         kevent_fd = kqueue();
33         if (kevent_fd < 0)
34                 return 0;
35         return 1;
36 }
37
38 static void engine_kevent_add(struct _IOSocket *iosock) {
39         //add Socket FD to the kevent queue
40         struct kevent changes[2];
41         int nchanges = 0;
42         int res;
43
44         EV_SET(&changes[nchanges++], iosock->fd, EVFILT_READ, EV_ADD, 0, 0, iosock);
45         if (iosocket_wants_writes(iosock))
46                 EV_SET(&changes[nchanges++], iosock->fd, EVFILT_WRITE, EV_ADD, 0, 0, iosock);
47         
48         res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
49         if(res < 0)
50                 iolog_trigger(IOLOG_ERROR, "could not add _IOSocket %d to kevent queue. (returned: %d)", iosock->fd, res);
51 }
52
53 static void engine_kevent_remove(struct _IOSocket *iosock) {
54         struct kevent changes[2];
55         int nchanges = 0;
56
57         EV_SET(&changes[nchanges++], iosock->fd, EVFILT_READ, EV_DELETE, 0, 0, iosock);
58         EV_SET(&changes[nchanges++], iosock->fd, EVFILT_WRITE, EV_DELETE, 0, 0, iosock);
59         kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
60 }
61
62 static void engine_kevent_update(struct _IOSocket *iosock) {
63         struct kevent changes[2];
64         int nchanges = 0;
65         int res;
66
67         EV_SET(&changes[nchanges++], iosock->fd, EVFILT_READ, EV_ADD, 0, 0, iosock);
68         EV_SET(&changes[nchanges++], iosock->fd, EVFILT_WRITE, iosocket_wants_writes(iosock) ? EV_ADD : EV_DELETE, 0, 0, iosock);
69         
70         res = kevent(kevent_fd, changes, nchanges, NULL, 0, NULL);
71         if(res < 0)
72                 iolog_trigger(IOLOG_ERROR, "could not update _IOSocket %d in kevent queue. (returned: %d)", iosock->fd, res);
73 }
74
75 static void engine_kevent_loop(struct timeval *timeout) {
76         struct kevent events[MAX_EVENTS];
77         struct timespec ts;
78         int msec;
79         int kevent_result;
80         struct timeval now, tout;
81         
82         //check timers
83         gettimeofday(&now, NULL);
84         if(iotimer_sorted_descriptors && timeval_is_bigger(now, iotimer_sorted_descriptors->timeout))
85                 _trigger_timer();
86         
87         //get timeout (timer or given timeout)
88         if(iotimer_sorted_descriptors) {
89                 tout = iotimer_sorted_descriptors->timeout;
90                 tout.tv_sec -= now.tv_sec;
91                 tout.tv_usec -= now.tv_usec;
92                 if(tout.tv_usec < 0) {
93                         tout.tv_sec --;
94                         tout.tv_usec += 1000000;
95                 }
96         }
97         if(timeout) {
98                 if(!iotimer_sorted_descriptors || timeval_is_smaler((*timeout), tout)) {
99                         tout.tv_usec = timeout->tv_usec;
100                         tout.tv_sec = timeout->tv_sec;
101                 }
102                 timeout = &tout;
103         } else if(iotimer_sorted_descriptors)
104                 timeout = &tout;
105         
106         
107         //select system call
108         kevent_result = kevent(kevent_fd, NULL, 0, events, MAX_EVENTS, timeout);
109         
110         if (kevent_result < 0) {
111                 if (errno != EINTR) {
112                         iolog_trigger(IOLOG_FATAL, "kevent() failed with errno %d: %s", errno, strerror(errno));
113                         return;
114                 }
115         } else {
116                 int i;
117                 for(i = 0; i < kevent_result; i++)
118                         iosocket_events_callback(events[i].udata, (events[i].filter == EVFILT_READ), (events[i].filter == EVFILT_WRITE));
119         }
120         
121         //check timers
122         gettimeofday(&now, NULL);
123         if(iotimer_sorted_descriptors && timeval_is_bigger(now, iotimer_sorted_descriptors->timeout))
124                 _trigger_timer();
125 }
126
127 static void engine_kevent_cleanup() {
128         close(kevent_fd);
129 }
130
131 struct IOEngine engine_kevent = {
132         .name = "kevent",
133         .init = engine_kevent_init,
134         .add = engine_kevent_add,
135         .remove = engine_kevent_remove,
136         .update = engine_kevent_update,
137         .loop = engine_kevent_loop,
138         .cleanup = engine_kevent_cleanup,
139 };
140
141 #else
142
143 struct IOEngine engine_kevent = {
144         .name = "kevent",
145         .init = NULL,
146         .add = NULL,
147         .remove = NULL,
148         .update = NULL,
149         .loop = NULL,
150         .cleanup = NULL,
151 };
152
153 #endif