1 /* ioset epoll_*() backend for srvx
2 * Copyright 2006 srvx Development Team
4 * This file is part of srvx.
6 * srvx is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #include "ioset-impl.h"
25 #ifdef HAVE_SYS_EPOLL_H
26 # include <sys/epoll.h>
29 #ifdef HAVE_SYS_SOCKET_H
30 # include <sys/socket.h>
33 extern int clock_skew;
37 ioset_epoll_init(void)
39 epoll_fd = epoll_create(1024);
46 ioset_epoll_events(struct io_fd *fd)
49 | (fd_wants_reads(fd) ? EPOLLIN : 0)
50 | (fd_wants_writes(fd) ? EPOLLOUT : 0)
55 ioset_epoll_add(struct io_fd *fd)
57 struct epoll_event evt;
60 evt.events = ioset_epoll_events(fd);
62 res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd->fd, &evt);
64 log_module(MAIN_LOG, LOG_ERROR, "Unable to add fd %d to epoll: %s", fd->fd, strerror(errno));
68 ioset_epoll_remove(struct io_fd *fd)
70 static struct epoll_event evt;
71 (void)epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd->fd, &evt);
75 ioset_epoll_update(struct io_fd *fd)
77 struct epoll_event evt;
80 evt.events = ioset_epoll_events(fd);
82 res = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd->fd, &evt);
84 log_module(MAIN_LOG, LOG_ERROR, "Unable to modify fd %d for epoll: %s", fd->fd, strerror(errno));
88 ioset_epoll_cleanup(void)
94 ioset_epoll_loop(struct timeval *timeout)
96 struct epoll_event evts[32];
102 msec = timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : -1;
104 res = epoll_wait(epoll_fd, evts, ArrayLength(evts), msec);
106 if (errno != EINTR) {
107 log_module(MAIN_LOG, LOG_ERROR, "epoll_wait() error %d: %s", errno, strerror(errno));
113 for (ii = 0; ii < res; ++ii) {
114 events = evts[ii].events;
115 ioset_events(evts[ii].data.ptr, (events & (EPOLLIN | EPOLLHUP)), (events & EPOLLOUT));
121 struct io_engine io_engine_epoll = {
123 .init = ioset_epoll_init,
124 .add = ioset_epoll_add,
125 .remove = ioset_epoll_remove,
126 .update = ioset_epoll_update,
127 .loop = ioset_epoll_loop,
128 .cleanup = ioset_epoll_cleanup,