4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
34 #include <sys/queue.h>
35 #include <sys/event.h>
40 kqueue_change (EV_P_ int fd, int filter, int flags, int fflags)
43 array_needsize (struct kevent, kqueue_changes, kqueue_changemax, kqueue_changecnt, EMPTY2);
45 EV_SET (&kqueue_changes [kqueue_changecnt - 1], fd, filter, flags, fflags, 0, 0);
53 kqueue_modify (EV_P_ int fd, int oev, int nev)
58 kqueue_change (EV_A_ fd, EVFILT_READ , EV_DELETE, 0);
61 kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_DELETE, 0);
64 /* to detect close/reopen reliably, we have to re-add */
65 /* event requests even when oev == nev */
68 kqueue_change (EV_A_ fd, EVFILT_READ , EV_ADD, NOTE_EOF);
71 kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_ADD, NOTE_EOF);
75 kqueue_poll (EV_P_ ev_tstamp timeout)
80 /* need to resize so there is enough space for errors */
81 if (kqueue_changecnt > kqueue_eventmax)
83 ev_free (kqueue_events);
84 kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_changecnt);
85 kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
88 ts.tv_sec = (time_t)timeout;
89 ts.tv_nsec = (long)((timeout - (ev_tstamp)ts.tv_sec) * 1e9);
90 res = kevent (backend_fd, kqueue_changes, kqueue_changecnt, kqueue_events, kqueue_eventmax, &ts);
93 if (expect_false (res < 0))
96 syserr ("(libev) kevent");
101 for (i = 0; i < res; ++i)
103 int fd = kqueue_events [i].ident;
105 if (expect_false (kqueue_events [i].flags & EV_ERROR))
107 int err = kqueue_events [i].data;
109 /* we are only interested in errors for fds that we are interested in :) */
110 if (anfds [fd].events)
112 if (err == ENOENT) /* resubmit changes on ENOENT */
113 kqueue_modify (EV_A_ fd, 0, anfds [fd].events);
114 else if (err == EBADF) /* on EBADF, we re-check the fd */
117 kqueue_modify (EV_A_ fd, 0, anfds [fd].events);
121 else /* on all other errors, we error out on the fd */
129 kqueue_events [i].filter == EVFILT_READ ? EV_READ
130 : kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE
135 if (expect_false (res == kqueue_eventmax))
137 ev_free (kqueue_events);
138 kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_eventmax + 1);
139 kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
144 kqueue_init (EV_P_ int flags)
146 /* Initalize the kernel queue */
147 if ((backend_fd = kqueue ()) < 0)
150 fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
153 backend_modify = kqueue_modify;
154 backend_poll = kqueue_poll;
156 kqueue_eventmax = 64; /* initial number of events receivable per poll */
157 kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
160 kqueue_changemax = 0;
161 kqueue_changecnt = 0;
163 return EVBACKEND_KQUEUE;
167 kqueue_destroy (EV_P)
169 ev_free (kqueue_events);
170 ev_free (kqueue_changes);
178 while ((backend_fd = kqueue ()) < 0)
179 syserr ("(libev) kqueue");
181 fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
183 /* re-register interest in fds */