1 /* IOEngine_epoll.c - TransparentIRC 0.1
2 * Copyright (C) 2011-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/>.
18 #include "IOHandler.h"
20 #ifdef HAVE_SYS_EPOLL_H
21 #include <sys/epoll.h>
27 static int engine_epoll_init() {
28 epoll_fd = epoll_create(1024);
34 static void engine_epoll_add(struct IODescriptor *iofd) {
35 if(iofd->type == IOTYPE_TIMER) return;
36 //add descriptor to the epoll queue
37 struct epoll_event evt;
40 evt.events = EPOLLHUP | EPOLLIN | (iohandler_wants_writes(iofd) ? EPOLLOUT : 0);
42 res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, iofd->fd, &evt);
44 iohandler_log(IOLOG_ERROR, "could not add IODescriptor %d to epoll queue. (returned: %d)", iofd->fd, res);
48 static void engine_epoll_remove(struct IODescriptor *iofd) {
49 if(iofd->type == IOTYPE_TIMER) return;
50 struct epoll_event evt;
51 epoll_ctl(epoll_fd, EPOLL_CTL_DEL, iofd->fd, &evt);
54 static void engine_epoll_update(struct IODescriptor *iofd) {
55 if(iofd->type == IOTYPE_TIMER) return;
56 struct epoll_event evt;
59 evt.events = EPOLLHUP | EPOLLIN | (iohandler_wants_writes(iofd) ? EPOLLOUT : 0);
61 res = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, iofd->fd, &evt);
63 iohandler_log(IOLOG_ERROR, "could not update IODescriptor %d in epoll queue. (returned: %d)", iofd->fd, res);
67 static void engine_epoll_loop(struct timeval *timeout) {
68 struct epoll_event evts[MAX_EVENTS];
69 struct timeval now, tdiff;
74 gettimeofday(&now, NULL);
76 while(timer_priority) {
77 tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
78 tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
79 if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
80 iohandler_events(timer_priority, 0, 0);
81 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
83 } else if(tdiff.tv_usec < 0) {
85 tdiff.tv_usec += 1000000; //1 sec
87 if(timeval_is_smaler((&tdiff), timeout)) {
88 timeout->tv_sec = tdiff.tv_sec;
89 timeout->tv_usec = tdiff.tv_usec;
94 msec = timeout ? ((timeout->tv_sec * 1000 + timeout->tv_usec / 1000) + (timeout->tv_usec % 1000 != 0 ? 1 : 0)) : -1;
97 epoll_result = epoll_wait(epoll_fd, evts, MAX_EVENTS, msec);
99 if (epoll_result < 0) {
100 if (errno != EINTR) {
101 iohandler_log(IOLOG_FATAL, "epoll_wait() failed with errno %d: %s", errno, strerror(errno));
106 for(i = 0; i < epoll_result; i++) {
107 events = evts[i].events;
108 iohandler_events(evts[i].data.ptr, (events & (EPOLLIN | EPOLLHUP)), (events & EPOLLOUT));
113 while(timer_priority) {
114 tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
115 tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
116 if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
117 iohandler_events(timer_priority, 0, 0);
118 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
126 static void engine_epoll_cleanup() {
130 struct IOEngine engine_epoll = {
132 .init = engine_epoll_init,
133 .add = engine_epoll_add,
134 .remove = engine_epoll_remove,
135 .update = engine_epoll_update,
136 .loop = engine_epoll_loop,
137 .cleanup = engine_epoll_cleanup,
142 struct IOEngine engine_epoll = {