[IOMultiplexer] added auto-reloading timers
[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             if(timer_priority->constant_timeout) {
87                 tdiff.tv_sec = 0;
88                 iohandler_set_timeout(timer_priority, &tdiff);
89                 iohandler_events(timer_priority, 0, 0);
90             } else {
91                 iohandler_events(timer_priority, 0, 0);
92                 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
93             }
94             continue;
95         } else if(tdiff.tv_usec < 0) {
96             tdiff.tv_sec--;
97             tdiff.tv_usec += 1000000; //1 sec
98         }
99         if(timeval_is_smaler((&tdiff), timeout)) {
100             timeout->tv_sec = tdiff.tv_sec;
101             timeout->tv_usec = tdiff.tv_usec;
102         }
103         break;
104     }
105     
106     msec = timeout ? ((timeout->tv_sec * 1000 + timeout->tv_usec / 1000) + (timeout->tv_usec % 1000 != 0 ? 1 : 0)) : -1;
107     
108     //select system call
109     epoll_result = epoll_wait(epoll_fd, evts, MAX_EVENTS, msec);
110     
111     if (epoll_result < 0) {
112         if (errno != EINTR) {
113             iohandler_log(IOLOG_FATAL, "epoll_wait() failed with errno %d: %s", errno, strerror(errno));
114             return;
115         }
116     } else {
117         int i;
118         for(i = 0; i < epoll_result; i++) {
119             events = evts[i].events;
120             iohandler_events(evts[i].data.ptr, (events & (EPOLLIN | EPOLLHUP)), (events & EPOLLOUT));
121         }
122     }
123     
124     //check timers
125     while(timer_priority) {
126         tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
127         tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
128         if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
129             if(timer_priority->constant_timeout) {
130                 tdiff.tv_sec = 0;
131                 iohandler_set_timeout(timer_priority, &tdiff);
132                 iohandler_events(timer_priority, 0, 0);
133             } else {
134                 iohandler_events(timer_priority, 0, 0);
135                 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
136             }
137             continue;
138         }
139         break;
140     }
141     
142 }
143
144 static void engine_epoll_cleanup() {
145     close(epoll_fd);
146 }
147
148 struct IOEngine engine_epoll = {
149     .name = "epoll",
150     .init = engine_epoll_init,
151     .add = engine_epoll_add,
152     .remove = engine_epoll_remove,
153     .update = engine_epoll_update,
154     .loop = engine_epoll_loop,
155     .cleanup = engine_epoll_cleanup,
156 };
157
158 #else
159
160 struct IOEngine engine_epoll = {
161     .name = "epoll",
162     .init = NULL,
163     .add = NULL,
164     .remove = NULL,
165     .update = NULL,
166     .loop = NULL,
167     .cleanup = NULL,
168 };
169
170 #endif