/*
* libev event processing core, watcher management
*
- * Copyright (c) 2007,2008,2009 Marc Alexander Lehmann <libev@schmorp.de>
+ * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
# endif
# endif
-# ifndef EV_USE_NANOSLEEP
-# if HAVE_NANOSLEEP
-# define EV_USE_NANOSLEEP 1
-# else
-# define EV_USE_NANOSLEEP 0
+# 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
-# ifndef EV_USE_SELECT
-# if HAVE_SELECT && HAVE_SYS_SELECT_H
-# define EV_USE_SELECT 1
-# else
-# define EV_USE_SELECT 0
+# 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
-# ifndef EV_USE_POLL
-# if HAVE_POLL && HAVE_POLL_H
-# define EV_USE_POLL 1
-# else
-# define EV_USE_POLL 0
+# 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
-# ifndef EV_USE_EPOLL
-# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
-# define EV_USE_EPOLL 1
-# else
-# define EV_USE_EPOLL 0
+# 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
-# 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
+# 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
-# ifndef EV_USE_PORT
-# if HAVE_PORT_H && HAVE_PORT_CREATE
-# define EV_USE_PORT 1
-# else
-# define EV_USE_PORT 0
+# 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
-# ifndef EV_USE_INOTIFY
-# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
-# define EV_USE_INOTIFY 1
-# else
-# define EV_USE_INOTIFY 0
+# 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
-# ifndef EV_USE_SIGNALFD
-# if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H
-# define EV_USE_SIGNALFD 1
-# else
-# define EV_USE_SIGNALFD 0
+# 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
-# ifndef EV_USE_EVENTFD
-# if HAVE_EVENTFD
-# define EV_USE_EVENTFD 1
-# else
-# define EV_USE_EVENTFD 0
+# 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
#include <errno.h>
#include <sys/types.h>
#include <time.h>
+#include <limits.h>
#include <signal.h>
# ifndef EV_SELECT_IS_WINSOCKET
# define EV_SELECT_IS_WINSOCKET 1
# endif
+# undef EV_AVOID_STDIO
#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 */
#elif defined (MAX_SIG)
# define EV_NSIG (MAX_SIG+1)
#elif defined (SIGARRAYSIZE)
-# define EV_NSIG SIGARRAYSIZE /* Assume ary[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 */
+/* to make it compile regardless, just remove the above line, */
+/* but consider reporting it, too! :) */
# define EV_NSIG 65
#endif
#ifndef EV_USE_CLOCK_SYSCALL
# if __linux && __GLIBC__ >= 2
-# define EV_USE_CLOCK_SYSCALL 1
+# define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS
# else
# define EV_USE_CLOCK_SYSCALL 0
# endif
#ifndef EV_USE_MONOTONIC
# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
-# define EV_USE_MONOTONIC 1
+# define EV_USE_MONOTONIC EV_FEATURE_OS
# else
# define EV_USE_MONOTONIC 0
# endif
#ifndef EV_USE_NANOSLEEP
# if _POSIX_C_SOURCE >= 199309L
-# define EV_USE_NANOSLEEP 1
+# define EV_USE_NANOSLEEP EV_FEATURE_OS
# else
# define EV_USE_NANOSLEEP 0
# endif
#endif
#ifndef EV_USE_SELECT
-# define EV_USE_SELECT 1
+# define EV_USE_SELECT EV_FEATURE_BACKENDS
#endif
#ifndef EV_USE_POLL
# ifdef _WIN32
# define EV_USE_POLL 0
# else
-# define EV_USE_POLL 1
+# 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 1
+# define EV_USE_EPOLL EV_FEATURE_BACKENDS
# else
# define EV_USE_EPOLL 0
# endif
#ifndef EV_USE_INOTIFY
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
-# define EV_USE_INOTIFY 1
+# define EV_USE_INOTIFY EV_FEATURE_OS
# else
# define EV_USE_INOTIFY 0
# endif
#endif
#ifndef EV_PID_HASHSIZE
-# if EV_MINIMAL
-# define EV_PID_HASHSIZE 1
-# else
-# define EV_PID_HASHSIZE 16
-# endif
+# define EV_PID_HASHSIZE EV_FEATURE_DATA ? 16 : 1
#endif
#ifndef EV_INOTIFY_HASHSIZE
-# if EV_MINIMAL
-# define EV_INOTIFY_HASHSIZE 1
-# else
-# define EV_INOTIFY_HASHSIZE 16
-# endif
+# 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 1
+# define EV_USE_EVENTFD EV_FEATURE_OS
# else
# define EV_USE_EVENTFD 0
# endif
#ifndef EV_USE_SIGNALFD
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
-# define EV_USE_SIGNALFD 1
+# define EV_USE_SIGNALFD EV_FEATURE_OS
# else
# define EV_USE_SIGNALFD 0
# endif
#endif
#ifndef EV_VERIFY
-# define EV_VERIFY !EV_MINIMAL
+# define EV_VERIFY (EV_FEATURE_API ? 1 : 0)
#endif
#ifndef EV_USE_4HEAP
-# define EV_USE_4HEAP !EV_MINIMAL
+# define EV_USE_4HEAP EV_FEATURE_DATA
#endif
#ifndef EV_HEAP_CACHE_AT
-# define EV_HEAP_CACHE_AT !EV_MINIMAL
+# define EV_HEAP_CACHE_AT EV_FEATURE_DATA
#endif
/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
/* 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
# ifdef __cplusplus
extern "C" {
# endif
-int eventfd (unsigned int initval, int flags);
+int (eventfd) (unsigned int initval, int flags);
# ifdef __cplusplus
}
# endif
# endif
#endif
-
/**/
#if EV_VERIFY >= 3
-# define EV_FREQUENT_CHECK ev_loop_verify (EV_A)
+# define EV_FREQUENT_CHECK ev_verify (EV_A)
#else
# define EV_FREQUENT_CHECK do { } while (0)
#endif
#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
+#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
+#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
+
#if __GNUC__ >= 4
# define expect(expr,value) __builtin_expect ((expr),(value))
# define noinline __attribute__ ((noinline))
#define expect_true(expr) expect ((expr) != 0, 1)
#define inline_size static inline
-#if EV_MINIMAL
-# define inline_speed static noinline
-#else
+#if EV_FEATURE_CODE
# define inline_speed static inline
+#else
+# define inline_speed static noinline
#endif
#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
#if EV_USE_REALTIME
/* 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 */
+/* giving it a reasonably high chance of working on typical architectures */
static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
#endif
# 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 (fd, 0)
+# 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)
/*****************************************************************************/
+#if EV_AVOID_STDIO
+static void noinline
+ev_printerr (const char *msg)
+{
+ write (STDERR_FILENO, msg, strlen (msg));
+}
+#endif
+
static void (*syserr_cb)(const char *msg);
void
syserr_cb (msg);
else
{
+#if EV_AVOID_STDIO
+ const char *err = strerror (errno);
+
+ ev_printerr (msg);
+ ev_printerr (": ");
+ ev_printerr (err);
+ ev_printerr ("\n");
+#else
perror (msg);
+#endif
abort ();
}
}
static void *
ev_realloc_emul (void *ptr, long size)
{
+#if __GLIBC__
+ return realloc (ptr, size);
+#else
/* some systems, notably openbsd and darwin, fail to properly
- * implement realloc (x, 0) (as required by both ansi c-98 and
+ * implement realloc (x, 0) (as required by both ansi c-89 and
* the single unix specification, so work around them here.
*/
free (ptr);
return 0;
+#endif
}
static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
if (!ptr && size)
{
+#if EV_AVOID_STDIO
+ ev_printerr ("libev: memory allocation failed, aborting.\n");
+#else
fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
+#endif
abort ();
}
#endif
-#if EV_MINIMAL < 2
+#if EV_FEATURE_API
# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A)
# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A)
# define EV_INVOKE_PENDING invoke_cb (EV_A)
# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
#endif
-#define EVUNLOOP_RECURSE 0x80
+#define EVBREAK_RECURSE 0x80
/*****************************************************************************/
#if EV_USE_NANOSLEEP
struct timespec ts;
- ts.tv_sec = (time_t)delay;
- ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9);
-
+ EV_TS_SET (ts, delay);
nanosleep (&ts, 0);
#elif defined(_WIN32)
Sleep ((unsigned long)(delay * 1e3));
#else
struct timeval tv;
- tv.tv_sec = (time_t)delay;
- tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
-
/* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
/* something not guaranteed by newer posix versions, but guaranteed */
/* by older ones */
+ EV_TV_SET (tv, delay);
select (0, 0, 0, 0, &tv);
#endif
}
#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
/* find a suitable new size for the given array, */
-/* hopefully by rounding to a ncie-to-malloc size */
+/* hopefully by rounding to a nice-to-malloc size */
inline_size int
array_nextsize (int elem, int cur, int cnt)
{
/*****************************************************************************/
inline_speed void
-fd_event_nc (EV_P_ int fd, int revents)
+fd_event_nocheck (EV_P_ int fd, int revents)
{
ANFD *anfd = anfds + fd;
ev_io *w;
ANFD *anfd = anfds + fd;
if (expect_true (!anfd->reify))
- fd_event_nc (EV_A_ fd, revents);
+ fd_event_nocheck (EV_A_ fd, revents);
}
void
ev_feed_fd_event (EV_P_ int fd, int revents)
{
if (fd >= 0 && fd < anfdmax)
- fd_event_nc (EV_A_ fd, revents);
+ fd_event_nocheck (EV_A_ fd, revents);
}
/* make sure the external fd watch events are in-sync */
ANFD *anfd = anfds + fd;
ev_io *w;
- unsigned char events = 0;
+ unsigned char o_events = anfd->events;
+ unsigned char o_reify = anfd->reify;
- for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
- events |= (unsigned char)w->events;
+ anfd->reify = 0;
#if EV_SELECT_IS_WINSOCKET
- if (events)
+ if (o_reify & EV__IOFDSET)
{
unsigned long arg;
anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
}
#endif
- {
- unsigned char o_events = anfd->events;
- unsigned char o_reify = anfd->reify;
+ /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
+ {
+ anfd->events = 0;
- anfd->reify = 0;
- anfd->events = events;
+ for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
+ anfd->events |= (unsigned char)w->events;
- if (o_events != events || o_reify & EV__IOFDSET)
- backend_modify (EV_A_ fd, o_events, events);
- }
+ if (o_events != anfd->events)
+ o_reify = EV__IOFDSET; /* actually |= */
+ }
+
+ if (o_reify & EV__IOFDSET)
+ backend_modify (EV_A_ fd, o_events, anfd->events);
}
fdchangecnt = 0;
}
}
-/* check whether the given fd is atcually valid, for error recovery */
+/* check whether the given fd is actually valid, for error recovery */
inline_size int
fd_valid (int fd)
{
#ifdef _WIN32
- return _get_osfhandle (fd) != -1;
+ return EV_FD_TO_WIN32_HANDLE (fd) != -1;
#else
return fcntl (fd, F_GETFD) != -1;
#endif
}
}
+/* used to prepare libev internal fd's */
+/* this is not fork-safe */
+inline_speed void
+fd_intern (int fd)
+{
+#ifdef _WIN32
+ unsigned long arg = 1;
+ ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
+#else
+ fcntl (fd, F_SETFD, FD_CLOEXEC);
+ fcntl (fd, F_SETFL, O_NONBLOCK);
+#endif
+}
+
/*****************************************************************************/
/*
- * the heap functions want a real array index. array index 0 uis guaranteed to not
+ * the heap functions want a real array index. array index 0 is guaranteed to not
* be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
* the branching factor of the d-tree.
*/
/*****************************************************************************/
-/* used to prepare libev internal fd's */
-/* this is not fork-safe */
-inline_speed void
-fd_intern (int fd)
-{
-#ifdef _WIN32
- unsigned long arg = 1;
- ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
-#else
- fcntl (fd, F_SETFD, FD_CLOEXEC);
- fcntl (fd, F_SETFL, O_NONBLOCK);
-#endif
-}
+#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
static void noinline
evpipe_init (EV_P)
{
if (!ev_is_active (&pipe_w))
{
-#if EV_USE_EVENTFD
+# if EV_USE_EVENTFD
evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
if (evfd < 0 && errno == EINVAL)
evfd = eventfd (0, 0);
ev_io_set (&pipe_w, evfd, EV_READ);
}
else
-#endif
+# endif
{
while (pipe (evpipe))
ev_syserr ("(libev) error creating signal/async pipe");
if (!*flag)
{
int old_errno = errno; /* save errno because write might clobber it */
+ char dummy;
*flag = 1;
}
else
#endif
- write (evpipe [1], &old_errno, 1);
+ /* win32 people keep sending patches that change this write() to send() */
+ /* and then run away. but send() is wrong, it wants a socket handle on win32 */
+ /* so when you think this write should be a send instead, please find out */
+ /* where your send() is from - it's definitely not the microsoft send, and */
+ /* tell me. thank you. */
+ write (evpipe [1], &dummy, 1);
errno = old_errno;
}
#endif
{
char dummy;
+ /* see discussion in evpipe_write when you think this read should be recv in win32 */
read (evpipe [0], &dummy, 1);
}
EV_P = signals [signum - 1].loop;
#endif
-#if _WIN32
+#ifdef _WIN32
signal (signum, ev_sighandler);
#endif
}
#endif
+#endif
+
/*****************************************************************************/
+#if EV_CHILD_ENABLE
static WL childs [EV_PID_HASHSIZE];
-#ifndef _WIN32
-
static ev_signal childev;
#ifndef WIFCONTINUED
ev_child *w;
int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
- for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
+ for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
{
if ((w->pid == pid || !w->pid)
&& (!traced || (w->flags & 1)))
ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
child_reap (EV_A_ pid, pid, status);
- if (EV_PID_HASHSIZE > 1)
+ if ((EV_PID_HASHSIZE) > 1)
child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
}
flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */
flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */
#endif
+#ifdef __FreeBSD__
+ flags &= ~EVBACKEND_POLL; /* poll return value is unusable (http://forums.freebsd.org/archive/index.php/t-10270.html) */
+#endif
return flags;
}
return backend;
}
-#if EV_MINIMAL < 2
+#if EV_FEATURE_API
unsigned int
-ev_loop_count (EV_P)
+ev_iteration (EV_P)
{
return loop_count;
}
unsigned int
-ev_loop_depth (EV_P)
+ev_depth (EV_P)
{
return loop_depth;
}
mn_now = get_clock ();
now_floor = mn_now;
rtmn_diff = ev_rt_now - mn_now;
-#if EV_MINIMAL < 2
+#if EV_FEATURE_API
invoke_cb = ev_invoke_pending;
#endif
ev_prepare_init (&pending_w, pendingcb);
+#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
ev_init (&pipe_w, pipecb);
ev_set_priority (&pipe_w, EV_MAXPRI);
+#endif
}
}
EV_WIN32_CLOSE_FD (evpipe [1]);
}
+#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
evpipe_init (EV_A);
/* now iterate over everything, in case we missed something */
pipecb (EV_A_ &pipe_w, EV_READ);
+#endif
}
postfork = 0;
}
#endif
-#if EV_MINIMAL < 2
+#if EV_FEATURE_API
void
-ev_loop_verify (EV_P)
+ev_verify (EV_P)
{
#if EV_VERIFY
int i;
array_verify (EV_A_ (W *)asyncs, asynccnt);
#endif
+#if EV_PREPARE_ENABLE
assert (preparemax >= preparecnt);
array_verify (EV_A_ (W *)prepares, preparecnt);
+#endif
+#if EV_CHECK_ENABLE
assert (checkmax >= checkcnt);
array_verify (EV_A_ (W *)checks, checkcnt);
+#endif
# if 0
- for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
+#if EV_CHILD_ENABLE
+ for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
+#endif
# endif
#endif
}
if (ev_backend (EV_A))
{
-#ifndef _WIN32
+#if EV_CHILD_ENABLE
ev_signal_init (&childev, childcb, SIGCHLD);
ev_set_priority (&childev, EV_MAXPRI);
ev_signal_start (EV_A_ &childev);
ev_default_loop_ptr = 0;
-#ifndef _WIN32
+#if EV_CHILD_ENABLE
ev_ref (EV_A); /* child watcher */
ev_signal_stop (EV_A_ &childev);
#endif
}
while (timercnt && ANHE_at (timers [HEAP0]) < mn_now);
- feed_reverse_done (EV_A_ EV_TIMEOUT);
+ feed_reverse_done (EV_A_ EV_TIMER);
}
}
}
/* simply recalculate all periodics */
-/* TODO: maybe ensure that at leats one event happens when jumping forward? */
+/* TODO: maybe ensure that at least one event happens when jumping forward? */
static void noinline
periodics_reschedule (EV_P)
{
}
/* fetch new monotonic and realtime times from the kernel */
-/* also detetc if there was a timejump, and act accordingly */
+/* also detect if there was a timejump, and act accordingly */
inline_speed void
time_update (EV_P_ ev_tstamp max_block)
{
}
void
-ev_loop (EV_P_ int flags)
+ev_run (EV_P_ int flags)
{
-#if EV_MINIMAL < 2
+#if EV_FEATURE_API
++loop_depth;
#endif
- assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
+ assert (("libev: ev_loop recursion during release detected", loop_done != EVBREAK_RECURSE));
- loop_done = EVUNLOOP_CANCEL;
+ loop_done = EVBREAK_CANCEL;
EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
do
{
#if EV_VERIFY >= 2
- ev_loop_verify (EV_A);
+ ev_verify (EV_A);
#endif
#ifndef _WIN32
}
#endif
+#if EV_PREPARE_ENABLE
/* queue prepare watchers (and execute them) */
if (expect_false (preparecnt))
{
queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
EV_INVOKE_PENDING;
}
+#endif
if (expect_false (loop_done))
break;
ev_tstamp waittime = 0.;
ev_tstamp sleeptime = 0.;
- if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
- {
- /* remember old timestamp for io_blocktime calculation */
- ev_tstamp prev_mn_now = mn_now;
+ /* remember old timestamp for io_blocktime calculation */
+ ev_tstamp prev_mn_now = mn_now;
- /* update time to cancel out callback processing overhead */
- time_update (EV_A_ 1e100);
+ /* update time to cancel out callback processing overhead */
+ time_update (EV_A_ 1e100);
+ if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt)))
+ {
waittime = MAX_BLOCKTIME;
if (timercnt)
}
}
-#if EV_MINIMAL < 2
+#if EV_FEATURE_API
++loop_count;
#endif
- assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
+ assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
backend_poll (EV_A_ waittime);
- assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
+ assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
/* update ev_rt_now, do magic */
time_update (EV_A_ waittime + sleeptime);
idle_reify (EV_A);
#endif
+#if EV_CHECK_ENABLE
/* queue check watchers, to be executed first */
if (expect_false (checkcnt))
queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
+#endif
EV_INVOKE_PENDING;
}
while (expect_true (
activecnt
&& !loop_done
- && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
+ && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT))
));
- if (loop_done == EVUNLOOP_ONE)
- loop_done = EVUNLOOP_CANCEL;
+ if (loop_done == EVBREAK_ONE)
+ loop_done = EVBREAK_CANCEL;
-#if EV_MINIMAL < 2
+#if EV_FEATURE_API
--loop_depth;
#endif
}
void
-ev_unloop (EV_P_ int how)
+ev_break (EV_P_ int how)
{
loop_done = how;
}
return;
assert (("libev: ev_io_start called with negative fd", fd >= 0));
- assert (("libev: ev_io start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE))));
+ assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE))));
EV_FREQUENT_CHECK;
wlist_del (&anfds[w->fd].head, (WL)w);
ev_stop (EV_A_ (W)w);
- fd_change (EV_A_ w->fd, 1);
+ fd_change (EV_A_ w->fd, EV_ANFD_REIFY);
EV_FREQUENT_CHECK;
}
}
}
- EV_FREQUENT_CHECK;
-
ev_at (w) -= mn_now;
ev_stop (EV_A_ (W)w);
+
+ EV_FREQUENT_CHECK;
}
void noinline
}
}
- EV_FREQUENT_CHECK;
-
ev_stop (EV_A_ (W)w);
+
+ EV_FREQUENT_CHECK;
}
void noinline
# define SA_RESTART 0
#endif
+#if EV_SIGNAL_ENABLE
+
void noinline
ev_signal_start (EV_P_ ev_signal *w)
{
if (sigfd < 0) /*TODO*/
# endif
{
-# if _WIN32
+# ifdef _WIN32
evpipe_init (EV_A);
signal (w->signum, ev_sighandler);
EV_FREQUENT_CHECK;
}
+#endif
+
+#if EV_CHILD_ENABLE
+
void
ev_child_start (EV_P_ ev_child *w)
{
EV_FREQUENT_CHECK;
ev_start (EV_A_ (W)w, 1);
- wlist_add (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
+ wlist_add (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
EV_FREQUENT_CHECK;
}
EV_FREQUENT_CHECK;
- wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
+ wlist_del (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
ev_stop (EV_A_ (W)w);
EV_FREQUENT_CHECK;
}
+#endif
+
#if EV_STAT_ENABLE
# ifdef _WIN32
static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
#if EV_USE_INOTIFY
-# define EV_INOTIFY_BUFSIZE 8192
+
+/* the * 2 is to allow for alignment padding, which for some reason is >> 8 */
+# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX)
static void noinline
infy_add (EV_P_ ev_stat *w)
}
if (w->wd >= 0)
- wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
+ wlist_add (&fs_hash [w->wd & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
/* now re-arm timer, if required */
if (ev_is_active (&w->timer)) ev_ref (EV_A);
return;
w->wd = -2;
- slot = wd & (EV_INOTIFY_HASHSIZE - 1);
+ slot = wd & ((EV_INOTIFY_HASHSIZE) - 1);
wlist_del (&fs_hash [slot].head, (WL)w);
/* remove this watcher, if others are watching it, they will rearm */
{
if (slot < 0)
/* overflow, need to check for all hash slots */
- for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
+ for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
infy_wd (EV_A_ slot, wd, ev);
else
{
WL w_;
- for (w_ = fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head; w_; )
+ for (w_ = fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head; w_; )
{
ev_stat *w = (ev_stat *)w_;
w_ = w_->next; /* lets us remove this watcher and all before it */
{
if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF))
{
- wlist_del (&fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
+ wlist_del (&fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
w->wd = -1;
infy_add (EV_A_ w); /* re-add, no matter what */
}
infy_cb (EV_P_ ev_io *w, int revents)
{
char buf [EV_INOTIFY_BUFSIZE];
- struct inotify_event *ev = (struct inotify_event *)buf;
int ofs;
int len = read (fs_fd, buf, sizeof (buf));
- for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len)
- infy_wd (EV_A_ ev->wd, ev->wd, ev);
+ for (ofs = 0; ofs < len; )
+ {
+ struct inotify_event *ev = (struct inotify_event *)(buf + ofs);
+ infy_wd (EV_A_ ev->wd, ev->wd, ev);
+ ofs += sizeof (struct inotify_event) + ev->len;
+ }
}
-inline_size void
-check_2625 (EV_P)
+inline_size unsigned int
+ev_linux_version (void)
{
- /* kernels < 2.6.25 are borked
- * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
- */
struct utsname buf;
- int major, minor, micro;
+ unsigned int v;
+ int i;
+ char *p = buf.release;
if (uname (&buf))
- return;
+ return 0;
- if (sscanf (buf.release, "%d.%d.%d", &major, &minor, µ) != 3)
- return;
+ 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;
+}
- if (major < 2
- || (major == 2 && minor < 6)
- || (major == 2 && minor == 6 && micro < 25))
+inline_size void
+ev_check_2625 (EV_P)
+{
+ /* kernels < 2.6.25 are borked
+ * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
+ */
+ if (ev_linux_version () < 0x020619)
return;
fs_2625 = 1;
fs_fd = -1;
- check_2625 (EV_A);
+ ev_check_2625 (EV_A);
fs_fd = infy_newfd ();
ev_unref (EV_A);
}
- for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
+ for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
{
WL w_ = fs_hash [slot].head;
fs_hash [slot].head = 0;
}
#endif
+#if EV_PREPARE_ENABLE
void
ev_prepare_start (EV_P_ ev_prepare *w)
{
EV_FREQUENT_CHECK;
}
+#endif
+#if EV_CHECK_ENABLE
void
ev_check_start (EV_P_ ev_check *w)
{
EV_FREQUENT_CHECK;
}
+#endif
#if EV_EMBED_ENABLE
void noinline
ev_embed_sweep (EV_P_ ev_embed *w)
{
- ev_loop (w->other, EVLOOP_NONBLOCK);
+ ev_run (w->other, EVRUN_NOWAIT);
}
static void
if (ev_cb (w))
ev_feed_event (EV_A_ (W)w, EV_EMBED);
else
- ev_loop (w->other, EVLOOP_NONBLOCK);
+ ev_run (w->other, EVRUN_NOWAIT);
}
static void
while (fdchangecnt)
{
fd_reify (EV_A);
- ev_loop (EV_A_ EVLOOP_NONBLOCK);
+ ev_run (EV_A_ EVRUN_NOWAIT);
}
}
}
EV_P = w->other;
ev_loop_fork (EV_A);
- ev_loop (EV_A_ EVLOOP_NONBLOCK);
+ ev_run (EV_A_ EVRUN_NOWAIT);
}
ev_embed_start (EV_A_ w);
ev_prepare_stop (EV_A_ &w->prepare);
ev_fork_stop (EV_A_ &w->fork);
+ ev_stop (EV_A_ (W)w);
+
EV_FREQUENT_CHECK;
}
#endif
if (expect_false (ev_is_active (w)))
return;
+ w->sent = 0;
+
evpipe_init (EV_A);
EV_FREQUENT_CHECK;
if (expect_false (!once))
{
- cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
+ cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMER, arg);
return;
}
cb (EV_A_ EV_ASYNC, asyncs [i]);
#endif
+#if EV_PREPARE_ENABLE
if (types & EV_PREPARE)
for (i = preparecnt; i--; )
-#if EV_EMBED_ENABLE
+# if EV_EMBED_ENABLE
if (ev_cb (prepares [i]) != embed_prepare_cb)
-#endif
+# endif
cb (EV_A_ EV_PREPARE, prepares [i]);
+#endif
+#if EV_CHECK_ENABLE
if (types & EV_CHECK)
for (i = checkcnt; i--; )
cb (EV_A_ EV_CHECK, checks [i]);
+#endif
+#if EV_SIGNAL_ENABLE
if (types & EV_SIGNAL)
for (i = 0; i < EV_NSIG - 1; ++i)
for (wl = signals [i].head; wl; )
cb (EV_A_ EV_SIGNAL, wl);
wl = wn;
}
+#endif
+#if EV_CHILD_ENABLE
if (types & EV_CHILD)
- for (i = EV_PID_HASHSIZE; i--; )
+ for (i = (EV_PID_HASHSIZE); i--; )
for (wl = childs [i]; wl; )
{
wn = wl->next;
cb (EV_A_ EV_CHILD, wl);
wl = wn;
}
+#endif
/* EV_STAT 0x00001000 /* stat data changed */
/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */
}
/*
* libev native API header
*
- * Copyright (c) 2007,2008,2009 Marc Alexander Lehmann <libev@schmorp.de>
+ * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
extern "C" {
#endif
-typedef double ev_tstamp;
+/*****************************************************************************/
+
+/* pre-4.0 compatibility */
+#ifndef EV_COMPAT3
+# define EV_COMPAT3 1
+#endif
+
+#ifndef EV_FEATURES
+# define EV_FEATURES 0x7f
+#endif
+
+#define EV_FEATURE_CODE ((EV_FEATURES) & 1)
+#define EV_FEATURE_DATA ((EV_FEATURES) & 2)
+#define EV_FEATURE_CONFIG ((EV_FEATURES) & 4)
+#define EV_FEATURE_API ((EV_FEATURES) & 8)
+#define EV_FEATURE_WATCHERS ((EV_FEATURES) & 16)
+#define EV_FEATURE_BACKENDS ((EV_FEATURES) & 32)
+#define EV_FEATURE_OS ((EV_FEATURES) & 64)
-/* these priorities are inclusive, higher priorities will be called earlier */
+/* these priorities are inclusive, higher priorities will be invoked earlier */
#ifndef EV_MINPRI
-# define EV_MINPRI -2
+# define EV_MINPRI (EV_FEATURE_CONFIG ? -2 : 0)
#endif
#ifndef EV_MAXPRI
-# define EV_MAXPRI +2
+# define EV_MAXPRI (EV_FEATURE_CONFIG ? +2 : 0)
#endif
#ifndef EV_MULTIPLICITY
-# define EV_MULTIPLICITY 1
+# define EV_MULTIPLICITY EV_FEATURE_CONFIG
#endif
#ifndef EV_PERIODIC_ENABLE
-# define EV_PERIODIC_ENABLE 1
+# define EV_PERIODIC_ENABLE EV_FEATURE_WATCHERS
#endif
#ifndef EV_STAT_ENABLE
-# define EV_STAT_ENABLE 1
+# define EV_STAT_ENABLE EV_FEATURE_WATCHERS
+#endif
+
+#ifndef EV_PREPARE_ENABLE
+# define EV_PREPARE_ENABLE EV_FEATURE_WATCHERS
+#endif
+
+#ifndef EV_CHECK_ENABLE
+# define EV_CHECK_ENABLE EV_FEATURE_WATCHERS
#endif
#ifndef EV_IDLE_ENABLE
-# define EV_IDLE_ENABLE 1
+# define EV_IDLE_ENABLE EV_FEATURE_WATCHERS
#endif
#ifndef EV_FORK_ENABLE
-# define EV_FORK_ENABLE 1
+# define EV_FORK_ENABLE EV_FEATURE_WATCHERS
#endif
-#ifndef EV_EMBED_ENABLE
-# define EV_EMBED_ENABLE 1
+#ifndef EV_SIGNAL_ENABLE
+# define EV_SIGNAL_ENABLE EV_FEATURE_WATCHERS
+#endif
+
+#ifndef EV_CHILD_ENABLE
+# ifdef _WIN32
+# define EV_CHILD_ENABLE 0
+# else
+# define EV_CHILD_ENABLE EV_FEATURE_WATCHERS
+#endif
#endif
#ifndef EV_ASYNC_ENABLE
-# define EV_ASYNC_ENABLE 1
+# define EV_ASYNC_ENABLE EV_FEATURE_WATCHERS
+#endif
+
+#ifndef EV_EMBED_ENABLE
+# define EV_EMBED_ENABLE EV_FEATURE_WATCHERS
#endif
#ifndef EV_WALK_ENABLE
# define EV_WALK_ENABLE 0 /* not yet */
#endif
+/*****************************************************************************/
+
+#if EV_CHILD_ENABLE && !EV_SIGNAL_ENABLE
+# undef EV_SIGNAL_ENABLE
+# define EV_SIGNAL_ENABLE 1
+#endif
+
+/*****************************************************************************/
+
+typedef double ev_tstamp;
+
#ifndef EV_ATOMIC_T
# include <signal.h>
# define EV_ATOMIC_T sig_atomic_t volatile
#endif
-/*****************************************************************************/
-
#if EV_STAT_ENABLE
# ifdef _WIN32
# include <time.h>
# define EV_INLINE static
#endif
+#ifndef EV_PROTOTYPES
+# define EV_PROTOTYPES 1
+#endif
+
/*****************************************************************************/
+#define EV_VERSION_MAJOR 4
+#define EV_VERSION_MINOR 0
+
/* eventmask, revents, events... */
-#define EV_UNDEF -1 /* guaranteed to be invalid */
-#define EV_NONE 0x00 /* no events */
-#define EV_READ 0x01 /* ev_io detected read will not block */
-#define EV_WRITE 0x02 /* ev_io detected write will not block */
-#define EV__IOFDSET 0x80 /* internal use only */
-#define EV_IO EV_READ /* alias for type-detection */
-#define EV_TIMEOUT 0x00000100 /* timer timed out */
-#define EV_TIMER EV_TIMEOUT /* alias for type-detection */
-#define EV_PERIODIC 0x00000200 /* periodic timer timed out */
-#define EV_SIGNAL 0x00000400 /* signal was received */
-#define EV_CHILD 0x00000800 /* child/pid had status change */
-#define EV_STAT 0x00001000 /* stat data changed */
-#define EV_IDLE 0x00002000 /* event loop is idling */
-#define EV_PREPARE 0x00004000 /* event loop about to poll */
-#define EV_CHECK 0x00008000 /* event loop finished poll */
-#define EV_EMBED 0x00010000 /* embedded event loop needs sweep */
-#define EV_FORK 0x00020000 /* event loop resumed in child */
-#define EV_ASYNC 0x00040000 /* async intra-loop signal */
-#define EV_CUSTOM 0x01000000 /* for use by user code */
-#define EV_ERROR 0x80000000 /* sent when an error occurs */
+enum {
+ EV_UNDEF = -1, /* guaranteed to be invalid */
+ EV_NONE = 0x00, /* no events */
+ EV_READ = 0x01, /* ev_io detected read will not block */
+ EV_WRITE = 0x02, /* ev_io detected write will not block */
+ EV__IOFDSET = 0x80, /* internal use only */
+ EV_IO = EV_READ, /* alias for type-detection */
+ EV_TIMER = 0x00000100, /* timer timed out */
+ EV_TIMEOUT = EV_TIMER, /* pre 4.0 API compatibility */
+ EV_PERIODIC = 0x00000200, /* periodic timer timed out */
+ EV_SIGNAL = 0x00000400, /* signal was received */
+ EV_CHILD = 0x00000800, /* child/pid had status change */
+ EV_STAT = 0x00001000, /* stat data changed */
+ EV_IDLE = 0x00002000, /* event loop is idling */
+ EV_PREPARE = 0x00004000, /* event loop about to poll */
+ EV_CHECK = 0x00008000, /* event loop finished poll */
+ EV_EMBED = 0x00010000, /* embedded event loop needs sweep */
+ EV_FORK = 0x00020000, /* event loop resumed in child */
+ EV_ASYNC = 0x00040000, /* async intra-loop signal */
+ EV_CUSTOM = 0x01000000, /* for use by user code */
+ EV_ERROR = 0x80000000 /* sent when an error occurs */
+};
/* can be used to add custom fields to all watchers, while losing binary compatibility */
#ifndef EV_COMMON
# define EV_COMMON void *data;
#endif
-#ifndef EV_PROTOTYPES
-# define EV_PROTOTYPES 1
-#endif
-
-#define EV_VERSION_MAJOR 3
-#define EV_VERSION_MINOR 9
#ifndef EV_CB_DECLARE
# define EV_CB_DECLARE(type) void (*cb)(EV_P_ struct type *w, int revents);
# define EV_CB_INVOKE(watcher,revents) (watcher)->cb (EV_A_ (watcher), (revents))
#endif
+/* not official, do not use */
+#define EV_CB(type,name) void name (EV_P_ struct ev_ ## type *w, int revents)
+
/*
* struct member types:
* private: you may look at them, but not change them,
#if EV_MINPRI == EV_MAXPRI
# define EV_DECL_PRIORITY
-#else
+#elif !defined (EV_DECL_PRIORITY)
# define EV_DECL_PRIORITY int priority;
#endif
#endif
};
-/* bits for ev_default_loop and ev_loop_new */
-/* the default */
-#define EVFLAG_AUTO 0x00000000U /* not quite a mask */
-/* flag bits */
-#define EVFLAG_NOENV 0x01000000U /* do NOT consult environment */
-#define EVFLAG_FORKCHECK 0x02000000U /* check for a fork in each iteration */
-/* debugging/feature disable */
-#define EVFLAG_NOINOTIFY 0x00100000U /* do not attempt to use inotify */
-#define EVFLAG_NOSIGFD 0 /* compatibility to pre-3.9 */
-#define EVFLAG_SIGNALFD 0x00200000U /* attempt to use signalfd */
+/* flag bits for ev_default_loop and ev_loop_new */
+enum {
+ /* the default */
+ EVFLAG_AUTO = 0x00000000U, /* not quite a mask */
+ /* flag bits */
+ EVFLAG_NOENV = 0x01000000U, /* do NOT consult environment */
+ EVFLAG_FORKCHECK = 0x02000000U, /* check for a fork in each iteration */
+ /* debugging/feature disable */
+ EVFLAG_NOINOTIFY = 0x00100000U, /* do not attempt to use inotify */
+#if EV_COMPAT3
+ EVFLAG_NOSIGFD = 0, /* compatibility to pre-3.9 */
+#endif
+ EVFLAG_SIGNALFD = 0x00200000U /* attempt to use signalfd */
+};
+
/* method bits to be ored together */
-#define EVBACKEND_SELECT 0x00000001U /* about anywhere */
-#define EVBACKEND_POLL 0x00000002U /* !win */
-#define EVBACKEND_EPOLL 0x00000004U /* linux */
-#define EVBACKEND_KQUEUE 0x00000008U /* bsd */
-#define EVBACKEND_DEVPOLL 0x00000010U /* solaris 8 */ /* NYI */
-#define EVBACKEND_PORT 0x00000020U /* solaris 10 */
-#define EVBACKEND_ALL 0x0000003FU
+enum {
+ EVBACKEND_SELECT = 0x00000001U, /* about anywhere */
+ EVBACKEND_POLL = 0x00000002U, /* !win */
+ EVBACKEND_EPOLL = 0x00000004U, /* linux */
+ EVBACKEND_KQUEUE = 0x00000008U, /* bsd */
+ EVBACKEND_DEVPOLL = 0x00000010U, /* solaris 8 */ /* NYI */
+ EVBACKEND_PORT = 0x00000020U, /* solaris 10 */
+ EVBACKEND_ALL = 0x0000003FU
+};
#if EV_PROTOTYPES
int ev_version_major (void);
#endif /* prototypes */
-#define EVLOOP_NONBLOCK 1 /* do not block/wait */
-#define EVLOOP_ONESHOT 2 /* block *once* only */
-#define EVUNLOOP_CANCEL 0 /* undo unloop */
-#define EVUNLOOP_ONE 1 /* unloop once */
-#define EVUNLOOP_ALL 2 /* unloop all loops */
+/* ev_run flags values */
+enum {
+ EVRUN_NOWAIT = 1, /* do not block/wait */
+ EVRUN_ONCE = 2 /* block *once* only */
+};
+
+/* ev_break how values */
+enum {
+ EVBREAK_CANCEL = 0, /* undo unloop */
+ EVBREAK_ONE = 1, /* unloop once */
+ EVBREAK_ALL = 2 /* unloop all loops */
+};
#if EV_PROTOTYPES
-void ev_loop (EV_P_ int flags);
-void ev_unloop (EV_P_ int how); /* set to 1 to break out of event loop, set to 2 to break out of all event loops */
+void ev_run (EV_P_ int flags);
+void ev_break (EV_P_ int how); /* set to 1 to break out of event loop, set to 2 to break out of all event loops */
/*
* ref/unref can be used to add or remove a refcount on the mainloop. every watcher
*/
void ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg);
-# if EV_MINIMAL < 2
-unsigned int ev_loop_count (EV_P); /* number of loop iterations */
-unsigned int ev_loop_depth (EV_P); /* #ev_loop enters - #ev_loop leaves */
-void ev_loop_verify (EV_P); /* abort if loop data corrupted */
+# if EV_FEATURE_API
+unsigned int ev_iteration (EV_P); /* number of loop iterations */
+unsigned int ev_depth (EV_P); /* #ev_loop enters - #ev_loop leaves */
+void ev_verify (EV_P); /* abort if loop data corrupted */
void ev_set_io_collect_interval (EV_P_ ev_tstamp interval); /* sleep at least this time, default 0 */
void ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval); /* sleep at least this time, default 0 */
#define ev_check_set(ev) /* nop, yes, this is a serious in-joke */
#define ev_embed_set(ev,other_) do { (ev)->other = (other_); } while (0)
#define ev_fork_set(ev) /* nop, yes, this is a serious in-joke */
-#define ev_async_set(ev) do { (ev)->sent = 0; } while (0)
+#define ev_async_set(ev) /* nop, yes, this is a serious in-joke */
#define ev_io_init(ev,cb,fd,events) do { ev_init ((ev), (cb)); ev_io_set ((ev),(fd),(events)); } while (0)
#define ev_timer_init(ev,cb,after,repeat) do { ev_init ((ev), (cb)); ev_timer_set ((ev),(after),(repeat)); } while (0)
# define ev_set_priority(ev,pri) ((ev), (pri))
#else
# define ev_priority(ev) (+(((ev_watcher *)(void *)(ev))->priority))
-# define ev_set_priority(ev,pri) ( (ev_watcher *)(void *)(ev))->priority = (pri)
+# define ev_set_priority(ev,pri) ( (ev_watcher *)(void *)(ev))->priority = (pri)
#endif
#define ev_periodic_at(ev) (+((ev_watcher_time *)(ev))->at)
/* accepts any ev_watcher type */
void ev_feed_event (EV_P_ void *w, int revents);
void ev_feed_fd_event (EV_P_ int fd, int revents);
+#if EV_SIGNAL_ENABLE
void ev_feed_signal_event (EV_P_ int signum);
+#endif
void ev_invoke (EV_P_ void *w, int revents);
int ev_clear_pending (EV_P_ void *w);
#endif
/* only supported in the default loop */
+#if EV_SIGNAL_ENABLE
void ev_signal_start (EV_P_ ev_signal *w);
void ev_signal_stop (EV_P_ ev_signal *w);
+#endif
/* only supported in the default loop */
+# if EV_CHILD_ENABLE
void ev_child_start (EV_P_ ev_child *w);
void ev_child_stop (EV_P_ ev_child *w);
+# endif
# if EV_STAT_ENABLE
void ev_stat_start (EV_P_ ev_stat *w);
void ev_idle_stop (EV_P_ ev_idle *w);
# endif
+#if EV_PREPARE_ENABLE
void ev_prepare_start (EV_P_ ev_prepare *w);
void ev_prepare_stop (EV_P_ ev_prepare *w);
+#endif
+#if EV_CHECK_ENABLE
void ev_check_start (EV_P_ ev_check *w);
void ev_check_stop (EV_P_ ev_check *w);
+#endif
# if EV_FORK_ENABLE
void ev_fork_start (EV_P_ ev_fork *w);
void ev_async_send (EV_P_ ev_async *w);
# endif
+#if EV_COMPAT3
+ #define EVLOOP_NONBLOCK EVRUN_NOWAIT
+ #define EVLOOP_ONESHOT EVRUN_ONCE
+ #define EVUNLOOP_CANCEL EVBREAK_CANCEL
+ #define EVUNLOOP_ONE EVBREAK_ONE
+ #define EVUNLOOP_ALL EVBREAK_ALL
+ #if EV_PROTOTYPES
+ EV_INLINE void ev_loop (EV_P_ int flags) { ev_run (EV_A_ flags); }
+ EV_INLINE void ev_unloop (EV_P_ int how ) { ev_break (EV_A_ how ); }
+ #if EV_FEATURE_API
+ EV_INLINE void ev_loop_count (EV_P) { ev_iteration (EV_A); }
+ EV_INLINE void ev_loop_depth (EV_P) { ev_depth (EV_A); }
+ EV_INLINE void ev_loop_verify (EV_P) { ev_verify (EV_A); }
+ #endif
+ #endif
+#else
+ typedef struct ev_loop ev_loop;
+#endif
+
#endif
#ifdef __cplusplus
// with its corresponding stop function.
ev_io_stop (EV_A_ w);
- // this causes all nested ev_loop's to stop iterating
- ev_unloop (EV_A_ EVUNLOOP_ALL);
+ // this causes all nested ev_run's to stop iterating
+ ev_break (EV_A_ EVBREAK_ALL);
}
// another callback, this time for a time-out
timeout_cb (EV_P_ ev_timer *w, int revents)
{
puts ("timeout");
- // this causes the innermost ev_loop to stop iterating
- ev_unloop (EV_A_ EVUNLOOP_ONE);
+ // this causes the innermost ev_run to stop iterating
+ ev_break (EV_A_ EVBREAK_ONE);
}
int
ev_timer_start (loop, &timeout_watcher);
// now wait for events to arrive
- ev_loop (loop, 0);
+ ev_run (loop, 0);
// unloop was called, so exit
return 0;
on event-based programming, nor will it introduce event-based programming
with libev.
-Familarity with event based programming techniques in general is assumed
+Familiarity with event based programming techniques in general is assumed
throughout this document.
=head1 ABOUT LIBEV
=head2 TIME REPRESENTATION
Libev represents time as a single floating point number, representing
-the (fractional) number of seconds since the (POSIX) epoch (somewhere
-near the beginning of 1970, details are complicated, don't ask). This
-type is called C<ev_tstamp>, which is what you should use too. It usually
-aliases to the C<double> type in C. When you need to do any calculations
-on it, you should treat it as some floating point value. Unlike the name
-component C<stamp> might indicate, it is also used for time differences
-throughout libev.
+the (fractional) number of seconds since the (POSIX) epoch (in practise
+somewhere near the beginning of 1970, details are complicated, don't
+ask). This type is called C<ev_tstamp>, which is what you should use
+too. It usually aliases to the C<double> type in C. When you need to do
+any calculations on it, you should treat it as some floating point value.
+
+Unlike the name component C<stamp> might indicate, it is also used for
+time differences (e.g. delays) throughout libev.
=head1 ERROR HANDLING
not a problem.
Example: Make sure we haven't accidentally been linked against the wrong
-version.
+version (note, however, that this will not detect ABI mismatches :).
assert (("libev version mismatch",
ev_version_major () == EV_VERSION_MAJOR
=head1 FUNCTIONS CONTROLLING THE EVENT LOOP
-An event loop is described by a C<struct ev_loop *> (the C<struct>
-is I<not> optional in this case, as there is also an C<ev_loop>
-I<function>).
+An event loop is described by a C<struct ev_loop *> (the C<struct> is
+I<not> optional in this case unless libev 3 compatibility is disabled, as
+libev 3 had an C<ev_loop> function colliding with the struct name).
The library knows two types of such loops, the I<default> loop, which
-supports signals and child events, and dynamically created loops which do
-not.
+supports signals and child events, and dynamically created event loops
+which do not.
=over 4
=item C<EVFLAG_FORKCHECK>
-Instead of calling C<ev_default_fork> or C<ev_loop_fork> manually after
-a fork, you can also make libev check for a fork in each iteration by
-enabling this flag.
+Instead of calling C<ev_loop_fork> manually after a fork, you can also
+make libev check for a fork in each iteration by enabling this flag.
This works by calling C<getpid ()> on every iteration of the loop,
and thus this might slow down your event loop if you do a lot of loop
even remove them from the set) than registered in the set (especially
on SMP systems). Libev tries to counter these spurious notifications by
employing an additional generation counter and comparing that against the
-events to filter out spurious ones, recreating the set when required.
+events to filter out spurious ones, recreating the set when required. Last
+not least, it also refuses to work with some file descriptors which work
+perfectly fine with C<select> (files, many character devices...).
While stopping, setting and starting an I/O watcher in the same iteration
will result in some caching, there is still a system call per such
=item struct ev_loop *ev_loop_new (unsigned int flags)
Similar to C<ev_default_loop>, but always creates a new event loop that is
-always distinct from the default loop. Unlike the default loop, it cannot
-handle signal and child watchers, and attempts to do so will be greeted by
-undefined behaviour (or a failed assertion if assertions are enabled).
+always distinct from the default loop.
-Note that this function I<is> thread-safe, and the recommended way to use
+Note that this function I<is> thread-safe, and one common way to use
libev with threads is indeed to create one loop per thread, and using the
default loop in the "main" or "initial" thread.
=item ev_default_destroy ()
-Destroys the default loop again (frees all memory and kernel state
-etc.). None of the active event watchers will be stopped in the normal
-sense, so e.g. C<ev_is_active> might still return true. It is your
-responsibility to either stop all watchers cleanly yourself I<before>
-calling this function, or cope with the fact afterwards (which is usually
-the easiest thing, you can just ignore the watchers and/or C<free ()> them
-for example).
+Destroys the default loop (frees all memory and kernel state etc.). None
+of the active event watchers will be stopped in the normal sense, so
+e.g. C<ev_is_active> might still return true. It is your responsibility to
+either stop all watchers cleanly yourself I<before> calling this function,
+or cope with the fact afterwards (which is usually the easiest thing, you
+can just ignore the watchers and/or C<free ()> them for example).
Note that certain global state, such as signal state (and installed signal
handlers), will not be freed by this function, and related watchers (such
=item ev_default_fork ()
-This function sets a flag that causes subsequent C<ev_loop> iterations
+This function sets a flag that causes subsequent C<ev_run> iterations
to reinitialise the kernel state for backends that have one. Despite the
name, you can call it anytime, but it makes most sense after forking, in
the child process (or both child and parent, but that again makes little
sense). You I<must> call it in the child before using any of the libev
-functions, and it will only take effect at the next C<ev_loop> iteration.
+functions, and it will only take effect at the next C<ev_run> iteration.
+
+Again, you I<have> to call it on I<any> loop that you want to re-use after
+a fork, I<even if you do not plan to use the loop in the parent>. This is
+because some kernel interfaces *cough* I<kqueue> *cough* do funny things
+during fork.
On the other hand, you only need to call this function in the child
-process if and only if you want to use the event library in the child. If
-you just fork+exec, you don't have to call it at all.
+process if and only if you want to use the event loop in the child. If
+you just fork+exec or create a new loop in the child, you don't have to
+call it at all (in fact, C<epoll> is so badly broken that it makes a
+difference, but libev will usually detect this case on its own and do a
+costly reset of the backend).
The function itself is quite fast and it's usually not a problem to call
it just in case after a fork. To make this easy, the function will fit in
Like C<ev_default_fork>, but acts on an event loop created by
C<ev_loop_new>. Yes, you have to call this on every allocated event loop
-after fork that you want to re-use in the child, and how you do this is
-entirely your own problem.
+after fork that you want to re-use in the child, and how you keep track of
+them is entirely your own problem.
=item int ev_is_default_loop (loop)
Returns true when the given loop is, in fact, the default loop, and false
otherwise.
-=item unsigned int ev_loop_count (loop)
+=item unsigned int ev_iteration (loop)
-Returns the count of loop iterations for the loop, which is identical to
-the number of times libev did poll for new events. It starts at C<0> and
-happily wraps around with enough iterations.
+Returns the current iteration count for the event loop, which is identical
+to the number of times libev did poll for new events. It starts at C<0>
+and happily wraps around with enough iterations.
This value can sometimes be useful as a generation counter of sorts (it
"ticks" the number of loop iterations), as it roughly corresponds with
-C<ev_prepare> and C<ev_check> calls.
+C<ev_prepare> and C<ev_check> calls - and is incremented between the
+prepare and check phases.
-=item unsigned int ev_loop_depth (loop)
+=item unsigned int ev_depth (loop)
-Returns the number of times C<ev_loop> was entered minus the number of
-times C<ev_loop> was exited, in other words, the recursion depth.
+Returns the number of times C<ev_run> was entered minus the number of
+times C<ev_run> was exited, in other words, the recursion depth.
-Outside C<ev_loop>, this number is zero. In a callback, this number is
-C<1>, unless C<ev_loop> was invoked recursively (or from another thread),
+Outside C<ev_run>, this number is zero. In a callback, this number is
+C<1>, unless C<ev_run> was invoked recursively (or from another thread),
in which case it is higher.
-Leaving C<ev_loop> abnormally (setjmp/longjmp, cancelling the thread
-etc.), doesn't count as exit.
+Leaving C<ev_run> abnormally (setjmp/longjmp, cancelling the thread
+etc.), doesn't count as "exit" - consider this as a hint to avoid such
+ungentleman-like behaviour unless it's really convenient.
=item unsigned int ev_backend (loop)
Establishes the current time by querying the kernel, updating the time
returned by C<ev_now ()> in the progress. This is a costly operation and
-is usually done automatically within C<ev_loop ()>.
+is usually done automatically within C<ev_run ()>.
This function is rarely useful, but when some event callback runs for a
very long time without entering the event loop, updating libev's idea of
=item ev_resume (loop)
-These two functions suspend and resume a loop, for use when the loop is
-not used for a while and timeouts should not be processed.
+These two functions suspend and resume an event loop, for use when the
+loop is not used for a while and timeouts should not be processed.
A typical use case would be an interactive program such as a game: When
the user presses C<^Z> to suspend the game and resumes it an hour later it
Effectively, all C<ev_timer> watchers will be delayed by the time spend
between C<ev_suspend> and C<ev_resume>, and all C<ev_periodic> watchers
will be rescheduled (that is, they will lose any events that would have
-occured while suspended).
+occurred while suspended).
After calling C<ev_suspend> you B<must not> call I<any> function on the
given loop other than C<ev_resume>, and you B<must not> call C<ev_resume>
Calling C<ev_suspend>/C<ev_resume> has the side effect of updating the
event loop time (see C<ev_now_update>).
-=item ev_loop (loop, int flags)
+=item ev_run (loop, int flags)
Finally, this is it, the event handler. This function usually is called
after you have initialised all your watchers and you want to start
-handling events.
+handling events. It will ask the operating system for any new events, call
+the watcher callbacks, an then repeat the whole process indefinitely: This
+is why event loops are called I<loops>.
-If the flags argument is specified as C<0>, it will not return until
-either no event watchers are active anymore or C<ev_unloop> was called.
+If the flags argument is specified as C<0>, it will keep handling events
+until either no event watchers are active anymore or C<ev_break> was
+called.
-Please note that an explicit C<ev_unloop> is usually better than
+Please note that an explicit C<ev_break> is usually better than
relying on all watchers to be stopped when deciding when a program has
finished (especially in interactive programs), but having a program
that automatically loops as long as it has to and no longer by virtue
of relying on its watchers stopping correctly, that is truly a thing of
beauty.
-A flags value of C<EVLOOP_NONBLOCK> will look for new events, will handle
-those events and any already outstanding ones, but will not block your
-process in case there are no events and will return after one iteration of
-the loop.
+A flags value of C<EVRUN_NOWAIT> will look for new events, will handle
+those events and any already outstanding ones, but will not wait and
+block your process in case there are no events and will return after one
+iteration of the loop. This is sometimes useful to poll and handle new
+events while doing lengthy calculations, to keep the program responsive.
-A flags value of C<EVLOOP_ONESHOT> will look for new events (waiting if
+A flags value of C<EVRUN_ONCE> will look for new events (waiting if
necessary) and will handle those and any already outstanding ones. It
will block your process until at least one new event arrives (which could
be an event internal to libev itself, so there is no guarantee that a
This is useful if you are waiting for some external event in conjunction
with something not expressible using other libev watchers (i.e. "roll your
-own C<ev_loop>"). However, a pair of C<ev_prepare>/C<ev_check> watchers is
+own C<ev_run>"). However, a pair of C<ev_prepare>/C<ev_check> watchers is
usually a better approach for this kind of thing.
-Here are the gory details of what C<ev_loop> does:
+Here are the gory details of what C<ev_run> does:
+ - Increment loop depth.
+ - Reset the ev_break status.
- Before the first iteration, call any pending watchers.
- * If EVFLAG_FORKCHECK was used, check for a fork.
+ LOOP:
+ - If EVFLAG_FORKCHECK was used, check for a fork.
- If a fork was detected (by any means), queue and call all fork watchers.
- Queue and call all prepare watchers.
+ - If ev_break was called, goto FINISH.
- If we have been forked, detach and recreate the kernel state
as to not disturb the other process.
- Update the kernel state with all outstanding changes.
- Update the "event loop time" (ev_now ()).
- Calculate for how long to sleep or block, if at all
- (active idle watchers, EVLOOP_NONBLOCK or not having
+ (active idle watchers, EVRUN_NOWAIT or not having
any active watchers at all will result in not sleeping).
- Sleep if the I/O and timer collect interval say so.
+ - Increment loop iteration counter.
- Block the process, waiting for any events.
- Queue all outstanding I/O (fd) events.
- Update the "event loop time" (ev_now ()), and do time jump adjustments.
- Queue all expired timers.
- Queue all expired periodics.
- - Unless any events are pending now, queue all idle watchers.
+ - Queue all idle watchers with priority higher than that of pending events.
- Queue all check watchers.
- Call all queued watchers in reverse order (i.e. check watchers first).
Signals and child watchers are implemented as I/O watchers, and will
be handled here by queueing them when their watcher gets executed.
- - If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK
- were used, or there are no active watchers, return, otherwise
- continue with step *.
+ - If ev_break has been called, or EVRUN_ONCE or EVRUN_NOWAIT
+ were used, or there are no active watchers, goto FINISH, otherwise
+ continue with step LOOP.
+ FINISH:
+ - Reset the ev_break status iff it was EVBREAK_ONE.
+ - Decrement the loop depth.
+ - Return.
Example: Queue some jobs and then loop until no events are outstanding
anymore.
... queue jobs here, make sure they register event watchers as long
... as they still have work to do (even an idle watcher will do..)
- ev_loop (my_loop, 0);
+ ev_run (my_loop, 0);
... jobs done or somebody called unloop. yeah!
-=item ev_unloop (loop, how)
+=item ev_break (loop, how)
-Can be used to make a call to C<ev_loop> return early (but only after it
+Can be used to make a call to C<ev_run> return early (but only after it
has processed all outstanding events). The C<how> argument must be either
-C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or
-C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return.
+C<EVBREAK_ONE>, which will make the innermost C<ev_run> call return, or
+C<EVBREAK_ALL>, which will make all nested C<ev_run> calls return.
-This "unloop state" will be cleared when entering C<ev_loop> again.
+This "unloop state" will be cleared when entering C<ev_run> again.
-It is safe to call C<ev_unloop> from otuside any C<ev_loop> calls.
+It is safe to call C<ev_break> from outside any C<ev_run> calls. ##TODO##
=item ev_ref (loop)
Ref/unref can be used to add or remove a reference count on the event
loop: Every watcher keeps one reference, and as long as the reference
-count is nonzero, C<ev_loop> will not return on its own.
+count is nonzero, C<ev_run> will not return on its own.
This is useful when you have a watcher that you never intend to
-unregister, but that nevertheless should not keep C<ev_loop> from
+unregister, but that nevertheless should not keep C<ev_run> from
returning. In such a case, call C<ev_unref> after starting, and C<ev_ref>
before stopping it.
As an example, libev itself uses this for its internal signal pipe: It
-is not visible to the libev user and should not keep C<ev_loop> from
+is not visible to the libev user and should not keep C<ev_run> from
exiting if no event watchers registered by it are active. It is also an
excellent way to do this for generic recurring timers or from within
third-party libraries. Just remember to I<unref after start> and I<ref
(e.g. non-repeating timers) in which case you have to C<ev_ref>
in the callback).
-Example: Create a signal watcher, but keep it from keeping C<ev_loop>
+Example: Create a signal watcher, but keep it from keeping C<ev_run>
running when nothing else is active.
ev_signal exitsig;
you do transactions with the outside world and you can't increase the
parallelity, then this setting will limit your transaction rate (if you
need to poll once per transaction and the I/O collect interval is 0.01,
-then you can't do more than 100 transations per second).
+then you can't do more than 100 transactions per second).
Setting the I<timeout collect interval> can improve the opportunity for
saving power, as the program will "bundle" timer callback invocations that
=item ev_invoke_pending (loop)
This call will simply invoke all pending watchers while resetting their
-pending state. Normally, C<ev_loop> does this automatically when required,
+pending state. Normally, C<ev_run> does this automatically when required,
but when overriding the invoke callback this call comes handy.
=item int ev_pending_count (loop)
=item ev_set_invoke_pending_cb (loop, void (*invoke_pending_cb)(EV_P))
This overrides the invoke pending functionality of the loop: Instead of
-invoking all pending watchers when there are any, C<ev_loop> will call
+invoking all pending watchers when there are any, C<ev_run> will call
this callback instead. This is useful, for example, when you want to
invoke the actual watchers inside another context (another thread etc.).
can be done relatively simply by putting mutex_lock/unlock calls around
each call to a libev function.
-However, C<ev_loop> can run an indefinite time, so it is not feasible to
-wait for it to return. One way around this is to wake up the loop via
-C<ev_unloop> and C<av_async_send>, another way is to set these I<release>
-and I<acquire> callbacks on the loop.
+However, C<ev_run> can run an indefinite time, so it is not feasible
+to wait for it to return. One way around this is to wake up the event
+loop via C<ev_break> and C<av_async_send>, another way is to set these
+I<release> and I<acquire> callbacks on the loop.
When set, then C<release> will be called just before the thread is
suspended waiting for new events, and C<acquire> is called just
C<release> and C<acquire> (that's their only purpose after all), no
modifications done will affect the event loop, i.e. adding watchers will
have no effect on the set of file descriptors being watched, or the time
-waited. Use an C<ev_async> watcher to wake up C<ev_loop> when you want it
+waited. Use an C<ev_async> watcher to wake up C<ev_run> when you want it
to take note of any changes you made.
-In theory, threads executing C<ev_loop> will be async-cancel safe between
+In theory, threads executing C<ev_run> will be async-cancel safe between
invocations of C<release> and C<acquire>.
See also the locking example in the C<THREADS> section later in this
C<acquire> callbacks described above, but of course can be (ab-)used for
any other purpose as well.
-=item ev_loop_verify (loop)
+=item ev_verify (loop)
This function only does something when C<EV_VERIFY> support has been
compiled in, which is the default for non-minimal builds. It tries to go
watcher type, e.g. C<ev_TYPE_start> can mean C<ev_timer_start> for timer
watchers and C<ev_io_start> for I/O watchers.
-A watcher is a structure that you create and register to record your
-interest in some event. For instance, if you want to wait for STDIN to
-become readable, you would create an C<ev_io> watcher for that:
+A watcher is an opaque structure that you allocate and register to record
+your interest in some event. To make a concrete example, imagine you want
+to wait for STDIN to become readable, you would create an C<ev_io> watcher
+for that:
static void my_cb (struct ev_loop *loop, ev_io *w, int revents)
{
ev_io_stop (w);
- ev_unloop (loop, EVUNLOOP_ALL);
+ ev_break (loop, EVBREAK_ALL);
}
struct ev_loop *loop = ev_default_loop (0);
ev_io_set (&stdin_watcher, STDIN_FILENO, EV_READ);
ev_io_start (loop, &stdin_watcher);
- ev_loop (loop, 0);
+ ev_run (loop, 0);
As you can see, you are responsible for allocating the memory for your
watcher structures (and it is I<usually> a bad idea to do this on the
Each watcher has an associated watcher structure (called C<struct ev_TYPE>
or simply C<ev_TYPE>, as typedefs are provided for all watcher structs).
-Each watcher structure must be initialised by a call to C<ev_init
-(watcher *, callback)>, which expects a callback to be provided. This
-callback gets invoked each time the event occurs (or, in the case of I/O
-watchers, each time the event loop detects that the file descriptor given
-is readable and/or writable).
+Each watcher structure must be initialised by a call to C<ev_init (watcher
+*, callback)>, which expects a callback to be provided. This callback is
+invoked each time the event occurs (or, in the case of I/O watchers, each
+time the event loop detects that the file descriptor given is readable
+and/or writable).
Each watcher type further has its own C<< ev_TYPE_set (watcher *, ...) >>
macro to configure it, with arguments specific to the watcher type. There
The file descriptor in the C<ev_io> watcher has become readable and/or
writable.
-=item C<EV_TIMEOUT>
+=item C<EV_TIMER>
The C<ev_timer> watcher has timed out.
=item C<EV_CHECK>
-All C<ev_prepare> watchers are invoked just I<before> C<ev_loop> starts
+All C<ev_prepare> watchers are invoked just I<before> C<ev_run> starts
to gather new events, and all C<ev_check> watchers are invoked just after
-C<ev_loop> has gathered them, but before it invokes any callbacks for any
+C<ev_run> has gathered them, but before it invokes any callbacks for any
received events. Callbacks of both watcher types can start and stop as
many watchers as they want, and all of them will be taken into account
(for example, a C<ev_prepare> watcher might start an idle watcher to keep
-C<ev_loop> from blocking).
+C<ev_run> from blocking).
=item C<EV_EMBED>
=back
+=head2 WATCHER STATES
+
+There are various watcher states mentioned throughout this manual -
+active, pending and so on. In this section these states and the rules to
+transition between them will be described in more detail - and while these
+rules might look complicated, they usually do "the right thing".
+
+=over 4
+
+=item initialiased
+
+Before a watcher can be registered with the event looop it has to be
+initialised. This can be done with a call to C<ev_TYPE_init>, or calls to
+C<ev_init> followed by the watcher-specific C<ev_TYPE_set> function.
+
+In this state it is simply some block of memory that is suitable for use
+in an event loop. It can be moved around, freed, reused etc. at will.
+
+=item started/running/active
+
+Once a watcher has been started with a call to C<ev_TYPE_start> it becomes
+property of the event loop, and is actively waiting for events. While in
+this state it cannot be accessed (except in a few documented ways), moved,
+freed or anything else - the only legal thing is to keep a pointer to it,
+and call libev functions on it that are documented to work on active watchers.
+
+=item pending
+
+If a watcher is active and libev determines that an event it is interested
+in has occurred (such as a timer expiring), it will become pending. It will
+stay in this pending state until either it is stopped or its callback is
+about to be invoked, so it is not normally pending inside the watcher
+callback.
+
+The watcher might or might not be active while it is pending (for example,
+an expired non-repeating timer can be pending but no longer active). If it
+is stopped, it can be freely accessed (e.g. by calling C<ev_TYPE_set>),
+but it is still property of the event loop at this time, so cannot be
+moved, freed or reused. And if it is active the rules described in the
+previous item still apply.
+
+It is also possible to feed an event on a watcher that is not active (e.g.
+via C<ev_feed_event>), in which case it becomes pending without being
+active.
+
+=item stopped
+
+A watcher can be stopped implicitly by libev (in which case it might still
+be pending), or explicitly by calling its C<ev_TYPE_stop> function. The
+latter will clear any pending state the watcher might be in, regardless
+of whether it was active or not, so stopping a watcher explicitly before
+freeing it is often a good idea.
+
+While stopped (and not pending) the watcher is essentially in the
+initialised state, that is it can be reused, moved, modified in any way
+you wish.
+
+=back
+
=head2 GENERIC WATCHER FUNCTIONS
=over 4
you can associate an C<ev_idle> watcher to each such watcher, and in
the normal watcher callback, you just start the idle watcher. The real
processing is done in the idle watcher callback. This causes libev to
-continously poll and process kernel event data for the watcher, but when
+continuously poll and process kernel event data for the watcher, but when
the lock-out case is known to be rare (which in turn is rare :), this is
workable.
// are not yet ready to handle it.
ev_io_stop (EV_A_ w);
- // start the idle watcher to ahndle the actual event.
+ // start the idle watcher to handle the actual event.
// it will not be executed as long as other watchers
// with the default priority are receiving events.
ev_idle_start (EV_A_ &idle);
known-to-be-good backend (at the time of this writing, this includes only
C<EVBACKEND_SELECT> and C<EVBACKEND_POLL>). The same applies to file
descriptors for which non-blocking operation makes no sense (such as
-files) - libev doesn't guarentee any specific behaviour in that case.
+files) - libev doesn't guarantee any specific behaviour in that case.
Another thing you have to watch out for is that it is quite easy to
receive "spurious" readiness notifications, that is your callback might
ignore SIGPIPE (and maybe make sure you log the exit status of your daemon
somewhere, as that would have given you a big clue).
+=head3 The special problem of accept()ing when you can't
+
+Many implementations of the POSIX C<accept> function (for example,
+found in post-2004 Linux) have the peculiar behaviour of not removing a
+connection from the pending queue in all error cases.
+
+For example, larger servers often run out of file descriptors (because
+of resource limits), causing C<accept> to fail with C<ENFILE> but not
+rejecting the connection, leading to libev signalling readiness on
+the next iteration again (the connection still exists after all), and
+typically causing the program to loop at 100% CPU usage.
+
+Unfortunately, the set of errors that cause this issue differs between
+operating systems, there is usually little the app can do to remedy the
+situation, and no known thread-safe method of removing the connection to
+cope with overload is known (to me).
+
+One of the easiest ways to handle this situation is to just ignore it
+- when the program encounters an overload, it will just loop until the
+situation is over. While this is a form of busy waiting, no OS offers an
+event-based way to handle this situation, so it's the best one can do.
+
+A better way to handle the situation is to log any errors other than
+C<EAGAIN> and C<EWOULDBLOCK>, making sure not to flood the log with such
+messages, and continue as usual, which at least gives the user an idea of
+what could be wrong ("raise the ulimit!"). For extra points one could stop
+the C<ev_io> watcher on the listening fd "for a while", which reduces CPU
+usage.
+
+If your program is single-threaded, then you could also keep a dummy file
+descriptor for overload situations (e.g. by opening F</dev/null>), and
+when you run into C<ENFILE> or C<EMFILE>, close it, run C<accept>,
+close that fd, and create a new dummy fd. This will gracefully refuse
+clients under typical overload conditions.
+
+The last way to handle it is to simply log the error and C<exit>, as
+is often done with C<malloc> failures, but this results in an easy
+opportunity for a DoS attack.
=head3 Watcher-Specific Functions
ev_io stdin_readable;
ev_io_init (&stdin_readable, stdin_readable_cb, STDIN_FILENO, EV_READ);
ev_io_start (loop, &stdin_readable);
- ev_loop (loop, 0);
+ ev_run (loop, 0);
=head2 C<ev_timer> - relative and optionally repeating timeouts
might introduce a small delay). If multiple timers become ready during the
same loop iteration then the ones with earlier time-out values are invoked
before ones of the same priority with later time-out values (but this is
-no longer true when a callback calls C<ev_loop> recursively).
+no longer true when a callback calls C<ev_run> recursively).
=head3 Be smart about timeouts
// if last_activity + 60. is older than now, we did time out
if (timeout < now)
{
- // timeout occured, take action
+ // timeout occurred, take action
}
else
{
ev_init (timer, callback);
last_activity = ev_now (loop);
- callback (loop, timer, EV_TIMEOUT);
+ callback (loop, timer, EV_TIMER);
And when there is some activity, simply store the current time in
C<last_activity>, no libev calls at all:
- last_actiivty = ev_now (loop);
+ last_activity = ev_now (loop);
This technique is slightly more complex, but in most cases where the
time-out is unlikely to be triggered, much more efficient.
Establishing the current time is a costly operation (it usually takes at
least two system calls): EV therefore updates its idea of the current
-time only before and after C<ev_loop> collects new events, which causes a
+time only before and after C<ev_run> collects new events, which causes a
growing difference between C<ev_now ()> and C<ev_time ()> when handling
lots of events in one iteration.
the timeout value currently configured.
That is, after an C<ev_timer_set (w, 5, 7)>, C<ev_timer_remaining> returns
-C<5>. When the timer is started and one second passes, C<ev_timer_remain>
+C<5>. When the timer is started and one second passes, C<ev_timer_remaining>
will return C<4>. When the timer expires and is restarted, it will return
roughly C<7> (likely slightly less as callback invocation takes some time,
too), and so on.
ev_timer mytimer;
ev_timer_init (&mytimer, timeout_cb, 0., 10.); /* note, only repeat used */
ev_timer_again (&mytimer); /* start timer */
- ev_loop (loop, 0);
+ ev_run (loop, 0);
// and in some piece of code that gets executed on any "activity":
// reset the timeout to start ticking again at 10 seconds
point in time where it is supposed to trigger has passed. If multiple
timers become ready during the same loop iteration then the ones with
earlier time-out values are invoked before ones with later time-out values
-(but this is no longer true when a callback calls C<ev_loop> recursively).
+(but this is no longer true when a callback calls C<ev_run> recursively).
=head3 Watcher-Specific Functions and Data Members
potentially a lot of jitter, but good long-term stability.
static void
- clock_cb (struct ev_loop *loop, ev_io *w, int revents)
+ clock_cb (struct ev_loop *loop, ev_periodic *w, int revents)
{
... its now a full hour (UTC, or TAI or whatever your clock follows)
}
static void
sigint_cb (struct ev_loop *loop, ev_signal *w, int revents)
{
- ev_unloop (loop, EVUNLOOP_ALL);
+ ev_break (loop, EVBREAK_ALL);
}
ev_signal signal_watcher;
prepare watchers get invoked before the process blocks and check watchers
afterwards.
-You I<must not> call C<ev_loop> or similar functions that enter
+You I<must not> call C<ev_run> or similar functions that enter
the current event loop from either C<ev_prepare> or C<ev_check>
watchers. Other loops than the current one are fine, however. The
rationale behind this is that you do not need to check for recursion in
// create/start timer
// poll
- ev_loop (EV_A_ 0);
+ ev_run (EV_A_ 0);
// stop timer again
if (timeout >= 0)
=item ev_embed_sweep (loop, ev_embed *)
Make a single, non-blocking sweep over the embedded loop. This works
-similarly to C<ev_loop (embedded_loop, EVLOOP_NONBLOCK)>, but in the most
+similarly to C<ev_run (embedded_loop, EVRUN_NOWAIT)>, but in the most
appropriate way for embedded loops.
=item struct ev_loop *other [read-only]
=head3 The special problem of life after fork - how is it possible?
-Most uses of C<fork()> consist of forking, then some simple calls to ste
+Most uses of C<fork()> consist of forking, then some simple calls to set
up/change the process environment, followed by a call to C<exec()>. This
sequence should be handled by libev without any problems.
=back
-=head2 C<ev_async> - how to wake up another event loop
+=head2 C<ev_async> - how to wake up an event loop
-In general, you cannot use an C<ev_loop> from multiple threads or other
+In general, you cannot use an C<ev_run> from multiple threads or other
asynchronous sources such as signal handlers (as opposed to multiple event
loops - those are of course safe to use in different threads).
-Sometimes, however, you need to wake up another event loop you do not
-control, for example because it belongs to another thread. This is what
-C<ev_async> watchers do: as long as the C<ev_async> watcher is active, you
-can signal it by calling C<ev_async_send>, which is thread- and signal
-safe.
+Sometimes, however, you need to wake up an event loop you do not control,
+for example because it belongs to another thread. This is what C<ev_async>
+watchers do: as long as the C<ev_async> watcher is active, you can signal
+it by calling C<ev_async_send>, which is thread- and signal safe.
This functionality is very similar to C<ev_signal> watchers, as signals,
too, are asynchronous in nature, and signals, too, will be compressed
started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and
repeat = 0) will be started. C<0> is a valid timeout.
-The callback has the type C<void (*cb)(int revents, void *arg)> and gets
+The callback has the type C<void (*cb)(int revents, void *arg)> and is
passed an C<revents> set like normal event callbacks (a combination of
-C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg>
+C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMER>) and the C<arg>
value passed to C<ev_once>. Note that it is possible to receive I<both>
a timeout and an io event at the same time - you probably should give io
events precedence.
{
if (revents & EV_READ)
/* stdin might have data for us, joy! */;
- else if (revents & EV_TIMEOUT)
+ else if (revents & EV_TIMER)
/* doh, nothing entered */;
}
=item w->set (object *)
-This is an B<experimental> feature that might go away in a future version.
-
This is a variation of a method callback - leaving out the method to call
will default the method to C<operator ()>, which makes it possible to use
functor objects without having to manually specify the C<operator ()> all
=item w->set ([arguments])
-Basically the same as C<ev_TYPE_set>, with the same arguments. Must be
-called at least once. Unlike the C counterpart, an active watcher gets
-automatically stopped and restarted when reconfiguring it with this
-method.
+Basically the same as C<ev_TYPE_set>, with the same arguments. Either this
+method or a suitable start method must be called at least once. Unlike the
+C counterpart, an active watcher gets automatically stopped and restarted
+when reconfiguring it with this method.
=item w->start ()
Starts the watcher. Note that there is no C<loop> argument, as the
constructor already stores the event loop.
+=item w->start ([arguments])
+
+Instead of calling C<set> and C<start> methods separately, it is often
+convenient to wrap them in one call. Uses the same type of arguments as
+the configure C<set> method of the watcher.
+
=item w->stop ()
Stops the watcher if it is active. Again, no C<loop> argument.
=back
-Example: Define a class with an IO and idle watcher, start one of them in
-the constructor.
+Example: Define a class with two I/O and idle watchers, start the I/O
+watchers in the constructor.
class myclass
{
ev::io io ; void io_cb (ev::io &w, int revents);
+ ev::io2 io2 ; void io2_cb (ev::io &w, int revents);
ev::idle idle; void idle_cb (ev::idle &w, int revents);
myclass (int fd)
{
io .set <myclass, &myclass::io_cb > (this);
+ io2 .set <myclass, &myclass::io2_cb > (this);
idle.set <myclass, &myclass::idle_cb> (this);
- io.start (fd, ev::READ);
+ io.set (fd, ev::WRITE); // configure the watcher
+ io.start (); // start it whenever convenient
+
+ io2.start (fd, ev::READ); // set + start in one call
}
};
=item Lua
-Brian Maher has written a partial interface to libev
-for lua (only C<ev_io> and C<ev_timer>), to be found at
+Brian Maher has written a partial interface to libev for lua (at the
+time of this writing, only C<ev_io> and C<ev_timer>), to be found at
L<http://github.com/brimworks/lua-ev>.
=back
ev_unref (EV_A);
ev_timer_add (EV_A_ watcher);
- ev_loop (EV_A_ 0);
+ ev_run (EV_A_ 0);
It assumes the variable C<loop> of type C<struct ev_loop *> is in scope,
which is often provided by the following macro.
ev_check check;
ev_check_init (&check, check_cb);
ev_check_start (EV_DEFAULT_ &check);
- ev_loop (EV_DEFAULT_ 0);
+ ev_run (EV_DEFAULT_ 0);
=head1 EMBEDDING
=head2 PREPROCESSOR SYMBOLS/MACROS
Libev can be configured via a variety of preprocessor symbols you have to
-define before including any of its files. The default in the absence of
-autoconf is documented for every option.
+define before including (or compiling) any of its files. The default in
+the absence of autoconf is documented for every option.
+
+Symbols marked with "(h)" do not change the ABI, and can have different
+values when compiling libev vs. including F<ev.h>, so it is permissible
+to redefine them before including F<ev.h> without breaking compatibility
+to a compiled library. All other symbols change the ABI, which means all
+users of libev and the libev code itself must be compiled with compatible
+settings.
=over 4
-=item EV_STANDALONE
+=item EV_COMPAT3 (h)
+
+Backwards compatibility is a major concern for libev. This is why this
+release of libev comes with wrappers for the functions and symbols that
+have been renamed between libev version 3 and 4.
+
+You can disable these wrappers (to test compatibility with future
+versions) by defining C<EV_COMPAT3> to C<0> when compiling your
+sources. This has the additional advantage that you can drop the C<struct>
+from C<struct ev_loop> declarations, as libev will provide an C<ev_loop>
+typedef in that case.
+
+In some future version, the default for C<EV_COMPAT3> will become C<0>,
+and in some even more future version the compatibility code will be
+removed completely.
+
+=item EV_STANDALONE (h)
Must always be C<1> if you do not use autoconf configuration, which
keeps libev from including F<config.h>, and it also defines dummy
In the absence of this define, libev will use C<sig_atomic_t volatile>
(from F<signal.h>), which is usually good enough on most platforms.
-=item EV_H
+=item EV_H (h)
The name of the F<ev.h> header file used to include it. The default if
undefined is C<"ev.h"> in F<event.h>, F<ev.c> and F<ev++.h>. This can be
used to virtually rename the F<ev.h> header file in case of conflicts.
-=item EV_CONFIG_H
+=item EV_CONFIG_H (h)
If C<EV_STANDALONE> isn't C<1>, this variable can be used to override
F<ev.c>'s idea of where to find the F<config.h> file, similarly to
C<EV_H>, above.
-=item EV_EVENT_H
+=item EV_EVENT_H (h)
Similarly to C<EV_H>, this macro can be used to override F<event.c>'s idea
of how the F<event.h> header can be found, the default is C<"event.h">.
-=item EV_PROTOTYPES
+=item EV_PROTOTYPES (h)
If defined to be C<0>, then F<ev.h> will not define any function
prototypes, but still define all the structs and other symbols. This is
If your embedding application does not need any priorities, defining these
both to C<0> will save some memory and CPU.
-=item EV_PERIODIC_ENABLE
+=item EV_PERIODIC_ENABLE, EV_IDLE_ENABLE, EV_EMBED_ENABLE, EV_STAT_ENABLE,
+EV_PREPARE_ENABLE, EV_CHECK_ENABLE, EV_FORK_ENABLE, EV_SIGNAL_ENABLE,
+EV_ASYNC_ENABLE, EV_CHILD_ENABLE.
+
+If undefined or defined to be C<1> (and the platform supports it), then
+the respective watcher type is supported. If defined to be C<0>, then it
+is not. Disabling watcher types mainly saves code size.
+
+=item EV_FEATURES
-If undefined or defined to be C<1>, then periodic timers are supported. If
-defined to be C<0>, then they are not. Disabling them saves a few kB of
-code.
+If you need to shave off some kilobytes of code at the expense of some
+speed (but with the full API), you can define this symbol to request
+certain subsets of functionality. The default is to enable all features
+that can be enabled on the platform.
-=item EV_IDLE_ENABLE
+A typical way to use this symbol is to define it to C<0> (or to a bitset
+with some broad features you want) and then selectively re-enable
+additional parts you want, for example if you want everything minimal,
+but multiple event loop support, async and child watchers and the poll
+backend, use this:
-If undefined or defined to be C<1>, then idle watchers are supported. If
-defined to be C<0>, then they are not. Disabling them saves a few kB of
-code.
+ #define EV_FEATURES 0
+ #define EV_MULTIPLICITY 1
+ #define EV_USE_POLL 1
+ #define EV_CHILD_ENABLE 1
+ #define EV_ASYNC_ENABLE 1
-=item EV_EMBED_ENABLE
+The actual value is a bitset, it can be a combination of the following
+values:
-If undefined or defined to be C<1>, then embed watchers are supported. If
-defined to be C<0>, then they are not. Embed watchers rely on most other
-watcher types, which therefore must not be disabled.
+=over 4
-=item EV_STAT_ENABLE
+=item C<1> - faster/larger code
-If undefined or defined to be C<1>, then stat watchers are supported. If
-defined to be C<0>, then they are not.
+Use larger code to speed up some operations.
-=item EV_FORK_ENABLE
+Currently this is used to override some inlining decisions (enlarging the
+code size by roughly 30% on amd64).
-If undefined or defined to be C<1>, then fork watchers are supported. If
-defined to be C<0>, then they are not.
+When optimising for size, use of compiler flags such as C<-Os> with
+gcc is recommended, as well as C<-DNDEBUG>, as libev contains a number of
+assertions.
-=item EV_ASYNC_ENABLE
+=item C<2> - faster/larger data structures
-If undefined or defined to be C<1>, then async watchers are supported. If
-defined to be C<0>, then they are not.
+Replaces the small 2-heap for timer management by a faster 4-heap, larger
+hash table sizes and so on. This will usually further increase code size
+and can additionally have an effect on the size of data structures at
+runtime.
-=item EV_MINIMAL
+=item C<4> - full API configuration
-If you need to shave off some kilobytes of code at the expense of some
-speed (but with the full API), define this symbol to C<1>. Currently this
-is used to override some inlining decisions, saves roughly 30% code size
-on amd64. It also selects a much smaller 2-heap for timer management over
-the default 4-heap.
+This enables priorities (sets C<EV_MAXPRI>=2 and C<EV_MINPRI>=-2), and
+enables multiplicity (C<EV_MULTIPLICITY>=1).
+
+=item C<8> - full API
+
+This enables a lot of the "lesser used" API functions. See C<ev.h> for
+details on which parts of the API are still available without this
+feature, and do not complain if this subset changes over time.
+
+=item C<16> - enable all optional watcher types
+
+Enables all optional watcher types. If you want to selectively enable
+only some watcher types other than I/O and timers (e.g. prepare,
+embed, async, child...) you can enable them manually by defining
+C<EV_watchertype_ENABLE> to C<1> instead.
+
+=item C<32> - enable all backends
+
+This enables all backends - without this feature, you need to enable at
+least one backend manually (C<EV_USE_SELECT> is a good choice).
+
+=item C<64> - enable OS-specific "helper" APIs
+
+Enable inotify, eventfd, signalfd and similar OS-specific helper APIs by
+default.
+
+=back
+
+Compiling with C<gcc -Os -DEV_STANDALONE -DEV_USE_EPOLL=1 -DEV_FEATURES=0>
+reduces the compiled size of libev from 24.7Kb code/2.8Kb data to 6.5Kb
+code/0.3Kb data on my GNU/Linux amd64 system, while still giving you I/O
+watchers, timers and monotonic clock support.
+
+With an intelligent-enough linker (gcc+binutils are intelligent enough
+when you use C<-Wl,--gc-sections -ffunction-sections>) functions unused by
+your program might be left out as well - a binary starting a timer and an
+I/O watcher then might come out at only 5Kb.
-You can save even more by disabling watcher types you do not need
-and setting C<EV_MAXPRI> == C<EV_MINPRI>. Also, disabling C<assert>
-(C<-DNDEBUG>) will usually reduce code size a lot.
+=item EV_AVOID_STDIO
-Defining C<EV_MINIMAL> to C<2> will additionally reduce the core API to
-provide a bare-bones event library. See C<ev.h> for details on what parts
-of the API are still available, and do not complain if this subset changes
-over time.
+If this is set to C<1> at compiletime, then libev will avoid using stdio
+functions (printf, scanf, perror etc.). This will increase the code size
+somewhat, but if your program doesn't otherwise depend on stdio and your
+libc allows it, this avoids linking in the stdio library which is quite
+big.
+
+Note that error messages might become less precise when this option is
+enabled.
=item EV_NSIG
signals): Normally, libev tries to deduce the maximum number of signals
automatically, but sometimes this fails, in which case it can be
specified. Also, using a lower number than detected (C<32> should be
-good for about any system in existance) can save some memory, as libev
+good for about any system in existence) can save some memory, as libev
statically allocates some 12-24 bytes per signal number.
=item EV_PID_HASHSIZE
C<ev_child> watchers use a small hash table to distribute workload by
-pid. The default size is C<16> (or C<1> with C<EV_MINIMAL>), usually more
-than enough. If you need to manage thousands of children you might want to
-increase this value (I<must> be a power of two).
+pid. The default size is C<16> (or C<1> with C<EV_FEATURES> disabled),
+usually more than enough. If you need to manage thousands of children you
+might want to increase this value (I<must> be a power of two).
=item EV_INOTIFY_HASHSIZE
C<ev_stat> watchers use a small hash table to distribute workload by
-inotify watch id. The default size is C<16> (or C<1> with C<EV_MINIMAL>),
-usually more than enough. If you need to manage thousands of C<ev_stat>
-watchers you might want to increase this value (I<must> be a power of
-two).
+inotify watch id. The default size is C<16> (or C<1> with C<EV_FEATURES>
+disabled), usually more than enough. If you need to manage thousands of
+C<ev_stat> watchers you might want to increase this value (I<must> be a
+power of two).
=item EV_USE_4HEAP
to C<1>. The 4-heap uses more complicated (longer) code but has noticeably
faster performance with many (thousands) of watchers.
-The default is C<1> unless C<EV_MINIMAL> is set in which case it is C<0>
-(disabled).
+The default is C<1>, unless C<EV_FEATURES> overrides it, in which case it
+will be C<0>.
=item EV_HEAP_CACHE_AT
but avoids random read accesses on heap changes. This improves performance
noticeably with many (hundreds) of watchers.
-The default is C<1> unless C<EV_MINIMAL> is set in which case it is C<0>
-(disabled).
+The default is C<1>, unless C<EV_FEATURES> overrides it, in which case it
+will be C<0>.
=item EV_VERIFY
-Controls how much internal verification (see C<ev_loop_verify ()>) will
+Controls how much internal verification (see C<ev_verify ()>) will
be done: If set to C<0>, no internal verification code will be compiled
in. If set to C<1>, then verification code will be compiled in, but not
called. If set to C<2>, then the internal verification code will be
verification code will be called very frequently, which will slow down
libev considerably.
-The default is C<1>, unless C<EV_MINIMAL> is set, in which case it will be
-C<0>.
+The default is C<1>, unless C<EV_FEATURES> overrides it, in which case it
+will be C<0>.
=item EV_COMMON
By default, all watchers have a C<void *data> member. By redefining
-this macro to a something else you can include more and other types of
+this macro to something else you can include more and other types of
members. You have to define it each time you include one of the files,
though, and it must be identical each time.
The usage in rxvt-unicode is simpler. It has a F<ev_cpp.h> header file
that everybody includes and which overrides some configure choices:
- #define EV_MINIMAL 1
- #define EV_USE_POLL 0
- #define EV_MULTIPLICITY 0
- #define EV_PERIODIC_ENABLE 0
- #define EV_STAT_ENABLE 0
- #define EV_FORK_ENABLE 0
+ #define EV_FEATURES 8
+ #define EV_USE_SELECT 1
+ #define EV_PREPARE_ENABLE 1
+ #define EV_IDLE_ENABLE 1
+ #define EV_SIGNAL_ENABLE 1
+ #define EV_CHILD_ENABLE 1
+ #define EV_USE_STDEXCEPT 0
#define EV_CONFIG_H <config.h>
- #define EV_MINPRI 0
- #define EV_MAXPRI 0
#include "ev++.h"
}
The event loop thread first acquires the mutex, and then jumps straight
-into C<ev_loop>:
+into C<ev_run>:
void *
l_run (void *thr_arg)
l_acquire (EV_A);
pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
- ev_loop (EV_A_ 0);
+ ev_run (EV_A_ 0);
l_release (EV_A);
return 0;
Libev is very accommodating to coroutines ("cooperative threads"):
libev fully supports nesting calls to its functions from different
-coroutines (e.g. you can call C<ev_loop> on the same loop from two
+coroutines (e.g. you can call C<ev_run> on the same loop from two
different coroutines, and switch freely between both coroutines running
the loop, as long as you don't confuse yourself). The only exception is
that you must not do this from C<ev_periodic> reschedule callbacks.
Care has been taken to ensure that libev does not keep local state inside
-C<ev_loop>, and other calls do not usually allow for coroutine switches as
+C<ev_run>, and other calls do not usually allow for coroutine switches as
they do not call any callbacks.
=head2 COMPILER WARNINGS
And of course, some compiler warnings are just plain stupid, or simply
wrong (because they don't actually warn about the condition their message
seems to warn about). For example, certain older gcc versions had some
-warnings that resulted an extreme number of false positives. These have
+warnings that resulted in an extreme number of false positives. These have
been fixed, but some people still insist on making code warn-free with
such buggy versions.
=head1 PORTABILITY NOTES
+=head2 GNU/LINUX 32 BIT LIMITATIONS
+
+GNU/Linux is the only common platform that supports 64 bit file/large file
+interfaces but I<disables> them by default.
+
+That means that libev compiled in the default environment doesn't support
+files larger than 2GiB or so, which mainly affects C<ev_stat> watchers.
+
+Unfortunately, many programs try to work around this GNU/Linux issue
+by enabling the large file API, which makes them incompatible with the
+standard libev compiled for their system.
+
+Likewise, libev cannot enable the large file API itself as this would
+suddenly make it incompatible to the default compile time environment,
+i.e. all programs not using special compile switches.
+
+=head2 OS/X AND DARWIN BUGS
+
+The whole thing is a bug if you ask me - basically any system interface
+you touch is broken, whether it is locales, poll, kqueue or even the
+OpenGL drivers.
+
+=head3 C<kqueue> is buggy
+
+The kqueue syscall is broken in all known versions - most versions support
+only sockets, many support pipes.
+
+Libev tries to work around this by not using C<kqueue> by default on
+this rotten platform, but of course you can still ask for it when creating
+a loop.
+
+=head3 C<poll> is buggy
+
+Instead of fixing C<kqueue>, Apple replaced their (working) C<poll>
+implementation by something calling C<kqueue> internally around the 10.5.6
+release, so now C<kqueue> I<and> C<poll> are broken.
+
+Libev tries to work around this by not using C<poll> by default on
+this rotten platform, but of course you can still ask for it when creating
+a loop.
+
+=head3 C<select> is buggy
+
+All that's left is C<select>, and of course Apple found a way to fuck this
+one up as well: On OS/X, C<select> actively limits the number of file
+descriptors you can pass in to 1024 - your program suddenly crashes when
+you use more.
+
+There is an undocumented "workaround" for this - defining
+C<_DARWIN_UNLIMITED_SELECT>, which libev tries to use, so select I<should>
+work on OS/X.
+
+=head2 SOLARIS PROBLEMS AND WORKAROUNDS
+
+=head3 C<errno> reentrancy
+
+The default compile environment on Solaris is unfortunately so
+thread-unsafe that you can't even use components/libraries compiled
+without C<-D_REENTRANT> (as long as they use C<errno>), which, of course,
+isn't defined by default.
+
+If you want to use libev in threaded environments you have to make sure
+it's compiled with C<_REENTRANT> defined.
+
+=head3 Event port backend
+
+The scalable event interface for Solaris is called "event ports". Unfortunately,
+this mechanism is very buggy. If you run into high CPU usage, your program
+freezes or you get a large number of spurious wakeups, make sure you have
+all the relevant and latest kernel patches applied. No, I don't know which
+ones, but there are multiple ones.
+
+If you can't get it to work, you can try running the program by setting
+the environment variable C<LIBEV_FLAGS=3> to only allow C<poll> and
+C<select> backends.
+
+=head2 AIX POLL BUG
+
+AIX unfortunately has a broken C<poll.h> header. Libev works around
+this by trying to avoid the poll backend altogether (i.e. it's not even
+compiled in), which normally isn't a big problem as C<select> works fine
+with large bitsets, and AIX is dead anyway.
+
=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS
+=head3 General issues
+
Win32 doesn't support any of the standards (e.g. POSIX) that libev
requires, and its I/O model is fundamentally incompatible with the POSIX
model. Libev still offers limited functionality on this platform in
the form of the C<EVBACKEND_SELECT> backend, and only supports socket
descriptors. This only applies when using Win32 natively, not when using
-e.g. cygwin.
+e.g. cygwin. Actually, it only applies to the microsofts own compilers,
+as every compielr comes with a slightly differently broken/incompatible
+environment.
Lifting these limitations would basically require the full
-re-implementation of the I/O system. If you are into these kinds of
-things, then note that glib does exactly that for you in a very portable
-way (note also that glib is the slowest event library known to man).
+re-implementation of the I/O system. If you are into this kind of thing,
+then note that glib does exactly that for you in a very portable way (note
+also that glib is the slowest event library known to man).
There is no supported compilation method available on windows except
embedding it into other applications.
#include "evwrap.h"
#include "ev.c"
-=over 4
-
-=item The winsocket select function
+=head3 The winsocket C<select> function
The winsocket C<select> function doesn't follow POSIX in that it
requires socket I<handles> and not socket I<file descriptors> (it is
Note that winsockets handling of fd sets is O(n), so you can easily get a
complexity in the O(n²) range when using win32.
-=item Limited number of file descriptors
+=head3 Limited number of file descriptors
Windows has numerous arbitrary (and low) limits on things.
you need to wrap all I/O functions and provide your own fd management, but
the cost of calling select (O(n²)) will likely make this unworkable.
-=back
-
=head2 PORTABILITY REQUIREMENTS
In addition to a working ISO-C implementation and of course the
=item C<double> must hold a time value in seconds with enough accuracy
The type C<double> is used to represent timestamps. It is required to
-have at least 51 bits of mantissa (and 9 bits of exponent), which is good
-enough for at least into the year 4000. This requirement is fulfilled by
-implementations implementing IEEE 754, which is basically all existing
-ones. With IEEE 754 doubles, you get microsecond accuracy until at least
-2200.
+have at least 51 bits of mantissa (and 9 bits of exponent), which is
+good enough for at least into the year 4000 with millisecond accuracy
+(the design goal for libev). This requirement is overfulfilled by
+implementations using IEEE 754, which is basically all existing ones. With
+IEEE 754 doubles, you get microsecond accuracy until at least 2200.
=back
=back
+=head1 PORTING FROM LIBEV 3.X TO 4.X
+
+The major version 4 introduced some minor incompatible changes to the API.
+
+At the moment, the C<ev.h> header file tries to implement superficial
+compatibility, so most programs should still compile. Those might be
+removed in later versions of libev, so better update early than late.
+
+=over 4
+
+=item function/symbol renames
+
+A number of functions and symbols have been renamed:
+
+ ev_loop => ev_run
+ EVLOOP_NONBLOCK => EVRUN_NOWAIT
+ EVLOOP_ONESHOT => EVRUN_ONCE
+
+ ev_unloop => ev_break
+ EVUNLOOP_CANCEL => EVBREAK_CANCEL
+ EVUNLOOP_ONE => EVBREAK_ONE
+ EVUNLOOP_ALL => EVBREAK_ALL
+
+ EV_TIMEOUT => EV_TIMER
+
+ ev_loop_count => ev_iteration
+ ev_loop_depth => ev_depth
+ ev_loop_verify => ev_verify
+
+Most functions working on C<struct ev_loop> objects don't have an
+C<ev_loop_> prefix, so it was removed; C<ev_loop>, C<ev_unloop> and
+associated constants have been renamed to not collide with the C<struct
+ev_loop> anymore and C<EV_TIMER> now follows the same naming scheme
+as all other watcher types. Note that C<ev_loop_fork> is still called
+C<ev_loop_fork> because it would otherwise clash with the C<ev_fork>
+typedef.
+
+=item C<EV_COMPAT3> backwards compatibility mechanism
+
+The backward compatibility mechanism can be controlled by
+C<EV_COMPAT3>. See L<PREPROCESSOR SYMBOLS/MACROS> in the L<EMBEDDING>
+section.
+
+=item C<EV_MINIMAL> mechanism replaced by C<EV_FEATURES>
+
+The preprocessor symbol C<EV_MINIMAL> has been replaced by a different
+mechanism, C<EV_FEATURES>. Programs using C<EV_MINIMAL> usually compile
+and work, but the library code will of course be larger.
+
+=back
+
+
=head1 GLOSSARY
=over 4
any other events happening anymore.
In libev, events are represented as single bits (such as C<EV_READ> or
-C<EV_TIMEOUT>).
+C<EV_TIMER>).
=item event library