+void
+ev_feed_fd_event (EV_P_ int fd, int revents)
+{
+ fd_event (EV_A_ fd, revents);
+}
+
+/*****************************************************************************/
+
+static void
+fd_reify (EV_P)
+{
+ int i;
+
+ for (i = 0; i < fdchangecnt; ++i)
+ {
+ int fd = fdchanges [i];
+ ANFD *anfd = anfds + fd;
+ struct ev_io *w;
+
+ int events = 0;
+
+ for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
+ events |= w->events;
+
+#if EV_SELECT_IS_WINSOCKET
+ if (events)
+ {
+ unsigned long argp;
+ anfd->handle = _get_osfhandle (fd);
+ assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
+ }
+#endif
+
+ anfd->reify = 0;
+
+ method_modify (EV_A_ fd, anfd->events, events);
+ anfd->events = events;
+ }
+
+ fdchangecnt = 0;
+}
+
+static void
+fd_change (EV_P_ int fd)
+{
+ if (anfds [fd].reify)
+ return;
+
+ anfds [fd].reify = 1;
+
+ ++fdchangecnt;
+ array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
+ fdchanges [fdchangecnt - 1] = fd;
+}
+
+static void
+fd_kill (EV_P_ int fd)
+{
+ struct ev_io *w;
+
+ while ((w = (struct ev_io *)anfds [fd].head))
+ {
+ ev_io_stop (EV_A_ w);
+ ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
+ }
+}
+
+static int
+fd_valid (int fd)
+{
+#ifdef _WIN32
+ return _get_osfhandle (fd) != -1;
+#else
+ return fcntl (fd, F_GETFD) != -1;
+#endif
+}
+
+/* called on EBADF to verify fds */
+static void
+fd_ebadf (EV_P)
+{
+ int fd;
+
+ for (fd = 0; fd < anfdmax; ++fd)
+ if (anfds [fd].events)
+ if (!fd_valid (fd) == -1 && errno == EBADF)
+ fd_kill (EV_A_ fd);
+}
+
+/* called on ENOMEM in select/poll to kill some fds and retry */
+static void
+fd_enomem (EV_P)
+{
+ int fd;
+
+ for (fd = anfdmax; fd--; )
+ if (anfds [fd].events)
+ {
+ fd_kill (EV_A_ fd);
+ return;
+ }
+}
+
+/* usually called after fork if method needs to re-arm all fds from scratch */
+static void
+fd_rearm_all (EV_P)
+{
+ int fd;
+
+ /* this should be highly optimised to not do anything but set a flag */
+ for (fd = 0; fd < anfdmax; ++fd)
+ if (anfds [fd].events)
+ {
+ anfds [fd].events = 0;
+ fd_change (EV_A_ fd);
+ }
+}
+
+/*****************************************************************************/
+
+static void
+upheap (WT *heap, int k)
+{
+ WT w = heap [k];
+
+ while (k && heap [k >> 1]->at > w->at)
+ {
+ heap [k] = heap [k >> 1];
+ ((W)heap [k])->active = k + 1;
+ k >>= 1;
+ }
+
+ heap [k] = w;
+ ((W)heap [k])->active = k + 1;
+
+}
+
+static void
+downheap (WT *heap, int N, int k)
+{
+ WT w = heap [k];
+
+ while (k < (N >> 1))
+ {
+ int j = k << 1;
+
+ if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
+ ++j;
+
+ if (w->at <= heap [j]->at)
+ break;
+
+ heap [k] = heap [j];
+ ((W)heap [k])->active = k + 1;
+ k = j;
+ }
+
+ heap [k] = w;
+ ((W)heap [k])->active = k + 1;
+}
+
+inline void
+adjustheap (WT *heap, int N, int k)
+{
+ upheap (heap, k);
+ downheap (heap, N, k);
+}
+
+/*****************************************************************************/
+
+typedef struct
+{
+ WL head;
+ sig_atomic_t volatile gotsig;
+} ANSIG;
+
+static ANSIG *signals;
+static int signalmax;
+
+static int sigpipe [2];
+static sig_atomic_t volatile gotsig;
+static struct ev_io sigev;
+
+static void
+signals_init (ANSIG *base, int count)
+{
+ while (count--)
+ {
+ base->head = 0;
+ base->gotsig = 0;
+
+ ++base;
+ }
+}
+
+static void
+sighandler (int signum)
+{
+#if _WIN32
+ signal (signum, sighandler);
+#endif
+
+ signals [signum - 1].gotsig = 1;
+
+ if (!gotsig)
+ {
+ int old_errno = errno;
+ gotsig = 1;
+ write (sigpipe [1], &signum, 1);
+ errno = old_errno;
+ }
+}
+
+void
+ev_feed_signal_event (EV_P_ int signum)
+{
+ WL w;
+
+#if EV_MULTIPLICITY
+ assert (("feeding signal events is only supported in the default loop", loop == default_loop));
+#endif
+
+ --signum;
+
+ if (signum < 0 || signum >= signalmax)
+ return;
+
+ signals [signum].gotsig = 0;
+
+ for (w = signals [signum].head; w; w = w->next)
+ ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
+}
+
+static void
+sigcb (EV_P_ struct ev_io *iow, int revents)
+{
+ int signum;
+
+ read (sigpipe [0], &revents, 1);
+ gotsig = 0;
+
+ for (signum = signalmax; signum--; )
+ if (signals [signum].gotsig)
+ ev_feed_signal_event (EV_A_ signum + 1);
+}
+
+inline void
+fd_intern (int fd)
+{
+#ifdef _WIN32
+ int arg = 1;
+ ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
+#else
+ fcntl (fd, F_SETFD, FD_CLOEXEC);
+ fcntl (fd, F_SETFL, O_NONBLOCK);
+#endif
+}
+
+static void
+siginit (EV_P)
+{
+ fd_intern (sigpipe [0]);
+ fd_intern (sigpipe [1]);
+
+ ev_io_set (&sigev, sigpipe [0], EV_READ);
+ ev_io_start (EV_A_ &sigev);
+ ev_unref (EV_A); /* child watcher should not keep loop alive */
+}
+
+/*****************************************************************************/
+
+static struct ev_child *childs [PID_HASHSIZE];
+
+#ifndef _WIN32
+
+static struct ev_signal childev;
+
+#ifndef WCONTINUED
+# define WCONTINUED 0
+#endif
+
+static void
+child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status)
+{
+ struct ev_child *w;
+
+ for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
+ if (w->pid == pid || !w->pid)
+ {
+ ev_priority (w) = ev_priority (sw); /* need to do it *now* */
+ w->rpid = pid;
+ w->rstatus = status;
+ ev_feed_event (EV_A_ (W)w, EV_CHILD);
+ }
+}
+
+static void
+childcb (EV_P_ struct ev_signal *sw, int revents)
+{
+ int pid, status;
+
+ if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
+ {
+ /* make sure we are called again until all childs have been reaped */
+ ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
+
+ child_reap (EV_A_ sw, pid, pid, status);
+ child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
+ }
+}
+
+#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 */
+static int
+enable_secure (void)
+{
+#ifdef _WIN32
+ return 0;
+#else
+ return getuid () != geteuid ()
+ || getgid () != getegid ();
+#endif
+}
+
+unsigned int
+ev_method (EV_P)
+{
+ return method;
+}
+
+static void
+loop_init (EV_P_ unsigned int flags)
+{
+ if (!method)
+ {
+#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;
+
+ if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
+ flags = atoi (getenv ("LIBEV_FLAGS"));
+
+ if (!(flags & 0x0000ffff))
+ flags |= 0x0000ffff;
+
+ method = 0;
+#if EV_USE_KQUEUE
+ if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
+#endif
+#if EV_USE_EPOLL
+ if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
+#endif
+#if EV_USE_POLL
+ if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
+#endif
+#if EV_USE_SELECT
+ if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
+#endif
+
+ ev_init (&sigev, sigcb);
+ ev_set_priority (&sigev, EV_MAXPRI);
+ }
+}
+
+void
+loop_destroy (EV_P)
+{
+ int i;
+
+#if EV_USE_KQUEUE
+ if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
+#endif
+#if EV_USE_EPOLL
+ if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
+#endif
+#if EV_USE_POLL
+ if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
+#endif
+#if EV_USE_SELECT
+ if (method == EVMETHOD_SELECT) select_destroy (EV_A);
+#endif
+
+ for (i = NUMPRI; i--; )
+ array_free (pending, [i]);
+
+ /* have to use the microsoft-never-gets-it-right macro */
+ array_free (fdchange, EMPTY);
+ array_free (timer, EMPTY);
+#if EV_PERIODICS
+ array_free (periodic, EMPTY);
+#endif
+ array_free (idle, EMPTY);
+ array_free (prepare, EMPTY);
+ array_free (check, EMPTY);
+
+ method = 0;
+}
+
+static void
+loop_fork (EV_P)
+{
+#if EV_USE_EPOLL
+ if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
+#endif
+#if EV_USE_KQUEUE
+ if (method == EVMETHOD_KQUEUE) kqueue_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_method (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 *
+#else
+int
+#endif
+ev_default_loop (unsigned int flags)
+{
+ if (sigpipe [0] == sigpipe [1])
+ if (pipe (sigpipe))
+ return 0;
+
+ if (!default_loop)
+ {
+#if EV_MULTIPLICITY
+ struct ev_loop *loop = default_loop = &default_loop_struct;
+#else
+ default_loop = 1;
+#endif
+
+ loop_init (EV_A_ flags);
+
+ if (ev_method (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
+ default_loop = 0;
+ }
+
+ return default_loop;
+}
+
+void
+ev_default_destroy (void)
+{
+#if EV_MULTIPLICITY
+ struct ev_loop *loop = default_loop;
+#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 = default_loop;
+#endif
+
+ if (method)
+ postfork = 1;
+}
+
+/*****************************************************************************/
+
+static int
+any_pending (EV_P)
+{
+ int pri;
+
+ for (pri = NUMPRI; pri--; )
+ if (pendingcnt [pri])
+ return 1;
+
+ return 0;
+}
+