+#if EV_USE_PORT
+# include "ev_port.c"
+#endif
+#if EV_USE_KQUEUE
+# include "ev_kqueue.c"
+#endif
+#if EV_USE_EPOLL
+# include "ev_epoll.c"
+#endif
+#if EV_USE_POLL
+# include "ev_poll.c"
+#endif
+#if EV_USE_SELECT
+# include "ev_select.c"
+#endif
+
+int
+ev_version_major (void)
+{
+ return EV_VERSION_MAJOR;
+}
+
+int
+ev_version_minor (void)
+{
+ return EV_VERSION_MINOR;
+}
+
+/* return true if we are running with elevated privileges and should ignore env variables */
+int inline_size
+enable_secure (void)
+{
+#ifdef _WIN32
+ return 0;
+#else
+ return getuid () != geteuid ()
+ || getgid () != getegid ();
+#endif
+}
+
+unsigned int
+ev_supported_backends (void)
+{
+ unsigned int flags = 0;
+
+ if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
+ if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
+ if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
+ if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
+ if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
+
+ return flags;
+}
+
+unsigned int
+ev_recommended_backends (void)
+{
+ unsigned int flags = ev_supported_backends ();
+
+#ifndef __NetBSD__
+ /* kqueue is borked on everything but netbsd apparently */
+ /* it usually doesn't work correctly on anything but sockets and pipes */
+ flags &= ~EVBACKEND_KQUEUE;
+#endif
+#ifdef __APPLE__
+ // flags &= ~EVBACKEND_KQUEUE; for documentation
+ flags &= ~EVBACKEND_POLL;
+#endif
+
+ return flags;
+}
+
+unsigned int
+ev_embeddable_backends (void)
+{
+ return EVBACKEND_EPOLL
+ | EVBACKEND_KQUEUE
+ | EVBACKEND_PORT;
+}
+
+unsigned int
+ev_backend (EV_P)
+{
+ return backend;
+}
+
+unsigned int
+ev_loop_count (EV_P)
+{
+ return loop_count;
+}
+
+static void noinline
+loop_init (EV_P_ unsigned int flags)
+{
+ if (!backend)
+ {
+#if EV_USE_MONOTONIC
+ {
+ struct timespec ts;
+ if (!clock_gettime (CLOCK_MONOTONIC, &ts))
+ have_monotonic = 1;
+ }
+#endif
+
+ ev_rt_now = ev_time ();
+ mn_now = get_clock ();
+ now_floor = mn_now;
+ rtmn_diff = ev_rt_now - mn_now;
+
+ /* pid check not overridable via env */
+#ifndef _WIN32
+ if (flags & EVFLAG_FORKCHECK)
+ curpid = getpid ();
+#endif
+
+ if (!(flags & EVFLAG_NOENV)
+ && !enable_secure ()
+ && getenv ("LIBEV_FLAGS"))
+ flags = atoi (getenv ("LIBEV_FLAGS"));
+
+ if (!(flags & 0x0000ffffUL))
+ flags |= ev_recommended_backends ();
+
+ backend = 0;
+ backend_fd = -1;
+#if EV_USE_INOTIFY
+ fs_fd = -2;
+#endif
+
+#if EV_USE_PORT
+ if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
+#endif
+#if EV_USE_KQUEUE
+ if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
+#endif
+#if EV_USE_EPOLL
+ if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
+#endif
+#if EV_USE_POLL
+ if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
+#endif
+#if EV_USE_SELECT
+ if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
+#endif
+
+ ev_init (&sigev, sigcb);
+ ev_set_priority (&sigev, EV_MAXPRI);
+ }
+}
+
+static void noinline
+loop_destroy (EV_P)
+{
+ int i;
+
+#if EV_USE_INOTIFY
+ if (fs_fd >= 0)
+ close (fs_fd);
+#endif
+
+ if (backend_fd >= 0)
+ close (backend_fd);
+
+#if EV_USE_PORT
+ if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
+#endif
+#if EV_USE_KQUEUE
+ if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
+#endif
+#if EV_USE_EPOLL
+ if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
+#endif
+#if EV_USE_POLL
+ if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
+#endif
+#if EV_USE_SELECT
+ if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
+#endif
+
+ for (i = NUMPRI; i--; )
+ {
+ array_free (pending, [i]);
+#if EV_IDLE_ENABLE
+ array_free (idle, [i]);
+#endif
+ }
+
+ /* have to use the microsoft-never-gets-it-right macro */
+ array_free (fdchange, EMPTY);
+ array_free (timer, EMPTY);
+#if EV_PERIODIC_ENABLE
+ array_free (periodic, EMPTY);
+#endif
+ array_free (prepare, EMPTY);
+ array_free (check, EMPTY);
+
+ backend = 0;
+}
+
+void inline_size infy_fork (EV_P);
+
+void inline_size
+loop_fork (EV_P)
+{
+#if EV_USE_PORT
+ if (backend == EVBACKEND_PORT ) port_fork (EV_A);
+#endif
+#if EV_USE_KQUEUE
+ if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
+#endif
+#if EV_USE_EPOLL
+ if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
+#endif
+#if EV_USE_INOTIFY
+ infy_fork (EV_A);
+#endif
+
+ if (ev_is_active (&sigev))
+ {
+ /* default loop */
+
+ ev_ref (EV_A);
+ ev_io_stop (EV_A_ &sigev);
+ close (sigpipe [0]);
+ close (sigpipe [1]);
+
+ while (pipe (sigpipe))
+ syserr ("(libev) error creating pipe");
+
+ siginit (EV_A);
+ }
+
+ postfork = 0;
+}
+
+#if EV_MULTIPLICITY
+struct ev_loop *
+ev_loop_new (unsigned int flags)
+{
+ struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
+
+ memset (loop, 0, sizeof (struct ev_loop));
+
+ loop_init (EV_A_ flags);
+
+ if (ev_backend (EV_A))
+ return loop;
+
+ return 0;
+}
+
+void
+ev_loop_destroy (EV_P)
+{
+ loop_destroy (EV_A);
+ ev_free (loop);
+}
+
+void
+ev_loop_fork (EV_P)
+{
+ postfork = 1;
+}
+
+#endif
+
+#if EV_MULTIPLICITY
+struct ev_loop *
+ev_default_loop_init (unsigned int flags)
+#else
+int
+ev_default_loop (unsigned int flags)
+#endif
+{
+ if (sigpipe [0] == sigpipe [1])
+ if (pipe (sigpipe))
+ return 0;
+
+ if (!ev_default_loop_ptr)
+ {
+#if EV_MULTIPLICITY
+ struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
+#else
+ ev_default_loop_ptr = 1;
+#endif
+
+ loop_init (EV_A_ flags);
+
+ if (ev_backend (EV_A))
+ {
+ siginit (EV_A);
+
+#ifndef _WIN32
+ ev_signal_init (&childev, childcb, SIGCHLD);
+ ev_set_priority (&childev, EV_MAXPRI);
+ ev_signal_start (EV_A_ &childev);
+ ev_unref (EV_A); /* child watcher should not keep loop alive */
+#endif
+ }
+ else
+ ev_default_loop_ptr = 0;
+ }
+
+ return ev_default_loop_ptr;
+}
+
+void
+ev_default_destroy (void)
+{
+#if EV_MULTIPLICITY
+ struct ev_loop *loop = ev_default_loop_ptr;
+#endif
+
+#ifndef _WIN32
+ ev_ref (EV_A); /* child watcher */
+ ev_signal_stop (EV_A_ &childev);
+#endif
+
+ ev_ref (EV_A); /* signal watcher */
+ ev_io_stop (EV_A_ &sigev);
+
+ close (sigpipe [0]); sigpipe [0] = 0;
+ close (sigpipe [1]); sigpipe [1] = 0;
+
+ loop_destroy (EV_A);
+}
+
+void
+ev_default_fork (void)
+{
+#if EV_MULTIPLICITY
+ struct ev_loop *loop = ev_default_loop_ptr;
+#endif
+
+ if (backend)
+ postfork = 1;
+}
+
+/*****************************************************************************/
+
+void inline_speed
+call_pending (EV_P)
+{
+ int pri;
+
+ for (pri = NUMPRI; pri--; )
+ while (pendingcnt [pri])
+ {
+ ANPENDING *p = pendings [pri] + --pendingcnt [pri];
+
+ if (expect_true (p->w))
+ {
+ /*assert (("non-pending watcher on pending list", p->w->pending));*/
+
+ p->w->pending = 0;
+ EV_CB_INVOKE (p->w, p->events);
+ }
+ }
+}
+
+void inline_size
+timers_reify (EV_P)
+{
+ while (timercnt && ((WT)timers [0])->at <= mn_now)
+ {
+ ev_timer *w = timers [0];
+
+ /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
+
+ /* first reschedule or stop timer */
+ if (w->repeat)
+ {
+ assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
+
+ ((WT)w)->at += w->repeat;
+ if (((WT)w)->at < mn_now)
+ ((WT)w)->at = mn_now;
+
+ downheap ((WT *)timers, timercnt, 0);
+ }
+ else
+ ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
+
+ ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
+ }
+}
+
+#if EV_PERIODIC_ENABLE
+void inline_size
+periodics_reify (EV_P)
+{
+ while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
+ {
+ ev_periodic *w = periodics [0];
+
+ /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
+
+ /* first reschedule or stop timer */
+ if (w->reschedule_cb)
+ {
+ ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
+ assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
+ downheap ((WT *)periodics, periodiccnt, 0);
+ }
+ else if (w->interval)
+ {
+ ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
+ assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
+ downheap ((WT *)periodics, periodiccnt, 0);
+ }
+ else
+ ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
+
+ ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
+ }
+}
+
+static void noinline
+periodics_reschedule (EV_P)
+{
+ int i;
+
+ /* adjust periodics after time jump */
+ for (i = 0; i < periodiccnt; ++i)
+ {
+ ev_periodic *w = periodics [i];
+
+ if (w->reschedule_cb)
+ ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
+ else if (w->interval)
+ ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
+ }
+
+ /* now rebuild the heap */
+ for (i = periodiccnt >> 1; i--; )
+ downheap ((WT *)periodics, periodiccnt, i);
+}
+#endif
+
+#if EV_IDLE_ENABLE
+void inline_size
+idle_reify (EV_P)
+{
+ if (expect_false (idleall))
+ {
+ int pri;
+
+ for (pri = NUMPRI; pri--; )
+ {
+ if (pendingcnt [pri])
+ break;
+
+ if (idlecnt [pri])
+ {
+ queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
+ break;
+ }
+ }
+ }
+}
+#endif
+
+int inline_size
+time_update_monotonic (EV_P)
+{
+ mn_now = get_clock ();
+
+ if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
+ {
+ ev_rt_now = rtmn_diff + mn_now;
+ return 0;
+ }
+ else
+ {
+ now_floor = mn_now;
+ ev_rt_now = ev_time ();
+ return 1;
+ }
+}
+
+void inline_size
+time_update (EV_P)