|
|
|
/*
|
|
|
|
* libev event processing core, watcher management
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef EV_STANDALONE
|
|
|
|
# ifdef EV_CONFIG_H
|
|
|
|
# include EV_CONFIG_H
|
|
|
|
# else
|
|
|
|
# include "config.h"
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if HAVE_CLOCK_GETTIME
|
|
|
|
# ifndef EV_USE_MONOTONIC
|
|
|
|
# define EV_USE_MONOTONIC 1
|
|
|
|
# endif
|
|
|
|
# ifndef EV_USE_REALTIME
|
|
|
|
# define EV_USE_REALTIME 1
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
# ifndef EV_USE_MONOTONIC
|
|
|
|
# define EV_USE_MONOTONIC 0
|
|
|
|
# endif
|
|
|
|
# ifndef EV_USE_REALTIME
|
|
|
|
# define EV_USE_REALTIME 0
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifndef EV_USE_SELECT
|
|
|
|
# if HAVE_SELECT && HAVE_SYS_SELECT_H
|
|
|
|
# define EV_USE_SELECT 1
|
|
|
|
# else
|
|
|
|
# define EV_USE_SELECT 0
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifndef EV_USE_POLL
|
|
|
|
# if HAVE_POLL && HAVE_POLL_H
|
|
|
|
# define EV_USE_POLL 1
|
|
|
|
# else
|
|
|
|
# define EV_USE_POLL 0
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifndef EV_USE_EPOLL
|
|
|
|
# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
|
|
|
|
# define EV_USE_EPOLL 1
|
|
|
|
# else
|
|
|
|
# define EV_USE_EPOLL 0
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifndef EV_USE_KQUEUE
|
|
|
|
# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
|
|
|
|
# define EV_USE_KQUEUE 1
|
|
|
|
# else
|
|
|
|
# define EV_USE_KQUEUE 0
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifndef EV_USE_PORT
|
|
|
|
# if HAVE_PORT_H && HAVE_PORT_CREATE
|
|
|
|
# define EV_USE_PORT 1
|
|
|
|
# else
|
|
|
|
# define EV_USE_PORT 0
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
# include <sys/time.h>
|
|
|
|
# include <sys/wait.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
#else
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# include <windows.h>
|
|
|
|
# ifndef EV_SELECT_IS_WINSOCKET
|
|
|
|
# define EV_SELECT_IS_WINSOCKET 1
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**/
|
|
|
|
|
|
|
|
#ifndef EV_USE_MONOTONIC
|
|
|
|
# define EV_USE_MONOTONIC 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef EV_USE_REALTIME
|
|
|
|
# define EV_USE_REALTIME 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef EV_USE_SELECT
|
|
|
|
# define EV_USE_SELECT 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef EV_USE_POLL
|
|
|
|
# ifdef _WIN32
|
|
|
|
# define EV_USE_POLL 0
|
|
|
|
# else
|
|
|
|
# define EV_USE_POLL 1
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef EV_USE_EPOLL
|
|
|
|
# define EV_USE_EPOLL 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef EV_USE_KQUEUE
|
|
|
|
# define EV_USE_KQUEUE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef EV_USE_PORT
|
|
|
|
# define EV_USE_PORT 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**/
|
|
|
|
|
|
|
|
#ifndef CLOCK_MONOTONIC
|
|
|
|
# undef EV_USE_MONOTONIC
|
|
|
|
# define EV_USE_MONOTONIC 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CLOCK_REALTIME
|
|
|
|
# undef EV_USE_REALTIME
|
|
|
|
# define EV_USE_REALTIME 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if EV_SELECT_IS_WINSOCKET
|
|
|
|
# include <winsock.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**/
|
|
|
|
|
|
|
|
#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
|
|
|
|
#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
|
|
|
|
#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
|
|
|
|
/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
|
|
|
|
|
|
|
|
#ifdef EV_H
|
|
|
|
# include EV_H
|
|
|
|
#else
|
|
|
|
# include "ev.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __GNUC__ >= 3
|
|
|
|
# define expect(expr,value) __builtin_expect ((expr),(value))
|
|
|
|
# define inline_size static inline /* inline for codesize */
|
|
|
|
# if EV_MINIMAL
|
|
|
|
# define noinline __attribute__ ((noinline))
|
|
|
|
# define inline_speed static noinline
|
|
|
|
# else
|
|
|
|
# define noinline
|
|
|
|
# define inline_speed static inline
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define expect(expr,value) (expr)
|
|
|
|
# define inline_speed static
|
|
|
|
# define inline_minimal static
|
|
|
|
# define noinline
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define expect_false(expr) expect ((expr) != 0, 0)
|
|
|
|
#define expect_true(expr) expect ((expr) != 0, 1)
|
|
|
|
|
|
|
|
#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
|
|
|
|
#define ABSPRI(w) ((w)->priority - EV_MINPRI)
|
|
|
|
|
|
|
|
#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
|
|
|
|
#define EMPTY2(a,b) /* used to suppress some warnings */
|
|
|
|
|
|
|
|
typedef ev_watcher *W;
|
|
|
|
typedef ev_watcher_list *WL;
|
|
|
|
typedef ev_watcher_time *WT;
|
|
|
|
|
|
|
|
static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
# include "ev_win32.c"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
static void (*syserr_cb)(const char *msg);
|
|
|
|
|
|
|
|
void ev_set_syserr_cb (void (*cb)(const char *msg))
|
|
|
|
{
|
|
|
|
syserr_cb = cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
syserr (const char *msg)
|
|
|
|
{
|
|
|
|
if (!msg)
|
|
|
|
msg = "(libev) system error";
|
|
|
|
|
|
|
|
if (syserr_cb)
|
|
|
|
syserr_cb (msg);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
perror (msg);
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *(*alloc)(void *ptr, long size);
|
|
|
|
|
|
|
|
void ev_set_allocator (void *(*cb)(void *ptr, long size))
|
|
|
|
{
|
|
|
|
alloc = cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
ev_realloc (void *ptr, long size)
|
|
|
|
{
|
|
|
|
ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
|
|
|
|
|
|
|
|
if (!ptr && size)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ev_malloc(size) ev_realloc (0, (size))
|
|
|
|
#define ev_free(ptr) ev_realloc ((ptr), 0)
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
WL head;
|
|
|
|
unsigned char events;
|
|
|
|
unsigned char reify;
|
|
|
|
#if EV_SELECT_IS_WINSOCKET
|
|
|
|
SOCKET handle;
|
|
|
|
#endif
|
|
|
|
} ANFD;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
W w;
|
|
|
|
int events;
|
|
|
|
} ANPENDING;
|
|
|
|
|
|
|
|
#if EV_MULTIPLICITY
|
|
|
|
|
|
|
|
struct ev_loop
|
|
|
|
{
|
|
|
|
ev_tstamp ev_rt_now;
|
|
|
|
#define ev_rt_now ((loop)->ev_rt_now)
|
|
|
|
#define VAR(name,decl) decl;
|
|
|
|
#include "ev_vars.h"
|
|
|
|
#undef VAR
|
|
|
|
};
|
|
|
|
#include "ev_wrap.h"
|
|
|
|
|
|
|
|
static struct ev_loop default_loop_struct;
|
|
|
|
struct ev_loop *ev_default_loop_ptr;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
ev_tstamp ev_rt_now;
|
|
|
|
#define VAR(name,decl) static decl;
|
|
|
|
#include "ev_vars.h"
|
|
|
|
#undef VAR
|
|
|
|
|
|
|
|
static int ev_default_loop_ptr;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
ev_tstamp noinline
|
|
|
|
ev_time (void)
|
|
|
|
{
|
|
|
|
#if EV_USE_REALTIME
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime (CLOCK_REALTIME, &ts);
|
|
|
|
return ts.tv_sec + ts.tv_nsec * 1e-9;
|
|
|
|
#else
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday (&tv, 0);
|
|
|
|
return tv.tv_sec + tv.tv_usec * 1e-6;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ev_tstamp inline_size
|
|
|
|
get_clock (void)
|
|
|
|
{
|
|
|
|
#if EV_USE_MONOTONIC
|
|
|
|
if (expect_true (have_monotonic))
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime (CLOCK_MONOTONIC, &ts);
|
|
|
|
return ts.tv_sec + ts.tv_nsec * 1e-9;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ev_time ();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if EV_MULTIPLICITY
|
|
|
|
ev_tstamp
|
|
|
|
ev_now (EV_P)
|
|
|
|
{
|
|
|
|
return ev_rt_now;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define array_roundsize(type,n) (((n) | 4) & ~3)
|
|
|
|
|
|
|
|
#define array_needsize(type,base,cur,cnt,init) \
|
|
|
|
if (expect_false ((cnt) > cur)) \
|
|
|
|
{ \
|
|
|
|
int newcnt = cur; \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
newcnt = array_roundsize (type, newcnt << 1); \
|
|
|
|
} \
|
|
|
|
while ((cnt) > newcnt); \
|
|
|
|
\
|
|
|
|
base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
|
|
|
|
init (base + cur, newcnt - cur); \
|
|
|
|
cur = newcnt; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define array_slim(type,stem) \
|
|
|
|
if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
|
|
|
|
{ \
|
|
|
|
stem ## max = array_roundsize (stem ## cnt >> 1); \
|
|
|
|
base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
|
|
|
|
fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
|
|
|
|
}
|
|
|
|
|
|
|
|
#define array_free(stem, idx) \
|
|
|
|
ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
void inline_size
|
|
|
|
anfds_init (ANFD *base, int count)
|
|
|
|
{
|
|
|
|
while (count--)
|
|
|
|
{
|
|
|
|
base->head = 0;
|
|
|
|
base->events = EV_NONE;
|
|
|
|
base->reify = 0;
|
|
|
|
|
|
|
|
++base;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void noinline
|
|
|
|
ev_feed_event (EV_P_ void *w, int revents)
|
|
|
|
{
|
|
|
|
W w_ = (W)w;
|
|
|
|
|
|
|
|
if (expect_false (w_->pending))
|
|
|
|
{
|
|
|
|
pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
w_->pending = ++pendingcnt [ABSPRI (w_)];
|
|
|
|
array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
|
|
|
|
pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
|
|
|
|
pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
queue_events (EV_P_ W *events, int eventcnt, int type)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < eventcnt; ++i)
|
|
|
|
ev_feed_event (EV_A_ events [i], type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void inline_speed
|
|
|
|
fd_event (EV_P_ int fd, int revents)
|
|
|
|
{
|
|
|
|
ANFD *anfd = anfds + fd;
|
|
|
|
ev_io *w;
|
|
|
|
|
|
|
|
for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
|
|
|
|
{
|
|
|
|
int ev = w->events & revents;
|
|
|
|
|
|
|
|
if (ev)
|
|
|
|
ev_feed_event (EV_A_ (W)w, ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ev_feed_fd_event (EV_P_ int fd, int revents)
|
|
|
|
{
|
|
|
|
fd_event (EV_A_ fd, revents);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
void inline_size
|
|
|
|
fd_reify (EV_P)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < fdchangecnt; ++i)
|
|
|
|
{
|
|
|
|
int fd = fdchanges [i];
|
|
|
|
ANFD *anfd = anfds + fd;
|
|
|
|
ev_io *w;
|
|
|
|
|
|
|
|
int events = 0;
|
|
|
|
|
|
|
|
for (w = (ev_io *)anfd->head; w; w = (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;
|
|
|
|
|
|
|
|
backend_modify (EV_A_ fd, anfd->events, events);
|
|
|
|
anfd->events = events;
|
|
|
|
}
|
|
|
|
|
|
|
|
fdchangecnt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void inline_size
|
|
|
|
fd_change (EV_P_ int fd)
|
|
|
|
{
|
|
|
|
if (expect_false (anfds [fd].reify))
|
|
|
|
return;
|
|
|
|
|
|
|
|
anfds [fd].reify = 1;
|
|
|
|
|
|
|
|
++fdchangecnt;
|
|
|
|
array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
|
|
|
|
fdchanges [fdchangecnt - 1] = fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void inline_speed
|
|
|
|
fd_kill (EV_P_ int fd)
|
|
|
|
{
|
|
|
|
ev_io *w;
|
|
|
|
|
|
|
|
while ((w = (ev_io *)anfds [fd].head))
|
|
|
|
{
|
|
|
|
ev_io_stop (EV_A_ w);
|
|
|
|
ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int inline_size
|
|
|
|
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 noinline
|
|
|
|
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 noinline
|
|
|
|
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 backend needs to re-arm all fds from scratch */
|
|
|
|
static void noinline
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
void inline_speed
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void inline_speed
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void inline_size
|
|
|
|
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 ev_io sigev;
|
|
|
|
|
|
|
|
void inline_size
|
|
|
|
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 noinline
|
|
|
|
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 == ev_default_loop_ptr));
|
|
|
|
#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_ 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void inline_size
|
|
|
|
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 noinline
|
|
|
|
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 ev_child *childs [PID_HASHSIZE];
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
static ev_signal childev;
|
|