2007-10-31 14:44:14 +00:00
|
|
|
/*
|
2007-11-01 13:11:11 +00:00
|
|
|
* libev event processing core, watcher management
|
|
|
|
*
|
2007-10-31 14:44:14 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2007-11-03 19:41:55 +00:00
|
|
|
#ifndef EV_STANDALONE
|
2007-11-01 08:10:03 +00:00
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2007-10-31 14:44:14 +00:00
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
2007-10-31 00:24:16 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
2007-10-31 13:57:34 +00:00
|
|
|
#include <stddef.h>
|
2007-10-30 20:59:31 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2007-10-30 23:10:33 +00:00
|
|
|
#include <assert.h>
|
2007-10-30 20:59:31 +00:00
|
|
|
#include <errno.h>
|
2007-10-31 19:07:43 +00:00
|
|
|
#include <sys/types.h>
|
2007-11-03 09:19:58 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
# include <sys/wait.h>
|
|
|
|
#endif
|
2007-10-30 20:59:31 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2007-11-02 11:02:23 +00:00
|
|
|
/**/
|
|
|
|
|
2007-11-01 08:10:03 +00:00
|
|
|
#ifndef EV_USE_MONOTONIC
|
2007-11-01 13:33:12 +00:00
|
|
|
# define EV_USE_MONOTONIC 1
|
|
|
|
#endif
|
|
|
|
|
2007-11-01 08:10:03 +00:00
|
|
|
#ifndef EV_USE_SELECT
|
|
|
|
# define EV_USE_SELECT 1
|
2007-10-31 07:36:03 +00:00
|
|
|
#endif
|
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
#ifndef EV_USEV_POLL
|
|
|
|
# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */
|
2007-11-02 16:54:34 +00:00
|
|
|
#endif
|
|
|
|
|
2007-11-01 08:10:03 +00:00
|
|
|
#ifndef EV_USE_EPOLL
|
|
|
|
# define EV_USE_EPOLL 0
|
2007-10-31 07:36:03 +00:00
|
|
|
#endif
|
|
|
|
|
2007-11-02 20:59:14 +00:00
|
|
|
#ifndef EV_USE_KQUEUE
|
|
|
|
# define EV_USE_KQUEUE 0
|
|
|
|
#endif
|
|
|
|
|
2007-11-02 11:02:23 +00:00
|
|
|
#ifndef EV_USE_REALTIME
|
|
|
|
# define EV_USE_REALTIME 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**/
|
|
|
|
|
|
|
|
#ifndef CLOCK_MONOTONIC
|
|
|
|
# undef EV_USE_MONOTONIC
|
|
|
|
# define EV_USE_MONOTONIC 0
|
|
|
|
#endif
|
|
|
|
|
2007-11-01 09:05:33 +00:00
|
|
|
#ifndef CLOCK_REALTIME
|
2007-11-02 11:02:23 +00:00
|
|
|
# undef EV_USE_REALTIME
|
2007-11-01 09:05:33 +00:00
|
|
|
# define EV_USE_REALTIME 0
|
|
|
|
#endif
|
2007-11-02 11:02:23 +00:00
|
|
|
|
|
|
|
/**/
|
2007-10-30 20:59:31 +00:00
|
|
|
|
2007-10-30 23:10:33 +00:00
|
|
|
#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
|
2007-11-02 11:02:23 +00:00
|
|
|
#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
|
2007-11-01 09:05:33 +00:00
|
|
|
#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
|
2007-11-02 11:02:23 +00:00
|
|
|
/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
|
2007-10-30 20:59:31 +00:00
|
|
|
|
|
|
|
#include "ev.h"
|
|
|
|
|
2007-11-02 11:02:23 +00:00
|
|
|
#if __GNUC__ >= 3
|
|
|
|
# define expect(expr,value) __builtin_expect ((expr),(value))
|
|
|
|
# define inline inline
|
|
|
|
#else
|
|
|
|
# define expect(expr,value) (expr)
|
|
|
|
# define inline static
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define expect_false(expr) expect ((expr) != 0, 0)
|
|
|
|
#define expect_true(expr) expect ((expr) != 0, 1)
|
|
|
|
|
2007-11-02 20:05:05 +00:00
|
|
|
#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
|
|
|
|
#define ABSPRI(w) ((w)->priority - EV_MINPRI)
|
|
|
|
|
2007-10-31 07:36:03 +00:00
|
|
|
typedef struct ev_watcher *W;
|
|
|
|
typedef struct ev_watcher_list *WL;
|
2007-10-31 09:23:17 +00:00
|
|
|
typedef struct ev_watcher_time *WT;
|
2007-10-31 07:36:03 +00:00
|
|
|
|
2007-11-03 22:31:11 +00:00
|
|
|
/*****************************************************************************/
|
2007-11-03 21:58:51 +00:00
|
|
|
|
2007-11-03 22:31:11 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
struct ev_watcher_list *head;
|
|
|
|
unsigned char events;
|
|
|
|
unsigned char reify;
|
|
|
|
} ANFD;
|
2007-11-03 21:58:51 +00:00
|
|
|
|
2007-11-03 22:31:11 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
W w;
|
|
|
|
int events;
|
|
|
|
} ANPENDING;
|
2007-11-03 21:58:51 +00:00
|
|
|
|
2007-11-03 22:31:11 +00:00
|
|
|
#ifdef EV_MULTIPLICITY
|
|
|
|
struct ev_loop
|
|
|
|
{
|
|
|
|
# define VAR(name,decl) decl
|
|
|
|
# include "ev_vars.h"
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
# define VAR(name,decl) static decl
|
|
|
|
# include "ev_vars.h"
|
2007-11-03 21:58:51 +00:00
|
|
|
#endif
|
2007-11-03 22:31:11 +00:00
|
|
|
#undef VAR
|
2007-10-30 20:59:31 +00:00
|
|
|
|
2007-10-31 00:32:33 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
inline ev_tstamp
|
2007-10-30 20:59:31 +00:00
|
|
|
ev_time (void)
|
|
|
|
{
|
2007-11-01 08:10:03 +00:00
|
|
|
#if EV_USE_REALTIME
|
2007-10-30 20:59:31 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
inline ev_tstamp
|
2007-10-30 20:59:31 +00:00
|
|
|
get_clock (void)
|
|
|
|
{
|
2007-11-01 08:10:03 +00:00
|
|
|
#if EV_USE_MONOTONIC
|
2007-11-02 11:02:23 +00:00
|
|
|
if (expect_true (have_monotonic))
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime (CLOCK_MONOTONIC, &ts);
|
|
|
|
return ts.tv_sec + ts.tv_nsec * 1e-9;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ev_time ();
|
|
|
|
}
|
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
ev_tstamp
|
|
|
|
ev_now (EV_P)
|
|
|
|
{
|
|
|
|
return rt_now;
|
|
|
|
}
|
|
|
|
|
2007-11-01 08:28:33 +00:00
|
|
|
#define array_roundsize(base,n) ((n) | 4 & ~3)
|
2007-11-01 08:10:03 +00:00
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
#define array_needsize(base,cur,cnt,init) \
|
2007-11-02 11:02:23 +00:00
|
|
|
if (expect_false ((cnt) > cur)) \
|
2007-10-30 20:59:31 +00:00
|
|
|
{ \
|
2007-10-31 20:10:17 +00:00
|
|
|
int newcnt = cur; \
|
|
|
|
do \
|
|
|
|
{ \
|
2007-11-01 08:28:33 +00:00
|
|
|
newcnt = array_roundsize (base, newcnt << 1); \
|
2007-10-31 20:10:17 +00:00
|
|
|
} \
|
|
|
|
while ((cnt) > newcnt); \
|
|
|
|
\
|
2007-10-30 20:59:31 +00:00
|
|
|
base = realloc (base, sizeof (*base) * (newcnt)); \
|
|
|
|
init (base + cur, newcnt - cur); \
|
|
|
|
cur = newcnt; \
|
|
|
|
}
|
|
|
|
|
2007-10-31 00:32:33 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
static void
|
|
|
|
anfds_init (ANFD *base, int count)
|
|
|
|
{
|
|
|
|
while (count--)
|
|
|
|
{
|
2007-10-31 22:16:36 +00:00
|
|
|
base->head = 0;
|
|
|
|
base->events = EV_NONE;
|
2007-11-01 11:11:22 +00:00
|
|
|
base->reify = 0;
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
++base;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
event (EV_P_ W w, int events)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-11-01 09:21:51 +00:00
|
|
|
if (w->pending)
|
|
|
|
{
|
2007-11-02 20:05:05 +00:00
|
|
|
pendings [ABSPRI (w)][w->pending - 1].events |= events;
|
2007-11-01 09:21:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-02 20:05:05 +00:00
|
|
|
w->pending = ++pendingcnt [ABSPRI (w)];
|
|
|
|
array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
|
|
|
|
pendings [ABSPRI (w)][w->pending - 1].w = w;
|
|
|
|
pendings [ABSPRI (w)][w->pending - 1].events = events;
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 22:16:36 +00:00
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
queue_events (EV_P_ W *events, int eventcnt, int type)
|
2007-10-31 22:16:36 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < eventcnt; ++i)
|
2007-11-03 21:58:51 +00:00
|
|
|
event (EV_A_ events [i], type);
|
2007-10-31 22:16:36 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
fd_event (EV_P_ int fd, int events)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
|
|
|
ANFD *anfd = anfds + fd;
|
|
|
|
struct ev_io *w;
|
|
|
|
|
2007-11-03 19:41:55 +00:00
|
|
|
for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
|
|
|
int ev = w->events & events;
|
|
|
|
|
|
|
|
if (ev)
|
2007-11-03 21:58:51 +00:00
|
|
|
event (EV_A_ (W)w, ev);
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-31 22:16:36 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-10-31 07:24:17 +00:00
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
fd_reify (EV_P)
|
2007-10-31 07:24:17 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-10-31 22:16:36 +00:00
|
|
|
for (i = 0; i < fdchangecnt; ++i)
|
|
|
|
{
|
|
|
|
int fd = fdchanges [i];
|
|
|
|
ANFD *anfd = anfds + fd;
|
|
|
|
struct ev_io *w;
|
|
|
|
|
|
|
|
int events = 0;
|
|
|
|
|
2007-11-03 19:41:55 +00:00
|
|
|
for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
|
2007-10-31 22:16:36 +00:00
|
|
|
events |= w->events;
|
|
|
|
|
2007-11-01 11:11:22 +00:00
|
|
|
anfd->reify = 0;
|
2007-10-31 22:16:36 +00:00
|
|
|
|
|
|
|
if (anfd->events != events)
|
|
|
|
{
|
2007-11-03 21:58:51 +00:00
|
|
|
method_modify (EV_A_ fd, anfd->events, events);
|
2007-10-31 22:16:36 +00:00
|
|
|
anfd->events = events;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fdchangecnt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
fd_change (EV_P_ int fd)
|
2007-10-31 22:16:36 +00:00
|
|
|
{
|
2007-11-01 11:11:22 +00:00
|
|
|
if (anfds [fd].reify || fdchangecnt < 0)
|
2007-10-31 22:16:36 +00:00
|
|
|
return;
|
|
|
|
|
2007-11-01 11:11:22 +00:00
|
|
|
anfds [fd].reify = 1;
|
2007-10-31 22:16:36 +00:00
|
|
|
|
|
|
|
++fdchangecnt;
|
|
|
|
array_needsize (fdchanges, fdchangemax, fdchangecnt, );
|
|
|
|
fdchanges [fdchangecnt - 1] = fd;
|
2007-10-31 07:24:17 +00:00
|
|
|
}
|
|
|
|
|
2007-11-02 16:54:34 +00:00
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
fd_kill (EV_P_ int fd)
|
2007-11-02 16:54:34 +00:00
|
|
|
{
|
|
|
|
struct ev_io *w;
|
|
|
|
|
2007-11-03 19:41:55 +00:00
|
|
|
while ((w = (struct ev_io *)anfds [fd].head))
|
2007-11-02 16:54:34 +00:00
|
|
|
{
|
2007-11-03 21:58:51 +00:00
|
|
|
ev_io_stop (EV_A_ w);
|
|
|
|
event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
|
2007-11-02 16:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-31 17:55:55 +00:00
|
|
|
/* called on EBADF to verify fds */
|
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
fd_ebadf (EV_P)
|
2007-10-31 17:55:55 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
for (fd = 0; fd < anfdmax; ++fd)
|
2007-10-31 22:16:36 +00:00
|
|
|
if (anfds [fd].events)
|
2007-10-31 17:55:55 +00:00
|
|
|
if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
|
2007-11-03 21:58:51 +00:00
|
|
|
fd_kill (EV_A_ fd);
|
2007-11-02 16:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* called on ENOMEM in select/poll to kill some fds and retry */
|
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
fd_enomem (EV_P)
|
2007-11-02 16:54:34 +00:00
|
|
|
{
|
|
|
|
int fd = anfdmax;
|
|
|
|
|
|
|
|
while (fd--)
|
|
|
|
if (anfds [fd].events)
|
|
|
|
{
|
|
|
|
close (fd);
|
2007-11-03 21:58:51 +00:00
|
|
|
fd_kill (EV_A_ fd);
|
2007-11-02 16:54:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-10-31 17:55:55 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 00:32:33 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
static void
|
2007-10-31 09:23:17 +00:00
|
|
|
upheap (WT *timers, int k)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-10-31 09:23:17 +00:00
|
|
|
WT w = timers [k];
|
2007-10-30 20:59:31 +00:00
|
|
|
|
|
|
|
while (k && timers [k >> 1]->at > w->at)
|
|
|
|
{
|
|
|
|
timers [k] = timers [k >> 1];
|
|
|
|
timers [k]->active = k + 1;
|
|
|
|
k >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
timers [k] = w;
|
|
|
|
timers [k]->active = k + 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-10-31 09:23:17 +00:00
|
|
|
downheap (WT *timers, int N, int k)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-10-31 09:23:17 +00:00
|
|
|
WT w = timers [k];
|
2007-10-30 20:59:31 +00:00
|
|
|
|
2007-10-30 23:10:33 +00:00
|
|
|
while (k < (N >> 1))
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
|
|
|
int j = k << 1;
|
|
|
|
|
2007-10-30 23:10:33 +00:00
|
|
|
if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
|
2007-10-30 20:59:31 +00:00
|
|
|
++j;
|
|
|
|
|
|
|
|
if (w->at <= timers [j]->at)
|
|
|
|
break;
|
|
|
|
|
|
|
|
timers [k] = timers [j];
|
2007-10-30 21:42:12 +00:00
|
|
|
timers [k]->active = k + 1;
|
2007-10-30 20:59:31 +00:00
|
|
|
k = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
timers [k] = w;
|
|
|
|
timers [k]->active = k + 1;
|
|
|
|
}
|
|
|
|
|
2007-10-31 00:32:33 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-10-31 00:24:16 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2007-11-03 19:41:55 +00:00
|
|
|
struct ev_watcher_list *head;
|
2007-11-01 11:43:11 +00:00
|
|
|
sig_atomic_t volatile gotsig;
|
2007-10-31 00:24:16 +00:00
|
|
|
} ANSIG;
|
|
|
|
|
|
|
|
static ANSIG *signals;
|
2007-10-30 23:10:33 +00:00
|
|
|
static int signalmax;
|
2007-10-30 20:59:31 +00:00
|
|
|
|
2007-10-31 00:24:16 +00:00
|
|
|
static int sigpipe [2];
|
2007-11-01 11:43:11 +00:00
|
|
|
static sig_atomic_t volatile gotsig;
|
2007-10-31 00:24:16 +00:00
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
static void
|
2007-10-31 00:24:16 +00:00
|
|
|
signals_init (ANSIG *base, int count)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
|
|
|
while (count--)
|
2007-10-31 00:24:16 +00:00
|
|
|
{
|
|
|
|
base->head = 0;
|
|
|
|
base->gotsig = 0;
|
2007-11-01 11:11:22 +00:00
|
|
|
|
2007-10-31 00:24:16 +00:00
|
|
|
++base;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sighandler (int signum)
|
|
|
|
{
|
|
|
|
signals [signum - 1].gotsig = 1;
|
|
|
|
|
|
|
|
if (!gotsig)
|
|
|
|
{
|
2007-11-03 12:19:31 +00:00
|
|
|
int old_errno = errno;
|
2007-10-31 00:24:16 +00:00
|
|
|
gotsig = 1;
|
2007-11-01 11:43:11 +00:00
|
|
|
write (sigpipe [1], &signum, 1);
|
2007-11-03 12:19:31 +00:00
|
|
|
errno = old_errno;
|
2007-10-31 00:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
sigcb (EV_P_ struct ev_io *iow, int revents)
|
2007-10-31 00:24:16 +00:00
|
|
|
{
|
2007-11-03 19:41:55 +00:00
|
|
|
struct ev_watcher_list *w;
|
2007-11-01 15:21:13 +00:00
|
|
|
int signum;
|
2007-10-31 00:24:16 +00:00
|
|
|
|
|
|
|
read (sigpipe [0], &revents, 1);
|
2007-11-01 11:43:11 +00:00
|
|
|
gotsig = 0;
|
2007-10-31 00:24:16 +00:00
|
|
|
|
2007-11-01 15:21:13 +00:00
|
|
|
for (signum = signalmax; signum--; )
|
|
|
|
if (signals [signum].gotsig)
|
2007-10-31 00:24:16 +00:00
|
|
|
{
|
2007-11-01 15:21:13 +00:00
|
|
|
signals [signum].gotsig = 0;
|
2007-10-31 00:24:16 +00:00
|
|
|
|
2007-11-01 15:21:13 +00:00
|
|
|
for (w = signals [signum].head; w; w = w->next)
|
2007-11-03 21:58:51 +00:00
|
|
|
event (EV_A_ (W)w, EV_SIGNAL);
|
2007-10-31 00:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
siginit (EV_P)
|
2007-10-31 00:24:16 +00:00
|
|
|
{
|
2007-11-03 09:19:58 +00:00
|
|
|
#ifndef WIN32
|
2007-10-31 00:24:16 +00:00
|
|
|
fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
|
|
|
|
fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
|
|
|
|
|
|
|
|
/* rather than sort out wether we really need nb, set it */
|
|
|
|
fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
|
|
|
|
fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
|
2007-11-03 09:19:58 +00:00
|
|
|
#endif
|
2007-10-31 00:24:16 +00:00
|
|
|
|
2007-11-01 06:48:49 +00:00
|
|
|
ev_io_set (&sigev, sigpipe [0], EV_READ);
|
|
|
|
ev_io_start (&sigev);
|
2007-11-03 22:10:39 +00:00
|
|
|
ev_unref (EV_A); /* child watcher should not keep loop alive */
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 00:32:33 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-11-03 09:19:58 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
|
2007-10-31 19:07:43 +00:00
|
|
|
#ifndef WCONTINUED
|
|
|
|
# define WCONTINUED 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status)
|
2007-10-31 19:07:43 +00:00
|
|
|
{
|
|
|
|
struct ev_child *w;
|
2007-11-03 11:44:44 +00:00
|
|
|
|
2007-11-03 19:41:55 +00:00
|
|
|
for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
|
2007-11-03 11:44:44 +00:00
|
|
|
if (w->pid == pid || !w->pid)
|
|
|
|
{
|
|
|
|
w->priority = sw->priority; /* need to do it *now* */
|
|
|
|
w->rpid = pid;
|
|
|
|
w->rstatus = status;
|
2007-11-03 21:58:51 +00:00
|
|
|
event (EV_A_ (W)w, EV_CHILD);
|
2007-11-03 11:44:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
childcb (EV_P_ struct ev_signal *sw, int revents)
|
2007-11-03 11:44:44 +00:00
|
|
|
{
|
2007-10-31 19:07:43 +00:00
|
|
|
int pid, status;
|
|
|
|
|
2007-11-03 11:44:44 +00:00
|
|
|
if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
|
|
|
|
{
|
|
|
|
/* make sure we are called again until all childs have been reaped */
|
2007-11-03 21:58:51 +00:00
|
|
|
event (EV_A_ (W)sw, EV_SIGNAL);
|
2007-11-03 11:44:44 +00:00
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
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 */
|
2007-11-03 11:44:44 +00:00
|
|
|
}
|
2007-10-31 19:07:43 +00:00
|
|
|
}
|
|
|
|
|
2007-11-03 09:19:58 +00:00
|
|
|
#endif
|
|
|
|
|
2007-10-31 19:07:43 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-11-02 20:59:14 +00:00
|
|
|
#if EV_USE_KQUEUE
|
|
|
|
# include "ev_kqueue.c"
|
|
|
|
#endif
|
2007-11-01 08:10:03 +00:00
|
|
|
#if EV_USE_EPOLL
|
2007-10-30 20:59:31 +00:00
|
|
|
# include "ev_epoll.c"
|
|
|
|
#endif
|
2007-11-03 21:58:51 +00:00
|
|
|
#if EV_USEV_POLL
|
2007-11-02 16:54:34 +00:00
|
|
|
# include "ev_poll.c"
|
|
|
|
#endif
|
2007-11-01 08:10:03 +00:00
|
|
|
#if EV_USE_SELECT
|
2007-10-30 20:59:31 +00:00
|
|
|
# include "ev_select.c"
|
|
|
|
#endif
|
|
|
|
|
2007-10-31 20:46:44 +00:00
|
|
|
int
|
|
|
|
ev_version_major (void)
|
|
|
|
{
|
|
|
|
return EV_VERSION_MAJOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ev_version_minor (void)
|
|
|
|
{
|
|
|
|
return EV_VERSION_MINOR;
|
|
|
|
}
|
|
|
|
|
2007-11-03 16:16:58 +00:00
|
|
|
/* return true if we are running with elevated privileges and should ignore env variables */
|
2007-11-02 16:54:34 +00:00
|
|
|
static int
|
2007-11-03 21:58:51 +00:00
|
|
|
enable_secure (void)
|
2007-11-02 16:54:34 +00:00
|
|
|
{
|
2007-11-03 16:16:58 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
return 0;
|
|
|
|
#else
|
2007-11-02 16:54:34 +00:00
|
|
|
return getuid () != geteuid ()
|
|
|
|
|| getgid () != getegid ();
|
2007-11-03 16:16:58 +00:00
|
|
|
#endif
|
2007-11-02 16:54:34 +00:00
|
|
|
}
|
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
int
|
|
|
|
ev_method (EV_P)
|
|
|
|
{
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ev_init (EV_P_ int methods)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-11-03 22:31:11 +00:00
|
|
|
#ifdef EV_MULTIPLICITY
|
|
|
|
memset (loop, 0, sizeof (struct ev_loop));
|
|
|
|
#endif
|
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
if (!method)
|
2007-10-31 20:10:17 +00:00
|
|
|
{
|
2007-11-01 08:10:03 +00:00
|
|
|
#if EV_USE_MONOTONIC
|
2007-10-31 20:10:17 +00:00
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
if (!clock_gettime (CLOCK_MONOTONIC, &ts))
|
|
|
|
have_monotonic = 1;
|
|
|
|
}
|
2007-10-30 20:59:31 +00:00
|
|
|
#endif
|
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
rt_now = ev_time ();
|
|
|
|
mn_now = get_clock ();
|
|
|
|
now_floor = mn_now;
|
|
|
|
diff = rt_now - mn_now;
|
2007-10-30 20:59:31 +00:00
|
|
|
|
2007-10-31 20:10:17 +00:00
|
|
|
if (pipe (sigpipe))
|
|
|
|
return 0;
|
2007-10-31 00:24:16 +00:00
|
|
|
|
2007-11-02 16:54:34 +00:00
|
|
|
if (methods == EVMETHOD_AUTO)
|
2007-11-03 21:58:51 +00:00
|
|
|
if (!enable_secure () && getenv ("LIBmethodS"))
|
|
|
|
methods = atoi (getenv ("LIBmethodS"));
|
2007-11-03 19:41:55 +00:00
|
|
|
else
|
|
|
|
methods = EVMETHOD_ANY;
|
2007-11-02 16:54:34 +00:00
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
method = 0;
|
2007-11-02 20:59:14 +00:00
|
|
|
#if EV_USE_KQUEUE
|
2007-11-03 21:58:51 +00:00
|
|
|
if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
|
2007-11-02 20:59:14 +00:00
|
|
|
#endif
|
2007-11-01 08:10:03 +00:00
|
|
|
#if EV_USE_EPOLL
|
2007-11-03 21:58:51 +00:00
|
|
|
if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
|
2007-11-02 16:54:34 +00:00
|
|
|
#endif
|
2007-11-03 21:58:51 +00:00
|
|
|
#if EV_USEV_POLL
|
|
|
|
if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
|
2007-10-30 20:59:31 +00:00
|
|
|
#endif
|
2007-11-01 08:10:03 +00:00
|
|
|
#if EV_USE_SELECT
|
2007-11-03 21:58:51 +00:00
|
|
|
if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
|
2007-10-30 20:59:31 +00:00
|
|
|
#endif
|
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
if (method)
|
2007-10-31 20:10:17 +00:00
|
|
|
{
|
2007-11-01 06:48:49 +00:00
|
|
|
ev_watcher_init (&sigev, sigcb);
|
2007-11-03 11:44:44 +00:00
|
|
|
ev_set_priority (&sigev, EV_MAXPRI);
|
2007-11-03 21:58:51 +00:00
|
|
|
siginit (EV_A);
|
2007-10-31 19:07:43 +00:00
|
|
|
|
2007-11-03 09:19:58 +00:00
|
|
|
#ifndef WIN32
|
2007-11-01 06:48:49 +00:00
|
|
|
ev_signal_init (&childev, childcb, SIGCHLD);
|
2007-11-03 11:44:44 +00:00
|
|
|
ev_set_priority (&childev, EV_MAXPRI);
|
2007-11-03 21:58:51 +00:00
|
|
|
ev_signal_start (EV_A_ &childev);
|
2007-11-03 22:10:39 +00:00
|
|
|
ev_unref (EV_A); /* child watcher should not keep loop alive */
|
2007-11-03 09:19:58 +00:00
|
|
|
#endif
|
2007-10-31 20:10:17 +00:00
|
|
|
}
|
2007-10-31 00:24:16 +00:00
|
|
|
}
|
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
return method;
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 00:32:33 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-10-31 20:46:44 +00:00
|
|
|
void
|
2007-11-01 11:55:54 +00:00
|
|
|
ev_fork_prepare (void)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-10-31 07:40:49 +00:00
|
|
|
/* nop */
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 20:46:44 +00:00
|
|
|
void
|
2007-11-01 11:55:54 +00:00
|
|
|
ev_fork_parent (void)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-10-31 07:40:49 +00:00
|
|
|
/* nop */
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 20:46:44 +00:00
|
|
|
void
|
2007-11-01 11:55:54 +00:00
|
|
|
ev_fork_child (void)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-11-01 08:10:03 +00:00
|
|
|
#if EV_USE_EPOLL
|
2007-11-03 21:58:51 +00:00
|
|
|
if (method == EVMETHOD_EPOLL)
|
2007-10-30 23:54:38 +00:00
|
|
|
epoll_postfork_child ();
|
2007-10-30 20:59:31 +00:00
|
|
|
#endif
|
2007-10-31 00:24:16 +00:00
|
|
|
|
2007-11-01 06:48:49 +00:00
|
|
|
ev_io_stop (&sigev);
|
2007-10-31 00:24:16 +00:00
|
|
|
close (sigpipe [0]);
|
|
|
|
close (sigpipe [1]);
|
|
|
|
pipe (sigpipe);
|
|
|
|
siginit ();
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 00:32:33 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
call_pending (EV_P)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-11-02 20:05:05 +00:00
|
|
|
int pri;
|
2007-10-30 20:59:31 +00:00
|
|
|
|
2007-11-02 20:05:05 +00:00
|
|
|
for (pri = NUMPRI; pri--; )
|
|
|
|
while (pendingcnt [pri])
|
|
|
|
{
|
|
|
|
ANPENDING *p = pendings [pri] + --pendingcnt [pri];
|
|
|
|
|
|
|
|
if (p->w)
|
|
|
|
{
|
|
|
|
p->w->pending = 0;
|
2007-11-03 21:58:51 +00:00
|
|
|
p->w->cb (EV_A_ p->w, p->events);
|
2007-11-02 20:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
timers_reify (EV_P)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-11-03 21:58:51 +00:00
|
|
|
while (timercnt && timers [0]->at <= mn_now)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
|
|
|
struct ev_timer *w = timers [0];
|
|
|
|
|
2007-10-30 23:10:33 +00:00
|
|
|
/* first reschedule or stop timer */
|
2007-10-30 20:59:31 +00:00
|
|
|
if (w->repeat)
|
|
|
|
{
|
2007-11-01 11:11:22 +00:00
|
|
|
assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
|
2007-11-03 21:58:51 +00:00
|
|
|
w->at = mn_now + w->repeat;
|
2007-10-31 09:23:17 +00:00
|
|
|
downheap ((WT *)timers, timercnt, 0);
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
else
|
2007-11-03 21:58:51 +00:00
|
|
|
ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
|
2007-11-01 08:28:33 +00:00
|
|
|
|
|
|
|
event ((W)w, EV_TIMEOUT);
|
2007-10-31 09:23:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
periodics_reify (EV_P)
|
2007-10-31 09:23:17 +00:00
|
|
|
{
|
2007-11-03 21:58:51 +00:00
|
|
|
while (periodiccnt && periodics [0]->at <= rt_now)
|
2007-10-31 09:23:17 +00:00
|
|
|
{
|
|
|
|
struct ev_periodic *w = periodics [0];
|
|
|
|
|
|
|
|
/* first reschedule or stop timer */
|
|
|
|
if (w->interval)
|
2007-10-30 23:10:33 +00:00
|
|
|
{
|
2007-11-03 21:58:51 +00:00
|
|
|
w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval;
|
|
|
|
assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now));
|
2007-10-31 09:23:17 +00:00
|
|
|
downheap ((WT *)periodics, periodiccnt, 0);
|
2007-10-30 23:10:33 +00:00
|
|
|
}
|
2007-10-31 09:23:17 +00:00
|
|
|
else
|
2007-11-03 21:58:51 +00:00
|
|
|
ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
|
2007-10-30 20:59:31 +00:00
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
event (EV_A_ (W)w, EV_PERIODIC);
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-31 09:23:17 +00:00
|
|
|
static void
|
2007-11-03 21:58:51 +00:00
|
|
|
periodics_reschedule (EV_P_ ev_tstamp diff)
|
2007-10-31 09:23:17 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-10-31 10:50:05 +00:00
|
|
|
/* adjust periodics after time jump */
|
2007-10-31 09:23:17 +00:00
|
|
|
for (i = 0; i < periodiccnt; ++i)
|
|
|
|
{
|
|
|
|
struct ev_periodic *w = periodics [i];
|
|
|
|
|
|
|
|
if (w->interval)
|
|
|
|
{
|
2007-11-03 21:58:51 +00:00
|
|
|
ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval;
|
2007-10-31 09:23:17 +00:00
|
|
|
|
|
|
|
if (fabs (diff) >= 1e-4)
|
|
|
|
{
|
2007-11-03 21:58:51 +00:00
|
|
|
ev_periodic_stop (EV_A_ w);
|
|
|
|
ev_periodic_start (EV_A_ w);
|
2007-10-31 09:23:17 +00:00
|
|
|
|
|
|
|
i = 0; /* restart loop, inefficient, but time jumps should be rare */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
inline int
|
|
|
|
time_update_monotonic (EV_P)
|
2007-11-02 11:02:23 +00:00
|
|
|
{
|
2007-11-03 21:58:51 +00:00
|
|
|
mn_now = get_clock ();
|
2007-11-02 11:02:23 +00:00
|
|
|
|
2007-11-03 21:58:51 +00:00
|
|
|
if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
|
2007-11-02 11:02:23 +00:00
|
|
|
{
|
2007-11-03 21:58:51 +00:00
|
|
|
rt_now = mn_now + diff;
|
2007-11-02 11:02:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|