+ /* we only need this for !monotonic clock or timers, but as we basically
+ always have timers, we just calculate it always */
+#if EV_USE_MONOTONIC
+ if (expect_true (have_monotonic))
+ time_update_monotonic (EV_A);
+ else
+#endif
+ {
+ ev_rt_now = ev_time ();
+ mn_now = ev_rt_now;
+ }
+
+ if (flags & EVLOOP_NONBLOCK || idlecnt)
+ block = 0.;
+ else
+ {
+ block = MAX_BLOCKTIME;
+
+ if (timercnt)
+ {
+ ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
+ if (block > to) block = to;
+ }
+
+#if EV_PERIODICS
+ if (periodiccnt)
+ {
+ ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
+ if (block > to) block = to;
+ }
+#endif
+
+ if (expect_false (block < 0.)) block = 0.;
+ }
+
+ method_poll (EV_A_ block);
+
+ /* update ev_rt_now, do magic */
+ time_update (EV_A);
+
+ /* queue pending timers and reschedule them */
+ timers_reify (EV_A); /* relative timers called last */
+#if EV_PERIODICS
+ periodics_reify (EV_A); /* absolute timers called first */
+#endif
+
+ /* queue idle watchers unless io or timers are pending */
+ if (idlecnt && !any_pending (EV_A))
+ queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
+
+ /* queue check watchers, to be executed first */
+ if (expect_false (checkcnt))
+ queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
+
+ call_pending (EV_A);
+
+ if (expect_false (loop_done))
+ break;
+ }
+
+ if (loop_done != 2)
+ loop_done = 0;
+}
+
+void
+ev_unloop (EV_P_ int how)
+{
+ loop_done = how;
+}
+
+/*****************************************************************************/
+
+inline void
+wlist_add (WL *head, WL elem)
+{
+ elem->next = *head;
+ *head = elem;
+}
+
+inline void
+wlist_del (WL *head, WL elem)
+{
+ while (*head)
+ {
+ if (*head == elem)
+ {
+ *head = elem->next;
+ return;
+ }
+
+ head = &(*head)->next;
+ }
+}
+
+inline void
+ev_clear_pending (EV_P_ W w)
+{
+ if (w->pending)
+ {
+ pendings [ABSPRI (w)][w->pending - 1].w = 0;
+ w->pending = 0;
+ }
+}
+
+inline void
+ev_start (EV_P_ W w, int active)
+{
+ if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
+ if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
+
+ w->active = active;
+ ev_ref (EV_A);
+}
+
+inline void
+ev_stop (EV_P_ W w)
+{
+ ev_unref (EV_A);
+ w->active = 0;
+}
+
+/*****************************************************************************/
+
+void
+ev_io_start (EV_P_ struct ev_io *w)
+{
+ int fd = w->fd;
+
+ if (expect_false (ev_is_active (w)))
+ return;
+
+ assert (("ev_io_start called with negative fd", fd >= 0));
+
+ ev_start (EV_A_ (W)w, 1);
+ array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
+ wlist_add ((WL *)&anfds[fd].head, (WL)w);
+
+ fd_change (EV_A_ fd);
+}
+
+void
+ev_io_stop (EV_P_ struct ev_io *w)
+{
+ ev_clear_pending (EV_A_ (W)w);
+ if (expect_false (!ev_is_active (w)))
+ return;
+
+ assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
+
+ wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
+ ev_stop (EV_A_ (W)w);
+
+ fd_change (EV_A_ w->fd);
+}
+
+void
+ev_timer_start (EV_P_ struct ev_timer *w)
+{
+ if (expect_false (ev_is_active (w)))
+ return;
+
+ ((WT)w)->at += mn_now;
+
+ assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
+
+ ev_start (EV_A_ (W)w, ++timercnt);
+ array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
+ timers [timercnt - 1] = w;
+ upheap ((WT *)timers, timercnt - 1);
+
+ assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
+}
+
+void
+ev_timer_stop (EV_P_ struct ev_timer *w)
+{
+ ev_clear_pending (EV_A_ (W)w);
+ if (expect_false (!ev_is_active (w)))
+ return;
+
+ assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
+
+ if (expect_true (((W)w)->active < timercnt--))
+ {
+ timers [((W)w)->active - 1] = timers [timercnt];
+ adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
+ }
+
+ ((WT)w)->at -= mn_now;
+
+ ev_stop (EV_A_ (W)w);
+}
+
+void
+ev_timer_again (EV_P_ struct ev_timer *w)
+{
+ if (ev_is_active (w))
+ {
+ if (w->repeat)
+ {
+ ((WT)w)->at = mn_now + w->repeat;
+ adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
+ }
+ else
+ ev_timer_stop (EV_A_ w);
+ }
+ else if (w->repeat)
+ {
+ w->at = w->repeat;
+ ev_timer_start (EV_A_ w);
+ }
+}
+
+#if EV_PERIODICS
+void
+ev_periodic_start (EV_P_ struct ev_periodic *w)
+{
+ if (expect_false (ev_is_active (w)))
+ return;
+
+ if (w->reschedule_cb)
+ ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
+ else if (w->interval)
+ {
+ assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
+ /* this formula differs from the one in periodic_reify because we do not always round up */
+ ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
+ }
+
+ ev_start (EV_A_ (W)w, ++periodiccnt);
+ array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
+ periodics [periodiccnt - 1] = w;
+ upheap ((WT *)periodics, periodiccnt - 1);
+
+ assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
+}
+
+void
+ev_periodic_stop (EV_P_ struct ev_periodic *w)
+{
+ ev_clear_pending (EV_A_ (W)w);
+ if (expect_false (!ev_is_active (w)))
+ return;
+
+ assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
+
+ if (expect_true (((W)w)->active < periodiccnt--))
+ {
+ periodics [((W)w)->active - 1] = periodics [periodiccnt];
+ adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
+ }
+
+ ev_stop (EV_A_ (W)w);
+}
+
+void
+ev_periodic_again (EV_P_ struct ev_periodic *w)
+{
+ /* TODO: use adjustheap and recalculation */
+ ev_periodic_stop (EV_A_ w);
+ ev_periodic_start (EV_A_ w);
+}
+#endif
+
+void
+ev_idle_start (EV_P_ struct ev_idle *w)
+{
+ if (expect_false (ev_is_active (w)))
+ return;
+
+ ev_start (EV_A_ (W)w, ++idlecnt);
+ array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
+ idles [idlecnt - 1] = w;
+}
+
+void
+ev_idle_stop (EV_P_ struct ev_idle *w)
+{
+ ev_clear_pending (EV_A_ (W)w);
+ if (expect_false (!ev_is_active (w)))
+ return;
+
+ idles [((W)w)->active - 1] = idles [--idlecnt];
+ ev_stop (EV_A_ (W)w);
+}
+
+void
+ev_prepare_start (EV_P_ struct ev_prepare *w)
+{
+ if (expect_false (ev_is_active (w)))
+ return;
+
+ ev_start (EV_A_ (W)w, ++preparecnt);
+ array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
+ prepares [preparecnt - 1] = w;
+}
+
+void
+ev_prepare_stop (EV_P_ struct ev_prepare *w)
+{
+ ev_clear_pending (EV_A_ (W)w);
+ if (expect_false (!ev_is_active (w)))
+ return;
+
+ prepares [((W)w)->active - 1] = prepares [--preparecnt];
+ ev_stop (EV_A_ (W)w);
+}
+
+void
+ev_check_start (EV_P_ struct ev_check *w)
+{
+ if (expect_false (ev_is_active (w)))
+ return;
+
+ ev_start (EV_A_ (W)w, ++checkcnt);
+ array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
+ checks [checkcnt - 1] = w;
+}
+
+void
+ev_check_stop (EV_P_ struct ev_check *w)
+{
+ ev_clear_pending (EV_A_ (W)w);
+ if (expect_false (!ev_is_active (w)))
+ return;
+
+ checks [((W)w)->active - 1] = checks [--checkcnt];
+ ev_stop (EV_A_ (W)w);
+}
+
+#ifndef SA_RESTART
+# define SA_RESTART 0
+#endif
+
+void
+ev_signal_start (EV_P_ struct ev_signal *w)
+{
+#if EV_MULTIPLICITY
+ assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
+#endif
+ if (expect_false (ev_is_active (w)))
+ return;
+
+ assert (("ev_signal_start called with illegal signal number", w->signum > 0));
+
+ ev_start (EV_A_ (W)w, 1);
+ array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
+ wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
+
+ if (!((WL)w)->next)
+ {
+#if _WIN32
+ signal (w->signum, sighandler);
+#else
+ struct sigaction sa;
+ sa.sa_handler = sighandler;
+ sigfillset (&sa.sa_mask);
+ sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
+ sigaction (w->signum, &sa, 0);
+#endif
+ }
+}
+
+void
+ev_signal_stop (EV_P_ struct ev_signal *w)
+{
+ ev_clear_pending (EV_A_ (W)w);
+ if (expect_false (!ev_is_active (w)))
+ return;
+
+ wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
+ ev_stop (EV_A_ (W)w);
+
+ if (!signals [w->signum - 1].head)
+ signal (w->signum, SIG_DFL);
+}
+
+void
+ev_child_start (EV_P_ struct ev_child *w)
+{
+#if EV_MULTIPLICITY
+ assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
+#endif
+ if (expect_false (ev_is_active (w)))
+ return;
+
+ ev_start (EV_A_ (W)w, 1);
+ wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
+}
+
+void
+ev_child_stop (EV_P_ struct ev_child *w)
+{
+ ev_clear_pending (EV_A_ (W)w);
+ if (expect_false (!ev_is_active (w)))
+ return;
+
+ wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
+ ev_stop (EV_A_ (W)w);
+}
+
+/*****************************************************************************/
+
+struct ev_once
+{
+ struct ev_io io;
+ struct ev_timer to;
+ void (*cb)(int revents, void *arg);
+ void *arg;
+};
+
+static void
+once_cb (EV_P_ struct ev_once *once, int revents)
+{
+ void (*cb)(int revents, void *arg) = once->cb;
+ void *arg = once->arg;
+
+ ev_io_stop (EV_A_ &once->io);
+ ev_timer_stop (EV_A_ &once->to);
+ ev_free (once);
+
+ cb (revents, arg);
+}
+
+static void
+once_cb_io (EV_P_ struct ev_io *w, int revents)
+{
+ once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
+}
+
+static void
+once_cb_to (EV_P_ struct ev_timer *w, int revents)
+{
+ once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
+}
+
+void
+ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
+{
+ struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
+
+ if (expect_false (!once))
+ {
+ cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
+ return;
+ }
+
+ once->cb = cb;
+ once->arg = arg;
+
+ ev_init (&once->io, once_cb_io);
+ if (fd >= 0)
+ {
+ ev_io_set (&once->io, fd, events);
+ ev_io_start (EV_A_ &once->io);
+ }
+
+ ev_init (&once->to, once_cb_to);
+ if (timeout >= 0.)
+ {
+ ev_timer_set (&once->to, timeout, 0.);
+ ev_timer_start (EV_A_ &once->to);
+ }
+}
+
+#ifdef __cplusplus
+}
+#endif