2 * Copyright 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
32 #include <sys/queue.h>
33 #include <sys/event.h>
38 static struct kevent *kq_changes;
39 static int kq_changemax, kq_changecnt;
40 static struct kevent *kq_events;
41 static int kq_eventmax;
44 kqueue_change (int fd, int filter, int flags, int fflags)
48 array_needsize (kq_changes, kq_changemax, ++kq_changecnt, );
50 ke = &kq_changes [kq_changecnt - 1];
51 memset (ke, 0, sizeof (struct kevent));
63 kqueue_modify (int fd, int oev, int nev)
65 if ((oev ^ nev) & EV_READ)
68 kqueue_change (fd, EVFILT_READ, EV_ADD, NOTE_EOF);
70 kqueue_change (fd, EVFILT_READ, EV_DELETE, 0);
73 if ((oev ^ nev) & EV_WRITE)
76 kqueue_change (fd, EVFILT_WRITE, EV_ADD, NOTE_EOF);
78 kqueue_change (fd, EVFILT_WRITE, EV_DELETE, 0);
83 kqueue_poll (ev_tstamp timeout)
88 ts.tv_sec = (time_t)timeout;
89 ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9;
90 res = kevent (kq_fd, kq_changes, kq_changecnt, kq_events, kq_eventmax, &ts);
96 for (i = 0; i < res; ++i)
98 if (kq_events [i].flags & EV_ERROR)
101 * Error messages that can happen, when a delete fails.
102 * EBADF happens when the file discriptor has been
104 * ENOENT when the file discriptor was closed and
106 * EINVAL for some reasons not understood; EINVAL
107 * should not be returned ever; but FreeBSD does :-\
108 * An error is also indicated when a callback deletes
109 * an event we are still processing. In that case
110 * the data field is set to ENOENT.
112 if (kq_events [i].data == EBADF)
113 fd_kill (kq_events [i].ident);
118 kq_events [i].filter == EVFILT_READ ? EV_READ
119 : kq_events [i].filter == EVFILT_WRITE ? EV_WRITE
124 if (expect_false (res == kq_eventmax))
127 kq_eventmax = array_roundsize (kq_events, kq_eventmax << 1);
128 kq_events = malloc (sizeof (struct kevent) * kq_eventmax);
133 kqueue_init (int flags)
135 struct kevent ch, ev;
137 /* Initalize the kernel queue */
138 if ((kq_fd = kqueue ()) < 0)
141 /* Check for Mac OS X kqueue bug. */
143 ch.filter = EVFILT_READ;
147 * If kqueue works, then kevent will succeed, and it will
148 * stick an error in ev. If kqueue is broken, then
151 if (kevent (kq_fd, &ch, 1, &ev, 1, 0) != 1
153 || ev.flags != EV_ERROR)
155 /* detected broken kqueue */
160 ev_method = EVMETHOD_KQUEUE;
161 method_fudge = 1e-3; /* needed to compensate for kevent returning early */
162 method_modify = kq_modify;
163 method_poll = kq_poll;
165 kq_eventmax = 64; /* intiial number of events receivable per poll */
166 kq_events = malloc (sizeof (struct kevent) * kq_eventmax);