1 /* ioset kqueue()/kevent() backend for srvx
2 * Copyright 2008 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_EVENT_H
26 # include <sys/event.h>
31 extern int clock_skew;
35 ioset_kevent_init(void)
42 ioset_kevent_add(struct io_fd *fd)
44 struct kevent changes[2];
48 EV_SET(&changes[nchanges++], fd->fd, EVFILT_READ, EV_ADD, 0, 0, fd);
49 if (fd_wants_writes(fd)) {
50 EV_SET(&changes[nchanges++], fd->fd, EVFILT_WRITE, EV_ADD, 0, 0, fd);
52 res = kevent(kq_fd, changes, nchanges, NULL, 0, NULL);
54 log_module(MAIN_LOG, LOG_ERROR, "kevent() add failed: %s", strerror(errno));
59 ioset_kevent_remove(struct io_fd *fd, int closed)
62 struct kevent changes[2];
66 EV_SET(&changes[nchanges++], fd->fd, EVFILT_READ, EV_DELETE, 0, 0, fd);
67 EV_SET(&changes[nchanges++], fd->fd, EVFILT_WRITE, EV_DELETE, 0, 0, fd);
68 res = kevent(kq_fd, changes, nchanges, NULL, 0, NULL);
70 log_module(MAIN_LOG, LOG_ERROR, "kevent() remove failed: %s", strerror(errno));
76 ioset_kevent_update(struct io_fd *fd)
78 struct kevent changes[2];
82 EV_SET(&changes[nchanges++], fd->fd, EVFILT_READ, EV_ADD, 0, 0, fd);
83 EV_SET(&changes[nchanges++], fd->fd, EVFILT_WRITE, fd_wants_writes(fd) ? EV_ADD : EV_DELETE, 0, 0, fd);
84 res = kevent(kq_fd, changes, nchanges, NULL, 0, NULL);
86 log_module(MAIN_LOG, LOG_ERROR, "kevent() add failed: %s", strerror(errno));
91 ioset_kevent_cleanup(void)
97 ioset_kevent_loop(struct timeval *timeout)
99 struct kevent events[MAX_EVENTS];
101 struct timespec *pts;
107 /* Try to get events from the kernel. */
109 ts.tv_sec = timeout->tv_sec;
110 ts.tv_nsec = timeout->tv_usec * 1000;
115 res = kevent(kq_fd, NULL, 0, events, MAX_EVENTS, pts);
116 if ((res < 0) && (errno != EINTR)) {
117 log_module(MAIN_LOG, LOG_ERROR, "kevent() poll failed: %s", strerror(errno));
120 now = time(NULL) + clock_skew;
122 /* Process the events we got. */
123 for (ii = 0; ii < res; ++ii) {
124 is_write = events[ii].filter == EVFILT_WRITE;
125 is_read = events[ii].filter == EVFILT_READ;
126 ioset_events(events[ii].udata, is_read, is_write);
132 struct io_engine io_engine_kevent = {
134 .init = ioset_kevent_init,
135 .add = ioset_kevent_add,
136 .remove = ioset_kevent_remove,
137 .update = ioset_kevent_update,
138 .loop = ioset_kevent_loop,
139 .cleanup = ioset_kevent_cleanup,