+ 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
+ }
+
+ ev_free (anfds); anfdmax = 0;
+
+ /* 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
+#if EV_FORK_ENABLE
+ array_free (fork, 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
+ev_invoke (EV_P_ void *w, int revents)
+{
+ EV_CB_INVOKE ((W)w, revents);
+}
+
+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 = (ev_timer *)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 (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 = (ev_periodic *)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 + TIME_EPSILON);
+ assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
+ downheap (periodics, periodiccnt, 0);
+ }
+ else if (w->interval)
+ {
+ ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
+ if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval;
+ assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
+ downheap (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 = (ev_periodic *)periodics [i];
+
+ if (w->reschedule_cb)
+ ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
+ else if (w->interval)
+ ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
+ }
+
+ /* now rebuild the heap */
+ for (i = periodiccnt >> 1; i--; )
+ downheap (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
+
+void inline_speed
+time_update (EV_P_ ev_tstamp max_block)
+{
+ int i;
+
+#if EV_USE_MONOTONIC
+ if (expect_true (have_monotonic))
+ {
+ ev_tstamp odiff = rtmn_diff;
+
+ mn_now = get_clock ();
+
+ /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
+ /* interpolate in the meantime */
+ if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
+ {
+ ev_rt_now = rtmn_diff + mn_now;
+ return;
+ }
+
+ now_floor = mn_now;
+ ev_rt_now = ev_time ();
+
+ /* loop a few times, before making important decisions.
+ * on the choice of "4": one iteration isn't enough,
+ * in case we get preempted during the calls to
+ * ev_time and get_clock. a second call is almost guaranteed
+ * to succeed in that case, though. and looping a few more times
+ * doesn't hurt either as we only do this on time-jumps or
+ * in the unlikely event of having been preempted here.
+ */
+ for (i = 4; --i; )
+ {
+ rtmn_diff = ev_rt_now - mn_now;
+
+ if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
+ return; /* all is well */
+
+ ev_rt_now = ev_time ();
+ mn_now = get_clock ();
+ now_floor = mn_now;
+ }
+
+# if EV_PERIODIC_ENABLE
+ periodics_reschedule (EV_A);
+# endif
+ /* no timer adjustment, as the monotonic clock doesn't jump */
+ /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
+ }
+ else
+#endif
+ {
+ ev_rt_now = ev_time ();
+
+ if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP))
+ {
+#if EV_PERIODIC_ENABLE
+ periodics_reschedule (EV_A);
+#endif
+ /* adjust timers. this is easy, as the offset is the same for all of them */
+ for (i = 0; i < timercnt; ++i)
+ ((WT)timers [i])->at += ev_rt_now - mn_now;
+ }
+
+ mn_now = ev_rt_now;
+ }
+}