Mirror of :pserver:anonymous@cvs.schmorp.de/schmorpforge libev http://software.schmorp.de/pkg/libev.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2547 lines
53 KiB

14 years ago
/*
* libev event processing core, watcher management
*
* Copyright (c) 2007,2008 Marc Alexander Lehmann <libev@schmorp.de>
14 years ago
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
* CIAL, 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 OTH-
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
14 years ago
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License ("GPL") version 2 or any later version,
* in which case the provisions of the GPL are applicable instead of
* the above. If you wish to allow the use of your version of this file
* only under the terms of the GPL and not to allow others to use your
* version of this file under the BSD license, indicate your decision
* by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete the
* provisions above, a recipient may use your version of this file under
* either the BSD or the GPL.
14 years ago
*/
#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_NANOSLEEP
# if HAVE_NANOSLEEP
# define EV_USE_NANOSLEEP 1
# else
# define EV_USE_NANOSLEEP 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
# ifndef EV_USE_INOTIFY
# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
# define EV_USE_INOTIFY 1
# else
# define EV_USE_INOTIFY 0
# endif
# endif
#endif
14 years ago
#include <math.h>
#include <stdlib.h>
14 years ago
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
14 years ago
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
#ifdef EV_H
# include EV_H
#else
# include "ev.h"
#endif
#ifndef _WIN32
# include <sys/time.h>
14 years ago
# 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
14 years ago
#endif
/**/
#ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 0
#endif
#ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
#endif
#ifndef EV_USE_NANOSLEEP
# define EV_USE_NANOSLEEP 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 EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
#endif
#ifndef EV_PID_HASHSIZE
# if EV_MINIMAL
# define EV_PID_HASHSIZE 1
# else
# define EV_PID_HASHSIZE 16
# endif
#endif
#ifndef EV_INOTIFY_HASHSIZE
# if EV_MINIMAL
# define EV_INOTIFY_HASHSIZE 1
# else
# define EV_INOTIFY_HASHSIZE 16
# endif
#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_STAT_ENABLE
14 years ago
# undef EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
#endif
#if !EV_USE_NANOSLEEP
# ifndef _WIN32
# include <sys/select.h>
# endif
#endif
#if EV_USE_INOTIFY
# include <sys/inotify.h>
#endif
14 years ago
#if EV_SELECT_IS_WINSOCKET
# include <winsock.h>
#endif
/**/
/*
* This is used to avoid floating point rounding problems.
* It is added to ev_rt_now when scheduling periodics
* to ensure progress, time-wise, even when rounding
* errors are against us.
* This value is good at least till the year 4000.
* Better solutions welcome.
*/
#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
14 years ago
#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 CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
14 years ago
#if __GNUC__ >= 4
# define expect(expr,value) __builtin_expect ((expr),(value))
14 years ago
# define noinline __attribute__ ((noinline))
#else
# define expect(expr,value) (expr)
# define noinline
14 years ago
# if __STDC_VERSION__ < 199901L
# define inline
# endif
#endif
#define expect_false(expr) expect ((expr) != 0, 0)
#define expect_true(expr) expect ((expr) != 0, 1)
14 years ago
#define inline_size static inline
#if EV_MINIMAL
# define inline_speed static noinline
#else
# define inline_speed static inline
#endif
#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
#define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
#define EMPTY /* 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;
#if EV_USE_MONOTONIC
/* sig_atomic_t is used to avoid per-thread variables or locking but still */
/* giving it a reasonably high chance of working on typical architetcures */
static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
#endif
#ifdef _WIN32
# include "ev_win32.c"
#endif
/*****************************************************************************/
14 years ago
static void (*syserr_cb)(const char *msg);
14 years ago
void
ev_set_syserr_cb (void (*cb)(const char *msg))
{
syserr_cb = cb;
}
14 years ago
static void noinline
14 years ago
syserr (const char *msg)
{
14 years ago
if (!msg)
msg = "(libev) system error";
if (syserr_cb)
14 years ago
syserr_cb (msg);
else
{
14 years ago
perror (msg);
abort ();
}
}
static void *(*alloc)(void *ptr, long size);
14 years ago
void
ev_set_allocator (void *(*cb)(void *ptr, long size))
{
alloc = cb;
}
14 years ago
inline_speed 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_USE_INOTIFY
typedef struct
{
WL head;
} ANFS;
#endif
#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
/*****************************************************************************/
14 years ago
ev_tstamp
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
void
ev_sleep (ev_tstamp delay)
{
if (delay > 0.)
{
#if EV_USE_NANOSLEEP
struct timespec ts;
ts.tv_sec = (time_t)delay;
ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9);
nanosleep (&ts, 0);
#elif defined(_WIN32)
Sleep (delay * 1e3);
#else
struct timeval tv;
tv.tv_sec = (time_t)delay;
tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
select (0, 0, 0, 0, &tv);
#endif
}
}
/*****************************************************************************/
int inline_size
array_nextsize (int elem, int cur, int cnt)
{
int ncur = cur + 1;
do
ncur <<= 1;
while (cnt > ncur);
/* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */
if (elem * ncur > 4096)
{
ncur *= elem;
ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095;
ncur = ncur - sizeof (void *) * 4;
ncur /= elem;
}
return ncur;
}
14 years ago
static noinline void *
array_realloc (int elem, void *base, int *cur, int cnt)
{
*cur = array_nextsize (elem, *cur, cnt);
return ev_realloc (base, elem * *cur);
}
#define array_needsize(type,base,cur,cnt,init) \
if (expect_false ((cnt) > (cur))) \
{ \
int ocur_ = (cur); \
(base) = (type *)array_realloc \
(sizeof (type), (base), &(cur), (cnt)); \
init ((base) + (ocur_), (cur) - ocur_); \
}
#if 0
#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*/\
}
#endif
#define array_free(stem, idx) \
ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
/*****************************************************************************/
void noinline
14 years ago
ev_feed_event (EV_P_ void *w, int revents)
{
14 years ago
W w_ = (W)w;
14 years ago
int pri = ABSPRI (w_);
14 years ago
if (expect_false (w_->pending))
14 years ago
pendings [pri][w_->pending - 1].events |= revents;
else
{
14 years ago
w_->pending = ++pendingcnt [pri];
array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
pendings [pri][w_->pending - 1].w = w_;
pendings [pri][w_->pending - 1].events = revents;
}
}
void inline_speed
queue_events (EV_P_ W *events, int eventcnt, int type)
{
int i;
for (i = 0; i < eventcnt; ++i)
14 years ago
ev_feed_event (EV_A_ events [i], type);
}
14 years ago
/*****************************************************************************/
void inline_size
anfds_init (ANFD *base, int count)
{
while (count--)
{
base->head = 0;
base->events = EV_NONE;
base->reify = 0;
++base;
}
}
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)
14 years ago
ev_feed_event (EV_A_ (W)w, ev);
}
}
void
ev_feed_fd_event (EV_P_ int fd, int revents)
{
if (fd >= 0 && fd < anfdmax)
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;
unsigned char events = 0;
for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
events |= (unsigned char)w->events;
#if EV_SELECT_IS_WINSOCKET
if (events)
{
unsigned long argp;
#ifdef EV_FD_TO_WIN32_HANDLE
anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
#else
anfd->handle = _get_osfhandle (fd);
#endif
assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
}
#endif
{
unsigned char o_events = anfd->events;
unsigned char o_reify = anfd->reify;
anfd->reify = 0;
anfd->events = events;
if (o_events != events || o_reify & EV_IOFDSET)
backend_modify (EV_A_ fd, o_events, events);
}
}
fdchangecnt = 0;
}
void inline_size
fd_change (EV_P_ int fd, int flags)
{
unsigned char reify = anfds [fd].reify;
anfds [fd].reify |= flags;
if (expect_true (!reify))
{
++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);
14 years ago
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
}
14 years ago
/* called on EBADF to verify fds */
static void noinline
fd_ebadf (EV_P)
14 years ago
{
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;
}
14 years ago
}
/* usually called after fork if backend needs to re-arm all fds from scratch */
static void noinline
fd_rearm_all (EV_P)
{
int fd;
for (fd = 0; fd < anfdmax; ++fd)
if (anfds [fd].events)
{
anfds [fd].events = 0;
fd_change (EV_A_ fd, EV_IOFDSET | 1);
}
}
/*****************************************************************************/
void inline_speed
upheap (WT *heap, int k)
{
WT w = heap [k];
while (k)
{
int p = (k - 1) >> 1;
if (heap [p]->at <= w->at)
break;
heap [k] = heap [p];
((W)heap [k])->active = k + 1;
k = p;
}
heap [k] = w;
((W)heap [k])->active = k + 1;
}
void inline_speed
downheap (WT *heap, int N, int k)
{
WT w = heap [k];
for (;;)
{
int c = (k << 1) + 1;
if (c >= N)
break;
c += c + 1 < N && heap [c]->at > heap [c + 1]->at
? 1 : 0;
if (w->at <= heap [c]->at)