[IOMultiplexer] initial commit
[NeonServV5.git] / src / IOEngine_epoll.c
1 /* IOEngine_epoll.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_EPOLL_H
20 #include <sys/epoll.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #define MAX_EVENTS 32
26
27 static int epoll_fd;
28
29 static int engine_epoll_init() {
30     epoll_fd = epoll_create(1024);
31     if (epoll_fd < 0)
32         return 0;
33     return 1;
34 }
35
36 static void engine_epoll_add(struct IODescriptor *iofd) {
37     if(iofd->type == IOTYPE_TIMER) return;
38     //add descriptor to the epoll queue
39     struct epoll_event evt;
40     int res;
41
42     evt.events = EPOLLHUP | EPOLLIN | (iohandler_wants_writes(iofd) ? EPOLLOUT : 0);
43     evt.data.ptr = iofd;
44     res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, iofd->fd, &evt);
45     if(res < 0) {
46         iohandler_log(IOLOG_ERROR, "could not add IODescriptor %d to epoll queue. (returned: %d)", iofd->fd, res);
47     }
48 }
49
50 static void engine_epoll_remove(struct IODescriptor *iofd) {
51     if(iofd->type == IOTYPE_TIMER) return;
52     struct epoll_event evt;
53     epoll_ctl(epoll_fd, EPOLL_CTL_DEL, iofd->fd, &evt);
54 }
55
56 static void engine_epoll_update(struct IODescriptor *iofd) {
57     if(iofd->type == IOTYPE_TIMER) return;
58     struct epoll_event evt;
59     int res;
60
61     evt.events = EPOLLHUP | EPOLLIN | (iohandler_wants_writes(iofd) ? EPOLLOUT : 0);
62     evt.data.ptr = iofd;
63     res = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, iofd->fd, &evt);
64     if(res < 0) {
65         iohandler_log(IOLOG_ERROR, "could not update IODescriptor %d in epoll queue. (returned: %d)", iofd->fd, res);
66     }
67 }
68
69 static void engine_epoll_loop(struct timeval *timeout) {
70     struct epoll_event evts[MAX_EVENTS];
71     struct timeval now, tdiff;
72     int msec;
73     int events;
74     int epoll_result;
75     
76     gettimeofday(&now, NULL);
77     
78     while(timer_priority) {
79         tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
80         tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
81         if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
82             iohandler_events(timer_priority, 0, 0);
83             iohandler_close(timer_priority); //also sets timer_priority to the next timed element
84             continue;
85         } else if(tdiff.tv_usec < 0) {
86             tdiff.tv_sec--;
87             tdiff.tv_usec += 1000000; //1 sec
88         }
89         if(timeval_is_smaler((&tdiff), timeout)) {
90             timeout->tv_sec = tdiff.tv_sec;
91             timeout->tv_usec = tdiff.tv_usec;
92         }
93         break;
94     }
95     
96     msec = timeout ? ((timeout->tv_sec * 1000 + timeout->tv_usec / 1000) + (timeout->tv_usec % 1000 != 0 ? 1 : 0)) : -1;
97     
98     //select system call
99     epoll_result = epoll_wait(epoll_fd, evts, MAX_EVENTS, msec);
100     
101     if (epoll_result < 0) {
102         if (errno != EINTR) {
103             iohandler_log(IOLOG_FATAL, "epoll_wait() failed with errno %d: %s", errno, strerror(errno));
104             return;
105         }
106     } else {
107         int i;
108         for(i = 0; i < epoll_result; i++) {
109             events = evts[i].events;
110             iohandler_events(evts[i].data.ptr, (events & (EPOLLIN | EPOLLHUP)), (events & EPOLLOUT));
111         }
112     }
113     
114     //check timers
115     while(timer_priority) {
116         tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
117         tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
118         if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
119             iohandler_events(timer_priority, 0, 0);
120             iohandler_close(timer_priority); //also sets timer_priority to the next timed element
121             continue;
122         }
123         break;
124     }
125     
126 }
127
128 static void engine_epoll_cleanup() {
129     close(epoll_fd);
130 }
131
132 struct IOEngine engine_epoll = {
133     .name = "epoll",
134     .init = engine_epoll_init,
135     .add = engine_epoll_add,
136     .remove = engine_epoll_remove,
137     .update = engine_epoll_update,
138     .loop = engine_epoll_loop,
139     .cleanup = engine_epoll_cleanup,
140 };
141
142 #else
143
144 struct IOEngine engine_epoll = {
145     .name = "epoll",
146     .init = NULL,
147     .add = NULL,
148     .remove = NULL,
149     .update = NULL,
150     .loop = NULL,
151     .cleanup = NULL,
152 };
153
154 #endif