You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libev/ev.c

4051 lines
90 KiB
C

16 years ago
/*
* libev event processing core, watcher management
*
* Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de>
16 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.
16 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.
16 years ago
*/
/* this big block deduces configuration from config.h */
#ifndef EV_STANDALONE
# ifdef EV_CONFIG_H
# include EV_CONFIG_H
# else
# include "config.h"
# endif
#if HAVE_FLOOR
# ifndef EV_USE_FLOOR
# define EV_USE_FLOOR 1
# endif
#endif
# if HAVE_CLOCK_SYSCALL
# ifndef EV_USE_CLOCK_SYSCALL
# define EV_USE_CLOCK_SYSCALL 1
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
# endif
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 1
# endif
# endif
# elif !defined(EV_USE_CLOCK_SYSCALL)
# define EV_USE_CLOCK_SYSCALL 0
# endif
# if HAVE_CLOCK_GETTIME
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 1
# endif
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
# endif
# else
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 0
# endif
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
# endif
# endif
# if HAVE_NANOSLEEP
# ifndef EV_USE_NANOSLEEP
# define EV_USE_NANOSLEEP EV_FEATURE_OS
# endif
# else
# undef EV_USE_NANOSLEEP
# define EV_USE_NANOSLEEP 0
# endif
# if HAVE_SELECT && HAVE_SYS_SELECT_H
# ifndef EV_USE_SELECT
# define EV_USE_SELECT EV_FEATURE_BACKENDS
# endif
# else
# undef EV_USE_SELECT
# define EV_USE_SELECT 0
# endif
# if HAVE_POLL && HAVE_POLL_H
# ifndef EV_USE_POLL
# define EV_USE_POLL EV_FEATURE_BACKENDS
# endif
# else
# undef EV_USE_POLL
# define EV_USE_POLL 0
# endif
# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
# ifndef EV_USE_EPOLL
# define EV_USE_EPOLL EV_FEATURE_BACKENDS
# endif
# else
# undef EV_USE_EPOLL
# define EV_USE_EPOLL 0
# endif
# if HAVE_KQUEUE && HAVE_SYS_EVENT_H
# ifndef EV_USE_KQUEUE
# define EV_USE_KQUEUE EV_FEATURE_BACKENDS
# endif
# else
# undef EV_USE_KQUEUE
# define EV_USE_KQUEUE 0
# endif
# if HAVE_PORT_H && HAVE_PORT_CREATE
# ifndef EV_USE_PORT
# define EV_USE_PORT EV_FEATURE_BACKENDS
# endif
# else
# undef EV_USE_PORT
# define EV_USE_PORT 0
# endif
# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
# ifndef EV_USE_INOTIFY
# define EV_USE_INOTIFY EV_FEATURE_OS
# endif
# else
# undef EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
# endif
# if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H
# ifndef EV_USE_SIGNALFD
# define EV_USE_SIGNALFD EV_FEATURE_OS
# endif
# else
# undef EV_USE_SIGNALFD
# define EV_USE_SIGNALFD 0
# endif
# if HAVE_EVENTFD
# ifndef EV_USE_EVENTFD
# define EV_USE_EVENTFD EV_FEATURE_OS
# endif
# else
# undef EV_USE_EVENTFD
# define EV_USE_EVENTFD 0
# endif
#endif
16 years ago
#include <stdlib.h>
#include <string.h>
16 years ago
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
16 years ago
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#include <time.h>
#include <limits.h>
#include <signal.h>
#ifdef EV_H
# include EV_H
#else
# include "ev.h"
#endif
EV_CPP(extern "C" {)
#ifndef _WIN32
# include <sys/time.h>
16 years ago
# include <sys/wait.h>
# include <unistd.h>
#else
# include <io.h>
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# ifndef EV_SELECT_IS_WINSOCKET
# define EV_SELECT_IS_WINSOCKET 1
# endif
13 years ago
# undef EV_AVOID_STDIO
16 years ago
#endif
/* OS X, in its infinite idiocy, actually HARDCODES
* a limit of 1024 into their select. Where people have brains,
* OS X engineers apparently have a vacuum. Or maybe they were
* ordered to have a vacuum, or they do anything for money.
* This might help. Or not.
*/
#define _DARWIN_UNLIMITED_SELECT 1
/* this block tries to deduce configuration from header-defined symbols and defaults */
/* try to deduce the maximum number of signals on this platform */
#if defined (EV_NSIG)
/* use what's provided */
#elif defined (NSIG)
# define EV_NSIG (NSIG)
#elif defined(_NSIG)
# define EV_NSIG (_NSIG)
#elif defined (SIGMAX)
# define EV_NSIG (SIGMAX+1)
#elif defined (SIG_MAX)
# define EV_NSIG (SIG_MAX+1)
#elif defined (_SIG_MAX)
# define EV_NSIG (_SIG_MAX+1)
#elif defined (MAXSIG)
# define EV_NSIG (MAXSIG+1)
#elif defined (MAX_SIG)
# define EV_NSIG (MAX_SIG+1)
#elif defined (SIGARRAYSIZE)
# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
#elif defined (_sys_nsig)
# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
#else
# error "unable to find value for NSIG, please report"
/* to make it compile regardless, just remove the above line, */
/* but consider reporting it, too! :) */
# define EV_NSIG 65
#endif
#ifndef EV_USE_FLOOR
# define EV_USE_FLOOR 0
#endif
#ifndef EV_USE_CLOCK_SYSCALL
# if __linux && __GLIBC__ >= 2
# define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS
# else
# define EV_USE_CLOCK_SYSCALL 0
# endif
#endif
#ifndef EV_USE_MONOTONIC
# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
# define EV_USE_MONOTONIC EV_FEATURE_OS
# else
# define EV_USE_MONOTONIC 0
# endif
#endif
#ifndef EV_USE_REALTIME
# define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL
#endif
#ifndef EV_USE_NANOSLEEP
# if _POSIX_C_SOURCE >= 199309L
# define EV_USE_NANOSLEEP EV_FEATURE_OS
# else
# define EV_USE_NANOSLEEP 0
# endif
#endif
#ifndef EV_USE_SELECT
# define EV_USE_SELECT EV_FEATURE_BACKENDS
#endif
#ifndef EV_USE_POLL
# ifdef _WIN32
# define EV_USE_POLL 0
# else
# define EV_USE_POLL EV_FEATURE_BACKENDS
# endif
#endif
#ifndef EV_USE_EPOLL
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
# define EV_USE_EPOLL EV_FEATURE_BACKENDS
# else
# define EV_USE_EPOLL 0
# endif
#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
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
# define EV_USE_INOTIFY EV_FEATURE_OS
# else
# define EV_USE_INOTIFY 0
# endif
#endif
#ifndef EV_PID_HASHSIZE
# define EV_PID_HASHSIZE EV_FEATURE_DATA ? 16 : 1
#endif
#ifndef EV_INOTIFY_HASHSIZE
# define EV_INOTIFY_HASHSIZE EV_FEATURE_DATA ? 16 : 1
#endif
#ifndef EV_USE_EVENTFD
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
# define EV_USE_EVENTFD EV_FEATURE_OS
# else
# define EV_USE_EVENTFD 0
# endif
#endif
#ifndef EV_USE_SIGNALFD
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
# define EV_USE_SIGNALFD EV_FEATURE_OS
# else
# define EV_USE_SIGNALFD 0
# endif
#endif
#if 0 /* debugging */
# define EV_VERIFY 3
# define EV_USE_4HEAP 1
# define EV_HEAP_CACHE_AT 1
#endif
#ifndef EV_VERIFY
# define EV_VERIFY (EV_FEATURE_API ? 1 : 0)
#endif
#ifndef EV_USE_4HEAP
# define EV_USE_4HEAP EV_FEATURE_DATA
#endif
#ifndef EV_HEAP_CACHE_AT
# define EV_HEAP_CACHE_AT EV_FEATURE_DATA
#endif
/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
/* which makes programs even slower. might work on other unices, too. */
#if EV_USE_CLOCK_SYSCALL
# include <syscall.h>
# ifdef SYS_clock_gettime
# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
# undef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 1
# else
# undef EV_USE_CLOCK_SYSCALL
# define EV_USE_CLOCK_SYSCALL 0
# endif
#endif
/* this block fixes any misconfiguration where we know we run into trouble otherwise */
#ifdef _AIX
/* AIX has a completely broken poll.h header */
# undef EV_USE_POLL
# define EV_USE_POLL 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_STAT_ENABLE
16 years ago
# undef EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
#endif
#if !EV_USE_NANOSLEEP
/* hp-ux has it in sys/time.h, which we unconditionally include above */
# if !defined(_WIN32) && !defined(__hpux)
# include <sys/select.h>
# endif
#endif
#if EV_USE_INOTIFY
# include <sys/statfs.h>
# include <sys/inotify.h>
/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
# ifndef IN_DONT_FOLLOW
# undef EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
# endif
#endif
16 years ago
#if EV_SELECT_IS_WINSOCKET
# include <winsock.h>
#endif
#if EV_USE_EVENTFD
/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
# include <stdint.h>
# ifndef EFD_NONBLOCK
# define EFD_NONBLOCK O_NONBLOCK
# endif
# ifndef EFD_CLOEXEC
# ifdef O_CLOEXEC
# define EFD_CLOEXEC O_CLOEXEC
# else
# define EFD_CLOEXEC 02000000
# endif
# endif
EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
#endif
#if EV_USE_SIGNALFD
/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
# include <stdint.h>
# ifndef SFD_NONBLOCK
# define SFD_NONBLOCK O_NONBLOCK
# endif
# ifndef SFD_CLOEXEC
# ifdef O_CLOEXEC
# define SFD_CLOEXEC O_CLOEXEC
# else
# define SFD_CLOEXEC 02000000
# endif
# endif
EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags);
struct signalfd_siginfo
{
uint32_t ssi_signo;
char pad[128 - sizeof (uint32_t)];
};
#endif
/**/
#if EV_VERIFY >= 3
# define EV_FREQUENT_CHECK ev_verify (EV_A)
#else
# define EV_FREQUENT_CHECK do { } while (0)
#endif
/*
* This is used to work around floating point rounding problems.
* This value is good at least till the year 4000.
*/
#define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */
/*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */
16 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 EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
13 years ago
#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
12 years ago
/* the following are taken from libecb */
/* ecb.h start */
/* many compilers define _GNUC_ to some versions but then only implement
* what their idiot authors think are the "more important" extensions,
* causing enourmous grief in return for some better fake benchmark numbers.
* or so.
* we try to detect these and simply assume they are not gcc - if they have
* an issue with that they should have done it right in the first place.
*/
#ifndef ECB_GCC_VERSION
#if !defined(__GNUC_MINOR__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__)
#define ECB_GCC_VERSION(major,minor) 0
#else
#define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
#endif
#endif
#if __cplusplus
#define ecb_inline static inline
#elif ECB_GCC_VERSION(2,5)
#define ecb_inline static __inline__
#elif ECB_C99
#define ecb_inline static inline
#else
12 years ago
#define ecb_inline static
#endif
12 years ago
#if ECB_GCC_VERSION(3,1)
#define ecb_attribute(attrlist) __attribute__(attrlist)
#define ecb_is_constant(expr) __builtin_constant_p (expr)
#define ecb_expect(expr,value) __builtin_expect ((expr),(value))
#define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
#else
#define ecb_attribute(attrlist)
#define ecb_is_constant(expr) 0
#define ecb_expect(expr,value) (expr)
#define ecb_prefetch(addr,rw,locality)
#endif
#define ecb_noinline ecb_attribute ((__noinline__))
#define ecb_noreturn ecb_attribute ((__noreturn__))
#define ecb_unused ecb_attribute ((__unused__))
#define ecb_const ecb_attribute ((__const__))
#define ecb_pure ecb_attribute ((__pure__))
#if ECB_GCC_VERSION(4,3)
#define ecb_artificial ecb_attribute ((__artificial__))
#define ecb_hot ecb_attribute ((__hot__))
#define ecb_cold ecb_attribute ((__cold__))
#else
#define ecb_artificial
#define ecb_hot
#define ecb_cold
#endif
/* put around conditional expressions if you are very sure that the */
/* expression is mostly true or mostly false. note that these return */
/* booleans, not the expression. */
#define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
#define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
/* ecb.h end */
#define expect_false(cond) ecb_expect_false (cond)
#define expect_true(cond) ecb_expect_true (cond)
#define noinline ecb_noinline
#define inline_size ecb_inline
16 years ago
#if EV_FEATURE_CODE
12 years ago
# define inline_speed ecb_inline
#else
# define inline_speed static noinline
16 years ago
#endif
#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
#if EV_MINPRI == EV_MAXPRI
# define ABSPRI(w) (((W)w), 0)
#else
# define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
#endif
#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;
#define ev_active(w) ((W)(w))->active
#define ev_at(w) ((WT)(w))->at
#if EV_USE_REALTIME
/* sig_atomic_t is used to avoid per-thread variables or locking but still */
13 years ago
/* giving it a reasonably high chance of working on typical architectures */
static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
#endif
#if EV_USE_MONOTONIC
static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
#endif
#ifndef EV_FD_TO_WIN32_HANDLE
# define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
#endif
#ifndef EV_WIN32_HANDLE_TO_FD
# define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0)
#endif
#ifndef EV_WIN32_CLOSE_FD
# define EV_WIN32_CLOSE_FD(fd) close (fd)
#endif
#ifdef _WIN32
# include "ev_win32.c"
#endif
/*****************************************************************************/
/* define a suitable floor function (only used by periodics atm) */
#if EV_USE_FLOOR
# include <math.h>
# define ev_floor(v) floor (v)
#else
#include <float.h>
/* a floor() replacement function, should be independent of ev_tstamp type */
static ev_tstamp noinline
ev_floor (ev_tstamp v)
{
/* the choice of shift factor is not terribly important */
#if FLT_RADIX != 2 /* assume FLT_RADIX == 10 */
const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.;
#else
const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.;
#endif
/* argument too large for an unsigned long? */
if (expect_false (v >= shift))
{
ev_tstamp f;
if (v == v - 1.)
return v; /* very large number */
f = shift * ev_floor (v * (1. / shift));
return f + ev_floor (v - f);
}
/* special treatment for negative args? */
if (expect_false (v < 0.))
{
ev_tstamp f = -ev_floor (-v);
return f - (f == v ? 0 : 1);
}
/* fits into an unsigned long */
return (unsigned long)v;
}
#endif
/*****************************************************************************/
#ifdef __linux
# include <sys/utsname.h>
#endif
12 years ago
static unsigned int noinline ecb_cold
ev_linux_version (void)
{
#ifdef __linux
unsigned int v = 0;
struct utsname buf;
int i;
char *p = buf.release;
if (uname (&buf))
return 0;
for (i = 3+1; --i; )
{
unsigned int c = 0;
for (;;)
{
if (*p >= '0' && *p <= '9')
c = c * 10 + *p++ - '0';
else
{
p += *p == '.';
break;
}
}
v = (v << 8) | c;
}
return v;
#else
return 0;
#endif
}
/*****************************************************************************/
13 years ago
#if EV_AVOID_STDIO
12 years ago
static void noinline ecb_cold
13 years ago
ev_printerr (const char *msg)
{
write (STDERR_FILENO, msg, strlen (msg));
}
#endif
16 years ago
static void (*syserr_cb)(const char *msg);
12 years ago
void ecb_cold
16 years ago
ev_set_syserr_cb (void (*cb)(const char *msg))
{
syserr_cb = cb;
}
12 years ago
static void noinline ecb_cold
ev_syserr (const char *msg)
{
16 years ago
if (!msg)
msg = "(libev) system error";
if (syserr_cb)
16 years ago
syserr_cb (msg);
else
{
13 years ago
#if EV_AVOID_STDIO
13 years ago
ev_printerr (msg);
ev_printerr (": ");
ev_printerr (strerror (errno));
13 years ago
ev_printerr ("\n");
13 years ago
#else
16 years ago
perror (msg);
13 years ago
#endif
abort ();
}
}
static void *
ev_realloc_emul (void *ptr, long size)
{
13 years ago
#if __GLIBC__
return realloc (ptr, size);
#else
/* some systems, notably openbsd and darwin, fail to properly
13 years ago
* implement realloc (x, 0) (as required by both ansi c-89 and
* the single unix specification, so work around them here.
*/
13 years ago
if (size)
return realloc (ptr, size);
free (ptr);
return 0;
13 years ago
#endif
}
static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
12 years ago
void ecb_cold
ev_set_allocator (void *(*cb)(void *ptr, long size))
{
alloc = cb;
}
16 years ago
inline_speed void *
ev_realloc (void *ptr, long size)
{
ptr = alloc (ptr, size);
if (!ptr && size)
{
13 years ago
#if EV_AVOID_STDIO
ev_printerr ("(libev) memory allocation failed, aborting.\n");
13 years ago
#else