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 3 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
)
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
, int closed
)
70 static struct epoll_event evt
;
72 (void)epoll_ctl(epoll_fd
, EPOLL_CTL_DEL
, fd
->fd
, &evt
);
76 ioset_epoll_update(struct io_fd
*fd
)
78 struct epoll_event evt
;
81 evt
.events
= ioset_epoll_events(fd
);
83 res
= epoll_ctl(epoll_fd
, EPOLL_CTL_MOD
, fd
->fd
, &evt
);
85 log_module(MAIN_LOG
, LOG_ERROR
, "Unable to modify fd %d for epoll: %s", fd
->fd
, strerror(errno
));
89 ioset_epoll_cleanup(void)
95 ioset_epoll_loop(struct timeval
*timeout
)
97 struct epoll_event evts
[32];
103 msec
= timeout
? (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000) : -1;
105 res
= epoll_wait(epoll_fd
, evts
, ArrayLength(evts
), msec
);
106 now
= time(NULL
) + clock_skew
;
108 if (errno
!= EINTR
) {
109 log_module(MAIN_LOG
, LOG_ERROR
, "epoll_wait() error %d: %s", errno
, strerror(errno
));
115 for (ii
= 0; ii
< res
; ++ii
) {
116 events
= evts
[ii
].events
;
117 ioset_events(evts
[ii
].data
.ptr
, (events
& (EPOLLIN
| EPOLLHUP
)), (events
& EPOLLOUT
));
123 struct io_engine io_engine_epoll
= {
125 .init
= ioset_epoll_init
,
126 .add
= ioset_epoll_add
,
127 .remove
= ioset_epoll_remove
,
128 .update
= ioset_epoll_update
,
129 .loop
= ioset_epoll_loop
,
130 .cleanup
= ioset_epoll_cleanup
,