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-01 08:10:03 +00:00
|
|
|
#if EV_USE_CONFIG_H
|
|
|
|
# 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>
|
|
|
|
#include <sys/wait.h>
|
2007-10-30 20:59:31 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
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
|
|
|
|
|
|
|
|
#ifndef CLOCK_MONOTONIC
|
|
|
|
# undef EV_USE_MONOTONIC
|
|
|
|
# define EV_USE_MONOTONIC 0
|
2007-10-30 20:59:31 +00:00
|
|
|
#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-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-01 09:05:33 +00:00
|
|
|
#ifndef CLOCK_REALTIME
|
|
|
|
# define EV_USE_REALTIME 0
|
|
|
|
#endif
|
2007-11-01 08:10:03 +00:00
|
|
|
#ifndef EV_USE_REALTIME
|
|
|
|
# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
|
2007-10-31 07:36:03 +00:00
|
|
|
#endif
|
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-01 09:05:33 +00:00
|
|
|
#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc 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 */
|
2007-10-30 20:59:31 +00:00
|
|
|
|
|
|
|
#include "ev.h"
|
|
|
|
|
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-10-30 23:10:33 +00:00
|
|
|
static ev_tstamp now, diff; /* monotonic clock */
|
2007-10-30 20:59:31 +00:00
|
|
|
ev_tstamp ev_now;
|
|
|
|
int ev_method;
|
|
|
|
|
|
|
|
static int have_monotonic; /* runtime */
|
|
|
|
|
|
|
|
static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
|
2007-10-30 23:54:38 +00:00
|
|
|
static void (*method_modify)(int fd, int oev, int nev);
|
2007-10-30 20:59:31 +00:00
|
|
|
static void (*method_poll)(ev_tstamp timeout);
|
|
|
|
|
2007-10-31 00:32:33 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
ev_tstamp
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
static ev_tstamp
|
|
|
|
get_clock (void)
|
|
|
|
{
|
2007-11-01 08:10:03 +00:00
|
|
|
#if EV_USE_MONOTONIC
|
2007-10-30 20:59:31 +00:00
|
|
|
if (have_monotonic)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime (CLOCK_MONOTONIC, &ts);
|
|
|
|
return ts.tv_sec + ts.tv_nsec * 1e-9;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ev_time ();
|
|
|
|
}
|
|
|
|
|
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) \
|
|
|
|
if ((cnt) > cur) \
|
|
|
|
{ \
|
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
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
struct ev_io *head;
|
2007-11-01 11:11:22 +00:00
|
|
|
unsigned char events;
|
|
|
|
unsigned char reify;
|
2007-10-30 20:59:31 +00:00
|
|
|
} ANFD;
|
|
|
|
|
|
|
|
static ANFD *anfds;
|
|
|
|
static int anfdmax;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2007-10-31 07:36:03 +00:00
|
|
|
W w;
|
2007-10-30 20:59:31 +00:00
|
|
|
int events;
|
|
|
|
} ANPENDING;
|
|
|
|
|
|
|
|
static ANPENDING *pendings;
|
|
|
|
static int pendingmax, pendingcnt;
|
|
|
|
|
|
|
|
static void
|
2007-10-31 07:36:03 +00:00
|
|
|
event (W w, int events)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-11-01 09:21:51 +00:00
|
|
|
if (w->pending)
|
|
|
|
{
|
|
|
|
pendings [w->pending - 1].events |= events;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-01 08:28:33 +00:00
|
|
|
w->pending = ++pendingcnt;
|
|
|
|
array_needsize (pendings, pendingmax, pendingcnt, );
|
|
|
|
pendings [pendingcnt - 1].w = w;
|
|
|
|
pendings [pendingcnt - 1].events = events;
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 22:16:36 +00:00
|
|
|
static void
|
|
|
|
queue_events (W *events, int eventcnt, int type)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < eventcnt; ++i)
|
|
|
|
event (events [i], type);
|
|
|
|
}
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
static void
|
|
|
|
fd_event (int fd, int events)
|
|
|
|
{
|
|
|
|
ANFD *anfd = anfds + fd;
|
|
|
|
struct ev_io *w;
|
|
|
|
|
|
|
|
for (w = anfd->head; w; w = w->next)
|
|
|
|
{
|
|
|
|
int ev = w->events & events;
|
|
|
|
|
|
|
|
if (ev)
|
2007-10-31 07:36:03 +00:00
|
|
|
event ((W)w, ev);
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-31 22:16:36 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
static int *fdchanges;
|
|
|
|
static int fdchangemax, fdchangecnt;
|
|
|
|
|
2007-10-31 07:24:17 +00:00
|
|
|
static void
|
2007-10-31 22:16:36 +00:00
|
|
|
fd_reify (void)
|
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;
|
|
|
|
|
|
|
|
for (w = anfd->head; w; w = w->next)
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
method_modify (fd, anfd->events, events);
|
|
|
|
anfd->events = events;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fdchangecnt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fd_change (int fd)
|
|
|
|
{
|
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-10-31 17:55:55 +00:00
|
|
|
/* called on EBADF to verify fds */
|
|
|
|
static void
|
2007-10-31 20:46:44 +00:00
|
|
|
fd_recheck (void)
|
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)
|
|
|
|
while (anfds [fd].head)
|
2007-10-31 20:46:44 +00:00
|
|
|
{
|
2007-11-01 06:48:49 +00:00
|
|
|
ev_io_stop (anfds [fd].head);
|
2007-11-01 11:11:22 +00:00
|
|
|
event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
|
2007-10-31 20:46:44 +00:00
|
|
|
}
|
2007-10-31 17:55:55 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 00:32:33 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-10-31 09:23:17 +00:00
|
|
|
static struct ev_timer **timers;
|
|
|
|
static int timermax, timercnt;
|
2007-10-30 23:10:33 +00:00
|
|
|
|
2007-10-31 09:23:17 +00:00
|
|
|
static struct ev_periodic **periodics;
|
|
|
|
static int periodicmax, periodiccnt;
|
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
|
|
|
|
{
|
|
|
|
struct ev_signal *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
|
|
|
static struct ev_io sigev;
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
gotsig = 1;
|
2007-11-01 11:43:11 +00:00
|
|
|
write (sigpipe [1], &signum, 1);
|
2007-10-31 00:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sigcb (struct ev_io *iow, int revents)
|
|
|
|
{
|
|
|
|
struct ev_signal *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-10-31 07:36:03 +00:00
|
|
|
event ((W)w, EV_SIGNAL);
|
2007-10-31 00:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
siginit (void)
|
|
|
|
{
|
|
|
|
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-01 06:48:49 +00:00
|
|
|
ev_io_set (&sigev, sigpipe [0], EV_READ);
|
|
|
|
ev_io_start (&sigev);
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 00:32:33 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-10-31 07:24:17 +00:00
|
|
|
static struct ev_idle **idles;
|
|
|
|
static int idlemax, idlecnt;
|
|
|
|
|
2007-10-31 18:28:00 +00:00
|
|
|
static struct ev_prepare **prepares;
|
|
|
|
static int preparemax, preparecnt;
|
|
|
|
|
2007-10-31 07:24:17 +00:00
|
|
|
static struct ev_check **checks;
|
|
|
|
static int checkmax, checkcnt;
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-10-31 19:07:43 +00:00
|
|
|
static struct ev_child *childs [PID_HASHSIZE];
|
|
|
|
static struct ev_signal childev;
|
|
|
|
|
|
|
|
#ifndef WCONTINUED
|
|
|
|
# define WCONTINUED 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
childcb (struct ev_signal *sw, int revents)
|
|
|
|
{
|
|
|
|
struct ev_child *w;
|
|
|
|
int pid, status;
|
|
|
|
|
|
|
|
while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
|
|
|
|
for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
|
2007-11-01 17:17:32 +00:00
|
|
|
if (w->pid == pid || !w->pid)
|
2007-10-31 19:07:43 +00:00
|
|
|
{
|
|
|
|
w->status = status;
|
|
|
|
event ((W)w, EV_CHILD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
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-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-10-30 20:59:31 +00:00
|
|
|
int ev_init (int flags)
|
|
|
|
{
|
2007-10-31 20:10:17 +00:00
|
|
|
if (!ev_method)
|
|
|
|
{
|
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-10-31 20:10:17 +00:00
|
|
|
ev_now = ev_time ();
|
|
|
|
now = get_clock ();
|
|
|
|
diff = ev_now - 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-10-31 20:10:17 +00:00
|
|
|
ev_method = EVMETHOD_NONE;
|
2007-11-01 08:10:03 +00:00
|
|
|
#if EV_USE_EPOLL
|
2007-10-31 20:10:17 +00:00
|
|
|
if (ev_method == EVMETHOD_NONE) epoll_init (flags);
|
2007-10-30 20:59:31 +00:00
|
|
|
#endif
|
2007-11-01 08:10:03 +00:00
|
|
|
#if EV_USE_SELECT
|
2007-10-31 20:10:17 +00:00
|
|
|
if (ev_method == EVMETHOD_NONE) select_init (flags);
|
2007-10-30 20:59:31 +00:00
|
|
|
#endif
|
|
|
|
|
2007-10-31 20:10:17 +00:00
|
|
|
if (ev_method)
|
|
|
|
{
|
2007-11-01 06:48:49 +00:00
|
|
|
ev_watcher_init (&sigev, sigcb);
|
2007-10-31 20:10:17 +00:00
|
|
|
siginit ();
|
2007-10-31 19:07:43 +00:00
|
|
|
|
2007-11-01 06:48:49 +00:00
|
|
|
ev_signal_init (&childev, childcb, SIGCHLD);
|
|
|
|
ev_signal_start (&childev);
|
2007-10-31 20:10:17 +00:00
|
|
|
}
|
2007-10-31 00:24:16 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
return ev_method;
|
|
|
|
}
|
|
|
|
|
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-10-30 23:54:38 +00:00
|
|
|
if (ev_method == EVMETHOD_EPOLL)
|
|
|
|
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-10-31 20:46:44 +00:00
|
|
|
call_pending (void)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-10-31 16:29:52 +00:00
|
|
|
while (pendingcnt)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-10-31 16:29:52 +00:00
|
|
|
ANPENDING *p = pendings + --pendingcnt;
|
2007-10-30 20:59:31 +00:00
|
|
|
|
|
|
|
if (p->w)
|
|
|
|
{
|
|
|
|
p->w->pending = 0;
|
|
|
|
p->w->cb (p->w, p->events);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-10-31 20:46:44 +00:00
|
|
|
timers_reify (void)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-10-30 23:10:33 +00:00
|
|
|
while (timercnt && timers [0]->at <= 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-10-31 09:23:17 +00:00
|
|
|
w->at = now + w->repeat;
|
|
|
|
downheap ((WT *)timers, timercnt, 0);
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
else
|
2007-11-01 06:48:49 +00:00
|
|
|
ev_timer_stop (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-10-31 20:46:44 +00:00
|
|
|
periodics_reify (void)
|
2007-10-31 09:23:17 +00:00
|
|
|
{
|
|
|
|
while (periodiccnt && periodics [0]->at <= ev_now)
|
|
|
|
{
|
|
|
|
struct ev_periodic *w = periodics [0];
|
|
|
|
|
|
|
|
/* first reschedule or stop timer */
|
|
|
|
if (w->interval)
|
2007-10-30 23:10:33 +00:00
|
|
|
{
|
2007-10-31 09:23:17 +00:00
|
|
|
w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
|
2007-11-01 11:11:22 +00:00
|
|
|
assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_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-01 06:48:49 +00:00
|
|
|
ev_periodic_stop (w); /* nonrepeating: stop timer */
|
2007-10-30 20:59:31 +00:00
|
|
|
|
2007-11-01 11:11:22 +00:00
|
|
|
event ((W)w, EV_PERIODIC);
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-31 09:23:17 +00:00
|
|
|
static void
|
2007-10-31 10:50:05 +00:00
|
|
|
periodics_reschedule (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)
|
|
|
|
{
|
|
|
|
ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
|
|
|
|
|
|
|
|
if (fabs (diff) >= 1e-4)
|
|
|
|
{
|
2007-11-01 06:48:49 +00:00
|
|
|
ev_periodic_stop (w);
|
|
|
|
ev_periodic_start (w);
|
2007-10-31 09:23:17 +00:00
|
|
|
|
|
|
|
i = 0; /* restart loop, inefficient, but time jumps should be rare */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-30 23:10:33 +00:00
|
|
|
static void
|
2007-10-31 20:46:44 +00:00
|
|
|
time_update (void)
|
2007-10-30 23:10:33 +00:00
|
|
|
{
|
|
|
|
int i;
|
2007-10-31 09:23:17 +00:00
|
|
|
|
2007-10-30 23:10:33 +00:00
|
|
|
ev_now = ev_time ();
|
|
|
|
|
|
|
|
if (have_monotonic)
|
|
|
|
{
|
|
|
|
ev_tstamp odiff = diff;
|
|
|
|
|
2007-10-31 09:23:17 +00:00
|
|
|
for (i = 4; --i; ) /* loop a few times, before making important decisions */
|
2007-10-30 23:10:33 +00:00
|
|
|
{
|
|
|
|
now = get_clock ();
|
|
|
|
diff = ev_now - now;
|
|
|
|
|
|
|
|
if (fabs (odiff - diff) < MIN_TIMEJUMP)
|
|
|
|
return; /* all is well */
|
|
|
|
|
|
|
|
ev_now = ev_time ();
|
|
|
|
}
|
|
|
|
|
2007-10-31 10:50:05 +00:00
|
|
|
periodics_reschedule (diff - odiff);
|
|
|
|
/* no timer adjustment, as the monotonic clock doesn't jump */
|
2007-10-30 23:10:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
|
2007-10-31 10:50:05 +00:00
|
|
|
{
|
|
|
|
periodics_reschedule (ev_now - now);
|
|
|
|
|
|
|
|
/* adjust timers. this is easy, as the offset is the same for all */
|
|
|
|
for (i = 0; i < timercnt; ++i)
|
|
|
|
timers [i]->at += diff;
|
|
|
|
}
|
2007-10-30 23:10:33 +00:00
|
|
|
|
|
|
|
now = ev_now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
int ev_loop_done;
|
|
|
|
|
2007-10-30 23:10:33 +00:00
|
|
|
void ev_loop (int flags)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
|
|
|
double block;
|
2007-10-31 21:50:15 +00:00
|
|
|
ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
|
2007-10-30 20:59:31 +00:00
|
|
|
|
2007-10-31 07:24:17 +00:00
|
|
|
do
|
|
|
|
{
|
2007-10-31 18:28:00 +00:00
|
|
|
/* queue check watchers (and execute them) */
|
2007-10-31 18:37:38 +00:00
|
|
|
if (preparecnt)
|
2007-10-31 18:28:00 +00:00
|
|
|
{
|
|
|
|
queue_events ((W *)prepares, preparecnt, EV_PREPARE);
|
|
|
|
call_pending ();
|
|
|
|
}
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
/* update fd-related kernel structures */
|
2007-10-30 23:54:38 +00:00
|
|
|
fd_reify ();
|
2007-10-30 20:59:31 +00:00
|
|
|
|
|
|
|
/* calculate blocking time */
|
2007-10-31 09:23:17 +00:00
|
|
|
|
2007-10-31 18:37:38 +00:00
|
|
|
/* we only need this for !monotonic clockor timers, but as we basically
|
|
|
|
always have timers, we just calculate it always */
|
2007-10-31 09:23:17 +00:00
|
|
|
ev_now = ev_time ();
|
|
|
|
|
2007-10-31 07:24:17 +00:00
|
|
|
if (flags & EVLOOP_NONBLOCK || idlecnt)
|
2007-10-30 20:59:31 +00:00
|
|
|
block = 0.;
|
|
|
|
else
|
|
|
|
{
|
2007-10-30 23:10:33 +00:00
|
|
|
block = MAX_BLOCKTIME;
|
|
|
|
|
2007-10-31 09:23:17 +00:00
|
|
|
if (timercnt)
|
2007-10-30 23:10:33 +00:00
|
|
|
{
|
2007-10-31 11:52:12 +00:00
|
|
|
ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
|
2007-10-30 23:10:33 +00:00
|
|
|
if (block > to) block = to;
|
|
|
|
}
|
|
|
|
|
2007-10-31 09:23:17 +00:00
|
|
|
if (periodiccnt)
|
2007-10-30 23:10:33 +00:00
|
|
|
{
|
2007-10-31 09:23:17 +00:00
|
|
|
ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
|
2007-10-30 23:10:33 +00:00
|
|
|
if (block > to) block = to;
|
|
|
|
}
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
if (block < 0.) block = 0.;
|
|
|
|
}
|
|
|
|
|
|
|
|
method_poll (block);
|
|
|
|
|
2007-10-30 23:10:33 +00:00
|
|
|
/* update ev_now, do magic */
|
|
|
|
time_update ();
|
|
|
|
|
2007-10-31 07:24:17 +00:00
|
|
|
/* queue pending timers and reschedule them */
|
2007-10-31 18:28:00 +00:00
|
|
|
timers_reify (); /* relative timers called last */
|
|
|
|
periodics_reify (); /* absolute timers called first */
|
2007-10-30 20:59:31 +00:00
|
|
|
|
2007-10-31 07:24:17 +00:00
|
|
|
/* queue idle watchers unless io or timers are pending */
|
|
|
|
if (!pendingcnt)
|
2007-10-31 07:36:03 +00:00
|
|
|
queue_events ((W *)idles, idlecnt, EV_IDLE);
|
2007-10-31 07:24:17 +00:00
|
|
|
|
2007-10-31 18:28:00 +00:00
|
|
|
/* queue check watchers, to be executed first */
|
|
|
|
if (checkcnt)
|
|
|
|
queue_events ((W *)checks, checkcnt, EV_CHECK);
|
2007-10-31 07:24:17 +00:00
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
call_pending ();
|
|
|
|
}
|
|
|
|
while (!ev_loop_done);
|
2007-10-31 10:50:05 +00:00
|
|
|
|
|
|
|
if (ev_loop_done != 2)
|
|
|
|
ev_loop_done = 0;
|
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-10-31 07:36:03 +00:00
|
|
|
wlist_add (WL *head, WL elem)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
|
|
|
elem->next = *head;
|
|
|
|
*head = elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-10-31 07:36:03 +00:00
|
|
|
wlist_del (WL *head, WL elem)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
|
|
|
while (*head)
|
|
|
|
{
|
|
|
|
if (*head == elem)
|
|
|
|
{
|
|
|
|
*head = elem->next;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
head = &(*head)->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-31 13:57:34 +00:00
|
|
|
static void
|
2007-11-01 11:11:22 +00:00
|
|
|
ev_clear_pending (W w)
|
2007-10-31 13:57:34 +00:00
|
|
|
{
|
|
|
|
if (w->pending)
|
|
|
|
{
|
|
|
|
pendings [w->pending - 1].w = 0;
|
|
|
|
w->pending = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
static void
|
2007-10-31 07:36:03 +00:00
|
|
|
ev_start (W w, int active)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
|
|
|
w->active = active;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-10-31 07:36:03 +00:00
|
|
|
ev_stop (W w)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
|
|
|
w->active = 0;
|
|
|
|
}
|
|
|
|
|
2007-10-31 00:32:33 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
void
|
2007-11-01 06:48:49 +00:00
|
|
|
ev_io_start (struct ev_io *w)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-11-01 13:33:12 +00:00
|
|
|
int fd = w->fd;
|
|
|
|
|
2007-10-30 20:59:31 +00:00
|
|
|
if (ev_is_active (w))
|
|
|
|
return;
|
|
|
|
|
2007-11-01 11:11:22 +00:00
|
|
|
assert (("ev_io_start called with negative fd", fd >= 0));
|
|
|
|
|
2007-10-31 07:36:03 +00:00
|
|
|
ev_start ((W)w, 1);
|
2007-10-30 20:59:31 +00:00
|
|
|
array_needsize (anfds, anfdmax, fd + 1, anfds_init);
|
2007-10-31 07:36:03 +00:00
|
|
|
wlist_add ((WL *)&anfds[fd].head, (WL)w);
|
2007-10-30 20:59:31 +00:00
|
|
|
|
2007-10-31 22:16:36 +00:00
|
|
|
fd_change (fd);
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-11-01 06:48:49 +00:00
|
|
|
ev_io_stop (struct ev_io *w)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
2007-11-01 11:11:22 +00:00
|
|
|
ev_clear_pending ((W)w);
|
2007-10-30 20:59:31 +00:00
|
|
|
if (!ev_is_active (w))
|
|
|
|
return;
|
|
|
|
|
2007-10-31 07:36:03 +00:00
|
|
|
wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
|
|
|
|
ev_stop ((W)w);
|
2007-10-30 20:59:31 +00:00
|
|
|
|
2007-10-31 22:16:36 +00:00
|
|
|
fd_change (w->fd);
|
2007-10-30 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-11-01 06:48:49 +00:00
|
|
|
ev_timer_start (struct ev_timer *w)
|
2007-10-30 20:59:31 +00:00
|
|
|
{
|
|
|
|
if (ev_is_active (w))
|
|
|
|
return;
|
|
|
|
|
2007-10-31 09:23:17 +00:00
|
|
|
w->at += now;
|
2007-10-30 20:59:31 +00:00
|
|
|
|
2007-11-01 11:11:22 +00:00
|
|
|
assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
|
2007-10-31 10:50:05 +00:00
|
|
|
|
2007-10-31 09:23:17 +00:00
|
|
|
ev_start ((W)w, ++timercnt);
|
|
|
|
array_needsize (timers, timermax, timercnt, );
|
|