e36394960f3af206bf4fa594fc10732e2de324f1
[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     if(iofd->state == IO_CLOSED) {
59         engine_epoll_remove(iofd);
60         return;
61     }
62     struct epoll_event evt;
63     int res;
64
65     evt.events = EPOLLHUP | EPOLLIN | (iohandler_wants_writes(iofd) ? EPOLLOUT : 0);
66     evt.data.ptr = iofd;
67     res = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, iofd->fd, &evt);
68     if(res < 0) {
69         iohandler_log(IOLOG_ERROR, "could not update IODescriptor %d in epoll queue. (returned: %d)", iofd->fd, res);
70     }
71 }
72
73 static void engine_epoll_loop(struct timeval *timeout) {
74     struct epoll_event evts[MAX_EVENTS];
75     struct timeval now, tdiff;
76     int msec;
77     int events;
78     int epoll_result;
79     
80     gettimeofday(&now, NULL);
81     
82     while(timer_priority) {
83         tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
84         tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
85         if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
86             iohandler_events(timer_priority, 0, 0);
87             iohandler_close(timer_priority); //also sets timer_priority to the next timed element
88             continue;
89         } else if(tdiff.tv_usec < 0) {
90             tdiff.tv_sec--;
91             tdiff.tv_usec += 1000000; //1 sec
92         }
93         if(timeval_is_smaler((&tdiff), timeout)) {
94             timeout->tv_sec = tdiff.tv_sec;
95             timeout->tv_usec = tdiff.tv_usec;
96         }
97         break;
98     }
99     
100     msec = timeout ? ((timeout->tv_sec * 1000 + timeout->tv_usec / 1000) + (timeout->tv_usec % 1000 != 0 ? 1 : 0)) : -1;
101     
102     //select system call
103     epoll_result = epoll_wait(epoll_fd, evts, MAX_EVENTS, msec);
104     
105     if (epoll_result < 0) {
106         if (errno != EINTR) {
107             iohandler_log(IOLOG_FATAL, "epoll_wait() failed with errno %d: %s", errno, strerror(errno));
108             return;
109         }
110     } else {
111         int i;
112         for(i = 0; i < epoll_result; i++) {
113             events = evts[i].events;
114             iohandler_events(evts[i].data.ptr, (events & (EPOLLIN | EPOLLHUP)), (events & EPOLLOUT));
115         }
116     }
117     
118     //check timers
119     while(timer_priority) {
120         tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
121         tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
122         if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
123             iohandler_events(timer_priority, 0, 0);
124             iohandler_close(timer_priority); //also sets timer_priority to the next timed element
125             continue;
126         }
127         break;
128     }
129     
130 }
131
132 static void engine_epoll_cleanup() {
133     close(epoll_fd);
134 }
135
136 struct IOEngine engine_epoll = {
137     .name = "epoll",
138     .init = engine_epoll_init,
139     .add = engine_epoll_add,
140     .remove = engine_epoll_remove,
141     .update = engine_epoll_update,
142     .loop = engine_epoll_loop,
143     .cleanup = engine_epoll_cleanup,
144 };
145
146 #else
147
148 struct IOEngine engine_epoll = {
149     .name = "epoll",
150     .init = NULL,
151     .add = NULL,
152     .remove = NULL,
153     .update = NULL,
154     .loop = NULL,
155     .cleanup = NULL,
156 };
157
158 #endif