- if ((kq_fd = kqueue ()) < 0)
- return;
-
- /* Check for Mac OS X kqueue bug. */
- ch.ident = -1;
- ch.filter = EVFILT_READ;
- ch.flags = EV_ADD;
-
- /*
- * If kqueue works, then kevent will succeed, and it will
- * stick an error in events[0]. If kqueue is broken, then
- * kevent will fail.
- */
- if (kevent (kq_fd, &ch, 1, &ev, 1, 0) != 1
- || ev.ident != -1
- || ev.flags != EV_ERROR)
- {
- /* detected broken kqueue */
- close (kq_fd);
- return;
- }
+ if ((backend_fd = kqueue ()) < 0)
+ return 0;
+
+ fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
+
+ /* fudge *might* be zero from the documentation, but bsd docs are notoriously wrong */
+ backend_fudge = 1e-3; /* needed to compensate for kevent returning early */
+ backend_modify = kqueue_modify;
+ backend_poll = kqueue_poll;
+
+ kqueue_eventmax = 64; /* initial number of events receivable per poll */
+ kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
+
+ kqueue_changes = 0;
+ kqueue_changemax = 0;
+ kqueue_changecnt = 0;
+
+ return EVBACKEND_KQUEUE;
+}
+
+static void
+kqueue_destroy (EV_P)
+{
+ close (backend_fd);
+
+ ev_free (kqueue_events);
+ ev_free (kqueue_changes);
+}
+
+static void
+kqueue_fork (EV_P)
+{
+ close (backend_fd);
+
+ while ((backend_fd = kqueue ()) < 0)
+ syserr ("(libev) kqueue");