1 /* IOEngine_epoll.c - IOMultiplexer
2 * Copyright (C) 2012 Philipp Kreil (pk910)
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.
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.
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/>.
19 #ifdef HAVE_SYS_EPOLL_H
20 #include <sys/epoll.h>
29 static int engine_epoll_init() {
30 epoll_fd = epoll_create(1024);
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;
42 evt.events = EPOLLHUP | EPOLLIN | (iohandler_wants_writes(iofd) ? EPOLLOUT : 0);
44 res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, iofd->fd, &evt);
46 iohandler_log(IOLOG_ERROR, "could not add IODescriptor %d to epoll queue. (returned: %d)", iofd->fd, res);
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);
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);
62 struct epoll_event evt;
65 evt.events = EPOLLHUP | EPOLLIN | (iohandler_wants_writes(iofd) ? EPOLLOUT : 0);
67 res = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, iofd->fd, &evt);
69 iohandler_log(IOLOG_ERROR, "could not update IODescriptor %d in epoll queue. (returned: %d)", iofd->fd, res);
73 static void engine_epoll_loop(struct timeval *timeout) {
74 struct epoll_event evts[MAX_EVENTS];
75 struct timeval now, tdiff;
80 gettimeofday(&now, NULL);
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
89 } else if(tdiff.tv_usec < 0) {
91 tdiff.tv_usec += 1000000; //1 sec
93 if(timeval_is_smaler((&tdiff), timeout)) {
94 timeout->tv_sec = tdiff.tv_sec;
95 timeout->tv_usec = tdiff.tv_usec;
100 msec = timeout ? ((timeout->tv_sec * 1000 + timeout->tv_usec / 1000) + (timeout->tv_usec % 1000 != 0 ? 1 : 0)) : -1;
103 epoll_result = epoll_wait(epoll_fd, evts, MAX_EVENTS, msec);
105 if (epoll_result < 0) {
106 if (errno != EINTR) {
107 iohandler_log(IOLOG_FATAL, "epoll_wait() failed with errno %d: %s", errno, strerror(errno));
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));
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
132 static void engine_epoll_cleanup() {
136 struct IOEngine engine_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,
148 struct IOEngine engine_epoll = {