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 IOLowlevelDescriptor *iold) {
37 if(iold->fd != -1) return;
38 //add descriptor to the epoll queue
39 struct epoll_event evt;
42 evt.events = EPOLLHUP | ((iold->flags & IOFLAGS_WANT_READ) ? EPOLLIN : 0) | ((iold->flags & IOFLAGS_WANT_WRITE) ? EPOLLOUT : 0);
44 res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, iold->fd, &evt);
46 iohandler_log(IOLOG_ERROR, "could not add IOLowlevelDescriptor %d to epoll queue. (returned: %d)", iold->fd, res);
50 static void engine_epoll_remove(struct IOLowlevelDescriptor *iold) {
51 if(iold->fd != -1) return;
52 struct epoll_event evt;
53 epoll_ctl(epoll_fd, EPOLL_CTL_DEL, iold->fd, &evt);
56 static void engine_epoll_update(struct IOLowlevelDescriptor *iold) {
57 if(iold->fd != -1) return;
58 struct IODescriptor *iofd;
59 if((iofd = IOLOWLEVEL_GET_IOFD(iold))) {
60 if(iofd->state == IO_CLOSED) {
61 engine_epoll_remove(iold);
66 struct epoll_event evt;
69 evt.events = EPOLLHUP | ((iold->flags & IOFLAGS_WANT_READ) ? EPOLLIN : 0) | ((iold->flags & IOFLAGS_WANT_WRITE) ? EPOLLOUT : 0);
71 res = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, iold->fd, &evt);
73 iohandler_log(IOLOG_ERROR, "could not update IOLowlevelDescriptor %d in epoll queue. (returned: %d)", iold->fd, res);
77 static void engine_epoll_loop(struct timeval *timeout) {
78 struct epoll_event evts[MAX_EVENTS];
79 struct timeval now, tdiff;
83 struct IOLowlevelDescriptor *iold;
85 gettimeofday(&now, NULL);
87 while(timer_priority) {
88 tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
89 tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
90 if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
91 if(timer_priority->constant_timeout) {
93 iohandler_set_timeout(timer_priority, &tdiff);
94 iohandler_events(timer_priority, 0, 0);
96 iohandler_events(timer_priority, 0, 0);
97 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
100 } else if(tdiff.tv_usec < 0) {
102 tdiff.tv_usec += 1000000; //1 sec
104 if(timeval_is_smaler((&tdiff), timeout)) {
105 timeout->tv_sec = tdiff.tv_sec;
106 timeout->tv_usec = tdiff.tv_usec;
111 msec = timeout ? ((timeout->tv_sec * 1000 + timeout->tv_usec / 1000) + (timeout->tv_usec % 1000 != 0 ? 1 : 0)) : -1;
114 epoll_result = epoll_wait(epoll_fd, evts, MAX_EVENTS, msec);
116 if (epoll_result < 0) {
117 if (errno != EINTR) {
118 iohandler_log(IOLOG_FATAL, "epoll_wait() failed with errno %d: %s", errno, strerror(errno));
123 for(i = 0; i < epoll_result; i++) {
124 events = evts[i].events;
125 iold = evts[i].data.ptr;
126 if(iold->flags & IOFLAGS_HAVE_IOFD)
127 iohandler_events(iold->data.iofd, (events & (EPOLLIN | EPOLLHUP)), (events & EPOLLOUT));
129 iold->data.callback(iold, (events & (EPOLLIN | EPOLLHUP)), (events & EPOLLOUT));
134 while(timer_priority) {
135 tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
136 tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
137 if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
138 if(timer_priority->constant_timeout) {
140 iohandler_set_timeout(timer_priority, &tdiff);
141 iohandler_events(timer_priority, 0, 0);
143 iohandler_events(timer_priority, 0, 0);
144 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
153 static void engine_epoll_cleanup() {
157 struct IOEngine engine_epoll = {
159 .init = engine_epoll_init,
160 .add = engine_epoll_add,
161 .remove = engine_epoll_remove,
162 .update = engine_epoll_update,
163 .loop = engine_epoll_loop,
164 .cleanup = engine_epoll_cleanup,
169 struct IOEngine engine_epoll = {