summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2011-01-11 08:45:32 -0800
committerRyan Dahl <ry@tinyclouds.org>2011-01-11 08:47:18 -0800
commitb7419dfaadc512f46898f4ca8dae30b19fede83f (patch)
tree7ee2deb446805afaa45de44b6516e79093e2400c
parent01148265cbf27548cf4a55024e2677119e237d2d (diff)
downloadnode-new-b7419dfaadc512f46898f4ca8dae30b19fede83f.tar.gz
Upgrade libev to 4.3
-rw-r--r--deps/libev/Changes53
-rw-r--r--deps/libev/Makefile.am4
-rw-r--r--deps/libev/autogen.sh2
-rw-r--r--deps/libev/config.h.in28
-rw-r--r--deps/libev/configure.ac2
-rw-r--r--deps/libev/ev++.h41
-rw-r--r--deps/libev/ev.31900
-rw-r--r--deps/libev/ev.c299
-rw-r--r--deps/libev/ev.h137
-rw-r--r--deps/libev/ev.pod1152
-rw-r--r--deps/libev/ev_epoll.c41
-rw-r--r--deps/libev/ev_port.c29
-rw-r--r--deps/libev/ev_vars.h18
-rw-r--r--deps/libev/ev_wrap.h18
-rw-r--r--deps/libev/event.c9
-rw-r--r--deps/libev/event.h2
16 files changed, 2446 insertions, 1289 deletions
diff --git a/deps/libev/Changes b/deps/libev/Changes
index 06379c8ada..53bb646195 100644
--- a/deps/libev/Changes
+++ b/deps/libev/Changes
@@ -1,12 +1,33 @@
Revision history for libev, a high-performance and full-featured event loop.
-TODO: include ev_xyz_start in each example?
-TODO: section watcher states/lifetime
-TODO: use enums //TODO: enum?
+4.03 Tue Jan 11 14:37:25 CET 2011
+ - officially support polling files with all backends.
+ - support files, /dev/zero etc. the same way as select in the epoll
+ backend, by generating events on our own.
+ - ports backend: work around solaris bug 6874410 and many related ones
+ (EINTR, maybe more), with no performance loss (note that the solaris
+ bug report is actually wrong, reality is far more bizarre and broken
+ than that).
+ - define EV_READ/EV_WRITE as macros in event.h, as some programs use
+ #ifdef to test for them.
+ - new (experimental) function: ev_feed_signal.
+ - new (to become default) EVFLAG_NOSIGMASK flag.
+ - new EVBACKEND_MASK symbol.
+ - updated COMMON IDIOMS SECTION.
+
+4.01 Fri Nov 5 21:51:29 CET 2010
+ - automake fucked it up, apparently, --add-missing -f is not quite enough
+ to make it update its files, so 4.00 didn't install ev++.h and
+ event.h on make install. grrr.
+ - ev_loop(count|depth) didn't return anything (Robin Haberkorn).
+ - change EV_UNDEF to 0xffffffff to silence some overzealous compilers.
+ - use "(libev) " prefix for all libev error messages now.
+
+4.00 Mon Oct 25 12:32:12 CEST 2010
- "PORTING FROM LIBEV 3.X TO 4.X" (in ev.pod) is recommended reading.
- ev_embed_stop did not correctly stop the watcher (very good
testcase by Vladimir Timofeev).
- - ev_run will now always update the current loop time - it errornously
+ - ev_run will now always update the current loop time - it erroneously
didn't when idle watchers were active, causing timers not to fire.
- fix a bug where a timeout of zero caused the timer not to fire
in the libevent emulation (testcase by Péter Szabó).
@@ -14,6 +35,8 @@ TODO: use enums //TODO: enum?
- replace EV_MINIMAL by EV_FEATURES.
- prefer EPOLL_CTL_ADD over EPOLL_CTL_MOD in some more cases, as it
seems the former is *much* faster than the latter.
+ - linux kernel version detection (for inotify bug workarounds)
+ did not work properly.
- reduce the number of spurious wake-ups with the ports backend.
- remove dependency on sys/queue.h on freebsd (patch by Vanilla Hsu).
- do async init within ev_async_start, not ev_async_set, which avoids
@@ -24,9 +47,11 @@ TODO: use enums //TODO: enum?
not to need it (helpfully pointed out by Tilghman Lesher).
- do not use poll by default on freebsd, it's broken (what isn't
on freebsd...).
+ - allow to embed epoll on kernels >= 2.6.32.
- configure now prepends -O3, not appends it, so one can still
override it.
- - greatly expanded the portability section.
+ - ev.pod: greatly expanded the portability section, added a porting
+ section, a description of watcher states and made lots of minor fixes.
- disable poll backend on AIX, the poll header spams the namespace
and it's not worth working around dead platforms (reported
and analyzed by Aivars Kalvans).
@@ -39,12 +64,18 @@ TODO: use enums //TODO: enum?
- update libev.m4 HAVE_CLOCK_SYSCALL test for newer glibcs.
- add section on accept() problems to the manpage.
- rename EV_TIMEOUT to EV_TIMER.
- - rename ev_loop_count/depth/verify.
+ - rename ev_loop_count/depth/verify/loop/unloop.
+ - remove ev_default_destroy and ev_default_fork.
- switch to two-digit minor version.
- work around an apparent gentoo compiler bug.
- define _DARWIN_UNLIMITED_SELECT. just so.
- use enum instead of #define for most constants.
- improve compatibility to older C++ compilers.
+ - (experimental) ev_run/ev_default_loop/ev_break/ev_loop_new have now
+ default arguments when compiled as C++.
+ - enable automake dependency tracking.
+ - ev_loop_new no longer leaks memory when loop creation failed.
+ - new ev_cleanup watcher type.
3.9 Thu Dec 31 07:59:59 CET 2009
- signalfd is no longer used by default and has to be requested
@@ -70,14 +101,14 @@ TODO: use enums //TODO: enum?
- add missing string.h include (Denis F. Latypoff).
- only replace ev_stat.prev when we detect an actual difference,
so prev is (almost) always different to attr. this might
- have caused the probems with 04_stat.t.
+ have caused the problems with 04_stat.t.
- add ev::timer->remaining () method to C++ API.
3.8 Sun Aug 9 14:30:45 CEST 2009
- incompatible change: do not necessarily reset signal handler
to SIG_DFL when a sighandler is stopped.
- ev_default_destroy did not properly free or zero some members,
- potentially causing crashes and memory corruption on repated
+ potentially causing crashes and memory corruption on repeated
ev_default_destroy/ev_default_loop calls.
- take advantage of signalfd on GNU/Linux systems.
- document that the signal mask might be in an unspecified
@@ -209,7 +240,7 @@ TODO: use enums //TODO: enum?
3.45 Tue Oct 21 21:59:26 CEST 2008
- disable inotify usage on linux <2.6.25, as it is broken
(reported by Yoann Vandoorselaere).
- - ev_stat errornously would try to add inotify watchers
+ - ev_stat erroneously would try to add inotify watchers
even when inotify wasn't available (this should only
have a performance impact).
- ev_once now passes both timeout and io to the callback if both
@@ -287,10 +318,10 @@ TODO: use enums //TODO: enum?
not clearing revents field in ev_poll (Brandon Black)
(no such systems are known at this time).
- work around a bug in realloc on openbsd and darwin,
- also makes the errornous valgrind complaints
+ also makes the erroneous valgrind complaints
go away (noted by various people).
- fix ev_async_pending, add c++ wrapper for ev_async
- (based on patch sent by Johannes Deisenhofer.
+ (based on patch sent by Johannes Deisenhofer).
- add sensible set method to ev::embed.
- made integer constants type int in ev.h.
diff --git a/deps/libev/Makefile.am b/deps/libev/Makefile.am
index 1b4f7e18d4..058c2cb0c1 100644
--- a/deps/libev/Makefile.am
+++ b/deps/libev/Makefile.am
@@ -1,6 +1,6 @@
-AUTOMAKE_OPTIONS = foreign no-dependencies
+AUTOMAKE_OPTIONS = foreign
-VERSION_INFO = 4:0
+VERSION_INFO = 4:0:0
EXTRA_DIST = LICENSE Changes libev.m4 autogen.sh \
ev_vars.h ev_wrap.h \
diff --git a/deps/libev/autogen.sh b/deps/libev/autogen.sh
index 371b4cde3f..087d2aa4e9 100644
--- a/deps/libev/autogen.sh
+++ b/deps/libev/autogen.sh
@@ -1,6 +1,6 @@
#!/bin/sh
libtoolize --force
-automake --add-missing
+automake --add-missing --force-missing
autoreconf
diff --git a/deps/libev/config.h.in b/deps/libev/config.h.in
index 774e6515b3..75da15cce7 100644
--- a/deps/libev/config.h.in
+++ b/deps/libev/config.h.in
@@ -93,5 +93,33 @@
/* Define to 1 if you have the <unistd.h> header file. */
#undef HAVE_UNISTD_H
+/* Define to the sub-directory in which libtool stores uninstalled libraries.
+ */
+#undef LT_OBJDIR
+
+/* Name of package */
+#undef PACKAGE
+
+/* Define to the address where bug reports for this package should be sent. */
+#undef PACKAGE_BUGREPORT
+
+/* Define to the full name of this package. */
+#undef PACKAGE_NAME
+
+/* Define to the full name and version of this package. */
+#undef PACKAGE_STRING
+
+/* Define to the one symbol short name of this package. */
+#undef PACKAGE_TARNAME
+
+/* Define to the home page for this package. */
+#undef PACKAGE_URL
+
+/* Define to the version of this package. */
+#undef PACKAGE_VERSION
+
/* Define to 1 if you have the ANSI C header files. */
#undef STDC_HEADERS
+
+/* Version number of package */
+#undef VERSION
diff --git a/deps/libev/configure.ac b/deps/libev/configure.ac
index 5047de3f43..5a8ea283ee 100644
--- a/deps/libev/configure.ac
+++ b/deps/libev/configure.ac
@@ -1,7 +1,7 @@
AC_INIT
AC_CONFIG_SRCDIR([ev_epoll.c])
-AM_INIT_AUTOMAKE(libev,4.00) dnl also update ev.h!
+AM_INIT_AUTOMAKE(libev,4.03) dnl also update ev.h!
AC_CONFIG_HEADERS([config.h])
AM_MAINTAINER_MODE
diff --git a/deps/libev/ev++.h b/deps/libev/ev++.h
index b764487fdf..d85be55c52 100644
--- a/deps/libev/ev++.h
+++ b/deps/libev/ev++.h
@@ -58,13 +58,15 @@ namespace ev {
typedef ev_tstamp tstamp;
- enum
- {
+ enum {
UNDEF = EV_UNDEF,
NONE = EV_NONE,
READ = EV_READ,
WRITE = EV_WRITE,
+#if EV_COMPAT3
TIMEOUT = EV_TIMEOUT,
+#endif
+ TIMER = EV_TIMER,
PERIODIC = EV_PERIODIC,
SIGNAL = EV_SIGNAL,
CHILD = EV_CHILD,
@@ -95,14 +97,18 @@ namespace ev {
enum
{
+#if EV_COMPAT3
NONBLOCK = EVLOOP_NONBLOCK,
- ONESHOT = EVLOOP_ONESHOT
+ ONESHOT = EVLOOP_ONESHOT,
+#endif
+ NOWAIT = EVRUN_NOWAIT,
+ ONCE = EVRUN_ONCE
};
enum how_t
{
- ONE = EVUNLOOP_ONE,
- ALL = EVUNLOOP_ALL
+ ONE = EVBREAK_ONE,
+ ALL = EVBREAK_ALL
};
struct bad_loop
@@ -188,23 +194,31 @@ namespace ev {
}
#endif
+#if EV_COMPAT3
void loop (int flags = 0)
{
- ev_loop (EV_AX_ flags);
+ ev_run (EV_AX_ flags);
}
void unloop (how_t how = ONE) throw ()
{
- ev_unloop (EV_AX_ how);
+ ev_break (EV_AX_ how);
+ }
+#endif
+
+ void run (int flags = 0)
+ {
+ ev_run (EV_AX_ flags);
+ }
+
+ void break_loop (how_t how = ONE) throw ()
+ {
+ ev_break (EV_AX_ how);
}
void post_fork () throw ()
{
-#if EV_MULTIPLICITY
ev_loop_fork (EV_AX);
-#else
- ev_default_fork ();
-#endif
}
unsigned int backend () const throw ()
@@ -377,11 +391,6 @@ namespace ev {
throw bad_loop ();
}
- ~default_loop () throw ()
- {
- ev_default_destroy ();
- }
-
private:
default_loop (const default_loop &);
default_loop &operator = (const default_loop &);
diff --git a/deps/libev/ev.3 b/deps/libev/ev.3
index 2c3c846e1f..a5c974ce16 100644
--- a/deps/libev/ev.3
+++ b/deps/libev/ev.3
@@ -124,7 +124,7 @@
.\" ========================================================================
.\"
.IX Title "LIBEV 3"
-.TH LIBEV 3 "2009-12-31" "libev-3.9" "libev - high performance full featured event loop"
+.TH LIBEV 3 "2011-01-11" "libev-4.03" "libev - high performance full featured event loop"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
@@ -159,8 +159,8 @@ libev \- a high performance full\-featured event loop written in C
\& // with its corresponding stop function.
\& ev_io_stop (EV_A_ w);
\&
-\& // this causes all nested ev_loop\*(Aqs to stop iterating
-\& ev_unloop (EV_A_ EVUNLOOP_ALL);
+\& // this causes all nested ev_run\*(Aqs to stop iterating
+\& ev_break (EV_A_ EVBREAK_ALL);
\& }
\&
\& // another callback, this time for a time\-out
@@ -168,15 +168,15 @@ libev \- a high performance full\-featured event loop written in C
\& 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
\& main (void)
\& {
\& // use the default event loop unless you have special needs
-\& struct ev_loop *loop = ev_default_loop (0);
+\& struct ev_loop *loop = EV_DEFAULT;
\&
\& // initialise an io watcher, then start it
\& // this one will watch for stdin to become readable
@@ -189,7 +189,7 @@ libev \- a high performance full\-featured event loop written in C
\& 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;
@@ -208,8 +208,15 @@ libev, its usage and the rationale behind its design, it is not a tutorial
on event-based programming, nor will it introduce event-based programming
with libev.
.PP
-Familarity with event based programming techniques in general is assumed
+Familiarity with event based programming techniques in general is assumed
throughout this document.
+.SH "WHAT TO READ WHEN IN A HURRY"
+.IX Header "WHAT TO READ WHEN IN A HURRY"
+This manual tries to be very detailed, but unfortunately, this also makes
+it very long. If you just want to know the basics of libev, I suggest
+reading \*(L"\s-1ANATOMY\s0 \s-1OF\s0 A \s-1WATCHER\s0\*(R", then the \*(L"\s-1EXAMPLE\s0 \s-1PROGRAM\s0\*(R" above and
+look up the missing functions in \*(L"\s-1GLOBAL\s0 \s-1FUNCTIONS\s0\*(R" and the \f(CW\*(C`ev_io\*(C'\fR and
+\&\f(CW\*(C`ev_timer\*(C'\fR sections in \*(L"\s-1WATCHER\s0 \s-1TYPES\s0\*(R".
.SH "ABOUT LIBEV"
.IX Header "ABOUT LIBEV"
Libev is an event loop: you register interest in certain events (such as a
@@ -253,13 +260,14 @@ this argument.
.SS "\s-1TIME\s0 \s-1REPRESENTATION\s0"
.IX Subsection "TIME REPRESENTATION"
Libev represents time as a single floating point number, representing
-the (fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere
-near the beginning of 1970, details are complicated, don't ask). This
-type is called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually
-aliases to the \f(CW\*(C`double\*(C'\fR 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 \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for time differences
-throughout libev.
+the (fractional) number of seconds since the (\s-1POSIX\s0) epoch (in practice
+somewhere near the beginning of 1970, details are complicated, don't
+ask). This type is called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use
+too. It usually aliases to the \f(CW\*(C`double\*(C'\fR type in C. When you need to do
+any calculations on it, you should treat it as some floating point value.
+.PP
+Unlike the name component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for
+time differences (e.g. delays) throughout libev.
.SH "ERROR HANDLING"
.IX Header "ERROR HANDLING"
Libev knows three classes of errors: operating system errors, usage errors
@@ -287,7 +295,8 @@ library in any way.
.IX Item "ev_tstamp ev_time ()"
Returns the current time as libev would use it. Please note that the
\&\f(CW\*(C`ev_now\*(C'\fR function is usually faster and also often returns the timestamp
-you actually want to know.
+you actually want to know. Also interesting is the combination of
+\&\f(CW\*(C`ev_update_now\*(C'\fR and \f(CW\*(C`ev_now\*(C'\fR.
.IP "ev_sleep (ev_tstamp interval)" 4
.IX Item "ev_sleep (ev_tstamp interval)"
Sleep for the given interval: The current thread will be blocked until
@@ -314,7 +323,8 @@ compatible to older versions, so a larger minor version alone is usually
not a problem.
.Sp
Example: Make sure we haven't accidentally been linked against the wrong
-version.
+version (note, however, that this will not detect other \s-1ABI\s0 mismatches,
+such as \s-1LFS\s0 or reentrancy).
.Sp
.Vb 3
\& assert (("libev version mismatch",
@@ -337,23 +347,24 @@ a must have and can we have a torrent of it please!!!11
.Ve
.IP "unsigned int ev_recommended_backends ()" 4
.IX Item "unsigned int ev_recommended_backends ()"
-Return the set of all backends compiled into this binary of libev and also
-recommended for this platform. This set is often smaller than the one
-returned by \f(CW\*(C`ev_supported_backends\*(C'\fR, as for example kqueue is broken on
-most BSDs and will not be auto-detected unless you explicitly request it
-(assuming you know what you are doing). This is the set of backends that
-libev will probe for if you specify no backends explicitly.
+Return the set of all backends compiled into this binary of libev and
+also recommended for this platform, meaning it will work for most file
+descriptor types. This set is often smaller than the one returned by
+\&\f(CW\*(C`ev_supported_backends\*(C'\fR, as for example kqueue is broken on most BSDs
+and will not be auto-detected unless you explicitly request it (assuming
+you know what you are doing). This is the set of backends that libev will
+probe for if you specify no backends explicitly.
.IP "unsigned int ev_embeddable_backends ()" 4
.IX Item "unsigned int ev_embeddable_backends ()"
Returns the set of backends that are embeddable in other event loops. This
-is the theoretical, all-platform, value. To find which backends
-might be supported on the current system, you would need to look at
-\&\f(CW\*(C`ev_embeddable_backends () & ev_supported_backends ()\*(C'\fR, likewise for
-recommended ones.
+value is platform-specific but can include backends not available on the
+current system. To find which embeddable backends might be supported on
+the current system, you would need to look at \f(CW\*(C`ev_embeddable_backends ()
+& ev_supported_backends ()\*(C'\fR, likewise for recommended ones.
.Sp
See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info.
-.IP "ev_set_allocator (void *(*cb)(void *ptr, long size)) [\s-1NOT\s0 \s-1REENTRANT\s0]" 4
-.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size)) [NOT REENTRANT]"
+.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4
+.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))"
Sets the allocation function to use (the prototype is similar \- the
semantics are identical to the \f(CW\*(C`realloc\*(C'\fR C89/SuS/POSIX function). It is
used to allocate and free memory (no surprises here). If it returns zero
@@ -389,8 +400,8 @@ retries (example requires a standards-compliant \f(CW\*(C`realloc\*(C'\fR).
\& ...
\& ev_set_allocator (persistent_realloc);
.Ve
-.IP "ev_set_syserr_cb (void (*cb)(const char *msg)); [\s-1NOT\s0 \s-1REENTRANT\s0]" 4
-.IX Item "ev_set_syserr_cb (void (*cb)(const char *msg)); [NOT REENTRANT]"
+.IP "ev_set_syserr_cb (void (*cb)(const char *msg))" 4
+.IX Item "ev_set_syserr_cb (void (*cb)(const char *msg))"
Set the callback function to call on a retryable system call error (such
as failed select, poll, epoll_wait). The message is a printable string
indicating the system call or subsystem causing the problem. If this
@@ -412,35 +423,75 @@ Example: This is basically the same thing that libev does internally, too.
\& ...
\& ev_set_syserr_cb (fatal_error);
.Ve
-.SH "FUNCTIONS CONTROLLING THE EVENT LOOP"
-.IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP"
-An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR (the \f(CW\*(C`struct\*(C'\fR
-is \fInot\fR optional in this case, as there is also an \f(CW\*(C`ev_loop\*(C'\fR
-\&\fIfunction\fR).
+.IP "ev_feed_signal (int signum)" 4
+.IX Item "ev_feed_signal (int signum)"
+This function can be used to \*(L"simulate\*(R" a signal receive. It is completely
+safe to call this function at any time, from any context, including signal
+handlers or random threads.
+.Sp
+Its main use is to customise signal handling in your process, especially
+in the presence of threads. For example, you could block signals
+by default in all threads (and specifying \f(CW\*(C`EVFLAG_NOSIGMASK\*(C'\fR when
+creating any loops), and in one thread, use \f(CW\*(C`sigwait\*(C'\fR or any other
+mechanism to wait for signals, then \*(L"deliver\*(R" them to libev by calling
+\&\f(CW\*(C`ev_feed_signal\*(C'\fR.
+.SH "FUNCTIONS CONTROLLING EVENT LOOPS"
+.IX Header "FUNCTIONS CONTROLLING EVENT LOOPS"
+An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR (the \f(CW\*(C`struct\*(C'\fR is
+\&\fInot\fR optional in this case unless libev 3 compatibility is disabled, as
+libev 3 had an \f(CW\*(C`ev_loop\*(C'\fR function colliding with the struct name).
.PP
The library knows two types of such loops, the \fIdefault\fR loop, which
-supports signals and child events, and dynamically created loops which do
-not.
+supports child process events, and dynamically created event loops which
+do not.
.IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4
.IX Item "struct ev_loop *ev_default_loop (unsigned int flags)"
-This will initialise the default event loop if it hasn't been initialised
-yet and return it. If the default loop could not be initialised, returns
-false. If it already was initialised it simply returns it (and ignores the
-flags. If that is troubling you, check \f(CW\*(C`ev_backend ()\*(C'\fR afterwards).
+This returns the \*(L"default\*(R" event loop object, which is what you should
+normally use when you just need \*(L"the event loop\*(R". Event loop objects and
+the \f(CW\*(C`flags\*(C'\fR parameter are described in more detail in the entry for
+\&\f(CW\*(C`ev_loop_new\*(C'\fR.
+.Sp
+If the default loop is already initialised then this function simply
+returns it (and ignores the flags. If that is troubling you, check
+\&\f(CW\*(C`ev_backend ()\*(C'\fR afterwards). Otherwise it will create it with the given
+flags, which should almost always be \f(CW0\fR, unless the caller is also the
+one calling \f(CW\*(C`ev_run\*(C'\fR or otherwise qualifies as \*(L"the main program\*(R".
.Sp
If you don't know what event loop to use, use the one returned from this
-function.
+function (or via the \f(CW\*(C`EV_DEFAULT\*(C'\fR macro).
.Sp
Note that this function is \fInot\fR thread-safe, so if you want to use it
-from multiple threads, you have to lock (note also that this is unlikely,
-as loops cannot be shared easily between threads anyway).
+from multiple threads, you have to employ some kind of mutex (note also
+that this case is unlikely, as loops cannot be shared easily between
+threads anyway).
+.Sp
+The default loop is the only loop that can handle \f(CW\*(C`ev_child\*(C'\fR watchers,
+and to do this, it always registers a handler for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is
+a problem for your application you can either create a dynamic loop with
+\&\f(CW\*(C`ev_loop_new\*(C'\fR which doesn't do that, or you can simply overwrite the
+\&\f(CW\*(C`SIGCHLD\*(C'\fR signal handler \fIafter\fR calling \f(CW\*(C`ev_default_init\*(C'\fR.
.Sp
-The default loop is the only loop that can handle \f(CW\*(C`ev_signal\*(C'\fR and
-\&\f(CW\*(C`ev_child\*(C'\fR watchers, and to do this, it always registers a handler
-for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is a problem for your application you can either
-create a dynamic loop with \f(CW\*(C`ev_loop_new\*(C'\fR that doesn't do that, or you
-can simply overwrite the \f(CW\*(C`SIGCHLD\*(C'\fR signal handler \fIafter\fR calling
-\&\f(CW\*(C`ev_default_init\*(C'\fR.
+Example: This is the most typical usage.
+.Sp
+.Vb 2
+\& if (!ev_default_loop (0))
+\& fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?");
+.Ve
+.Sp
+Example: Restrict libev to the select and poll backends, and do not allow
+environment settings to be taken into account:
+.Sp
+.Vb 1
+\& ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV);
+.Ve
+.IP "struct ev_loop *ev_loop_new (unsigned int flags)" 4
+.IX Item "struct ev_loop *ev_loop_new (unsigned int flags)"
+This will create and initialise a new event loop object. If the loop
+could not be initialised, returns false.
+.Sp
+This function 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 \*(L"main\*(R" or \*(L"initial\*(R" thread.
.Sp
The flags argument can be used to specify special behaviour or specific
backends to use, and is usually specified as \f(CW0\fR (or \f(CW\*(C`EVFLAG_AUTO\*(C'\fR).
@@ -464,9 +515,8 @@ around bugs.
.ie n .IP """EVFLAG_FORKCHECK""" 4
.el .IP "\f(CWEVFLAG_FORKCHECK\fR" 4
.IX Item "EVFLAG_FORKCHECK"
-Instead of calling \f(CW\*(C`ev_default_fork\*(C'\fR or \f(CW\*(C`ev_loop_fork\*(C'\fR manually after
-a fork, you can also make libev check for a fork in each iteration by
-enabling this flag.
+Instead of calling \f(CW\*(C`ev_loop_fork\*(C'\fR manually after a fork, you can also
+make libev check for a fork in each iteration by enabling this flag.
.Sp
This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop,
and thus this might slow down your event loop if you do a lot of loop
@@ -485,14 +535,14 @@ environment variable.
.el .IP "\f(CWEVFLAG_NOINOTIFY\fR" 4
.IX Item "EVFLAG_NOINOTIFY"
When this flag is specified, then libev will not attempt to use the
-\&\fIinotify\fR \s-1API\s0 for it's \f(CW\*(C`ev_stat\*(C'\fR watchers. Apart from debugging and
+\&\fIinotify\fR \s-1API\s0 for its \f(CW\*(C`ev_stat\*(C'\fR watchers. Apart from debugging and
testing, this flag can be useful to conserve inotify file descriptors, as
otherwise each loop using \f(CW\*(C`ev_stat\*(C'\fR watchers consumes one inotify handle.
.ie n .IP """EVFLAG_SIGNALFD""" 4
.el .IP "\f(CWEVFLAG_SIGNALFD\fR" 4
.IX Item "EVFLAG_SIGNALFD"
When this flag is specified, then libev will attempt to use the
-\&\fIsignalfd\fR \s-1API\s0 for it's \f(CW\*(C`ev_signal\*(C'\fR (and \f(CW\*(C`ev_child\*(C'\fR) watchers. This \s-1API\s0
+\&\fIsignalfd\fR \s-1API\s0 for its \f(CW\*(C`ev_signal\*(C'\fR (and \f(CW\*(C`ev_child\*(C'\fR) watchers. This \s-1API\s0
delivers signals synchronously, which makes it both faster and might make
it possible to get the queued signal data. It can also simplify signal
handling with threads, as long as you properly block signals in your
@@ -501,6 +551,18 @@ threads that are not interested in handling them.
Signalfd will not be used by default as this changes your signal mask, and
there are a lot of shoddy libraries and programs (glib's threadpool for
example) that can't properly initialise their signal masks.
+.ie n .IP """EVFLAG_NOSIGMASK""" 4
+.el .IP "\f(CWEVFLAG_NOSIGMASK\fR" 4
+.IX Item "EVFLAG_NOSIGMASK"
+When this flag is specified, then libev will avoid to modify the signal
+mask. Specifically, this means you ahve to make sure signals are unblocked
+when you want to receive them.
+.Sp
+This behaviour is useful when you want to do your own signal handling, or
+want to handle signals only in specific threads and want to avoid libev
+unblocking the signals.
+.Sp
+This flag's behaviour will become the default in future versions of libev.
.ie n .IP """EVBACKEND_SELECT"" (value 1, portable select backend)" 4
.el .IP "\f(CWEVBACKEND_SELECT\fR (value 1, portable select backend)" 4
.IX Item "EVBACKEND_SELECT (value 1, portable select backend)"
@@ -546,11 +608,13 @@ epoll scales either O(1) or O(active_fds).
The epoll mechanism deserves honorable mention as the most misdesigned
of the more advanced event mechanisms: mere annoyances include silently
dropping file descriptors, requiring a system call per change per file
-descriptor (and unnecessary guessing of parameters), problems with dup and
-so on. The biggest issue is fork races, however \- if a program forks then
-\&\fIboth\fR parent and child process have to recreate the epoll set, which can
-take considerable time (one syscall per file descriptor) and is of course
-hard to detect.
+descriptor (and unnecessary guessing of parameters), problems with dup,
+returning before the timeout value, resulting in additional iterations
+(and only giving 5ms accuracy while select on the same platform gives
+0.1ms) and so on. The biggest issue is fork races, however \- if a program
+forks then \fIboth\fR parent and child process have to recreate the epoll
+set, which can take considerable time (one syscall per file descriptor)
+and is of course hard to detect.
.Sp
Epoll is also notoriously buggy \- embedding epoll fds \fIshould\fR work, but
of course \fIdoesn't\fR, and epoll just loves to report events for totally
@@ -558,7 +622,13 @@ of course \fIdoesn't\fR, and epoll just loves to report events for totally
even remove them from the set) than registered in the set (especially
on \s-1SMP\s0 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 \f(CW\*(C`select\*(C'\fR (files, many character devices...).
+.Sp
+Epoll is truly the train wreck analog among event poll mechanisms,
+a frankenpoll, cobbled together in a hurry, no thought to design or
+interaction with others.
.Sp
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
@@ -634,19 +704,25 @@ immensely.
This uses the Solaris 10 event port mechanism. As with everything on Solaris,
it's really slow, but it still scales very well (O(active_fds)).
.Sp
-Please note that Solaris event ports can deliver a lot of spurious
-notifications, so you need to use non-blocking I/O or other means to avoid
-blocking when no data (or space) is available.
-.Sp
While this backend scales well, it requires one system call per active
file descriptor per loop iteration. For small and medium numbers of file
descriptors a \*(L"slow\*(R" \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR backend
might perform better.
.Sp
-On the positive side, with the exception of the spurious readiness
-notifications, this backend actually performed fully to specification
-in all tests and is fully embeddable, which is a rare feat among the
-OS-specific backends (I vastly prefer correctness over speed hacks).
+On the positive side, this backend actually performed fully to
+specification in all tests and is fully embeddable, which is a rare feat
+among the OS-specific backends (I vastly prefer correctness over speed
+hacks).
+.Sp
+On the negative side, the interface is \fIbizarre\fR \- so bizarre that
+even sun itself gets it wrong in their code examples: The event polling
+function sometimes returning events to the caller even though an error
+occurred, but with no indication whether it has done so or not (yes, it's
+even documented that way) \- deadly for edge-triggered interfaces where
+you absolutely have to know whether an event occurred or not because you
+have to re-arm the watcher.
+.Sp
+Fortunately libev seems to be able to work around these idiocies.
.Sp
This backend maps \f(CW\*(C`EV_READ\*(C'\fR and \f(CW\*(C`EV_WRITE\*(C'\fR in the same way as
\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR.
@@ -657,7 +733,15 @@ Try all backends (even potentially broken ones that wouldn't be tried
with \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). Since this is a mask, you can do stuff such as
\&\f(CW\*(C`EVBACKEND_ALL & ~EVBACKEND_KQUEUE\*(C'\fR.
.Sp
-It is definitely not recommended to use this flag.
+It is definitely not recommended to use this flag, use whatever
+\&\f(CW\*(C`ev_recommended_backends ()\*(C'\fR returns, or simply do not specify a backend
+at all.
+.ie n .IP """EVBACKEND_MASK""" 4
+.el .IP "\f(CWEVBACKEND_MASK\fR" 4
+.IX Item "EVBACKEND_MASK"
+Not a backend at all, but a mask to select all backend bits from a
+\&\f(CW\*(C`flags\*(C'\fR value, in case you want to mask out any backends from a flags
+value (e.g. when modifying the \f(CW\*(C`LIBEV_FLAGS\*(C'\fR environment variable).
.RE
.RS 4
.Sp
@@ -666,50 +750,24 @@ then only these backends will be tried (in the reverse order as listed
here). If none are specified, all backends in \f(CW\*(C`ev_recommended_backends
()\*(C'\fR will be tried.
.Sp
-Example: This is the most typical usage.
-.Sp
-.Vb 2
-\& if (!ev_default_loop (0))
-\& fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?");
-.Ve
-.Sp
-Example: Restrict libev to the select and poll backends, and do not allow
-environment settings to be taken into account:
+Example: Try to create a event loop that uses epoll and nothing else.
.Sp
-.Vb 1
-\& ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV);
+.Vb 3
+\& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV);
+\& if (!epoller)
+\& fatal ("no epoll found here, maybe it hides under your chair");
.Ve
.Sp
Example: Use whatever libev has to offer, but make sure that kqueue is
-used if available (warning, breaks stuff, best use only with your own
-private event loop and only if you know the \s-1OS\s0 supports your types of
-fds):
+used if available.
.Sp
.Vb 1
-\& ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE);
+\& struct ev_loop *loop = ev_loop_new (ev_recommended_backends () | EVBACKEND_KQUEUE);
.Ve
.RE
-.IP "struct ev_loop *ev_loop_new (unsigned int flags)" 4
-.IX Item "struct ev_loop *ev_loop_new (unsigned int flags)"
-Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, 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).
-.Sp
-Note that this function \fIis\fR thread-safe, and the recommended way to use
-libev with threads is indeed to create one loop per thread, and using the
-default loop in the \*(L"main\*(R" or \*(L"initial\*(R" thread.
-.Sp
-Example: Try to create a event loop that uses epoll and nothing else.
-.Sp
-.Vb 3
-\& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV);
-\& if (!epoller)
-\& fatal ("no epoll found here, maybe it hides under your chair");
-.Ve
-.IP "ev_default_destroy ()" 4
-.IX Item "ev_default_destroy ()"
-Destroys the default loop again (frees all memory and kernel state
+.IP "ev_loop_destroy (loop)" 4
+.IX Item "ev_loop_destroy (loop)"
+Destroys an event loop object (frees all memory and kernel state
etc.). None of the active event watchers will be stopped in the normal
sense, so e.g. \f(CW\*(C`ev_is_active\*(C'\fR might still return true. It is your
responsibility to either stop all watchers cleanly yourself \fIbefore\fR
@@ -721,64 +779,77 @@ Note that certain global state, such as signal state (and installed signal
handlers), will not be freed by this function, and related watchers (such
as signal and child watchers) would need to be stopped manually.
.Sp
-In general it is not advisable to call this function except in the
-rare occasion where you really need to free e.g. the signal handling
-pipe fds. If you need dynamically allocated loops it is better to use
-\&\f(CW\*(C`ev_loop_new\*(C'\fR and \f(CW\*(C`ev_loop_destroy\*(C'\fR.
-.IP "ev_loop_destroy (loop)" 4
-.IX Item "ev_loop_destroy (loop)"
-Like \f(CW\*(C`ev_default_destroy\*(C'\fR, but destroys an event loop created by an
-earlier call to \f(CW\*(C`ev_loop_new\*(C'\fR.
-.IP "ev_default_fork ()" 4
-.IX Item "ev_default_fork ()"
-This function sets a flag that causes subsequent \f(CW\*(C`ev_loop\*(C'\fR iterations
-to reinitialise the kernel state for backends that have one. Despite the
+This function is normally used on loop objects allocated by
+\&\f(CW\*(C`ev_loop_new\*(C'\fR, but it can also be used on the default loop returned by
+\&\f(CW\*(C`ev_default_loop\*(C'\fR, in which case it is not thread-safe.
+.Sp
+Note that it is not advisable to call this function on the default loop
+except in the rare occasion where you really need to free its resources.
+If you need dynamically allocated loops it is better to use \f(CW\*(C`ev_loop_new\*(C'\fR
+and \f(CW\*(C`ev_loop_destroy\*(C'\fR.
+.IP "ev_loop_fork (loop)" 4
+.IX Item "ev_loop_fork (loop)"
+This function sets a flag that causes subsequent \f(CW\*(C`ev_run\*(C'\fR 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 \fImust\fR call it in the child before using any of the libev
-functions, and it will only take effect at the next \f(CW\*(C`ev_loop\*(C'\fR iteration.
+the child process. You \fImust\fR call it (or use \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR) in the
+child before resuming or calling \f(CW\*(C`ev_run\*(C'\fR.
+.Sp
+Again, you \fIhave\fR to call it on \fIany\fR loop that you want to re-use after
+a fork, \fIeven if you do not plan to use the loop in the parent\fR. This is
+because some kernel interfaces *cough* \fIkqueue\fR *cough* do funny things
+during fork.
.Sp
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, \f(CW\*(C`epoll\*(C'\fR 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).
.Sp
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
-quite nicely into a call to \f(CW\*(C`pthread_atfork\*(C'\fR:
+it just in case after a fork.
.Sp
-.Vb 1
-\& pthread_atfork (0, 0, ev_default_fork);
+Example: Automate calling \f(CW\*(C`ev_loop_fork\*(C'\fR on the default loop when
+using pthreads.
+.Sp
+.Vb 5
+\& static void
+\& post_fork_child (void)
+\& {
+\& ev_loop_fork (EV_DEFAULT);
+\& }
+\&
+\& ...
+\& pthread_atfork (0, 0, post_fork_child);
.Ve
-.IP "ev_loop_fork (loop)" 4
-.IX Item "ev_loop_fork (loop)"
-Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by
-\&\f(CW\*(C`ev_loop_new\*(C'\fR. 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.
.IP "int ev_is_default_loop (loop)" 4
.IX Item "int ev_is_default_loop (loop)"
Returns true when the given loop is, in fact, the default loop, and false
otherwise.
-.IP "unsigned int ev_loop_count (loop)" 4
-.IX Item "unsigned int ev_loop_count (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 \f(CW0\fR and
-happily wraps around with enough iterations.
+.IP "unsigned int ev_iteration (loop)" 4
+.IX Item "unsigned int ev_iteration (loop)"
+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 \f(CW0\fR
+and happily wraps around with enough iterations.
.Sp
This value can sometimes be useful as a generation counter of sorts (it
\&\*(L"ticks\*(R" the number of loop iterations), as it roughly corresponds with
-\&\f(CW\*(C`ev_prepare\*(C'\fR and \f(CW\*(C`ev_check\*(C'\fR calls.
-.IP "unsigned int ev_loop_depth (loop)" 4
-.IX Item "unsigned int ev_loop_depth (loop)"
-Returns the number of times \f(CW\*(C`ev_loop\*(C'\fR was entered minus the number of
-times \f(CW\*(C`ev_loop\*(C'\fR was exited, in other words, the recursion depth.
-.Sp
-Outside \f(CW\*(C`ev_loop\*(C'\fR, this number is zero. In a callback, this number is
-\&\f(CW1\fR, unless \f(CW\*(C`ev_loop\*(C'\fR was invoked recursively (or from another thread),
+\&\f(CW\*(C`ev_prepare\*(C'\fR and \f(CW\*(C`ev_check\*(C'\fR calls \- and is incremented between the
+prepare and check phases.
+.IP "unsigned int ev_depth (loop)" 4
+.IX Item "unsigned int ev_depth (loop)"
+Returns the number of times \f(CW\*(C`ev_run\*(C'\fR was entered minus the number of
+times \f(CW\*(C`ev_run\*(C'\fR was exited normally, in other words, the recursion depth.
+.Sp
+Outside \f(CW\*(C`ev_run\*(C'\fR, this number is zero. In a callback, this number is
+\&\f(CW1\fR, unless \f(CW\*(C`ev_run\*(C'\fR was invoked recursively (or from another thread),
in which case it is higher.
.Sp
-Leaving \f(CW\*(C`ev_loop\*(C'\fR abnormally (setjmp/longjmp, cancelling the thread
-etc.), doesn't count as exit.
+Leaving \f(CW\*(C`ev_run\*(C'\fR abnormally (setjmp/longjmp, cancelling the thread,
+throwing an exception etc.), doesn't count as \*(L"exit\*(R" \- consider this
+as a hint to avoid such ungentleman-like behaviour unless it's really
+convenient, in which case it is fully supported.
.IP "unsigned int ev_backend (loop)" 4
.IX Item "unsigned int ev_backend (loop)"
Returns one of the \f(CW\*(C`EVBACKEND_*\*(C'\fR flags indicating the event backend in
@@ -794,7 +865,7 @@ event occurring (or more correctly, libev finding out about it).
.IX Item "ev_now_update (loop)"
Establishes the current time by querying the kernel, updating the time
returned by \f(CW\*(C`ev_now ()\*(C'\fR in the progress. This is a costly operation and
-is usually done automatically within \f(CW\*(C`ev_loop ()\*(C'\fR.
+is usually done automatically within \f(CW\*(C`ev_run ()\*(C'\fR.
.Sp
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
@@ -807,8 +878,8 @@ See also \*(L"The special problem of time updates\*(R" in the \f(CW\*(C`ev_timer
.IP "ev_resume (loop)" 4
.IX Item "ev_resume (loop)"
.PD
-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.
.Sp
A typical use case would be an interactive program such as a game: When
the user presses \f(CW\*(C`^Z\*(C'\fR to suspend the game and resumes it an hour later it
@@ -820,7 +891,7 @@ in your \f(CW\*(C`SIGTSTP\*(C'\fR handler, sending yourself a \f(CW\*(C`SIGSTOP\
Effectively, all \f(CW\*(C`ev_timer\*(C'\fR watchers will be delayed by the time spend
between \f(CW\*(C`ev_suspend\*(C'\fR and \f(CW\*(C`ev_resume\*(C'\fR, and all \f(CW\*(C`ev_periodic\*(C'\fR watchers
will be rescheduled (that is, they will lose any events that would have
-occured while suspended).
+occurred while suspended).
.Sp
After calling \f(CW\*(C`ev_suspend\*(C'\fR you \fBmust not\fR call \fIany\fR function on the
given loop other than \f(CW\*(C`ev_resume\*(C'\fR, and you \fBmust not\fR call \f(CW\*(C`ev_resume\*(C'\fR
@@ -828,28 +899,37 @@ without a previous call to \f(CW\*(C`ev_suspend\*(C'\fR.
.Sp
Calling \f(CW\*(C`ev_suspend\*(C'\fR/\f(CW\*(C`ev_resume\*(C'\fR has the side effect of updating the
event loop time (see \f(CW\*(C`ev_now_update\*(C'\fR).
-.IP "ev_loop (loop, int flags)" 4
-.IX Item "ev_loop (loop, int flags)"
+.IP "ev_run (loop, int flags)" 4
+.IX 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 \fIloops\fR.
.Sp
-If the flags argument is specified as \f(CW0\fR, it will not return until
-either no event watchers are active anymore or \f(CW\*(C`ev_unloop\*(C'\fR was called.
+If the flags argument is specified as \f(CW0\fR, it will keep handling events
+until either no event watchers are active anymore or \f(CW\*(C`ev_break\*(C'\fR was
+called.
.Sp
-Please note that an explicit \f(CW\*(C`ev_unloop\*(C'\fR is usually better than
+Please note that an explicit \f(CW\*(C`ev_break\*(C'\fR 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.
.Sp
-A flags value of \f(CW\*(C`EVLOOP_NONBLOCK\*(C'\fR 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.
+This function is also \fImostly\fR exception-safe \- you can break out of
+a \f(CW\*(C`ev_run\*(C'\fR call by calling \f(CW\*(C`longjmp\*(C'\fR in a callback, throwing a \*(C+
+exception and so on. This does not decrement the \f(CW\*(C`ev_depth\*(C'\fR value, nor
+will it clear any outstanding \f(CW\*(C`EVBREAK_ONE\*(C'\fR breaks.
.Sp
-A flags value of \f(CW\*(C`EVLOOP_ONESHOT\*(C'\fR will look for new events (waiting if
+A flags value of \f(CW\*(C`EVRUN_NOWAIT\*(C'\fR 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.
+.Sp
+A flags value of \f(CW\*(C`EVRUN_ONCE\*(C'\fR 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
@@ -858,37 +938,46 @@ iteration of the loop.
.Sp
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 \f(CW\*(C`ev_loop\*(C'\fR"). However, a pair of \f(CW\*(C`ev_prepare\*(C'\fR/\f(CW\*(C`ev_check\*(C'\fR watchers is
+own \f(CW\*(C`ev_run\*(C'\fR"). However, a pair of \f(CW\*(C`ev_prepare\*(C'\fR/\f(CW\*(C`ev_check\*(C'\fR watchers is
usually a better approach for this kind of thing.
.Sp
-Here are the gory details of what \f(CW\*(C`ev_loop\*(C'\fR does:
+Here are the gory details of what \f(CW\*(C`ev_run\*(C'\fR does:
.Sp
.Vb 10
+\& \- 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.
.Ve
.Sp
Example: Queue some jobs and then loop until no events are outstanding
@@ -897,19 +986,20 @@ anymore.
.Vb 4
\& ... 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!
.Ve
-.IP "ev_unloop (loop, how)" 4
-.IX Item "ev_unloop (loop, how)"
-Can be used to make a call to \f(CW\*(C`ev_loop\*(C'\fR return early (but only after it
+.IP "ev_break (loop, how)" 4
+.IX Item "ev_break (loop, how)"
+Can be used to make a call to \f(CW\*(C`ev_run\*(C'\fR return early (but only after it
has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either
-\&\f(CW\*(C`EVUNLOOP_ONE\*(C'\fR, which will make the innermost \f(CW\*(C`ev_loop\*(C'\fR call return, or
-\&\f(CW\*(C`EVUNLOOP_ALL\*(C'\fR, which will make all nested \f(CW\*(C`ev_loop\*(C'\fR calls return.
+\&\f(CW\*(C`EVBREAK_ONE\*(C'\fR, which will make the innermost \f(CW\*(C`ev_run\*(C'\fR call return, or
+\&\f(CW\*(C`EVBREAK_ALL\*(C'\fR, which will make all nested \f(CW\*(C`ev_run\*(C'\fR calls return.
.Sp
-This \*(L"unloop state\*(R" will be cleared when entering \f(CW\*(C`ev_loop\*(C'\fR again.
+This \*(L"break state\*(R" will be cleared on the next call to \f(CW\*(C`ev_run\*(C'\fR.
.Sp
-It is safe to call \f(CW\*(C`ev_unloop\*(C'\fR from otuside any \f(CW\*(C`ev_loop\*(C'\fR calls.
+It is safe to call \f(CW\*(C`ev_break\*(C'\fR from outside any \f(CW\*(C`ev_run\*(C'\fR calls, too, in
+which case it will have no effect.
.IP "ev_ref (loop)" 4
.IX Item "ev_ref (loop)"
.PD 0
@@ -918,15 +1008,15 @@ It is safe to call \f(CW\*(C`ev_unloop\*(C'\fR from otuside any \f(CW\*(C`ev_loo
.PD
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, \f(CW\*(C`ev_loop\*(C'\fR will not return on its own.
+count is nonzero, \f(CW\*(C`ev_run\*(C'\fR will not return on its own.
.Sp
This is useful when you have a watcher that you never intend to
-unregister, but that nevertheless should not keep \f(CW\*(C`ev_loop\*(C'\fR from
+unregister, but that nevertheless should not keep \f(CW\*(C`ev_run\*(C'\fR from
returning. In such a case, call \f(CW\*(C`ev_unref\*(C'\fR after starting, and \f(CW\*(C`ev_ref\*(C'\fR
before stopping it.
.Sp
As an example, libev itself uses this for its internal signal pipe: It
-is not visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from
+is not visible to the libev user and should not keep \f(CW\*(C`ev_run\*(C'\fR 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 \fIunref after start\fR and \fIref
@@ -935,14 +1025,14 @@ before, respectively. Note also that libev might stop watchers itself
(e.g. non-repeating timers) in which case you have to \f(CW\*(C`ev_ref\*(C'\fR
in the callback).
.Sp
-Example: Create a signal watcher, but keep it from keeping \f(CW\*(C`ev_loop\*(C'\fR
+Example: Create a signal watcher, but keep it from keeping \f(CW\*(C`ev_run\*(C'\fR
running when nothing else is active.
.Sp
.Vb 4
\& ev_signal exitsig;
\& ev_signal_init (&exitsig, sig_cb, SIGINT);
\& ev_signal_start (loop, &exitsig);
-\& evf_unref (loop);
+\& ev_unref (loop);
.Ve
.Sp
Example: For some weird reason, unregister the above signal handler again.
@@ -995,7 +1085,7 @@ as this approaches the timing granularity of most systems. Note that if
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).
.Sp
Setting the \fItimeout collect interval\fR can improve the opportunity for
saving power, as the program will \*(L"bundle\*(R" timer callback invocations that
@@ -1014,8 +1104,12 @@ more often than 100 times per second:
.IP "ev_invoke_pending (loop)" 4
.IX Item "ev_invoke_pending (loop)"
This call will simply invoke all pending watchers while resetting their
-pending state. Normally, \f(CW\*(C`ev_loop\*(C'\fR does this automatically when required,
-but when overriding the invoke callback this call comes handy.
+pending state. Normally, \f(CW\*(C`ev_run\*(C'\fR does this automatically when required,
+but when overriding the invoke callback this call comes handy. This
+function can be invoked from a watcher \- this can be useful for example
+when you want to do some lengthy calculation and want to pass further
+event handling to another thread (you still have to make sure only one
+thread executes within \f(CW\*(C`ev_invoke_pending\*(C'\fR or \f(CW\*(C`ev_run\*(C'\fR of course).
.IP "int ev_pending_count (loop)" 4
.IX Item "int ev_pending_count (loop)"
Returns the number of pending watchers \- zero indicates that no watchers
@@ -1023,7 +1117,7 @@ are pending.
.IP "ev_set_invoke_pending_cb (loop, void (*invoke_pending_cb)(\s-1EV_P\s0))" 4
.IX 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, \f(CW\*(C`ev_loop\*(C'\fR will call
+invoking all pending watchers when there are any, \f(CW\*(C`ev_run\*(C'\fR will call
this callback instead. This is useful, for example, when you want to
invoke the actual watchers inside another context (another thread etc.).
.Sp
@@ -1035,10 +1129,10 @@ Sometimes you want to share the same loop between multiple threads. This
can be done relatively simply by putting mutex_lock/unlock calls around
each call to a libev function.
.Sp
-However, \f(CW\*(C`ev_loop\*(C'\fR 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
-\&\f(CW\*(C`ev_unloop\*(C'\fR and \f(CW\*(C`av_async_send\*(C'\fR, another way is to set these \fIrelease\fR
-and \fIacquire\fR callbacks on the loop.
+However, \f(CW\*(C`ev_run\*(C'\fR 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 \f(CW\*(C`ev_break\*(C'\fR and \f(CW\*(C`av_async_send\*(C'\fR, another way is to set these
+\&\fIrelease\fR and \fIacquire\fR callbacks on the loop.
.Sp
When set, then \f(CW\*(C`release\*(C'\fR will be called just before the thread is
suspended waiting for new events, and \f(CW\*(C`acquire\*(C'\fR is called just
@@ -1051,10 +1145,10 @@ While event loop modifications are allowed between invocations of
\&\f(CW\*(C`release\*(C'\fR and \f(CW\*(C`acquire\*(C'\fR (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 \f(CW\*(C`ev_async\*(C'\fR watcher to wake up \f(CW\*(C`ev_loop\*(C'\fR when you want it
+waited. Use an \f(CW\*(C`ev_async\*(C'\fR watcher to wake up \f(CW\*(C`ev_run\*(C'\fR when you want it
to take note of any changes you made.
.Sp
-In theory, threads executing \f(CW\*(C`ev_loop\*(C'\fR will be async-cancel safe between
+In theory, threads executing \f(CW\*(C`ev_run\*(C'\fR will be async-cancel safe between
invocations of \f(CW\*(C`release\*(C'\fR and \f(CW\*(C`acquire\*(C'\fR.
.Sp
See also the locking example in the \f(CW\*(C`THREADS\*(C'\fR section later in this
@@ -1062,19 +1156,19 @@ document.
.IP "ev_set_userdata (loop, void *data)" 4
.IX Item "ev_set_userdata (loop, void *data)"
.PD 0
-.IP "ev_userdata (loop)" 4
-.IX Item "ev_userdata (loop)"
+.IP "void *ev_userdata (loop)" 4
+.IX Item "void *ev_userdata (loop)"
.PD
Set and retrieve a single \f(CW\*(C`void *\*(C'\fR associated with a loop. When
\&\f(CW\*(C`ev_set_userdata\*(C'\fR has never been called, then \f(CW\*(C`ev_userdata\*(C'\fR returns
-\&\f(CW0.\fR
+\&\f(CW0\fR.
.Sp
These two functions can be used to associate arbitrary data with a loop,
and are intended solely for the \f(CW\*(C`invoke_pending_cb\*(C'\fR, \f(CW\*(C`release\*(C'\fR and
\&\f(CW\*(C`acquire\*(C'\fR callbacks described above, but of course can be (ab\-)used for
any other purpose as well.
-.IP "ev_loop_verify (loop)" 4
-.IX Item "ev_loop_verify (loop)"
+.IP "ev_verify (loop)" 4
+.IX Item "ev_verify (loop)"
This function only does something when \f(CW\*(C`EV_VERIFY\*(C'\fR support has been
compiled in, which is the default for non-minimal builds. It tries to go
through all internal structures and checks them for validity. If anything
@@ -1090,15 +1184,16 @@ In the following description, uppercase \f(CW\*(C`TYPE\*(C'\fR in names stands f
watcher type, e.g. \f(CW\*(C`ev_TYPE_start\*(C'\fR can mean \f(CW\*(C`ev_timer_start\*(C'\fR for timer
watchers and \f(CW\*(C`ev_io_start\*(C'\fR for I/O watchers.
.PP
-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 \s-1STDIN\s0 to
-become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR 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 \s-1STDIN\s0 to become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher
+for that:
.PP
.Vb 5
\& 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);
@@ -1109,7 +1204,7 @@ become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that:
\& ev_io_set (&stdin_watcher, STDIN_FILENO, EV_READ);
\& ev_io_start (loop, &stdin_watcher);
\&
-\& ev_loop (loop, 0);
+\& ev_run (loop, 0);
.Ve
.PP
As you can see, you are responsible for allocating the memory for your
@@ -1119,11 +1214,11 @@ stack).
Each watcher has an associated watcher structure (called \f(CW\*(C`struct ev_TYPE\*(C'\fR
or simply \f(CW\*(C`ev_TYPE\*(C'\fR, as typedefs are provided for all watcher structs).
.PP
-Each watcher structure must be initialised by a call to \f(CW\*(C`ev_init
-(watcher *, callback)\*(C'\fR, 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 \f(CW\*(C`ev_init (watcher
+*, callback)\*(C'\fR, 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).
.PP
Each watcher type further has its own \f(CW\*(C`ev_TYPE_set (watcher *, ...)\*(C'\fR
macro to configure it, with arguments specific to the watcher type. There
@@ -1155,9 +1250,9 @@ are:
.PD
The file descriptor in the \f(CW\*(C`ev_io\*(C'\fR watcher has become readable and/or
writable.
-.ie n .IP """EV_TIMEOUT""" 4
-.el .IP "\f(CWEV_TIMEOUT\fR" 4
-.IX Item "EV_TIMEOUT"
+.ie n .IP """EV_TIMER""" 4
+.el .IP "\f(CWEV_TIMER\fR" 4
+.IX Item "EV_TIMER"
The \f(CW\*(C`ev_timer\*(C'\fR watcher has timed out.
.ie n .IP """EV_PERIODIC""" 4
.el .IP "\f(CWEV_PERIODIC\fR" 4
@@ -1187,13 +1282,13 @@ The \f(CW\*(C`ev_idle\*(C'\fR watcher has determined that you have nothing bette
.el .IP "\f(CWEV_CHECK\fR" 4
.IX Item "EV_CHECK"
.PD
-All \f(CW\*(C`ev_prepare\*(C'\fR watchers are invoked just \fIbefore\fR \f(CW\*(C`ev_loop\*(C'\fR starts
+All \f(CW\*(C`ev_prepare\*(C'\fR watchers are invoked just \fIbefore\fR \f(CW\*(C`ev_run\*(C'\fR starts
to gather new events, and all \f(CW\*(C`ev_check\*(C'\fR watchers are invoked just after
-\&\f(CW\*(C`ev_loop\*(C'\fR has gathered them, but before it invokes any callbacks for any
+\&\f(CW\*(C`ev_run\*(C'\fR 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 \f(CW\*(C`ev_prepare\*(C'\fR watcher might start an idle watcher to keep
-\&\f(CW\*(C`ev_loop\*(C'\fR from blocking).
+\&\f(CW\*(C`ev_run\*(C'\fR from blocking).
.ie n .IP """EV_EMBED""" 4
.el .IP "\f(CWEV_EMBED\fR" 4
.IX Item "EV_EMBED"
@@ -1203,6 +1298,10 @@ The embedded event loop specified in the \f(CW\*(C`ev_embed\*(C'\fR watcher need
.IX Item "EV_FORK"
The event loop has been resumed in the child process after fork (see
\&\f(CW\*(C`ev_fork\*(C'\fR).
+.ie n .IP """EV_CLEANUP""" 4
+.el .IP "\f(CWEV_CLEANUP\fR" 4
+.IX Item "EV_CLEANUP"
+The event loop is about to be destroyed (see \f(CW\*(C`ev_cleanup\*(C'\fR).
.ie n .IP """EV_ASYNC""" 4
.el .IP "\f(CWEV_ASYNC\fR" 4
.IX Item "EV_ASYNC"
@@ -1378,78 +1477,59 @@ not started in the first place.
.Sp
See also \f(CW\*(C`ev_feed_fd_event\*(C'\fR and \f(CW\*(C`ev_feed_signal_event\*(C'\fR for related
functions that do not need a watcher.
-.SS "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0"
-.IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER"
-Each watcher has, by default, a member \f(CW\*(C`void *data\*(C'\fR that you can change
-and read at any time: libev will completely ignore it. This can be used
-to associate arbitrary data with your watcher. If you need more data and
-don't want to allocate memory and store a pointer to it in that data
-member, you can also \*(L"subclass\*(R" the watcher type and provide your own
-data:
.PP
-.Vb 7
-\& struct my_io
-\& {
-\& ev_io io;
-\& int otherfd;
-\& void *somedata;
-\& struct whatever *mostinteresting;
-\& };
-\&
-\& ...
-\& struct my_io w;
-\& ev_io_init (&w.io, my_cb, fd, EV_READ);
-.Ve
-.PP
-And since your callback will be called with a pointer to the watcher, you
-can cast it back to your own type:
-.PP
-.Vb 5
-\& static void my_cb (struct ev_loop *loop, ev_io *w_, int revents)
-\& {
-\& struct my_io *w = (struct my_io *)w_;
-\& ...
-\& }
-.Ve
-.PP
-More interesting and less C\-conformant ways of casting your callback type
-instead have been omitted.
-.PP
-Another common scenario is to use some data structure with multiple
-embedded watchers:
-.PP
-.Vb 6
-\& struct my_biggy
-\& {
-\& int some_data;
-\& ev_timer t1;
-\& ev_timer t2;
-\& }
-.Ve
-.PP
-In this case getting the pointer to \f(CW\*(C`my_biggy\*(C'\fR is a bit more
-complicated: Either you store the address of your \f(CW\*(C`my_biggy\*(C'\fR struct
-in the \f(CW\*(C`data\*(C'\fR member of the watcher (for woozies), or you need to use
-some pointer arithmetic using \f(CW\*(C`offsetof\*(C'\fR inside your watchers (for real
-programmers):
-.PP
-.Vb 1
-\& #include <stddef.h>
-\&
-\& static void
-\& t1_cb (EV_P_ ev_timer *w, int revents)
-\& {
-\& struct my_biggy big = (struct my_biggy *)
-\& (((char *)w) \- offsetof (struct my_biggy, t1));
-\& }
-\&
-\& static void
-\& t2_cb (EV_P_ ev_timer *w, int revents)
-\& {
-\& struct my_biggy big = (struct my_biggy *)
-\& (((char *)w) \- offsetof (struct my_biggy, t2));
-\& }
-.Ve
+See also the \*(L"\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0\*(R" and \*(L"\s-1BUILDING\s0 \s-1YOUR\s0
+\&\s-1OWN\s0 \s-1COMPOSITE\s0 \s-1WATCHERS\s0\*(R" idioms.
+.SS "\s-1WATCHER\s0 \s-1STATES\s0"
+.IX Subsection "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 \*(L"the right thing\*(R".
+.IP "initialiased" 4
+.IX 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 \f(CW\*(C`ev_TYPE_init\*(C'\fR, or calls to
+\&\f(CW\*(C`ev_init\*(C'\fR followed by the watcher-specific \f(CW\*(C`ev_TYPE_set\*(C'\fR function.
+.Sp
+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.
+.IP "started/running/active" 4
+.IX Item "started/running/active"
+Once a watcher has been started with a call to \f(CW\*(C`ev_TYPE_start\*(C'\fR 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.
+.IP "pending" 4
+.IX 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.
+.Sp
+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 \f(CW\*(C`ev_TYPE_set\*(C'\fR),
+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.
+.Sp
+It is also possible to feed an event on a watcher that is not active (e.g.
+via \f(CW\*(C`ev_feed_event\*(C'\fR), in which case it becomes pending without being
+active.
+.IP "stopped" 4
+.IX Item "stopped"
+A watcher can be stopped implicitly by libev (in which case it might still
+be pending), or explicitly by calling its \f(CW\*(C`ev_TYPE_stop\*(C'\fR 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.
+.Sp
+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.
.SS "\s-1WATCHER\s0 \s-1PRIORITY\s0 \s-1MODELS\s0"
.IX Subsection "WATCHER PRIORITY MODELS"
Many event loops support \fIwatcher priorities\fR, which are usually small
@@ -1501,7 +1581,7 @@ For example, to emulate how many other event libraries handle priorities,
you can associate an \f(CW\*(C`ev_idle\*(C'\fR 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.
.PP
@@ -1526,7 +1606,7 @@ other events are pending:
\& // 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);
@@ -1584,26 +1664,19 @@ fd as you want (as long as you don't confuse yourself). Setting all file
descriptors to non-blocking mode is also usually a good idea (but not
required if you know what you are doing).
.PP
-If you cannot use non-blocking mode, then force the use of a
-known-to-be-good backend (at the time of this writing, this includes only
-\&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and \f(CW\*(C`EVBACKEND_POLL\*(C'\fR). 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.
-.PP
Another thing you have to watch out for is that it is quite easy to
-receive \*(L"spurious\*(R" readiness notifications, that is your callback might
+receive \*(L"spurious\*(R" readiness notifications, that is, your callback might
be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block
-because there is no data. Not only are some backends known to create a
-lot of those (for example Solaris ports), it is very easy to get into
-this situation even with a relatively standard program structure. Thus
-it is best to always use non-blocking I/O: An extra \f(CW\*(C`read\*(C'\fR(2) returning
-\&\f(CW\*(C`EAGAIN\*(C'\fR is far preferable to a program hanging until some data arrives.
+because there is no data. It is very easy to get into this situation even
+with a relatively standard program structure. Thus it is best to always
+use non-blocking I/O: An extra \f(CW\*(C`read\*(C'\fR(2) returning \f(CW\*(C`EAGAIN\*(C'\fR is far
+preferable to a program hanging until some data arrives.
.PP
If you cannot run the fd in non-blocking mode (for example you should
not play around with an Xlib connection), then you have to separately
re-test whether a file descriptor is really ready with a known-to-be good
-interface such as poll (fortunately in our Xlib example, Xlib already
-does this on its own, so its quite safe to use). Some people additionally
+interface such as poll (fortunately in the case of Xlib, it already does
+this on its own, so its quite safe to use). Some people additionally
use \f(CW\*(C`SIGALRM\*(C'\fR and an interval timer, just to be sure you won't block
indefinitely.
.PP
@@ -1643,17 +1716,50 @@ There is no workaround possible except not registering events
for potentially \f(CW\*(C`dup ()\*(C'\fR'ed file descriptors, or to resort to
\&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR.
.PP
+\fIThe special problem of files\fR
+.IX Subsection "The special problem of files"
+.PP
+Many people try to use \f(CW\*(C`select\*(C'\fR (or libev) on file descriptors
+representing files, and expect it to become ready when their program
+doesn't block on disk accesses (which can take a long time on their own).
+.PP
+However, this cannot ever work in the \*(L"expected\*(R" way \- you get a readiness
+notification as soon as the kernel knows whether and how much data is
+there, and in the case of open files, that's always the case, so you
+always get a readiness notification instantly, and your read (or possibly
+write) will still block on the disk I/O.
+.PP
+Another way to view it is that in the case of sockets, pipes, character
+devices and so on, there is another party (the sender) that delivers data
+on its own, but in the case of files, there is no such thing: the disk
+will not send data on its own, simply because it doesn't know what you
+wish to read \- you would first have to request some data.
+.PP
+Since files are typically not-so-well supported by advanced notification
+mechanism, libev tries hard to emulate \s-1POSIX\s0 behaviour with respect
+to files, even though you should not use it. The reason for this is
+convenience: sometimes you want to watch \s-1STDIN\s0 or \s-1STDOUT\s0, which is
+usually a tty, often a pipe, but also sometimes files or special devices
+(for example, \f(CW\*(C`epoll\*(C'\fR on Linux works with \fI/dev/random\fR but not with
+\&\fI/dev/urandom\fR), and even though the file might better be served with
+asynchronous I/O instead of with non-blocking I/O, it is still useful when
+it \*(L"just works\*(R" instead of freezing.
+.PP
+So avoid file descriptors pointing to files when you know it (e.g. use
+libeio), but use them when it is convenient, e.g. for \s-1STDIN/STDOUT\s0, or
+when you rarely read from a file instead of from a socket, and want to
+reuse the same code path.
+.PP
\fIThe special problem of fork\fR
.IX Subsection "The special problem of fork"
.PP
Some backends (epoll, kqueue) do not support \f(CW\*(C`fork ()\*(C'\fR at all or exhibit
useless behaviour. Libev fully supports fork, but needs to be told about
-it in the child.
+it in the child if you want to continue to use it in the child.
.PP
-To support fork in your programs, you either have to call
-\&\f(CW\*(C`ev_default_fork ()\*(C'\fR or \f(CW\*(C`ev_loop_fork ()\*(C'\fR after a fork in the child,
-enable \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR, or resort to \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or
-\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR.
+To support fork in your child processes, you have to call \f(CW\*(C`ev_loop_fork
+()\*(C'\fR after a fork in the child, enable \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR, or resort to
+\&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR.
.PP
\fIThe special problem of \s-1SIGPIPE\s0\fR
.IX Subsection "The special problem of SIGPIPE"
@@ -1667,6 +1773,46 @@ So when you encounter spurious, unexplained daemon exits, make sure you
ignore \s-1SIGPIPE\s0 (and maybe make sure you log the exit status of your daemon
somewhere, as that would have given you a big clue).
.PP
+\fIThe special problem of \fIaccept()\fIing when you can't\fR
+.IX Subsection "The special problem of accept()ing when you can't"
+.PP
+Many implementations of the \s-1POSIX\s0 \f(CW\*(C`accept\*(C'\fR 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.
+.PP
+For example, larger servers often run out of file descriptors (because
+of resource limits), causing \f(CW\*(C`accept\*(C'\fR to fail with \f(CW\*(C`ENFILE\*(C'\fR 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% \s-1CPU\s0 usage.
+.PP
+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).
+.PP
+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 \s-1OS\s0 offers an
+event-based way to handle this situation, so it's the best one can do.
+.PP
+A better way to handle the situation is to log any errors other than
+\&\f(CW\*(C`EAGAIN\*(C'\fR and \f(CW\*(C`EWOULDBLOCK\*(C'\fR, 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 (\*(L"raise the ulimit!\*(R"). For extra points one could stop
+the \f(CW\*(C`ev_io\*(C'\fR watcher on the listening fd \*(L"for a while\*(R", which reduces \s-1CPU\s0
+usage.
+.PP
+If your program is single-threaded, then you could also keep a dummy file
+descriptor for overload situations (e.g. by opening \fI/dev/null\fR), and
+when you run into \f(CW\*(C`ENFILE\*(C'\fR or \f(CW\*(C`EMFILE\*(C'\fR, close it, run \f(CW\*(C`accept\*(C'\fR,
+close that fd, and create a new dummy fd. This will gracefully refuse
+clients under typical overload conditions.
+.PP
+The last way to handle it is to simply log the error and \f(CW\*(C`exit\*(C'\fR, as
+is often done with \f(CW\*(C`malloc\*(C'\fR failures, but this results in an easy
+opportunity for a DoS attack.
+.PP
\fIWatcher-Specific Functions\fR
.IX Subsection "Watcher-Specific Functions"
.IP "ev_io_init (ev_io *, callback, int fd, int events)" 4
@@ -1705,7 +1851,7 @@ attempt to read a whole line in the callback.
\& 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);
.Ve
.ie n .SS """ev_timer"" \- relative and optionally repeating timeouts"
.el .SS "\f(CWev_timer\fP \- relative and optionally repeating timeouts"
@@ -1724,7 +1870,7 @@ passed (not \fIat\fR, so on systems with very low-resolution clocks this
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 \f(CW\*(C`ev_loop\*(C'\fR recursively).
+no longer true when a callback calls \f(CW\*(C`ev_run\*(C'\fR recursively).
.PP
\fIBe smart about timeouts\fR
.IX Subsection "Be smart about timeouts"
@@ -1828,7 +1974,7 @@ within the callback:
\& // if last_activity + 60. is older than now, we did time out
\& if (timeout < now)
\& {
-\& // timeout occured, take action
+\& // timeout occurred, take action
\& }
\& else
\& {
@@ -1862,14 +2008,14 @@ callback, which will \*(L"do the right thing\*(R" and start the timer:
.Vb 3
\& ev_init (timer, callback);
\& last_activity = ev_now (loop);
-\& callback (loop, timer, EV_TIMEOUT);
+\& callback (loop, timer, EV_TIMER);
.Ve
.Sp
And when there is some activity, simply store the current time in
\&\f(CW\*(C`last_activity\*(C'\fR, no libev calls at all:
.Sp
.Vb 1
-\& last_actiivty = ev_now (loop);
+\& last_activity = ev_now (loop);
.Ve
.Sp
This technique is slightly more complex, but in most cases where the
@@ -1916,7 +2062,7 @@ overkill :)
.PP
Establishing the current time is a costly operation (it usually takes at
least two system calls): \s-1EV\s0 therefore updates its idea of the current
-time only before and after \f(CW\*(C`ev_loop\*(C'\fR collects new events, which causes a
+time only before and after \f(CW\*(C`ev_run\*(C'\fR collects new events, which causes a
growing difference between \f(CW\*(C`ev_now ()\*(C'\fR and \f(CW\*(C`ev_time ()\*(C'\fR when handling
lots of events in one iteration.
.PP
@@ -2005,7 +2151,7 @@ then this time is relative to the current event loop time, otherwise it's
the timeout value currently configured.
.Sp
That is, after an \f(CW\*(C`ev_timer_set (w, 5, 7)\*(C'\fR, \f(CW\*(C`ev_timer_remaining\*(C'\fR returns
-\&\f(CW5\fR. When the timer is started and one second passes, \f(CW\*(C`ev_timer_remain\*(C'\fR
+\&\f(CW5\fR. When the timer is started and one second passes, \f(CW\*(C`ev_timer_remaining\*(C'\fR
will return \f(CW4\fR. When the timer expires and is restarted, it will return
roughly \f(CW7\fR (likely slightly less as callback invocation takes some time,
too), and so on.
@@ -2045,7 +2191,7 @@ inactivity.
\& 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
@@ -2081,7 +2227,7 @@ As with timers, the callback is guaranteed to be invoked only when the
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 \f(CW\*(C`ev_loop\*(C'\fR recursively).
+(but this is no longer true when a callback calls \f(CW\*(C`ev_run\*(C'\fR recursively).
.PP
\fIWatcher-Specific Functions and Data Members\fR
.IX Subsection "Watcher-Specific Functions and Data Members"
@@ -2218,7 +2364,7 @@ potentially a lot of jitter, but good long-term stability.
.PP
.Vb 5
\& 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)
\& }
@@ -2255,7 +2401,7 @@ Example: Call a callback every hour, starting now:
.IX Subsection "ev_signal - signal me when a signal gets signalled!"
Signal watchers will trigger an event when the process receives a specific
signal one or more times. Even though signals are very asynchronous, libev
-will try it's best to deliver signals synchronously, i.e. as part of the
+will try its best to deliver signals synchronously, i.e. as part of the
normal event processing, like any other event.
.PP
If you want signals to be delivered truly asynchronously, just use
@@ -2309,6 +2455,21 @@ So I can't stress this enough: \fIIf you do not reset your signal mask when
you expect it to be empty, you have a race condition in your code\fR. This
is not a libev-specific thing, this is true for most event libraries.
.PP
+\fIThe special problem of threads signal handling\fR
+.IX Subsection "The special problem of threads signal handling"
+.PP
+\&\s-1POSIX\s0 threads has problematic signal handling semantics, specifically,
+a lot of functionality (sigfd, sigwait etc.) only really works if all
+threads in a process block signals, which is hard to achieve.
+.PP
+When you want to use sigwait (or mix libev signal handling with your own
+for the same signals), you can tackle this problem by globally blocking
+all signals before creating any threads (or creating them with a fully set
+sigprocmask) and also specifying the \f(CW\*(C`EVFLAG_NOSIGMASK\*(C'\fR when creating
+loops. Then designate one thread as \*(L"signal receiver thread\*(R" which handles
+these signals. You can pass on any signals that libev might be interested
+in by calling \f(CW\*(C`ev_feed_signal\*(C'\fR.
+.PP
\fIWatcher-Specific Functions and Data Members\fR
.IX Subsection "Watcher-Specific Functions and Data Members"
.IP "ev_signal_init (ev_signal *, callback, int signum)" 4
@@ -2332,7 +2493,7 @@ Example: Try to exit cleanly on \s-1SIGINT\s0.
\& 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;
@@ -2727,7 +2888,7 @@ Prepare and check watchers are usually (but not always) used in pairs:
prepare watchers get invoked before the process blocks and check watchers
afterwards.
.PP
-You \fImust not\fR call \f(CW\*(C`ev_loop\*(C'\fR or similar functions that enter
+You \fImust not\fR call \f(CW\*(C`ev_run\*(C'\fR or similar functions that enter
the current event loop from either \f(CW\*(C`ev_prepare\*(C'\fR or \f(CW\*(C`ev_check\*(C'\fR
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
@@ -2909,7 +3070,7 @@ libglib event loop.
\& // create/start timer
\&
\& // poll
-\& ev_loop (EV_A_ 0);
+\& ev_run (EV_A_ 0);
\&
\& // stop timer again
\& if (timeout >= 0)
@@ -2997,7 +3158,7 @@ if you do not want that, you need to temporarily stop the embed watcher).
.IP "ev_embed_sweep (loop, ev_embed *)" 4
.IX Item "ev_embed_sweep (loop, ev_embed *)"
Make a single, non-blocking sweep over the embedded loop. This works
-similarly to \f(CW\*(C`ev_loop (embedded_loop, EVLOOP_NONBLOCK)\*(C'\fR, but in the most
+similarly to \f(CW\*(C`ev_run (embedded_loop, EVRUN_NOWAIT)\*(C'\fR, but in the most
appropriate way for embedded loops.
.IP "struct ev_loop *other [read\-only]" 4
.IX Item "struct ev_loop *other [read-only]"
@@ -3069,7 +3230,7 @@ handlers will be invoked, too, of course.
\fIThe special problem of life after fork \- how is it possible?\fR
.IX Subsection "The special problem of life after fork - how is it possible?"
.PP
-Most uses of \f(CW\*(C`fork()\*(C'\fR consist of forking, then some simple calls to ste
+Most uses of \f(CW\*(C`fork()\*(C'\fR consist of forking, then some simple calls to set
up/change the process environment, followed by a call to \f(CW\*(C`exec()\*(C'\fR. This
sequence should be handled by libev without any problems.
.PP
@@ -3095,35 +3256,75 @@ signal watchers).
.PP
When this is not possible, or you want to use the default loop for
other reasons, then in the process that wants to start \*(L"fresh\*(R", call
-\&\f(CW\*(C`ev_default_destroy ()\*(C'\fR followed by \f(CW\*(C`ev_default_loop (...)\*(C'\fR. Destroying
-the default loop will \*(L"orphan\*(R" (not stop) all registered watchers, so you
-have to be careful not to execute code that modifies those watchers. Note
-also that in that case, you have to re-register any signal watchers.
+\&\f(CW\*(C`ev_loop_destroy (EV_DEFAULT)\*(C'\fR followed by \f(CW\*(C`ev_default_loop (...)\*(C'\fR.
+Destroying the default loop will \*(L"orphan\*(R" (not stop) all registered
+watchers, so you have to be careful not to execute code that modifies
+those watchers. Note also that in that case, you have to re-register any
+signal watchers.
.PP
\fIWatcher-Specific Functions and Data Members\fR
.IX Subsection "Watcher-Specific Functions and Data Members"
-.IP "ev_fork_init (ev_signal *, callback)" 4
-.IX Item "ev_fork_init (ev_signal *, callback)"
+.IP "ev_fork_init (ev_fork *, callback)" 4
+.IX Item "ev_fork_init (ev_fork *, callback)"
Initialises and configures the fork watcher \- it has no parameters of any
kind. There is a \f(CW\*(C`ev_fork_set\*(C'\fR macro, but using it is utterly pointless,
-believe me.
-.ie n .SS """ev_async"" \- how to wake up another event loop"
-.el .SS "\f(CWev_async\fP \- how to wake up another event loop"
-.IX Subsection "ev_async - how to wake up another event loop"
-In general, you cannot use an \f(CW\*(C`ev_loop\*(C'\fR from multiple threads or other
+really.
+.ie n .SS """ev_cleanup"" \- even the best things end"
+.el .SS "\f(CWev_cleanup\fP \- even the best things end"
+.IX Subsection "ev_cleanup - even the best things end"
+Cleanup watchers are called just before the event loop is being destroyed
+by a call to \f(CW\*(C`ev_loop_destroy\*(C'\fR.
+.PP
+While there is no guarantee that the event loop gets destroyed, cleanup
+watchers provide a convenient method to install cleanup hooks for your
+program, worker threads and so on \- you just to make sure to destroy the
+loop when you want them to be invoked.
+.PP
+Cleanup watchers are invoked in the same way as any other watcher. Unlike
+all other watchers, they do not keep a reference to the event loop (which
+makes a lot of sense if you think about it). Like all other watchers, you
+can call libev functions in the callback, except \f(CW\*(C`ev_cleanup_start\*(C'\fR.
+.PP
+\fIWatcher-Specific Functions and Data Members\fR
+.IX Subsection "Watcher-Specific Functions and Data Members"
+.IP "ev_cleanup_init (ev_cleanup *, callback)" 4
+.IX Item "ev_cleanup_init (ev_cleanup *, callback)"
+Initialises and configures the cleanup watcher \- it has no parameters of
+any kind. There is a \f(CW\*(C`ev_cleanup_set\*(C'\fR macro, but using it is utterly
+pointless, I assure you.
+.PP
+Example: Register an atexit handler to destroy the default loop, so any
+cleanup functions are called.
+.PP
+.Vb 5
+\& static void
+\& program_exits (void)
+\& {
+\& ev_loop_destroy (EV_DEFAULT_UC);
+\& }
+\&
+\& ...
+\& atexit (program_exits);
+.Ve
+.ie n .SS """ev_async"" \- how to wake up an event loop"
+.el .SS "\f(CWev_async\fP \- how to wake up an event loop"
+.IX Subsection "ev_async - how to wake up an event loop"
+In general, you cannot use an \f(CW\*(C`ev_run\*(C'\fR 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).
.PP
-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
-\&\f(CW\*(C`ev_async\*(C'\fR watchers do: as long as the \f(CW\*(C`ev_async\*(C'\fR watcher is active, you
-can signal it by calling \f(CW\*(C`ev_async_send\*(C'\fR, 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 \f(CW\*(C`ev_async\*(C'\fR
+watchers do: as long as the \f(CW\*(C`ev_async\*(C'\fR watcher is active, you can signal
+it by calling \f(CW\*(C`ev_async_send\*(C'\fR, which is thread\- and signal safe.
.PP
This functionality is very similar to \f(CW\*(C`ev_signal\*(C'\fR watchers, as signals,
too, are asynchronous in nature, and signals, too, will be compressed
(i.e. the number of callback invocations may be less than the number of
-\&\f(CW\*(C`ev_async_sent\*(C'\fR calls).
+\&\f(CW\*(C`ev_async_sent\*(C'\fR calls). In fact, you could use signal watchers as a kind
+of \*(L"global async watchers\*(R" by using a watcher on an otherwise unused
+signal, and \f(CW\*(C`ev_feed_signal\*(C'\fR to signal this watcher from another thread,
+even without knowing which loop owns the signal.
.PP
Unlike \f(CW\*(C`ev_signal\*(C'\fR watchers, \f(CW\*(C`ev_async\*(C'\fR works with any event loop, not
just the default loop.
@@ -3270,9 +3471,9 @@ If \f(CW\*(C`timeout\*(C'\fR is less than 0, then no timeout watcher will be
started. Otherwise an \f(CW\*(C`ev_timer\*(C'\fR watcher with after = \f(CW\*(C`timeout\*(C'\fR (and
repeat = 0) will be started. \f(CW0\fR is a valid timeout.
.Sp
-The callback has the type \f(CW\*(C`void (*cb)(int revents, void *arg)\*(C'\fR and gets
+The callback has the type \f(CW\*(C`void (*cb)(int revents, void *arg)\*(C'\fR and is
passed an \f(CW\*(C`revents\*(C'\fR set like normal event callbacks (a combination of
-\&\f(CW\*(C`EV_ERROR\*(C'\fR, \f(CW\*(C`EV_READ\*(C'\fR, \f(CW\*(C`EV_WRITE\*(C'\fR or \f(CW\*(C`EV_TIMEOUT\*(C'\fR) and the \f(CW\*(C`arg\*(C'\fR
+\&\f(CW\*(C`EV_ERROR\*(C'\fR, \f(CW\*(C`EV_READ\*(C'\fR, \f(CW\*(C`EV_WRITE\*(C'\fR or \f(CW\*(C`EV_TIMER\*(C'\fR) and the \f(CW\*(C`arg\*(C'\fR
value passed to \f(CW\*(C`ev_once\*(C'\fR. Note that it is possible to receive \fIboth\fR
a timeout and an io event at the same time \- you probably should give io
events precedence.
@@ -3284,7 +3485,7 @@ Example: wait up to ten seconds for data to appear on \s-1STDIN_FILENO\s0.
\& {
\& if (revents & EV_READ)
\& /* stdin might have data for us, joy! */;
-\& else if (revents & EV_TIMEOUT)
+\& else if (revents & EV_TIMER)
\& /* doh, nothing entered */;
\& }
\&
@@ -3296,13 +3497,353 @@ Feed an event on the given fd, as if a file descriptor backend detected
the given events it.
.IP "ev_feed_signal_event (loop, int signum)" 4
.IX Item "ev_feed_signal_event (loop, int signum)"
-Feed an event as if the given signal occurred (\f(CW\*(C`loop\*(C'\fR must be the default
-loop!).
+Feed an event as if the given signal occurred. See also \f(CW\*(C`ev_feed_signal\*(C'\fR,
+which is async-safe.
+.SH "COMMON OR USEFUL IDIOMS (OR BOTH)"
+.IX Header "COMMON OR USEFUL IDIOMS (OR BOTH)"
+This section explains some common idioms that are not immediately
+obvious. Note that examples are sprinkled over the whole manual, and this
+section only contains stuff that wouldn't fit anywhere else.
+.SS "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0"
+.IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER"
+Each watcher has, by default, a \f(CW\*(C`void *data\*(C'\fR member that you can read
+or modify at any time: libev will completely ignore it. This can be used
+to associate arbitrary data with your watcher. If you need more data and
+don't want to allocate memory separately and store a pointer to it in that
+data member, you can also \*(L"subclass\*(R" the watcher type and provide your own
+data:
+.PP
+.Vb 7
+\& struct my_io
+\& {
+\& ev_io io;
+\& int otherfd;
+\& void *somedata;
+\& struct whatever *mostinteresting;
+\& };
+\&
+\& ...
+\& struct my_io w;
+\& ev_io_init (&w.io, my_cb, fd, EV_READ);
+.Ve
+.PP
+And since your callback will be called with a pointer to the watcher, you
+can cast it back to your own type:
+.PP
+.Vb 5
+\& static void my_cb (struct ev_loop *loop, ev_io *w_, int revents)
+\& {
+\& struct my_io *w = (struct my_io *)w_;
+\& ...
+\& }
+.Ve
+.PP
+More interesting and less C\-conformant ways of casting your callback
+function type instead have been omitted.
+.SS "\s-1BUILDING\s0 \s-1YOUR\s0 \s-1OWN\s0 \s-1COMPOSITE\s0 \s-1WATCHERS\s0"
+.IX Subsection "BUILDING YOUR OWN COMPOSITE WATCHERS"
+Another common scenario is to use some data structure with multiple
+embedded watchers, in effect creating your own watcher that combines
+multiple libev event sources into one \*(L"super-watcher\*(R":
+.PP
+.Vb 6
+\& struct my_biggy
+\& {
+\& int some_data;
+\& ev_timer t1;
+\& ev_timer t2;
+\& }
+.Ve
+.PP
+In this case getting the pointer to \f(CW\*(C`my_biggy\*(C'\fR is a bit more
+complicated: Either you store the address of your \f(CW\*(C`my_biggy\*(C'\fR struct in
+the \f(CW\*(C`data\*(C'\fR member of the watcher (for woozies or \*(C+ coders), or you need
+to use some pointer arithmetic using \f(CW\*(C`offsetof\*(C'\fR inside your watchers (for
+real programmers):
+.PP
+.Vb 1
+\& #include <stddef.h>
+\&
+\& static void
+\& t1_cb (EV_P_ ev_timer *w, int revents)
+\& {
+\& struct my_biggy big = (struct my_biggy *)
+\& (((char *)w) \- offsetof (struct my_biggy, t1));
+\& }
+\&
+\& static void
+\& t2_cb (EV_P_ ev_timer *w, int revents)
+\& {
+\& struct my_biggy big = (struct my_biggy *)
+\& (((char *)w) \- offsetof (struct my_biggy, t2));
+\& }
+.Ve
+.SS "\s-1MODEL/NESTED\s0 \s-1EVENT\s0 \s-1LOOP\s0 \s-1INVOCATIONS\s0 \s-1AND\s0 \s-1EXIT\s0 \s-1CONDITIONS\s0"
+.IX Subsection "MODEL/NESTED EVENT LOOP INVOCATIONS AND EXIT CONDITIONS"
+Often (especially in \s-1GUI\s0 toolkits) there are places where you have
+\&\fImodal\fR interaction, which is most easily implemented by recursively
+invoking \f(CW\*(C`ev_run\*(C'\fR.
+.PP
+This brings the problem of exiting \- a callback might want to finish the
+main \f(CW\*(C`ev_run\*(C'\fR call, but not the nested one (e.g. user clicked \*(L"Quit\*(R", but
+a modal \*(L"Are you sure?\*(R" dialog is still waiting), or just the nested one
+and not the main one (e.g. user clocked \*(L"Ok\*(R" in a modal dialog), or some
+other combination: In these cases, \f(CW\*(C`ev_break\*(C'\fR will not work alone.
+.PP
+The solution is to maintain \*(L"break this loop\*(R" variable for each \f(CW\*(C`ev_run\*(C'\fR
+invocation, and use a loop around \f(CW\*(C`ev_run\*(C'\fR until the condition is
+triggered, using \f(CW\*(C`EVRUN_ONCE\*(C'\fR:
+.PP
+.Vb 2
+\& // main loop
+\& int exit_main_loop = 0;
+\&
+\& while (!exit_main_loop)
+\& ev_run (EV_DEFAULT_ EVRUN_ONCE);
+\&
+\& // in a model watcher
+\& int exit_nested_loop = 0;
+\&
+\& while (!exit_nested_loop)
+\& ev_run (EV_A_ EVRUN_ONCE);
+.Ve
+.PP
+To exit from any of these loops, just set the corresponding exit variable:
+.PP
+.Vb 2
+\& // exit modal loop
+\& exit_nested_loop = 1;
+\&
+\& // exit main program, after modal loop is finished
+\& exit_main_loop = 1;
+\&
+\& // exit both
+\& exit_main_loop = exit_nested_loop = 1;
+.Ve
+.SS "\s-1THREAD\s0 \s-1LOCKING\s0 \s-1EXAMPLE\s0"
+.IX Subsection "THREAD LOCKING EXAMPLE"
+Here is a fictitious example of how to run an event loop in a different
+thread from where callbacks are being invoked and watchers are
+created/added/removed.
+.PP
+For a real-world example, see the \f(CW\*(C`EV::Loop::Async\*(C'\fR perl module,
+which uses exactly this technique (which is suited for many high-level
+languages).
+.PP
+The example uses a pthread mutex to protect the loop data, a condition
+variable to wait for callback invocations, an async watcher to notify the
+event loop thread and an unspecified mechanism to wake up the main thread.
+.PP
+First, you need to associate some data with the event loop:
+.PP
+.Vb 6
+\& typedef struct {
+\& mutex_t lock; /* global loop lock */
+\& ev_async async_w;
+\& thread_t tid;
+\& cond_t invoke_cv;
+\& } userdata;
+\&
+\& void prepare_loop (EV_P)
+\& {
+\& // for simplicity, we use a static userdata struct.
+\& static userdata u;
+\&
+\& ev_async_init (&u\->async_w, async_cb);
+\& ev_async_start (EV_A_ &u\->async_w);
+\&
+\& pthread_mutex_init (&u\->lock, 0);
+\& pthread_cond_init (&u\->invoke_cv, 0);
+\&
+\& // now associate this with the loop
+\& ev_set_userdata (EV_A_ u);
+\& ev_set_invoke_pending_cb (EV_A_ l_invoke);
+\& ev_set_loop_release_cb (EV_A_ l_release, l_acquire);
+\&
+\& // then create the thread running ev_loop
+\& pthread_create (&u\->tid, 0, l_run, EV_A);
+\& }
+.Ve
+.PP
+The callback for the \f(CW\*(C`ev_async\*(C'\fR watcher does nothing: the watcher is used
+solely to wake up the event loop so it takes notice of any new watchers
+that might have been added:
+.PP
+.Vb 5
+\& static void
+\& async_cb (EV_P_ ev_async *w, int revents)
+\& {
+\& // just used for the side effects
+\& }
+.Ve
+.PP
+The \f(CW\*(C`l_release\*(C'\fR and \f(CW\*(C`l_acquire\*(C'\fR callbacks simply unlock/lock the mutex
+protecting the loop data, respectively.
+.PP
+.Vb 6
+\& static void
+\& l_release (EV_P)
+\& {
+\& userdata *u = ev_userdata (EV_A);
+\& pthread_mutex_unlock (&u\->lock);
+\& }
+\&
+\& static void
+\& l_acquire (EV_P)
+\& {
+\& userdata *u = ev_userdata (EV_A);
+\& pthread_mutex_lock (&u\->lock);
+\& }
+.Ve
+.PP
+The event loop thread first acquires the mutex, and then jumps straight
+into \f(CW\*(C`ev_run\*(C'\fR:
+.PP
+.Vb 4
+\& void *
+\& l_run (void *thr_arg)
+\& {
+\& struct ev_loop *loop = (struct ev_loop *)thr_arg;
+\&
+\& l_acquire (EV_A);
+\& pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
+\& ev_run (EV_A_ 0);
+\& l_release (EV_A);
+\&
+\& return 0;
+\& }
+.Ve
+.PP
+Instead of invoking all pending watchers, the \f(CW\*(C`l_invoke\*(C'\fR callback will
+signal the main thread via some unspecified mechanism (signals? pipe
+writes? \f(CW\*(C`Async::Interrupt\*(C'\fR?) and then waits until all pending watchers
+have been called (in a while loop because a) spurious wakeups are possible
+and b) skipping inter-thread-communication when there are no pending
+watchers is very beneficial):
+.PP
+.Vb 4
+\& static void
+\& l_invoke (EV_P)
+\& {
+\& userdata *u = ev_userdata (EV_A);
+\&
+\& while (ev_pending_count (EV_A))
+\& {
+\& wake_up_other_thread_in_some_magic_or_not_so_magic_way ();
+\& pthread_cond_wait (&u\->invoke_cv, &u\->lock);
+\& }
+\& }
+.Ve
+.PP
+Now, whenever the main thread gets told to invoke pending watchers, it
+will grab the lock, call \f(CW\*(C`ev_invoke_pending\*(C'\fR and then signal the loop
+thread to continue:
+.PP
+.Vb 4
+\& static void
+\& real_invoke_pending (EV_P)
+\& {
+\& userdata *u = ev_userdata (EV_A);
+\&
+\& pthread_mutex_lock (&u\->lock);
+\& ev_invoke_pending (EV_A);
+\& pthread_cond_signal (&u\->invoke_cv);
+\& pthread_mutex_unlock (&u\->lock);
+\& }
+.Ve
+.PP
+Whenever you want to start/stop a watcher or do other modifications to an
+event loop, you will now have to lock:
+.PP
+.Vb 2
+\& ev_timer timeout_watcher;
+\& userdata *u = ev_userdata (EV_A);
+\&
+\& ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);
+\&
+\& pthread_mutex_lock (&u\->lock);
+\& ev_timer_start (EV_A_ &timeout_watcher);
+\& ev_async_send (EV_A_ &u\->async_w);
+\& pthread_mutex_unlock (&u\->lock);
+.Ve
+.PP
+Note that sending the \f(CW\*(C`ev_async\*(C'\fR watcher is required because otherwise
+an event loop currently blocking in the kernel will have no knowledge
+about the newly added timer. By waking up the loop it will pick up any new
+watchers in the next event loop iteration.
+.SS "\s-1THREADS\s0, \s-1COROUTINES\s0, \s-1CONTINUATIONS\s0, \s-1QUEUES\s0... \s-1INSTEAD\s0 \s-1OF\s0 \s-1CALLBACKS\s0"
+.IX Subsection "THREADS, COROUTINES, CONTINUATIONS, QUEUES... INSTEAD OF CALLBACKS"
+While the overhead of a callback that e.g. schedules a thread is small, it
+is still an overhead. If you embed libev, and your main usage is with some
+kind of threads or coroutines, you might want to customise libev so that
+doesn't need callbacks anymore.
+.PP
+Imagine you have coroutines that you can switch to using a function
+\&\f(CW\*(C`switch_to (coro)\*(C'\fR, that libev runs in a coroutine called \f(CW\*(C`libev_coro\*(C'\fR
+and that due to some magic, the currently active coroutine is stored in a
+global called \f(CW\*(C`current_coro\*(C'\fR. Then you can build your own \*(L"wait for libev
+event\*(R" primitive by changing \f(CW\*(C`EV_CB_DECLARE\*(C'\fR and \f(CW\*(C`EV_CB_INVOKE\*(C'\fR (note
+the differing \f(CW\*(C`;\*(C'\fR conventions):
+.PP
+.Vb 2
+\& #define EV_CB_DECLARE(type) struct my_coro *cb;
+\& #define EV_CB_INVOKE(watcher) switch_to ((watcher)\->cb)
+.Ve
+.PP
+That means instead of having a C callback function, you store the
+coroutine to switch to in each watcher, and instead of having libev call
+your callback, you instead have it switch to that coroutine.
+.PP
+A coroutine might now wait for an event with a function called
+\&\f(CW\*(C`wait_for_event\*(C'\fR. (the watcher needs to be started, as always, but it doesn't
+matter when, or whether the watcher is active or not when this function is
+called):
+.PP
+.Vb 6
+\& void
+\& wait_for_event (ev_watcher *w)
+\& {
+\& ev_cb_set (w) = current_coro;
+\& switch_to (libev_coro);
+\& }
+.Ve
+.PP
+That basically suspends the coroutine inside \f(CW\*(C`wait_for_event\*(C'\fR and
+continues the libev coroutine, which, when appropriate, switches back to
+this or any other coroutine. I am sure if you sue this your own :)
+.PP
+You can do similar tricks if you have, say, threads with an event queue \-
+instead of storing a coroutine, you store the queue object and instead of
+switching to a coroutine, you push the watcher onto the queue and notify
+any waiters.
+.PP
+To embed libev, see \s-1EMBEDDING\s0, but in short, it's easiest to create two
+files, \fImy_ev.h\fR and \fImy_ev.c\fR that include the respective libev files:
+.PP
+.Vb 4
+\& // my_ev.h
+\& #define EV_CB_DECLARE(type) struct my_coro *cb;
+\& #define EV_CB_INVOKE(watcher) switch_to ((watcher)\->cb);
+\& #include "../libev/ev.h"
+\&
+\& // my_ev.c
+\& #define EV_H "my_ev.h"
+\& #include "../libev/ev.c"
+.Ve
+.PP
+And then use \fImy_ev.h\fR when you would normally use \fIev.h\fR, and compile
+\&\fImy_ev.c\fR into your project. When properly specifying include paths, you
+can even use \fIev.h\fR as header file name directly.
.SH "LIBEVENT EMULATION"
.IX Header "LIBEVENT EMULATION"
Libev offers a compatibility emulation layer for libevent. It cannot
emulate the internals of libevent, so here are some usage hints:
.IP "\(bu" 4
+Only the libevent\-1.4.1\-beta \s-1API\s0 is being emulated.
+.Sp
+This was the newest libevent version available when libev was implemented,
+and is still mostly unchanged in 2010.
+.IP "\(bu" 4
Use it by including <event.h>, as usual.
.IP "\(bu" 4
The following members are fully supported: ev_base, ev_callback,
@@ -3317,7 +3858,7 @@ will fail and all watchers will have the same priority, even though there
is an ev_pri field.
.IP "\(bu" 4
In libevent, the last base created gets the signals, in libev, the
-first base created (== the default loop) gets the signals.
+base that registered the signal gets the signals.
.IP "\(bu" 4
Other members are not supported.
.IP "\(bu" 4
@@ -3345,11 +3886,11 @@ classes add (compared to plain C\-style watchers) is the event loop pointer
that the watcher is associated with (or no additional members at all if
you disable \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR when embedding libev).
.PP
-Currently, functions, and static and non-static member functions can be
-used as callbacks. Other types should be easy to add as long as they only
-need one additional pointer for context. If you need support for other
-types of functors please contact the author (preferably after implementing
-it).
+Currently, functions, static and non-static member functions and classes
+with \f(CW\*(C`operator ()\*(C'\fR can be used as callbacks. Other types should be easy
+to add as long as they only need one additional pointer for context. If
+you need support for other types of functors please contact the author
+(preferably after implementing it).
.PP
Here is a list of things available in the \f(CW\*(C`ev\*(C'\fR namespace:
.ie n .IP """ev::READ"", ""ev::WRITE"" etc." 4
@@ -3419,8 +3960,6 @@ Example: simple class declaration and watcher initialisation
.Ve
.IP "w\->set (object *)" 4
.IX Item "w->set (object *)"
-This is an \fBexperimental\fR feature that might go away in a future version.
-.Sp
This is a variation of a method callback \- leaving out the method to call
will default the method to \f(CW\*(C`operator ()\*(C'\fR, which makes it possible to use
functor objects without having to manually specify the \f(CW\*(C`operator ()\*(C'\fR all
@@ -3470,14 +4009,19 @@ Associates a different \f(CW\*(C`struct ev_loop\*(C'\fR with this watcher. You c
do this when the watcher is inactive (and not pending either).
.IP "w\->set ([arguments])" 4
.IX Item "w->set ([arguments])"
-Basically the same as \f(CW\*(C`ev_TYPE_set\*(C'\fR, 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 \f(CW\*(C`ev_TYPE_set\*(C'\fR, 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.
.IP "w\->start ()" 4
.IX Item "w->start ()"
Starts the watcher. Note that there is no \f(CW\*(C`loop\*(C'\fR argument, as the
constructor already stores the event loop.
+.IP "w\->start ([arguments])" 4
+.IX Item "w->start ([arguments])"
+Instead of calling \f(CW\*(C`set\*(C'\fR and \f(CW\*(C`start\*(C'\fR methods separately, it is often
+convenient to wrap them in one call. Uses the same type of arguments as
+the configure \f(CW\*(C`set\*(C'\fR method of the watcher.
.IP "w\->stop ()" 4
.IX Item "w->stop ()"
Stops the watcher if it is active. Again, no \f(CW\*(C`loop\*(C'\fR argument.
@@ -3498,21 +4042,26 @@ Invokes \f(CW\*(C`ev_stat_stat\*(C'\fR.
.RS 4
.RE
.PP
-Example: Define a class with an \s-1IO\s0 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.
.PP
-.Vb 4
+.Vb 5
\& 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
\& }
\& };
.Ve
@@ -3560,8 +4109,8 @@ Erkki Seppala has written Ocaml bindings for libev, to be found at
<http://modeemi.cs.tut.fi/~flux/software/ocaml\-ev/>.
.IP "Lua" 4
.IX Item "Lua"
-Brian Maher has written a partial interface to libev
-for lua (only \f(CW\*(C`ev_io\*(C'\fR and \f(CW\*(C`ev_timer\*(C'\fR), to be found at
+Brian Maher has written a partial interface to libev for lua (at the
+time of this writing, only \f(CW\*(C`ev_io\*(C'\fR and \f(CW\*(C`ev_timer\*(C'\fR), to be found at
<http://github.com/brimworks/lua\-ev>.
.SH "MACRO MAGIC"
.IX Header "MACRO MAGIC"
@@ -3581,7 +4130,7 @@ loop argument\*(R"). The \f(CW\*(C`EV_A\*(C'\fR form is used when this is the so
.Vb 3
\& ev_unref (EV_A);
\& ev_timer_add (EV_A_ watcher);
-\& ev_loop (EV_A_ 0);
+\& ev_run (EV_A_ 0);
.Ve
.Sp
It assumes the variable \f(CW\*(C`loop\*(C'\fR of type \f(CW\*(C`struct ev_loop *\*(C'\fR is in scope,
@@ -3633,7 +4182,7 @@ or not.
\& 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);
.Ve
.SH "EMBEDDING"
.IX Header "EMBEDDING"
@@ -3738,10 +4287,32 @@ For this of course you need the m4 file:
.SS "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0"
.IX Subsection "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.
-.IP "\s-1EV_STANDALONE\s0" 4
-.IX Item "EV_STANDALONE"
+define before including (or compiling) any of its files. The default in
+the absence of autoconf is documented for every option.
+.PP
+Symbols marked with \*(L"(h)\*(R" do not change the \s-1ABI\s0, and can have different
+values when compiling libev vs. including \fIev.h\fR, so it is permissible
+to redefine them before including \fIev.h\fR without breaking compatibility
+to a compiled library. All other symbols change the \s-1ABI\s0, which means all
+users of libev and the libev code itself must be compiled with compatible
+settings.
+.IP "\s-1EV_COMPAT3\s0 (h)" 4
+.IX 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.
+.Sp
+You can disable these wrappers (to test compatibility with future
+versions) by defining \f(CW\*(C`EV_COMPAT3\*(C'\fR to \f(CW0\fR when compiling your
+sources. This has the additional advantage that you can drop the \f(CW\*(C`struct\*(C'\fR
+from \f(CW\*(C`struct ev_loop\*(C'\fR declarations, as libev will provide an \f(CW\*(C`ev_loop\*(C'\fR
+typedef in that case.
+.Sp
+In some future version, the default for \f(CW\*(C`EV_COMPAT3\*(C'\fR will become \f(CW0\fR,
+and in some even more future version the compatibility code will be
+removed completely.
+.IP "\s-1EV_STANDALONE\s0 (h)" 4
+.IX Item "EV_STANDALONE (h)"
Must always be \f(CW1\fR if you do not use autoconf configuration, which
keeps libev from including \fIconfig.h\fR, and it also defines dummy
implementations for some libevent functions (such as logging, which is not
@@ -3881,22 +4452,22 @@ as well as for signal and thread safety in \f(CW\*(C`ev_async\*(C'\fR watchers.
.Sp
In the absence of this define, libev will use \f(CW\*(C`sig_atomic_t volatile\*(C'\fR
(from \fIsignal.h\fR), which is usually good enough on most platforms.
-.IP "\s-1EV_H\s0" 4
-.IX Item "EV_H"
+.IP "\s-1EV_H\s0 (h)" 4
+.IX Item "EV_H (h)"
The name of the \fIev.h\fR header file used to include it. The default if
undefined is \f(CW"ev.h"\fR in \fIevent.h\fR, \fIev.c\fR and \fIev++.h\fR. This can be
used to virtually rename the \fIev.h\fR header file in case of conflicts.
-.IP "\s-1EV_CONFIG_H\s0" 4
-.IX Item "EV_CONFIG_H"
+.IP "\s-1EV_CONFIG_H\s0 (h)" 4
+.IX Item "EV_CONFIG_H (h)"
If \f(CW\*(C`EV_STANDALONE\*(C'\fR isn't \f(CW1\fR, this variable can be used to override
\&\fIev.c\fR's idea of where to find the \fIconfig.h\fR file, similarly to
\&\f(CW\*(C`EV_H\*(C'\fR, above.
-.IP "\s-1EV_EVENT_H\s0" 4
-.IX Item "EV_EVENT_H"
+.IP "\s-1EV_EVENT_H\s0 (h)" 4
+.IX Item "EV_EVENT_H (h)"
Similarly to \f(CW\*(C`EV_H\*(C'\fR, this macro can be used to override \fIevent.c\fR's idea
of how the \fIevent.h\fR header can be found, the default is \f(CW"event.h"\fR.
-.IP "\s-1EV_PROTOTYPES\s0" 4
-.IX Item "EV_PROTOTYPES"
+.IP "\s-1EV_PROTOTYPES\s0 (h)" 4
+.IX Item "EV_PROTOTYPES (h)"
If defined to be \f(CW0\fR, then \fIev.h\fR will not define any function
prototypes, but still define all the structs and other symbols. This is
occasionally useful if you want to provide your own wrapper functions
@@ -3926,70 +4497,125 @@ fine.
.Sp
If your embedding application does not need any priorities, defining these
both to \f(CW0\fR will save some memory and \s-1CPU\s0.
-.IP "\s-1EV_PERIODIC_ENABLE\s0" 4
-.IX Item "EV_PERIODIC_ENABLE"
-If undefined or defined to be \f(CW1\fR, then periodic timers are supported. If
-defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of
-code.
-.IP "\s-1EV_IDLE_ENABLE\s0" 4
-.IX Item "EV_IDLE_ENABLE"
-If undefined or defined to be \f(CW1\fR, then idle watchers are supported. If
-defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of
-code.
-.IP "\s-1EV_EMBED_ENABLE\s0" 4
-.IX Item "EV_EMBED_ENABLE"
-If undefined or defined to be \f(CW1\fR, then embed watchers are supported. If
-defined to be \f(CW0\fR, then they are not. Embed watchers rely on most other
-watcher types, which therefore must not be disabled.
-.IP "\s-1EV_STAT_ENABLE\s0" 4
-.IX Item "EV_STAT_ENABLE"
-If undefined or defined to be \f(CW1\fR, then stat watchers are supported. If
-defined to be \f(CW0\fR, then they are not.
-.IP "\s-1EV_FORK_ENABLE\s0" 4
-.IX Item "EV_FORK_ENABLE"
-If undefined or defined to be \f(CW1\fR, then fork watchers are supported. If
-defined to be \f(CW0\fR, then they are not.
-.IP "\s-1EV_ASYNC_ENABLE\s0" 4
-.IX Item "EV_ASYNC_ENABLE"
-If undefined or defined to be \f(CW1\fR, then async watchers are supported. If
-defined to be \f(CW0\fR, then they are not.
-.IP "\s-1EV_MINIMAL\s0" 4
-.IX Item "EV_MINIMAL"
+.IP "\s-1EV_PERIODIC_ENABLE\s0, \s-1EV_IDLE_ENABLE\s0, \s-1EV_EMBED_ENABLE\s0, \s-1EV_STAT_ENABLE\s0, \s-1EV_PREPARE_ENABLE\s0, \s-1EV_CHECK_ENABLE\s0, \s-1EV_FORK_ENABLE\s0, \s-1EV_SIGNAL_ENABLE\s0, \s-1EV_ASYNC_ENABLE\s0, \s-1EV_CHILD_ENABLE\s0." 4
+.IX 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 \f(CW1\fR (and the platform supports it), then
+the respective watcher type is supported. If defined to be \f(CW0\fR, then it
+is not. Disabling watcher types mainly saves code size.
+.IP "\s-1EV_FEATURES\s0" 4
+.IX Item "EV_FEATURES"
If you need to shave off some kilobytes of code at the expense of some
-speed (but with the full \s-1API\s0), define this symbol to \f(CW1\fR. 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.
-.Sp
-You can save even more by disabling watcher types you do not need
-and setting \f(CW\*(C`EV_MAXPRI\*(C'\fR == \f(CW\*(C`EV_MINPRI\*(C'\fR. Also, disabling \f(CW\*(C`assert\*(C'\fR
-(\f(CW\*(C`\-DNDEBUG\*(C'\fR) will usually reduce code size a lot.
-.Sp
-Defining \f(CW\*(C`EV_MINIMAL\*(C'\fR to \f(CW2\fR will additionally reduce the core \s-1API\s0 to
-provide a bare-bones event library. See \f(CW\*(C`ev.h\*(C'\fR for details on what parts
-of the \s-1API\s0 are still available, and do not complain if this subset changes
-over time.
+speed (but with the full \s-1API\s0), 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.
+.Sp
+A typical way to use this symbol is to define it to \f(CW0\fR (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:
+.Sp
+.Vb 5
+\& #define EV_FEATURES 0
+\& #define EV_MULTIPLICITY 1
+\& #define EV_USE_POLL 1
+\& #define EV_CHILD_ENABLE 1
+\& #define EV_ASYNC_ENABLE 1
+.Ve
+.Sp
+The actual value is a bitset, it can be a combination of the following
+values:
+.RS 4
+.ie n .IP "1 \- faster/larger code" 4
+.el .IP "\f(CW1\fR \- faster/larger code" 4
+.IX Item "1 - faster/larger code"
+Use larger code to speed up some operations.
+.Sp
+Currently this is used to override some inlining decisions (enlarging the
+code size by roughly 30% on amd64).
+.Sp
+When optimising for size, use of compiler flags such as \f(CW\*(C`\-Os\*(C'\fR with
+gcc is recommended, as well as \f(CW\*(C`\-DNDEBUG\*(C'\fR, as libev contains a number of
+assertions.
+.ie n .IP "2 \- faster/larger data structures" 4
+.el .IP "\f(CW2\fR \- faster/larger data structures" 4
+.IX Item "2 - faster/larger data structures"
+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.
+.ie n .IP "4 \- full \s-1API\s0 configuration" 4
+.el .IP "\f(CW4\fR \- full \s-1API\s0 configuration" 4
+.IX Item "4 - full API configuration"
+This enables priorities (sets \f(CW\*(C`EV_MAXPRI\*(C'\fR=2 and \f(CW\*(C`EV_MINPRI\*(C'\fR=\-2), and
+enables multiplicity (\f(CW\*(C`EV_MULTIPLICITY\*(C'\fR=1).
+.ie n .IP "8 \- full \s-1API\s0" 4
+.el .IP "\f(CW8\fR \- full \s-1API\s0" 4
+.IX Item "8 - full API"
+This enables a lot of the \*(L"lesser used\*(R" \s-1API\s0 functions. See \f(CW\*(C`ev.h\*(C'\fR for
+details on which parts of the \s-1API\s0 are still available without this
+feature, and do not complain if this subset changes over time.
+.ie n .IP "16 \- enable all optional watcher types" 4
+.el .IP "\f(CW16\fR \- enable all optional watcher types" 4
+.IX Item "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
+\&\f(CW\*(C`EV_watchertype_ENABLE\*(C'\fR to \f(CW1\fR instead.
+.ie n .IP "32 \- enable all backends" 4
+.el .IP "\f(CW32\fR \- enable all backends" 4
+.IX Item "32 - enable all backends"
+This enables all backends \- without this feature, you need to enable at
+least one backend manually (\f(CW\*(C`EV_USE_SELECT\*(C'\fR is a good choice).
+.ie n .IP "64 \- enable OS-specific ""helper"" APIs" 4
+.el .IP "\f(CW64\fR \- enable OS-specific ``helper'' APIs" 4
+.IX Item "64 - enable OS-specific helper APIs"
+Enable inotify, eventfd, signalfd and similar OS-specific helper APIs by
+default.
+.RE
+.RS 4
+.Sp
+Compiling with \f(CW\*(C`gcc \-Os \-DEV_STANDALONE \-DEV_USE_EPOLL=1 \-DEV_FEATURES=0\*(C'\fR
+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.
+.Sp
+With an intelligent-enough linker (gcc+binutils are intelligent enough
+when you use \f(CW\*(C`\-Wl,\-\-gc\-sections \-ffunction\-sections\*(C'\fR) 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.
+.RE
+.IP "\s-1EV_AVOID_STDIO\s0" 4
+.IX Item "EV_AVOID_STDIO"
+If this is set to \f(CW1\fR 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.
+.Sp
+Note that error messages might become less precise when this option is
+enabled.
.IP "\s-1EV_NSIG\s0" 4
.IX Item "EV_NSIG"
The highest supported signal number, +1 (or, the number of
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 (\f(CW32\fR 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.
.IP "\s-1EV_PID_HASHSIZE\s0" 4
.IX Item "EV_PID_HASHSIZE"
\&\f(CW\*(C`ev_child\*(C'\fR watchers use a small hash table to distribute workload by
-pid. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), usually more
-than enough. If you need to manage thousands of children you might want to
-increase this value (\fImust\fR be a power of two).
+pid. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_FEATURES\*(C'\fR disabled),
+usually more than enough. If you need to manage thousands of children you
+might want to increase this value (\fImust\fR be a power of two).
.IP "\s-1EV_INOTIFY_HASHSIZE\s0" 4
.IX Item "EV_INOTIFY_HASHSIZE"
\&\f(CW\*(C`ev_stat\*(C'\fR watchers use a small hash table to distribute workload by
-inotify watch id. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR),
-usually more than enough. If you need to manage thousands of \f(CW\*(C`ev_stat\*(C'\fR
-watchers you might want to increase this value (\fImust\fR be a power of
-two).
+inotify watch id. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_FEATURES\*(C'\fR
+disabled), usually more than enough. If you need to manage thousands of
+\&\f(CW\*(C`ev_stat\*(C'\fR watchers you might want to increase this value (\fImust\fR be a
+power of two).
.IP "\s-1EV_USE_4HEAP\s0" 4
.IX Item "EV_USE_4HEAP"
Heaps are not very cache-efficient. To improve the cache-efficiency of the
@@ -3997,8 +4623,8 @@ timer and periodics heaps, libev uses a 4\-heap when this symbol is defined
to \f(CW1\fR. The 4\-heap uses more complicated (longer) code but has noticeably
faster performance with many (thousands) of watchers.
.Sp
-The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR
-(disabled).
+The default is \f(CW1\fR, unless \f(CW\*(C`EV_FEATURES\*(C'\fR overrides it, in which case it
+will be \f(CW0\fR.
.IP "\s-1EV_HEAP_CACHE_AT\s0" 4
.IX Item "EV_HEAP_CACHE_AT"
Heaps are not very cache-efficient. To improve the cache-efficiency of the
@@ -4008,11 +4634,11 @@ which uses 8\-12 bytes more per watcher and a few hundred bytes more code,
but avoids random read accesses on heap changes. This improves performance
noticeably with many (hundreds) of watchers.
.Sp
-The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR
-(disabled).
+The default is \f(CW1\fR, unless \f(CW\*(C`EV_FEATURES\*(C'\fR overrides it, in which case it
+will be \f(CW0\fR.
.IP "\s-1EV_VERIFY\s0" 4
.IX Item "EV_VERIFY"
-Controls how much internal verification (see \f(CW\*(C`ev_loop_verify ()\*(C'\fR) will
+Controls how much internal verification (see \f(CW\*(C`ev_verify ()\*(C'\fR) will
be done: If set to \f(CW0\fR, no internal verification code will be compiled
in. If set to \f(CW1\fR, then verification code will be compiled in, but not
called. If set to \f(CW2\fR, then the internal verification code will be
@@ -4020,12 +4646,12 @@ called once per loop, which can slow down libev. If set to \f(CW3\fR, then the
verification code will be called very frequently, which will slow down
libev considerably.
.Sp
-The default is \f(CW1\fR, unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set, in which case it will be
-\&\f(CW0\fR.
+The default is \f(CW1\fR, unless \f(CW\*(C`EV_FEATURES\*(C'\fR overrides it, in which case it
+will be \f(CW0\fR.
.IP "\s-1EV_COMMON\s0" 4
.IX Item "EV_COMMON"
By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR 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.
.Sp
@@ -4093,16 +4719,15 @@ file.
The usage in rxvt-unicode is simpler. It has a \fIev_cpp.h\fR header file
that everybody includes and which overrides some configure choices:
.PP
-.Vb 9
-\& #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
+.Vb 8
+\& #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"
.Ve
@@ -4113,8 +4738,8 @@ And a \fIev_cpp.C\fR implementation file that contains libev proper and is compi
\& #include "ev_cpp.h"
\& #include "ev.c"
.Ve
-.SH "INTERACTION WITH OTHER PROGRAMS OR LIBRARIES"
-.IX Header "INTERACTION WITH OTHER PROGRAMS OR LIBRARIES"
+.SH "INTERACTION WITH OTHER PROGRAMS, LIBRARIES OR THE ENVIRONMENT"
+.IX Header "INTERACTION WITH OTHER PROGRAMS, LIBRARIES OR THE ENVIRONMENT"
.SS "\s-1THREADS\s0 \s-1AND\s0 \s-1COROUTINES\s0"
.IX Subsection "THREADS AND COROUTINES"
\fI\s-1THREADS\s0\fR
@@ -4171,171 +4796,20 @@ work in the default loop by registering the signal watcher with the
default loop and triggering an \f(CW\*(C`ev_async\*(C'\fR watcher from the default loop
watcher callback into the event loop interested in the signal.
.PP
-\s-1THREAD\s0 \s-1LOCKING\s0 \s-1EXAMPLE\s0
-.IX Subsection "THREAD LOCKING EXAMPLE"
-.PP
-Here is a fictitious example of how to run an event loop in a different
-thread than where callbacks are being invoked and watchers are
-created/added/removed.
-.PP
-For a real-world example, see the \f(CW\*(C`EV::Loop::Async\*(C'\fR perl module,
-which uses exactly this technique (which is suited for many high-level
-languages).
-.PP
-The example uses a pthread mutex to protect the loop data, a condition
-variable to wait for callback invocations, an async watcher to notify the
-event loop thread and an unspecified mechanism to wake up the main thread.
-.PP
-First, you need to associate some data with the event loop:
-.PP
-.Vb 6
-\& typedef struct {
-\& mutex_t lock; /* global loop lock */
-\& ev_async async_w;
-\& thread_t tid;
-\& cond_t invoke_cv;
-\& } userdata;
-\&
-\& void prepare_loop (EV_P)
-\& {
-\& // for simplicity, we use a static userdata struct.
-\& static userdata u;
-\&
-\& ev_async_init (&u\->async_w, async_cb);
-\& ev_async_start (EV_A_ &u\->async_w);
-\&
-\& pthread_mutex_init (&u\->lock, 0);
-\& pthread_cond_init (&u\->invoke_cv, 0);
-\&
-\& // now associate this with the loop
-\& ev_set_userdata (EV_A_ u);
-\& ev_set_invoke_pending_cb (EV_A_ l_invoke);
-\& ev_set_loop_release_cb (EV_A_ l_release, l_acquire);
-\&
-\& // then create the thread running ev_loop
-\& pthread_create (&u\->tid, 0, l_run, EV_A);
-\& }
-.Ve
-.PP
-The callback for the \f(CW\*(C`ev_async\*(C'\fR watcher does nothing: the watcher is used
-solely to wake up the event loop so it takes notice of any new watchers
-that might have been added:
-.PP
-.Vb 5
-\& static void
-\& async_cb (EV_P_ ev_async *w, int revents)
-\& {
-\& // just used for the side effects
-\& }
-.Ve
-.PP
-The \f(CW\*(C`l_release\*(C'\fR and \f(CW\*(C`l_acquire\*(C'\fR callbacks simply unlock/lock the mutex
-protecting the loop data, respectively.
-.PP
-.Vb 6
-\& static void
-\& l_release (EV_P)
-\& {
-\& userdata *u = ev_userdata (EV_A);
-\& pthread_mutex_unlock (&u\->lock);
-\& }
-\&
-\& static void
-\& l_acquire (EV_P)
-\& {
-\& userdata *u = ev_userdata (EV_A);
-\& pthread_mutex_lock (&u\->lock);
-\& }
-.Ve
-.PP
-The event loop thread first acquires the mutex, and then jumps straight
-into \f(CW\*(C`ev_loop\*(C'\fR:
-.PP
-.Vb 4
-\& void *
-\& l_run (void *thr_arg)
-\& {
-\& struct ev_loop *loop = (struct ev_loop *)thr_arg;
-\&
-\& l_acquire (EV_A);
-\& pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
-\& ev_loop (EV_A_ 0);
-\& l_release (EV_A);
-\&
-\& return 0;
-\& }
-.Ve
-.PP
-Instead of invoking all pending watchers, the \f(CW\*(C`l_invoke\*(C'\fR callback will
-signal the main thread via some unspecified mechanism (signals? pipe
-writes? \f(CW\*(C`Async::Interrupt\*(C'\fR?) and then waits until all pending watchers
-have been called (in a while loop because a) spurious wakeups are possible
-and b) skipping inter-thread-communication when there are no pending
-watchers is very beneficial):
-.PP
-.Vb 4
-\& static void
-\& l_invoke (EV_P)
-\& {
-\& userdata *u = ev_userdata (EV_A);
-\&
-\& while (ev_pending_count (EV_A))
-\& {
-\& wake_up_other_thread_in_some_magic_or_not_so_magic_way ();
-\& pthread_cond_wait (&u\->invoke_cv, &u\->lock);
-\& }
-\& }
-.Ve
-.PP
-Now, whenever the main thread gets told to invoke pending watchers, it
-will grab the lock, call \f(CW\*(C`ev_invoke_pending\*(C'\fR and then signal the loop
-thread to continue:
-.PP
-.Vb 4
-\& static void
-\& real_invoke_pending (EV_P)
-\& {
-\& userdata *u = ev_userdata (EV_A);
-\&
-\& pthread_mutex_lock (&u\->lock);
-\& ev_invoke_pending (EV_A);
-\& pthread_cond_signal (&u\->invoke_cv);
-\& pthread_mutex_unlock (&u\->lock);
-\& }
-.Ve
-.PP
-Whenever you want to start/stop a watcher or do other modifications to an
-event loop, you will now have to lock:
-.PP
-.Vb 2
-\& ev_timer timeout_watcher;
-\& userdata *u = ev_userdata (EV_A);
-\&
-\& ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);
-\&
-\& pthread_mutex_lock (&u\->lock);
-\& ev_timer_start (EV_A_ &timeout_watcher);
-\& ev_async_send (EV_A_ &u\->async_w);
-\& pthread_mutex_unlock (&u\->lock);
-.Ve
-.PP
-Note that sending the \f(CW\*(C`ev_async\*(C'\fR watcher is required because otherwise
-an event loop currently blocking in the kernel will have no knowledge
-about the newly added timer. By waking up the loop it will pick up any new
-watchers in the next event loop iteration.
+See also \*(L"\s-1THREAD\s0 \s-1LOCKING\s0 \s-1EXAMPLE\s0\*(R".
.PP
\fI\s-1COROUTINES\s0\fR
.IX Subsection "COROUTINES"
.PP
Libev is very accommodating to coroutines (\*(L"cooperative threads\*(R"):
libev fully supports nesting calls to its functions from different
-coroutines (e.g. you can call \f(CW\*(C`ev_loop\*(C'\fR on the same loop from two
+coroutines (e.g. you can call \f(CW\*(C`ev_run\*(C'\fR 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 \f(CW\*(C`ev_periodic\*(C'\fR reschedule callbacks.
.PP
Care has been taken to ensure that libev does not keep local state inside
-\&\f(CW\*(C`ev_loop\*(C'\fR, and other calls do not usually allow for coroutine switches as
+\&\f(CW\*(C`ev_run\*(C'\fR, and other calls do not usually allow for coroutine switches as
they do not call any callbacks.
.SS "\s-1COMPILER\s0 \s-1WARNINGS\s0"
.IX Subsection "COMPILER WARNINGS"
@@ -4355,7 +4829,7 @@ maintainable.
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.
.PP
@@ -4399,19 +4873,111 @@ If you need, for some reason, empty reports from valgrind for your project
I suggest using suppression lists.
.SH "PORTABILITY NOTES"
.IX Header "PORTABILITY NOTES"
+.SS "\s-1GNU/LINUX\s0 32 \s-1BIT\s0 \s-1LIMITATIONS\s0"
+.IX Subsection "GNU/LINUX 32 BIT LIMITATIONS"
+GNU/Linux is the only common platform that supports 64 bit file/large file
+interfaces but \fIdisables\fR them by default.
+.PP
+That means that libev compiled in the default environment doesn't support
+files larger than 2GiB or so, which mainly affects \f(CW\*(C`ev_stat\*(C'\fR watchers.
+.PP
+Unfortunately, many programs try to work around this GNU/Linux issue
+by enabling the large file \s-1API\s0, which makes them incompatible with the
+standard libev compiled for their system.
+.PP
+Likewise, libev cannot enable the large file \s-1API\s0 itself as this would
+suddenly make it incompatible to the default compile time environment,
+i.e. all programs not using special compile switches.
+.SS "\s-1OS/X\s0 \s-1AND\s0 \s-1DARWIN\s0 \s-1BUGS\s0"
+.IX Subsection "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.
+.PP
+\fI\f(CI\*(C`kqueue\*(C'\fI is buggy\fR
+.IX Subsection "kqueue is buggy"
+.PP
+The kqueue syscall is broken in all known versions \- most versions support
+only sockets, many support pipes.
+.PP
+Libev tries to work around this by not using \f(CW\*(C`kqueue\*(C'\fR by default on this
+rotten platform, but of course you can still ask for it when creating a
+loop \- embedding a socket-only kqueue loop into a select-based one is
+probably going to work well.
+.PP
+\fI\f(CI\*(C`poll\*(C'\fI is buggy\fR
+.IX Subsection "poll is buggy"
+.PP
+Instead of fixing \f(CW\*(C`kqueue\*(C'\fR, Apple replaced their (working) \f(CW\*(C`poll\*(C'\fR
+implementation by something calling \f(CW\*(C`kqueue\*(C'\fR internally around the 10.5.6
+release, so now \f(CW\*(C`kqueue\*(C'\fR \fIand\fR \f(CW\*(C`poll\*(C'\fR are broken.
+.PP
+Libev tries to work around this by not using \f(CW\*(C`poll\*(C'\fR by default on
+this rotten platform, but of course you can still ask for it when creating
+a loop.
+.PP
+\fI\f(CI\*(C`select\*(C'\fI is buggy\fR
+.IX Subsection "select is buggy"
+.PP
+All that's left is \f(CW\*(C`select\*(C'\fR, and of course Apple found a way to fuck this
+one up as well: On \s-1OS/X\s0, \f(CW\*(C`select\*(C'\fR actively limits the number of file
+descriptors you can pass in to 1024 \- your program suddenly crashes when
+you use more.
+.PP
+There is an undocumented \*(L"workaround\*(R" for this \- defining
+\&\f(CW\*(C`_DARWIN_UNLIMITED_SELECT\*(C'\fR, which libev tries to use, so select \fIshould\fR
+work on \s-1OS/X\s0.
+.SS "\s-1SOLARIS\s0 \s-1PROBLEMS\s0 \s-1AND\s0 \s-1WORKAROUNDS\s0"
+.IX Subsection "SOLARIS PROBLEMS AND WORKAROUNDS"
+\fI\f(CI\*(C`errno\*(C'\fI reentrancy\fR
+.IX Subsection "errno reentrancy"
+.PP
+The default compile environment on Solaris is unfortunately so
+thread-unsafe that you can't even use components/libraries compiled
+without \f(CW\*(C`\-D_REENTRANT\*(C'\fR in a threaded program, which, of course, isn't
+defined by default. A valid, if stupid, implementation choice.
+.PP
+If you want to use libev in threaded environments you have to make sure
+it's compiled with \f(CW\*(C`_REENTRANT\*(C'\fR defined.
+.PP
+\fIEvent port backend\fR
+.IX Subsection "Event port backend"
+.PP
+The scalable event interface for Solaris is called \*(L"event
+ports\*(R". Unfortunately, this mechanism is very buggy in all major
+releases. If you run into high \s-1CPU\s0 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 to apply, and afterwards, event ports actually work
+great.
+.PP
+If you can't get it to work, you can try running the program by setting
+the environment variable \f(CW\*(C`LIBEV_FLAGS=3\*(C'\fR to only allow \f(CW\*(C`poll\*(C'\fR and
+\&\f(CW\*(C`select\*(C'\fR backends.
+.SS "\s-1AIX\s0 \s-1POLL\s0 \s-1BUG\s0"
+.IX Subsection "AIX POLL BUG"
+\&\s-1AIX\s0 unfortunately has a broken \f(CW\*(C`poll.h\*(C'\fR 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 \f(CW\*(C`select\*(C'\fR works fine
+with large bitsets on \s-1AIX\s0, and \s-1AIX\s0 is dead anyway.
.SS "\s-1WIN32\s0 \s-1PLATFORM\s0 \s-1LIMITATIONS\s0 \s-1AND\s0 \s-1WORKAROUNDS\s0"
.IX Subsection "WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS"
+\fIGeneral issues\fR
+.IX Subsection "General issues"
+.PP
Win32 doesn't support any of the standards (e.g. \s-1POSIX\s0) that libev
requires, and its I/O model is fundamentally incompatible with the \s-1POSIX\s0
model. Libev still offers limited functionality on this platform in
the form of the \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR 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.
.PP
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).
.PP
There is no supported compilation method available on windows except
embedding it into other applications.
@@ -4452,8 +5018,10 @@ you do \fInot\fR compile the \fIev.c\fR or any other embedded source files!):
\& #include "evwrap.h"
\& #include "ev.c"
.Ve
-.IP "The winsocket select function" 4
-.IX Item "The winsocket select function"
+.PP
+\fIThe winsocket \f(CI\*(C`select\*(C'\fI function\fR
+.IX Subsection "The winsocket select function"
+.PP
The winsocket \f(CW\*(C`select\*(C'\fR function doesn't follow \s-1POSIX\s0 in that it
requires socket \fIhandles\fR and not socket \fIfile descriptors\fR (it is
also extremely buggy). This makes select very inefficient, and also
@@ -4461,32 +5029,34 @@ requires a mapping from file descriptors to socket handles (the Microsoft
C runtime provides the function \f(CW\*(C`_open_osfhandle\*(C'\fR for this). See the
discussion of the \f(CW\*(C`EV_SELECT_USE_FD_SET\*(C'\fR, \f(CW\*(C`EV_SELECT_IS_WINSOCKET\*(C'\fR and
\&\f(CW\*(C`EV_FD_TO_WIN32_HANDLE\*(C'\fR preprocessor symbols for more info.
-.Sp
+.PP
The configuration for a \*(L"naked\*(R" win32 using the Microsoft runtime
libraries and raw winsocket select is:
-.Sp
+.PP
.Vb 2
\& #define EV_USE_SELECT 1
\& #define EV_SELECT_IS_WINSOCKET 1 /* forces EV_SELECT_USE_FD_SET, too */
.Ve
-.Sp
+.PP
Note that winsockets handling of fd sets is O(n), so you can easily get a
complexity in the O(nA\*^X) range when using win32.
-.IP "Limited number of file descriptors" 4
-.IX Item "Limited number of file descriptors"
+.PP
+\fILimited number of file descriptors\fR
+.IX Subsection "Limited number of file descriptors"
+.PP
Windows has numerous arbitrary (and low) limits on things.
-.Sp
+.PP
Early versions of winsocket's select only supported waiting for a maximum
of \f(CW64\fR handles (probably owning to the fact that all windows kernels
can only wait for \f(CW64\fR things at the same time internally; Microsoft
recommends spawning a chain of threads and wait for 63 handles and the
previous thread in each. Sounds great!).
-.Sp
+.PP
Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR
to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select
call (which might be in libev or elsewhere, for example, perl and many
other interpreters do their own select emulation on windows).
-.Sp
+.PP
Another limit is the number of file descriptors in the Microsoft runtime
libraries, which by default is \f(CW64\fR (there must be a hidden \fI64\fR
fetish or something like this inside Microsoft). You can increase this
@@ -4508,6 +5078,10 @@ structure (guaranteed by \s-1POSIX\s0 but not by \s-1ISO\s0 C for example), but
assumes that the same (machine) code can be used to call any watcher
callback: The watcher callbacks have different type signatures, but libev
calls them using an \f(CW\*(C`ev_watcher *\*(C'\fR internally.
+.IP "pointer accesses must be thread-atomic" 4
+.IX Item "pointer accesses must be thread-atomic"
+Accessing a pointer value must be atomic, it must both be readable and
+writable in one piece \- this is the case on all current architectures.
.ie n .IP """sig_atomic_t volatile"" must be thread-atomic as well" 4
.el .IP "\f(CWsig_atomic_t volatile\fR must be thread-atomic as well" 4
.IX Item "sig_atomic_t volatile must be thread-atomic as well"
@@ -4540,11 +5114,11 @@ watchers.
.el .IP "\f(CWdouble\fR must hold a time value in seconds with enough accuracy" 4
.IX Item "double must hold a time value in seconds with enough accuracy"
The type \f(CW\*(C`double\*(C'\fR 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 \s-1IEEE\s0 754, which is basically all existing
-ones. With \s-1IEEE\s0 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 \s-1IEEE\s0 754, which is basically all existing ones. With
+\&\s-1IEEE\s0 754 doubles, you get microsecond accuracy until at least 2200.
.PP
If you know of other additional requirements drop me a note.
.SH "ALGORITHMIC COMPLEXITIES"
@@ -4610,22 +5184,82 @@ watchers becomes O(1) with respect to priority handling.
Sending involves a system call \fIiff\fR there were no other \f(CW\*(C`ev_async_send\*(C'\fR
calls in the current loop iteration. Checking for async and signal events
involves iterating over all running async watchers or all signal numbers.
+.SH "PORTING FROM LIBEV 3.X TO 4.X"
+.IX Header "PORTING FROM LIBEV 3.X TO 4.X"
+The major version 4 introduced some incompatible changes to the \s-1API\s0.
+.PP
+At the moment, the \f(CW\*(C`ev.h\*(C'\fR header file provides compatibility definitions
+for all changes, so most programs should still compile. The compatibility
+layer might be removed in later versions of libev, so better update to the
+new \s-1API\s0 early than late.
+.ie n .IP """EV_COMPAT3"" backwards compatibility mechanism" 4
+.el .IP "\f(CWEV_COMPAT3\fR backwards compatibility mechanism" 4
+.IX Item "EV_COMPAT3 backwards compatibility mechanism"
+The backward compatibility mechanism can be controlled by
+\&\f(CW\*(C`EV_COMPAT3\*(C'\fR. See \*(L"\s-1MACROS\s0\*(R" in \s-1PREPROCESSOR\s0 \s-1SYMBOLS\s0 in the \s-1EMBEDDING\s0
+section.
+.ie n .IP """ev_default_destroy"" and ""ev_default_fork"" have been removed" 4
+.el .IP "\f(CWev_default_destroy\fR and \f(CWev_default_fork\fR have been removed" 4
+.IX Item "ev_default_destroy and ev_default_fork have been removed"
+These calls can be replaced easily by their \f(CW\*(C`ev_loop_xxx\*(C'\fR counterparts:
+.Sp
+.Vb 2
+\& ev_loop_destroy (EV_DEFAULT_UC);
+\& ev_loop_fork (EV_DEFAULT);
+.Ve
+.IP "function/symbol renames" 4
+.IX Item "function/symbol renames"
+A number of functions and symbols have been renamed:
+.Sp
+.Vb 3
+\& 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
+.Ve
+.Sp
+Most functions working on \f(CW\*(C`struct ev_loop\*(C'\fR objects don't have an
+\&\f(CW\*(C`ev_loop_\*(C'\fR prefix, so it was removed; \f(CW\*(C`ev_loop\*(C'\fR, \f(CW\*(C`ev_unloop\*(C'\fR and
+associated constants have been renamed to not collide with the \f(CW\*(C`struct
+ev_loop\*(C'\fR anymore and \f(CW\*(C`EV_TIMER\*(C'\fR now follows the same naming scheme
+as all other watcher types. Note that \f(CW\*(C`ev_loop_fork\*(C'\fR is still called
+\&\f(CW\*(C`ev_loop_fork\*(C'\fR because it would otherwise clash with the \f(CW\*(C`ev_fork\*(C'\fR
+typedef.
+.ie n .IP """EV_MINIMAL"" mechanism replaced by ""EV_FEATURES""" 4
+.el .IP "\f(CWEV_MINIMAL\fR mechanism replaced by \f(CWEV_FEATURES\fR" 4
+.IX Item "EV_MINIMAL mechanism replaced by EV_FEATURES"
+The preprocessor symbol \f(CW\*(C`EV_MINIMAL\*(C'\fR has been replaced by a different
+mechanism, \f(CW\*(C`EV_FEATURES\*(C'\fR. Programs using \f(CW\*(C`EV_MINIMAL\*(C'\fR usually compile
+and work, but the library code will of course be larger.
.SH "GLOSSARY"
.IX Header "GLOSSARY"
.IP "active" 4
.IX Item "active"
-A watcher is active as long as it has been started (has been attached to
-an event loop) but not yet stopped (disassociated from the event loop).
+A watcher is active as long as it has been started and not yet stopped.
+See \*(L"\s-1WATCHER\s0 \s-1STATES\s0\*(R" for details.
.IP "application" 4
.IX Item "application"
In this document, an application is whatever is using libev.
+.IP "backend" 4
+.IX Item "backend"
+The part of the code dealing with the operating system interfaces.
.IP "callback" 4
.IX Item "callback"
The address of a function that is called when some event has been
detected. Callbacks are being passed the event loop, the watcher that
received the event, and the actual event bitset.
-.IP "callback invocation" 4
-.IX Item "callback invocation"
+.IP "callback/watcher invocation" 4
+.IX Item "callback/watcher invocation"
The act of calling the callback associated with a watcher.
.IP "event" 4
.IX Item "event"
@@ -4634,7 +5268,7 @@ for reading on a file descriptor, time having passed or simply not having
any other events happening anymore.
.Sp
In libev, events are represented as single bits (such as \f(CW\*(C`EV_READ\*(C'\fR or
-\&\f(CW\*(C`EV_TIMEOUT\*(C'\fR).
+\&\f(CW\*(C`EV_TIMER\*(C'\fR).
.IP "event library" 4
.IX Item "event library"
A software package implementing an event model and loop.
@@ -4648,12 +5282,8 @@ The model used to describe how an event loop handles and processes
watchers and events.
.IP "pending" 4
.IX Item "pending"
-A watcher is pending as soon as the corresponding event has been detected,
-and stops being pending as soon as the watcher will be invoked or its
-pending status is explicitly cleared by the application.
-.Sp
-A watcher can be pending, but not active. Stopping a watcher also clears
-its pending status.
+A watcher is pending as soon as the corresponding event has been
+detected. See \*(L"\s-1WATCHER\s0 \s-1STATES\s0\*(R" for details.
.IP "real time" 4
.IX Item "real time"
The physical time that is observed. It is apparently strictly monotonic :)
@@ -4666,9 +5296,7 @@ clock.
.IX Item "watcher"
A data structure that describes interest in certain events. Watchers need
to be started (attached to an event loop) before they can receive events.
-.IP "watcher invocation" 4
-.IX Item "watcher invocation"
-The act of calling the callback associated with a watcher.
.SH "AUTHOR"
.IX Header "AUTHOR"
-Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael Magnusson.
+Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael
+Magnusson and Emanuele Giaquinta.
diff --git a/deps/libev/ev.c b/deps/libev/ev.c
index 168f93b7d1..1a8b3192f5 100644
--- a/deps/libev/ev.c
+++ b/deps/libev/ev.c
@@ -1,7 +1,7 @@
/*
* libev event processing core, watcher management
*
- * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de>
+ * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
@@ -37,10 +37,6 @@
* either the BSD or the GPL.
*/
-#ifdef __cplusplus
-extern "C" {
-#endif
-
/* this big block deduces configuration from config.h */
#ifndef EV_STANDALONE
# ifdef EV_CONFIG_H
@@ -184,6 +180,8 @@ extern "C" {
# include "ev.h"
#endif
+EV_CPP(extern "C" {)
+
#ifndef _WIN32
# include <sys/time.h>
# include <sys/wait.h>
@@ -386,7 +384,6 @@ extern "C" {
#endif
#if EV_USE_INOTIFY
-# include <sys/utsname.h>
# include <sys/statfs.h>
# include <sys/inotify.h>
/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
@@ -413,13 +410,7 @@ extern "C" {
# define EFD_CLOEXEC 02000000
# endif
# endif
-# ifdef __cplusplus
-extern "C" {
-# endif
-int (eventfd) (unsigned int initval, int flags);
-# ifdef __cplusplus
-}
-# endif
+EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
#endif
#if EV_USE_SIGNALFD
@@ -435,19 +426,13 @@ int (eventfd) (unsigned int initval, int flags);
# define SFD_CLOEXEC 02000000
# endif
# endif
-# ifdef __cplusplus
-extern "C" {
-# endif
-int signalfd (int fd, const sigset_t *mask, int flags);
+EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags);
struct signalfd_siginfo
{
uint32_t ssi_signo;
char pad[128 - sizeof (uint32_t)];
};
-# ifdef __cplusplus
-}
-# endif
#endif
/**/
@@ -539,6 +524,48 @@ static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work?
/*****************************************************************************/
+#ifdef __linux
+# include <sys/utsname.h>
+#endif
+
+static unsigned int noinline
+ev_linux_version (void)
+{
+#ifdef __linux
+ unsigned int v = 0;
+ struct utsname buf;
+ int i;
+ char *p = buf.release;
+
+ if (uname (&buf))
+ return 0;
+
+ for (i = 3+1; --i; )
+ {
+ unsigned int c = 0;
+
+ for (;;)
+ {
+ if (*p >= '0' && *p <= '9')
+ c = c * 10 + *p++ - '0';
+ else
+ {
+ p += *p == '.';
+ break;
+ }
+ }
+
+ v = (v << 8) | c;
+ }
+
+ return v;
+#else
+ return 0;
+#endif
+}
+
+/*****************************************************************************/
+
#if EV_AVOID_STDIO
static void noinline
ev_printerr (const char *msg)
@@ -566,11 +593,9 @@ ev_syserr (const char *msg)
else
{
#if EV_AVOID_STDIO
- const char *err = strerror (errno);
-
ev_printerr (msg);
ev_printerr (": ");
- ev_printerr (err);
+ ev_printerr (strerror (errno));
ev_printerr ("\n");
#else
perror (msg);
@@ -614,9 +639,9 @@ ev_realloc (void *ptr, long size)
if (!ptr && size)
{
#if EV_AVOID_STDIO
- ev_printerr ("libev: memory allocation failed, aborting.\n");
+ ev_printerr ("(libev) memory allocation failed, aborting.\n");
#else
- fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
+ fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size);
#endif
abort ();
}
@@ -643,9 +668,12 @@ typedef struct
#if EV_USE_EPOLL
unsigned int egen; /* generation counter to counter epoll bugs */
#endif
-#if EV_SELECT_IS_WINSOCKET
+#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
SOCKET handle;
#endif
+#if EV_USE_IOCP
+ OVERLAPPED or, ow;
+#endif
} ANFD;
/* stores the pending event set for a given watcher */
@@ -950,12 +978,13 @@ fd_reify (EV_P)
anfd->reify = 0;
-#if EV_SELECT_IS_WINSOCKET
+#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
if (o_reify & EV__IOFDSET)
{
unsigned long arg;
anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
+ printf ("oi %d %x\n", fd, anfd->handle);//D
}
#endif
@@ -1289,11 +1318,7 @@ evpipe_write (EV_P_ EV_ATOMIC_T *flag)
/* 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. */
-#ifdef __MINGW32__
- send(EV_FD_TO_WIN32_HANDLE(evpipe [1]), &dummy, 1, 0);
-#else
write (evpipe [1], &dummy, 1);
-#endif
errno = old_errno;
}
@@ -1317,11 +1342,7 @@ pipecb (EV_P_ ev_io *iow, int revents)
{
char dummy;
/* see discussion in evpipe_write when you think this read should be recv in win32 */
-#ifdef __MINGW32__
- recv(EV_FD_TO_WIN32_HANDLE(evpipe [0]), &dummy, 1, 0);
-#else
read (evpipe [0], &dummy, 1);
-#endif
}
if (sig_pending)
@@ -1350,19 +1371,28 @@ pipecb (EV_P_ ev_io *iow, int revents)
/*****************************************************************************/
-static void
-ev_sighandler (int signum)
+void
+ev_feed_signal (int signum)
{
#if EV_MULTIPLICITY
EV_P = signals [signum - 1].loop;
+
+ if (!EV_A)
+ return;
#endif
+ signals [signum - 1].pending = 1;
+ evpipe_write (EV_A_ &sig_pending);
+}
+
+static void
+ev_sighandler (int signum)
+{
#ifdef _WIN32
signal (signum, ev_sighandler);
#endif
- signals [signum - 1].pending = 1;
- evpipe_write (EV_A_ &sig_pending);
+ ev_feed_signal (signum);
}
void noinline
@@ -1472,6 +1502,9 @@ childcb (EV_P_ ev_signal *sw, int revents)
/*****************************************************************************/
+#if EV_USE_IOCP
+# include "ev_iocp.c"
+#endif
#if EV_USE_PORT
# include "ev_port.c"
#endif
@@ -1554,8 +1587,8 @@ ev_embeddable_backends (void)
int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
/* epoll embeddability broken on all linux versions up to at least 2.6.23 */
- /* please fix it and tell me how to detect the fix */
- flags &= ~EVBACKEND_EPOLL;
+ if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
+ flags &= ~EVBACKEND_EPOLL;
return flags;
}
@@ -1621,6 +1654,8 @@ loop_init (EV_P_ unsigned int flags)
{
if (!backend)
{
+ origflags = flags;
+
#if EV_USE_REALTIME
if (!have_realtime)
{
@@ -1675,9 +1710,12 @@ loop_init (EV_P_ unsigned int flags)
sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
#endif
- if (!(flags & 0x0000ffffU))
+ if (!(flags & EVBACKEND_MASK))
flags |= ev_recommended_backends ();
+#if EV_USE_IOCP
+ if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags);
+#endif
#if EV_USE_PORT
if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
#endif
@@ -1704,11 +1742,34 @@ loop_init (EV_P_ unsigned int flags)
}
/* free up a loop structure */
-static void noinline
-loop_destroy (EV_P)
+void
+ev_loop_destroy (EV_P)
{
int i;
+#if EV_MULTIPLICITY
+ /* mimic free (0) */
+ if (!EV_A)
+ return;
+#endif
+
+#if EV_CLEANUP_ENABLE
+ /* queue cleanup watchers (and execute them) */
+ if (expect_false (cleanupcnt))
+ {
+ queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP);
+ EV_INVOKE_PENDING;
+ }
+#endif
+
+#if EV_CHILD_ENABLE
+ if (ev_is_active (&childev))
+ {
+ ev_ref (EV_A); /* child watcher */
+ ev_signal_stop (EV_A_ &childev);
+ }
+#endif
+
if (ev_is_active (&pipe_w))
{
/*ev_ref (EV_A);*/
@@ -1739,6 +1800,9 @@ loop_destroy (EV_P)
if (backend_fd >= 0)
close (backend_fd);
+#if EV_USE_IOCP
+ if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A);
+#endif
#if EV_USE_PORT
if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
#endif
@@ -1775,6 +1839,9 @@ loop_destroy (EV_P)
#if EV_FORK_ENABLE
array_free (fork, EMPTY);
#endif
+#if EV_CLEANUP_ENABLE
+ array_free (cleanup, EMPTY);
+#endif
array_free (prepare, EMPTY);
array_free (check, EMPTY);
#if EV_ASYNC_ENABLE
@@ -1782,6 +1849,15 @@ loop_destroy (EV_P)
#endif
backend = 0;
+
+#if EV_MULTIPLICITY
+ if (ev_is_default_loop (EV_A))
+#endif
+ ev_default_loop_ptr = 0;
+#if EV_MULTIPLICITY
+ else
+ ev_free (EV_A);
+#endif
}
#if EV_USE_INOTIFY
@@ -1850,21 +1926,10 @@ ev_loop_new (unsigned int flags)
if (ev_backend (EV_A))
return EV_A;
+ ev_free (EV_A);
return 0;
}
-void
-ev_loop_destroy (EV_P)
-{
- loop_destroy (EV_A);
- ev_free (loop);
-}
-
-void
-ev_loop_fork (EV_P)
-{
- postfork = 1; /* must be in line with ev_default_fork */
-}
#endif /* multiplicity */
#if EV_VERIFY
@@ -1949,6 +2014,11 @@ ev_verify (EV_P)
array_verify (EV_A_ (W *)forks, forkcnt);
#endif
+#if EV_CLEANUP_ENABLE
+ assert (cleanupmax >= cleanupcnt);
+ array_verify (EV_A_ (W *)cleanups, cleanupcnt);
+#endif
+
#if EV_ASYNC_ENABLE
assert (asyncmax >= asynccnt);
array_verify (EV_A_ (W *)asyncs, asynccnt);
@@ -1976,11 +2046,10 @@ ev_verify (EV_P)
#if EV_MULTIPLICITY
struct ev_loop *
-ev_default_loop_init (unsigned int flags)
#else
int
-ev_default_loop (unsigned int flags)
#endif
+ev_default_loop (unsigned int flags)
{
if (!ev_default_loop_ptr)
{
@@ -2009,30 +2078,9 @@ ev_default_loop (unsigned int flags)
}
void
-ev_default_destroy (void)
-{
-#if EV_MULTIPLICITY
- EV_P = ev_default_loop_ptr;
-#endif
-
- ev_default_loop_ptr = 0;
-
-#if EV_CHILD_ENABLE
- ev_ref (EV_A); /* child watcher */
- ev_signal_stop (EV_A_ &childev);
-#endif
-
- loop_destroy (EV_A);
-}
-
-void
-ev_default_fork (void)
+ev_loop_fork (EV_P)
{
-#if EV_MULTIPLICITY
- EV_P = ev_default_loop_ptr;
-#endif
-
- postfork = 1; /* must be in line with ev_loop_fork */
+ postfork = 1; /* must be in line with ev_default_fork */
}
/*****************************************************************************/
@@ -2065,9 +2113,6 @@ ev_invoke_pending (EV_P)
{
ANPENDING *p = pendings [pri] + --pendingcnt [pri];
- /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/
- /* ^ this is no longer true, as pending_w could be here */
-
p->w->pending = 0;
EV_CB_INVOKE (p->w, p->events);
EV_FREQUENT_CHECK;
@@ -2844,9 +2889,12 @@ ev_signal_start (EV_P_ ev_signal *w)
sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
sigaction (w->signum, &sa, 0);
- sigemptyset (&sa.sa_mask);
- sigaddset (&sa.sa_mask, w->signum);
- sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0);
+ if (origflags & EVFLAG_NOSIGMASK)
+ {
+ sigemptyset (&sa.sa_mask);
+ sigaddset (&sa.sa_mask, w->signum);
+ sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0);
+ }
#endif
}
@@ -3074,38 +3122,6 @@ infy_cb (EV_P_ ev_io *w, int revents)
}
}
-inline_size unsigned int
-ev_linux_version (void)
-{
- struct utsname buf;
- unsigned int v;
- int i;
- char *p = buf.release;
-
- if (uname (&buf))
- return 0;
-
- for (i = 3+1; --i; )
- {
- unsigned int c = 0;
-
- for (;;)
- {
- if (*p >= '0' && *p <= '9')
- c = c * 10 + *p++ - '0';
- else
- {
- p += *p == '.';
- break;
- }
- }
-
- v = (v << 8) | c;
- }
-
- return v;
-}
-
inline_size void
ev_check_2625 (EV_P)
{
@@ -3580,6 +3596,47 @@ ev_fork_stop (EV_P_ ev_fork *w)
}
#endif
+#if EV_CLEANUP_ENABLE
+void
+ev_cleanup_start (EV_P_ ev_cleanup *w)
+{
+ if (expect_false (ev_is_active (w)))
+ return;
+
+ EV_FREQUENT_CHECK;
+
+ ev_start (EV_A_ (W)w, ++cleanupcnt);
+ array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2);
+ cleanups [cleanupcnt - 1] = w;
+
+ /* cleanup watchers should never keep a refcount on the loop */
+ ev_unref (EV_A);
+ EV_FREQUENT_CHECK;
+}
+
+void
+ev_cleanup_stop (EV_P_ ev_cleanup *w)
+{
+ clear_pending (EV_A_ (W)w);
+ if (expect_false (!ev_is_active (w)))
+ return;
+
+ EV_FREQUENT_CHECK;
+ ev_ref (EV_A);
+
+ {
+ int active = ev_active (w);
+
+ cleanups [active - 1] = cleanups [--cleanupcnt];
+ ev_active (cleanups [active - 1]) = active;
+ }
+
+ ev_stop (EV_A_ (W)w);
+
+ EV_FREQUENT_CHECK;
+}
+#endif
+
#if EV_ASYNC_ENABLE
void
ev_async_start (EV_P_ ev_async *w)
@@ -3817,7 +3874,5 @@ ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
#include "ev_wrap.h"
#endif
-#ifdef __cplusplus
-}
-#endif
+EV_CPP(})
diff --git a/deps/libev/ev.h b/deps/libev/ev.h
index 96e735871a..9edf6564e9 100644
--- a/deps/libev/ev.h
+++ b/deps/libev/ev.h
@@ -1,7 +1,7 @@
/*
* libev native API header
*
- * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de>
+ * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
@@ -41,9 +41,13 @@
#define EV_H_
#ifdef __cplusplus
-extern "C" {
+# define EV_CPP(x) x
+#else
+# define EV_CPP(x)
#endif
+EV_CPP(extern "C" {)
+
/*****************************************************************************/
/* pre-4.0 compatibility */
@@ -99,6 +103,10 @@ extern "C" {
# define EV_FORK_ENABLE EV_FEATURE_WATCHERS
#endif
+#ifndef EV_CLEANUP_ENABLE
+# define EV_CLEANUP_ENABLE EV_FEATURE_WATCHERS
+#endif
+
#ifndef EV_SIGNAL_ENABLE
# define EV_SIGNAL_ENABLE EV_FEATURE_WATCHERS
#endif
@@ -150,14 +158,14 @@ typedef double ev_tstamp;
/* support multiple event loops? */
#if EV_MULTIPLICITY
struct ev_loop;
-# define EV_P struct ev_loop *loop
-# define EV_P_ EV_P,
-# define EV_A loop
-# define EV_A_ EV_A,
-# define EV_DEFAULT_UC ev_default_loop_uc ()
-# define EV_DEFAULT_UC_ EV_DEFAULT_UC,
-# define EV_DEFAULT ev_default_loop (0)
-# define EV_DEFAULT_ EV_DEFAULT,
+# define EV_P struct ev_loop *loop /* a loop as sole parameter in a declaration */
+# define EV_P_ EV_P, /* a loop as first of multiple parameters */
+# define EV_A loop /* a loop as sole argument to a function call */
+# define EV_A_ EV_A, /* a loop as first of multiple arguments */
+# define EV_DEFAULT_UC ev_default_loop_uc_ () /* the default loop, if initialised, as sole arg */
+# define EV_DEFAULT_UC_ EV_DEFAULT_UC, /* the default loop as first of multiple arguments */
+# define EV_DEFAULT ev_default_loop (0) /* the default loop as sole arg */
+# define EV_DEFAULT_ EV_DEFAULT, /* the default loop as first of multiple arguments */
#else
# define EV_P void
# define EV_P_
@@ -170,12 +178,14 @@ struct ev_loop;
# undef EV_EMBED_ENABLE
#endif
+/* EV_INLINE is used for functions in header files */
#if __STDC_VERSION__ >= 199901L || __GNUC__ >= 3
# define EV_INLINE static inline
#else
# define EV_INLINE static
#endif
+/* EV_PROTOTYPES can be sued to switch of prototype declarations */
#ifndef EV_PROTOTYPES
# define EV_PROTOTYPES 1
#endif
@@ -183,18 +193,20 @@ struct ev_loop;
/*****************************************************************************/
#define EV_VERSION_MAJOR 4
-#define EV_VERSION_MINOR 0
+#define EV_VERSION_MINOR 3
/* eventmask, revents, events... */
enum {
- EV_UNDEF = -1, /* guaranteed to be invalid */
+ EV_UNDEF = 0xFFFFFFFF, /* 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 */
+#if EV_COMPAT3
EV_TIMEOUT = EV_TIMER, /* pre 4.0 API compatibility */
+#endif
EV_PERIODIC = 0x00000200, /* periodic timer timed out */
EV_SIGNAL = 0x00000400, /* signal was received */
EV_CHILD = 0x00000800, /* child/pid had status change */
@@ -204,7 +216,8 @@ enum {
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_CLEANUP = 0x00040000, /* event loop resumed in child */
+ EV_ASYNC = 0x00080000, /* async intra-loop signal */
EV_CUSTOM = 0x01000000, /* for use by user code */
EV_ERROR = 0x80000000 /* sent when an error occurs */
};
@@ -383,12 +396,22 @@ typedef struct ev_check
#if EV_FORK_ENABLE
/* the callback gets invoked before check in the child process when a fork was detected */
+/* revent EV_FORK */
typedef struct ev_fork
{
EV_WATCHER (ev_fork)
} ev_fork;
#endif
+#if EV_CLEANUP_ENABLE
+/* is invoked just before the loop gets destroyed */
+/* revent EV_CLEANUP */
+typedef struct ev_cleanup
+{
+ EV_WATCHER (ev_cleanup)
+} ev_cleanup;
+#endif
+
#if EV_EMBED_ENABLE
/* used to embed an event loop inside another */
/* the callback gets invoked when the event loop has handled events, and can be 0 */
@@ -404,6 +427,9 @@ typedef struct ev_embed
ev_periodic periodic; /* unused */
ev_idle idle; /* unused */
ev_fork fork; /* private */
+#if EV_CLEANUP_ENABLE
+ ev_cleanup cleanup; /* unused */
+#endif
} ev_embed;
#endif
@@ -442,6 +468,9 @@ union ev_any_watcher
#if EV_FORK_ENABLE
struct ev_fork fork;
#endif
+#if EV_CLEANUP_ENABLE
+ struct ev_cleanup cleanup;
+#endif
#if EV_EMBED_ENABLE
struct ev_embed embed;
#endif
@@ -462,7 +491,8 @@ enum {
#if EV_COMPAT3
EVFLAG_NOSIGFD = 0, /* compatibility to pre-3.9 */
#endif
- EVFLAG_SIGNALFD = 0x00200000U /* attempt to use signalfd */
+ EVFLAG_SIGNALFD = 0x00200000U, /* attempt to use signalfd */
+ EVFLAG_NOSIGMASK = 0x00400000U /* avoid modifying the signal mask */
};
/* method bits to be ored together */
@@ -473,7 +503,8 @@ enum {
EVBACKEND_KQUEUE = 0x00000008U, /* bsd */
EVBACKEND_DEVPOLL = 0x00000010U, /* solaris 8 */ /* NYI */
EVBACKEND_PORT = 0x00000020U, /* solaris 10 */
- EVBACKEND_ALL = 0x0000003FU
+ EVBACKEND_ALL = 0x0000003FU, /* all known backends */
+ EVBACKEND_MASK = 0x0000FFFFU /* all future backends */
};
#if EV_PROTOTYPES
@@ -502,41 +533,33 @@ void ev_set_allocator (void *(*cb)(void *ptr, long size));
void ev_set_syserr_cb (void (*cb)(const char *msg));
#if EV_MULTIPLICITY
+
+/* the default loop is the only one that handles signals and child watchers */
+/* you can call this as often as you like */
+struct ev_loop *ev_default_loop (unsigned int flags EV_CPP (= 0));
+
EV_INLINE struct ev_loop *
-ev_default_loop_uc (void)
+ev_default_loop_uc_ (void)
{
extern struct ev_loop *ev_default_loop_ptr;
return ev_default_loop_ptr;
}
-/* the default loop is the only one that handles signals and child watchers */
-/* you can call this as often as you like */
-EV_INLINE struct ev_loop *
-ev_default_loop (unsigned int flags)
+EV_INLINE int
+ev_is_default_loop (EV_P)
{
- struct ev_loop *loop = ev_default_loop_uc ();
-
- if (!loop)
- {
- extern struct ev_loop *ev_default_loop_init (unsigned int flags);
-
- loop = ev_default_loop_init (flags);
- }
-
- return loop;
+ return EV_A == EV_DEFAULT_UC;
}
/* create and destroy alternative loops that don't handle signals */
-struct ev_loop *ev_loop_new (unsigned int flags);
-void ev_loop_destroy (EV_P);
-void ev_loop_fork (EV_P);
+struct ev_loop *ev_loop_new (unsigned int flags EV_CPP (= 0));
ev_tstamp ev_now (EV_P); /* time w.r.t. timers and the eventloop, updated after each poll */
#else
-int ev_default_loop (unsigned int flags); /* returns true when successful */
+int ev_default_loop (unsigned int flags EV_CPP (= 0)); /* returns true when successful */
EV_INLINE ev_tstamp
ev_now (void)
@@ -545,26 +568,24 @@ ev_now (void)
return ev_rt_now;
}
-#endif /* multiplicity */
+/* looks weird, but ev_is_default_loop (EV_A) still works if this exists */
EV_INLINE int
-ev_is_default_loop (EV_P)
+ev_is_default_loop (void)
{
-#if EV_MULTIPLICITY
- extern struct ev_loop *ev_default_loop_ptr;
-
- return !!(EV_A == ev_default_loop_ptr);
-#else
return 1;
-#endif
}
-void ev_default_destroy (void); /* destroy the default loop */
-/* this needs to be called after fork, to duplicate the default loop */
-/* if you create alternative loops you have to call ev_loop_fork on them */
+#endif /* multiplicity */
+
+/* destroy event loops, also works for the default loop */
+void ev_loop_destroy (EV_P);
+
+/* this needs to be called after fork, to duplicate the loop */
+/* when you want to re-use it in the child */
/* you can call it in either the parent or the child */
/* you can actually call it at any time, anywhere :) */
-void ev_default_fork (void);
+void ev_loop_fork (EV_P);
unsigned int ev_backend (EV_P); /* backend in use by loop */
@@ -593,8 +614,8 @@ enum {
};
#if EV_PROTOTYPES
-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 */
+void ev_run (EV_P_ int flags EV_CPP (= 0));
+void ev_break (EV_P_ int how EV_CPP (= EVBREAK_ONE)); /* break out of the loop */
/*
* ref/unref can be used to add or remove a refcount on the mainloop. every watcher
@@ -656,6 +677,7 @@ void ev_resume (EV_P);
#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_cleanup_set(ev) /* nop, yes, this is a serious in-joke */
#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)
@@ -669,6 +691,7 @@ void ev_resume (EV_P);
#define ev_check_init(ev,cb) do { ev_init ((ev), (cb)); ev_check_set ((ev)); } while (0)
#define ev_embed_init(ev,cb,other) do { ev_init ((ev), (cb)); ev_embed_set ((ev),(other)); } while (0)
#define ev_fork_init(ev,cb) do { ev_init ((ev), (cb)); ev_fork_set ((ev)); } while (0)
+#define ev_cleanup_init(ev,cb) do { ev_init ((ev), (cb)); ev_cleanup_set ((ev)); } while (0)
#define ev_async_init(ev,cb) do { ev_init ((ev), (cb)); ev_async_set ((ev)); } while (0)
#define ev_is_pending(ev) (0 + ((ev_watcher *)(void *)(ev))->pending) /* ro, true when watcher is waiting for callback invocation */
@@ -699,6 +722,7 @@ void ev_resume (EV_P);
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 (int signum);
void ev_feed_signal_event (EV_P_ int signum);
#endif
void ev_invoke (EV_P_ void *w, int revents);
@@ -758,6 +782,11 @@ void ev_fork_start (EV_P_ ev_fork *w);
void ev_fork_stop (EV_P_ ev_fork *w);
# endif
+# if EV_CLEANUP_ENABLE
+void ev_cleanup_start (EV_P_ ev_cleanup *w);
+void ev_cleanup_stop (EV_P_ ev_cleanup *w);
+# endif
+
# if EV_EMBED_ENABLE
/* only supported when loop to be embedded is in fact embeddable */
void ev_embed_start (EV_P_ ev_embed *w);
@@ -780,10 +809,12 @@ void ev_async_send (EV_P_ ev_async *w);
#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 ); }
+ EV_INLINE void ev_default_destroy (void) { ev_loop_destroy (EV_DEFAULT); }
+ EV_INLINE void ev_default_fork (void) { ev_loop_fork (EV_DEFAULT); }
#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); }
+ EV_INLINE unsigned int ev_loop_count (EV_P) { return ev_iteration (EV_A); }
+ EV_INLINE unsigned int ev_loop_depth (EV_P) { return ev_depth (EV_A); }
+ EV_INLINE void ev_loop_verify (EV_P) { ev_verify (EV_A); }
#endif
#endif
#else
@@ -792,9 +823,7 @@ void ev_async_send (EV_P_ ev_async *w);
#endif
-#ifdef __cplusplus
-}
-#endif
+EV_CPP(})
#endif
diff --git a/deps/libev/ev.pod b/deps/libev/ev.pod
index ba192deaca..6cd777e658 100644
--- a/deps/libev/ev.pod
+++ b/deps/libev/ev.pod
@@ -45,7 +45,7 @@ libev - a high performance full-featured event loop written in C
main (void)
{
// use the default event loop unless you have special needs
- struct ev_loop *loop = ev_default_loop (0);
+ struct ev_loop *loop = EV_DEFAULT;
// initialise an io watcher, then start it
// this one will watch for stdin to become readable
@@ -80,6 +80,14 @@ with libev.
Familiarity with event based programming techniques in general is assumed
throughout this document.
+=head1 WHAT TO READ WHEN IN A HURRY
+
+This manual tries to be very detailed, but unfortunately, this also makes
+it very long. If you just want to know the basics of libev, I suggest
+reading L<ANATOMY OF A WATCHER>, then the L<EXAMPLE PROGRAM> above and
+look up the missing functions in L<GLOBAL FUNCTIONS> and the C<ev_io> and
+C<ev_timer> sections in L<WATCHER TYPES>.
+
=head1 ABOUT LIBEV
Libev is an event loop: you register interest in certain events (such as a
@@ -126,7 +134,7 @@ this argument.
=head2 TIME REPRESENTATION
Libev represents time as a single floating point number, representing
-the (fractional) number of seconds since the (POSIX) epoch (in practise
+the (fractional) number of seconds since the (POSIX) epoch (in practice
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
@@ -167,7 +175,8 @@ library in any way.
Returns the current time as libev would use it. Please note that the
C<ev_now> function is usually faster and also often returns the timestamp
-you actually want to know.
+you actually want to know. Also interesting is the combination of
+C<ev_update_now> and C<ev_now>.
=item ev_sleep (ev_tstamp interval)
@@ -194,7 +203,8 @@ compatible to older versions, so a larger minor version alone is usually
not a problem.
Example: Make sure we haven't accidentally been linked against the wrong
-version (note, however, that this will not detect ABI mismatches :).
+version (note, however, that this will not detect other ABI mismatches,
+such as LFS or reentrancy).
assert (("libev version mismatch",
ev_version_major () == EV_VERSION_MAJOR
@@ -215,24 +225,25 @@ a must have and can we have a torrent of it please!!!11
=item unsigned int ev_recommended_backends ()
-Return the set of all backends compiled into this binary of libev and also
-recommended for this platform. This set is often smaller than the one
-returned by C<ev_supported_backends>, as for example kqueue is broken on
-most BSDs and will not be auto-detected unless you explicitly request it
-(assuming you know what you are doing). This is the set of backends that
-libev will probe for if you specify no backends explicitly.
+Return the set of all backends compiled into this binary of libev and
+also recommended for this platform, meaning it will work for most file
+descriptor types. This set is often smaller than the one returned by
+C<ev_supported_backends>, as for example kqueue is broken on most BSDs
+and will not be auto-detected unless you explicitly request it (assuming
+you know what you are doing). This is the set of backends that libev will
+probe for if you specify no backends explicitly.
=item unsigned int ev_embeddable_backends ()
Returns the set of backends that are embeddable in other event loops. This
-is the theoretical, all-platform, value. To find which backends
-might be supported on the current system, you would need to look at
-C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for
-recommended ones.
+value is platform-specific but can include backends not available on the
+current system. To find which embeddable backends might be supported on
+the current system, you would need to look at C<ev_embeddable_backends ()
+& ev_supported_backends ()>, likewise for recommended ones.
See the description of C<ev_embed> watchers for more info.
-=item ev_set_allocator (void *(*cb)(void *ptr, long size)) [NOT REENTRANT]
+=item ev_set_allocator (void *(*cb)(void *ptr, long size))
Sets the allocation function to use (the prototype is similar - the
semantics are identical to the C<realloc> C89/SuS/POSIX function). It is
@@ -268,7 +279,7 @@ retries (example requires a standards-compliant C<realloc>).
...
ev_set_allocator (persistent_realloc);
-=item ev_set_syserr_cb (void (*cb)(const char *msg)); [NOT REENTRANT]
+=item ev_set_syserr_cb (void (*cb)(const char *msg))
Set the callback function to call on a retryable system call error (such
as failed select, poll, epoll_wait). The message is a printable string
@@ -290,40 +301,78 @@ Example: This is basically the same thing that libev does internally, too.
...
ev_set_syserr_cb (fatal_error);
+=item ev_feed_signal (int signum)
+
+This function can be used to "simulate" a signal receive. It is completely
+safe to call this function at any time, from any context, including signal
+handlers or random threads.
+
+Its main use is to customise signal handling in your process, especially
+in the presence of threads. For example, you could block signals
+by default in all threads (and specifying C<EVFLAG_NOSIGMASK> when
+creating any loops), and in one thread, use C<sigwait> or any other
+mechanism to wait for signals, then "deliver" them to libev by calling
+C<ev_feed_signal>.
+
=back
-=head1 FUNCTIONS CONTROLLING THE EVENT LOOP
+=head1 FUNCTIONS CONTROLLING EVENT LOOPS
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 event loops
-which do not.
+supports child process events, and dynamically created event loops which
+do not.
=over 4
=item struct ev_loop *ev_default_loop (unsigned int flags)
-This will initialise the default event loop if it hasn't been initialised
-yet and return it. If the default loop could not be initialised, returns
-false. If it already was initialised it simply returns it (and ignores the
-flags. If that is troubling you, check C<ev_backend ()> afterwards).
+This returns the "default" event loop object, which is what you should
+normally use when you just need "the event loop". Event loop objects and
+the C<flags> parameter are described in more detail in the entry for
+C<ev_loop_new>.
+
+If the default loop is already initialised then this function simply
+returns it (and ignores the flags. If that is troubling you, check
+C<ev_backend ()> afterwards). Otherwise it will create it with the given
+flags, which should almost always be C<0>, unless the caller is also the
+one calling C<ev_run> or otherwise qualifies as "the main program".
If you don't know what event loop to use, use the one returned from this
-function.
+function (or via the C<EV_DEFAULT> macro).
Note that this function is I<not> thread-safe, so if you want to use it
-from multiple threads, you have to lock (note also that this is unlikely,
-as loops cannot be shared easily between threads anyway).
+from multiple threads, you have to employ some kind of mutex (note also
+that this case is unlikely, as loops cannot be shared easily between
+threads anyway).
+
+The default loop is the only loop that can handle C<ev_child> watchers,
+and to do this, it always registers a handler for C<SIGCHLD>. If this is
+a problem for your application you can either create a dynamic loop with
+C<ev_loop_new> which doesn't do that, or you can simply overwrite the
+C<SIGCHLD> signal handler I<after> calling C<ev_default_init>.
+
+Example: This is the most typical usage.
+
+ if (!ev_default_loop (0))
+ fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?");
+
+Example: Restrict libev to the select and poll backends, and do not allow
+environment settings to be taken into account:
+
+ ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV);
+
+=item struct ev_loop *ev_loop_new (unsigned int flags)
+
+This will create and initialise a new event loop object. If the loop
+could not be initialised, returns false.
-The default loop is the only loop that can handle C<ev_signal> and
-C<ev_child> watchers, and to do this, it always registers a handler
-for C<SIGCHLD>. If this is a problem for your application you can either
-create a dynamic loop with C<ev_loop_new> that doesn't do that, or you
-can simply overwrite the C<SIGCHLD> signal handler I<after> calling
-C<ev_default_init>.
+This function 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.
The flags argument can be used to specify special behaviour or specific
backends to use, and is usually specified as C<0> (or C<EVFLAG_AUTO>).
@@ -368,14 +417,14 @@ environment variable.
=item C<EVFLAG_NOINOTIFY>
When this flag is specified, then libev will not attempt to use the
-I<inotify> API for it's C<ev_stat> watchers. Apart from debugging and
+I<inotify> API for its C<ev_stat> watchers. Apart from debugging and
testing, this flag can be useful to conserve inotify file descriptors, as
otherwise each loop using C<ev_stat> watchers consumes one inotify handle.
=item C<EVFLAG_SIGNALFD>
When this flag is specified, then libev will attempt to use the
-I<signalfd> API for it's C<ev_signal> (and C<ev_child>) watchers. This API
+I<signalfd> API for its C<ev_signal> (and C<ev_child>) watchers. This API
delivers signals synchronously, which makes it both faster and might make
it possible to get the queued signal data. It can also simplify signal
handling with threads, as long as you properly block signals in your
@@ -385,6 +434,18 @@ Signalfd will not be used by default as this changes your signal mask, and
there are a lot of shoddy libraries and programs (glib's threadpool for
example) that can't properly initialise their signal masks.
+=item C<EVFLAG_NOSIGMASK>
+
+When this flag is specified, then libev will avoid to modify the signal
+mask. Specifically, this means you ahve to make sure signals are unblocked
+when you want to receive them.
+
+This behaviour is useful when you want to do your own signal handling, or
+want to handle signals only in specific threads and want to avoid libev
+unblocking the signals.
+
+This flag's behaviour will become the default in future versions of libev.
+
=item C<EVBACKEND_SELECT> (value 1, portable select backend)
This is your standard select(2) backend. Not I<completely> standard, as
@@ -429,11 +490,13 @@ epoll scales either O(1) or O(active_fds).
The epoll mechanism deserves honorable mention as the most misdesigned
of the more advanced event mechanisms: mere annoyances include silently
dropping file descriptors, requiring a system call per change per file
-descriptor (and unnecessary guessing of parameters), problems with dup and
-so on. The biggest issue is fork races, however - if a program forks then
-I<both> parent and child process have to recreate the epoll set, which can
-take considerable time (one syscall per file descriptor) and is of course
-hard to detect.
+descriptor (and unnecessary guessing of parameters), problems with dup,
+returning before the timeout value, resulting in additional iterations
+(and only giving 5ms accuracy while select on the same platform gives
+0.1ms) and so on. The biggest issue is fork races, however - if a program
+forks then I<both> parent and child process have to recreate the epoll
+set, which can take considerable time (one syscall per file descriptor)
+and is of course hard to detect.
Epoll is also notoriously buggy - embedding epoll fds I<should> work, but
of course I<doesn't>, and epoll just loves to report events for totally
@@ -445,6 +508,10 @@ 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...).
+Epoll is truly the train wreck analog among event poll mechanisms,
+a frankenpoll, cobbled together in a hurry, no thought to design or
+interaction with others.
+
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
incident (because the same I<file descriptor> could point to a different
@@ -519,19 +586,25 @@ immensely.
This uses the Solaris 10 event port mechanism. As with everything on Solaris,
it's really slow, but it still scales very well (O(active_fds)).
-Please note that Solaris event ports can deliver a lot of spurious
-notifications, so you need to use non-blocking I/O or other means to avoid
-blocking when no data (or space) is available.
-
While this backend scales well, it requires one system call per active
file descriptor per loop iteration. For small and medium numbers of file
descriptors a "slow" C<EVBACKEND_SELECT> or C<EVBACKEND_POLL> backend
might perform better.
-On the positive side, with the exception of the spurious readiness
-notifications, this backend actually performed fully to specification
-in all tests and is fully embeddable, which is a rare feat among the
-OS-specific backends (I vastly prefer correctness over speed hacks).
+On the positive side, this backend actually performed fully to
+specification in all tests and is fully embeddable, which is a rare feat
+among the OS-specific backends (I vastly prefer correctness over speed
+hacks).
+
+On the negative side, the interface is I<bizarre> - so bizarre that
+even sun itself gets it wrong in their code examples: The event polling
+function sometimes returning events to the caller even though an error
+occurred, but with no indication whether it has done so or not (yes, it's
+even documented that way) - deadly for edge-triggered interfaces where
+you absolutely have to know whether an event occurred or not because you
+have to re-arm the watcher.
+
+Fortunately libev seems to be able to work around these idiocies.
This backend maps C<EV_READ> and C<EV_WRITE> in the same way as
C<EVBACKEND_POLL>.
@@ -542,7 +615,15 @@ Try all backends (even potentially broken ones that wouldn't be tried
with C<EVFLAG_AUTO>). Since this is a mask, you can do stuff such as
C<EVBACKEND_ALL & ~EVBACKEND_KQUEUE>.
-It is definitely not recommended to use this flag.
+It is definitely not recommended to use this flag, use whatever
+C<ev_recommended_backends ()> returns, or simply do not specify a backend
+at all.
+
+=item C<EVBACKEND_MASK>
+
+Not a backend at all, but a mask to select all backend bits from a
+C<flags> value, in case you want to mask out any backends from a flags
+value (e.g. when modifying the C<LIBEV_FLAGS> environment variable).
=back
@@ -551,69 +632,47 @@ then only these backends will be tried (in the reverse order as listed
here). If none are specified, all backends in C<ev_recommended_backends
()> will be tried.
-Example: This is the most typical usage.
-
- if (!ev_default_loop (0))
- fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?");
-
-Example: Restrict libev to the select and poll backends, and do not allow
-environment settings to be taken into account:
-
- ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV);
-
-Example: Use whatever libev has to offer, but make sure that kqueue is
-used if available (warning, breaks stuff, best use only with your own
-private event loop and only if you know the OS supports your types of
-fds):
-
- ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE);
-
-=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.
-
-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.
-
Example: Try to create a event loop that uses epoll and nothing else.
struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV);
if (!epoller)
fatal ("no epoll found here, maybe it hides under your chair");
-=item ev_default_destroy ()
+Example: Use whatever libev has to offer, but make sure that kqueue is
+used if available.
+
+ struct ev_loop *loop = ev_loop_new (ev_recommended_backends () | EVBACKEND_KQUEUE);
-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).
+=item ev_loop_destroy (loop)
+
+Destroys an event loop object (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
as signal and child watchers) would need to be stopped manually.
-In general it is not advisable to call this function except in the
-rare occasion where you really need to free e.g. the signal handling
-pipe fds. If you need dynamically allocated loops it is better to use
-C<ev_loop_new> and C<ev_loop_destroy>.
-
-=item ev_loop_destroy (loop)
+This function is normally used on loop objects allocated by
+C<ev_loop_new>, but it can also be used on the default loop returned by
+C<ev_default_loop>, in which case it is not thread-safe.
-Like C<ev_default_destroy>, but destroys an event loop created by an
-earlier call to C<ev_loop_new>.
+Note that it is not advisable to call this function on the default loop
+except in the rare occasion where you really need to free its resources.
+If you need dynamically allocated loops it is better to use C<ev_loop_new>
+and C<ev_loop_destroy>.
-=item ev_default_fork ()
+=item ev_loop_fork (loop)
-This function sets a flag that causes subsequent C<ev_run> iterations
-to reinitialise the kernel state for backends that have one. Despite the
+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_run> iteration.
+the child process. You I<must> call it (or use C<EVFLAG_FORKCHECK>) in the
+child before resuming or calling C<ev_run>.
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
@@ -628,17 +687,19 @@ 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
-quite nicely into a call to C<pthread_atfork>:
+it just in case after a fork.
- pthread_atfork (0, 0, ev_default_fork);
+Example: Automate calling C<ev_loop_fork> on the default loop when
+using pthreads.
-=item ev_loop_fork (loop)
+ static void
+ post_fork_child (void)
+ {
+ ev_loop_fork (EV_DEFAULT);
+ }
-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 keep track of
-them is entirely your own problem.
+ ...
+ pthread_atfork (0, 0, post_fork_child);
=item int ev_is_default_loop (loop)
@@ -659,15 +720,16 @@ prepare and check phases.
=item unsigned int ev_depth (loop)
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.
+times C<ev_run> was exited normally, in other words, the recursion depth.
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_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.
+Leaving C<ev_run> abnormally (setjmp/longjmp, cancelling the thread,
+throwing an exception etc.), doesn't count as "exit" - consider this
+as a hint to avoid such ungentleman-like behaviour unless it's really
+convenient, in which case it is fully supported.
=item unsigned int ev_backend (loop)
@@ -739,6 +801,11 @@ 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.
+This function is also I<mostly> exception-safe - you can break out of
+a C<ev_run> call by calling C<longjmp> in a callback, throwing a C++
+exception and so on. This does not decrement the C<ev_depth> value, nor
+will it clear any outstanding C<EVBREAK_ONE> breaks.
+
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
@@ -809,9 +876,10 @@ has processed all outstanding events). The C<how> argument must be either
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_run> again.
+This "break state" will be cleared on the next call to C<ev_run>.
-It is safe to call C<ev_break> from outside any C<ev_run> calls. ##TODO##
+It is safe to call C<ev_break> from outside any C<ev_run> calls, too, in
+which case it will have no effect.
=item ev_ref (loop)
@@ -842,7 +910,7 @@ running when nothing else is active.
ev_signal exitsig;
ev_signal_init (&exitsig, sig_cb, SIGINT);
ev_signal_start (loop, &exitsig);
- evf_unref (loop);
+ ev_unref (loop);
Example: For some weird reason, unregister the above signal handler again.
@@ -910,7 +978,11 @@ more often than 100 times per second:
This call will simply invoke all pending watchers while resetting their
pending state. Normally, C<ev_run> does this automatically when required,
-but when overriding the invoke callback this call comes handy.
+but when overriding the invoke callback this call comes handy. This
+function can be invoked from a watcher - this can be useful for example
+when you want to do some lengthy calculation and want to pass further
+event handling to another thread (you still have to make sure only one
+thread executes within C<ev_invoke_pending> or C<ev_run> of course).
=item int ev_pending_count (loop)
@@ -960,11 +1032,11 @@ document.
=item ev_set_userdata (loop, void *data)
-=item ev_userdata (loop)
+=item void *ev_userdata (loop)
Set and retrieve a single C<void *> associated with a loop. When
C<ev_set_userdata> has never been called, then C<ev_userdata> returns
-C<0.>
+C<0>.
These two functions can be used to associate arbitrary data with a loop,
and are intended solely for the C<invoke_pending_cb>, C<release> and
@@ -1102,6 +1174,10 @@ The embedded event loop specified in the C<ev_embed> watcher needs attention.
The event loop has been resumed in the child process after fork (see
C<ev_fork>).
+=item C<EV_CLEANUP>
+
+The event loop is about to be destroyed (see C<ev_cleanup>).
+
=item C<EV_ASYNC>
The given async watcher has been asynchronously notified (see C<ev_async>).
@@ -1132,65 +1208,6 @@ thing, so beware.
=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
@@ -1342,71 +1359,67 @@ functions that do not need a watcher.
=back
+See also the L<ASSOCIATING CUSTOM DATA WITH A WATCHER> and L<BUILDING YOUR
+OWN COMPOSITE WATCHERS> idioms.
-=head2 ASSOCIATING CUSTOM DATA WITH A WATCHER
+=head2 WATCHER STATES
-Each watcher has, by default, a member C<void *data> that you can change
-and read at any time: libev will completely ignore it. This can be used
-to associate arbitrary data with your watcher. If you need more data and
-don't want to allocate memory and store a pointer to it in that data
-member, you can also "subclass" the watcher type and provide your own
-data:
+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".
- struct my_io
- {
- ev_io io;
- int otherfd;
- void *somedata;
- struct whatever *mostinteresting;
- };
+=over 4
- ...
- struct my_io w;
- ev_io_init (&w.io, my_cb, fd, EV_READ);
+=item initialiased
-And since your callback will be called with a pointer to the watcher, you
-can cast it back to your own type:
+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.
- static void my_cb (struct ev_loop *loop, ev_io *w_, int revents)
- {
- struct my_io *w = (struct my_io *)w_;
- ...
- }
+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.
-More interesting and less C-conformant ways of casting your callback type
-instead have been omitted.
+=item started/running/active
-Another common scenario is to use some data structure with multiple
-embedded watchers:
+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.
- struct my_biggy
- {
- int some_data;
- ev_timer t1;
- ev_timer t2;
- }
+=item pending
-In this case getting the pointer to C<my_biggy> is a bit more
-complicated: Either you store the address of your C<my_biggy> struct
-in the C<data> member of the watcher (for woozies), or you need to use
-some pointer arithmetic using C<offsetof> inside your watchers (for real
-programmers):
+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.
- #include <stddef.h>
+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.
- static void
- t1_cb (EV_P_ ev_timer *w, int revents)
- {
- struct my_biggy big = (struct my_biggy *)
- (((char *)w) - offsetof (struct my_biggy, t1));
- }
+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.
- static void
- t2_cb (EV_P_ ev_timer *w, int revents)
- {
- struct my_biggy big = (struct my_biggy *)
- (((char *)w) - offsetof (struct my_biggy, t2));
- }
+=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 WATCHER PRIORITY MODELS
@@ -1543,26 +1556,19 @@ fd as you want (as long as you don't confuse yourself). Setting all file
descriptors to non-blocking mode is also usually a good idea (but not
required if you know what you are doing).
-If you cannot use non-blocking mode, then force the use of a
-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 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
+receive "spurious" readiness notifications, that is, your callback might
be called with C<EV_READ> but a subsequent C<read>(2) will actually block
-because there is no data. Not only are some backends known to create a
-lot of those (for example Solaris ports), it is very easy to get into
-this situation even with a relatively standard program structure. Thus
-it is best to always use non-blocking I/O: An extra C<read>(2) returning
-C<EAGAIN> is far preferable to a program hanging until some data arrives.
+because there is no data. It is very easy to get into this situation even
+with a relatively standard program structure. Thus it is best to always
+use non-blocking I/O: An extra C<read>(2) returning C<EAGAIN> is far
+preferable to a program hanging until some data arrives.
If you cannot run the fd in non-blocking mode (for example you should
not play around with an Xlib connection), then you have to separately
re-test whether a file descriptor is really ready with a known-to-be good
-interface such as poll (fortunately in our Xlib example, Xlib already
-does this on its own, so its quite safe to use). Some people additionally
+interface such as poll (fortunately in the case of Xlib, it already does
+this on its own, so its quite safe to use). Some people additionally
use C<SIGALRM> and an interval timer, just to be sure you won't block
indefinitely.
@@ -1600,16 +1606,48 @@ There is no workaround possible except not registering events
for potentially C<dup ()>'ed file descriptors, or to resort to
C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>.
+=head3 The special problem of files
+
+Many people try to use C<select> (or libev) on file descriptors
+representing files, and expect it to become ready when their program
+doesn't block on disk accesses (which can take a long time on their own).
+
+However, this cannot ever work in the "expected" way - you get a readiness
+notification as soon as the kernel knows whether and how much data is
+there, and in the case of open files, that's always the case, so you
+always get a readiness notification instantly, and your read (or possibly
+write) will still block on the disk I/O.
+
+Another way to view it is that in the case of sockets, pipes, character
+devices and so on, there is another party (the sender) that delivers data
+on its own, but in the case of files, there is no such thing: the disk
+will not send data on its own, simply because it doesn't know what you
+wish to read - you would first have to request some data.
+
+Since files are typically not-so-well supported by advanced notification
+mechanism, libev tries hard to emulate POSIX behaviour with respect
+to files, even though you should not use it. The reason for this is
+convenience: sometimes you want to watch STDIN or STDOUT, which is
+usually a tty, often a pipe, but also sometimes files or special devices
+(for example, C<epoll> on Linux works with F</dev/random> but not with
+F</dev/urandom>), and even though the file might better be served with
+asynchronous I/O instead of with non-blocking I/O, it is still useful when
+it "just works" instead of freezing.
+
+So avoid file descriptors pointing to files when you know it (e.g. use
+libeio), but use them when it is convenient, e.g. for STDIN/STDOUT, or
+when you rarely read from a file instead of from a socket, and want to
+reuse the same code path.
+
=head3 The special problem of fork
Some backends (epoll, kqueue) do not support C<fork ()> at all or exhibit
useless behaviour. Libev fully supports fork, but needs to be told about
-it in the child.
+it in the child if you want to continue to use it in the child.
-To support fork in your programs, you either have to call
-C<ev_default_fork ()> or C<ev_loop_fork ()> after a fork in the child,
-enable C<EVFLAG_FORKCHECK>, or resort to C<EVBACKEND_SELECT> or
-C<EVBACKEND_POLL>.
+To support fork in your child processes, you have to call C<ev_loop_fork
+()> after a fork in the child, enable C<EVFLAG_FORKCHECK>, or resort to
+C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>.
=head3 The special problem of SIGPIPE
@@ -2235,7 +2273,7 @@ Example: Call a callback every hour, starting now:
Signal watchers will trigger an event when the process receives a specific
signal one or more times. Even though signals are very asynchronous, libev
-will try it's best to deliver signals synchronously, i.e. as part of the
+will try its best to deliver signals synchronously, i.e. as part of the
normal event processing, like any other event.
If you want signals to be delivered truly asynchronously, just use
@@ -2288,6 +2326,20 @@ So I can't stress this enough: I<If you do not reset your signal mask when
you expect it to be empty, you have a race condition in your code>. This
is not a libev-specific thing, this is true for most event libraries.
+=head3 The special problem of threads signal handling
+
+POSIX threads has problematic signal handling semantics, specifically,
+a lot of functionality (sigfd, sigwait etc.) only really works if all
+threads in a process block signals, which is hard to achieve.
+
+When you want to use sigwait (or mix libev signal handling with your own
+for the same signals), you can tackle this problem by globally blocking
+all signals before creating any threads (or creating them with a fully set
+sigprocmask) and also specifying the C<EVFLAG_NOSIGMASK> when creating
+loops. Then designate one thread as "signal receiver thread" which handles
+these signals. You can pass on any signals that libev might be interested
+in by calling C<ev_feed_signal>.
+
=head3 Watcher-Specific Functions and Data Members
=over 4
@@ -3070,24 +3122,65 @@ signal watchers).
When this is not possible, or you want to use the default loop for
other reasons, then in the process that wants to start "fresh", call
-C<ev_default_destroy ()> followed by C<ev_default_loop (...)>. Destroying
-the default loop will "orphan" (not stop) all registered watchers, so you
-have to be careful not to execute code that modifies those watchers. Note
-also that in that case, you have to re-register any signal watchers.
+C<ev_loop_destroy (EV_DEFAULT)> followed by C<ev_default_loop (...)>.
+Destroying the default loop will "orphan" (not stop) all registered
+watchers, so you have to be careful not to execute code that modifies
+those watchers. Note also that in that case, you have to re-register any
+signal watchers.
=head3 Watcher-Specific Functions and Data Members
=over 4
-=item ev_fork_init (ev_signal *, callback)
+=item ev_fork_init (ev_fork *, callback)
Initialises and configures the fork watcher - it has no parameters of any
kind. There is a C<ev_fork_set> macro, but using it is utterly pointless,
-believe me.
+really.
=back
+=head2 C<ev_cleanup> - even the best things end
+
+Cleanup watchers are called just before the event loop is being destroyed
+by a call to C<ev_loop_destroy>.
+
+While there is no guarantee that the event loop gets destroyed, cleanup
+watchers provide a convenient method to install cleanup hooks for your
+program, worker threads and so on - you just to make sure to destroy the
+loop when you want them to be invoked.
+
+Cleanup watchers are invoked in the same way as any other watcher. Unlike
+all other watchers, they do not keep a reference to the event loop (which
+makes a lot of sense if you think about it). Like all other watchers, you
+can call libev functions in the callback, except C<ev_cleanup_start>.
+
+=head3 Watcher-Specific Functions and Data Members
+
+=over 4
+
+=item ev_cleanup_init (ev_cleanup *, callback)
+
+Initialises and configures the cleanup watcher - it has no parameters of
+any kind. There is a C<ev_cleanup_set> macro, but using it is utterly
+pointless, I assure you.
+
+=back
+
+Example: Register an atexit handler to destroy the default loop, so any
+cleanup functions are called.
+
+ static void
+ program_exits (void)
+ {
+ ev_loop_destroy (EV_DEFAULT_UC);
+ }
+
+ ...
+ atexit (program_exits);
+
+
=head2 C<ev_async> - how to wake up an event loop
In general, you cannot use an C<ev_run> from multiple threads or other
@@ -3102,7 +3195,10 @@ 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
(i.e. the number of callback invocations may be less than the number of
-C<ev_async_sent> calls).
+C<ev_async_sent> calls). In fact, you could use signal watchers as a kind
+of "global async watchers" by using a watcher on an otherwise unused
+signal, and C<ev_feed_signal> to signal this watcher from another thread,
+even without knowing which loop owns the signal.
Unlike C<ev_signal> watchers, C<ev_async> works with any event loop, not
just the default loop.
@@ -3288,12 +3384,322 @@ the given events it.
=item ev_feed_signal_event (loop, int signum)
-Feed an event as if the given signal occurred (C<loop> must be the default
-loop!).
+Feed an event as if the given signal occurred. See also C<ev_feed_signal>,
+which is async-safe.
=back
+=head1 COMMON OR USEFUL IDIOMS (OR BOTH)
+
+This section explains some common idioms that are not immediately
+obvious. Note that examples are sprinkled over the whole manual, and this
+section only contains stuff that wouldn't fit anywhere else.
+
+=head2 ASSOCIATING CUSTOM DATA WITH A WATCHER
+
+Each watcher has, by default, a C<void *data> member that you can read
+or modify at any time: libev will completely ignore it. This can be used
+to associate arbitrary data with your watcher. If you need more data and
+don't want to allocate memory separately and store a pointer to it in that
+data member, you can also "subclass" the watcher type and provide your own
+data:
+
+ struct my_io
+ {
+ ev_io io;
+ int otherfd;
+ void *somedata;
+ struct whatever *mostinteresting;
+ };
+
+ ...
+ struct my_io w;
+ ev_io_init (&w.io, my_cb, fd, EV_READ);
+
+And since your callback will be called with a pointer to the watcher, you
+can cast it back to your own type:
+
+ static void my_cb (struct ev_loop *loop, ev_io *w_, int revents)
+ {
+ struct my_io *w = (struct my_io *)w_;
+ ...
+ }
+
+More interesting and less C-conformant ways of casting your callback
+function type instead have been omitted.
+
+=head2 BUILDING YOUR OWN COMPOSITE WATCHERS
+
+Another common scenario is to use some data structure with multiple
+embedded watchers, in effect creating your own watcher that combines
+multiple libev event sources into one "super-watcher":
+
+ struct my_biggy
+ {
+ int some_data;
+ ev_timer t1;
+ ev_timer t2;
+ }
+
+In this case getting the pointer to C<my_biggy> is a bit more
+complicated: Either you store the address of your C<my_biggy> struct in
+the C<data> member of the watcher (for woozies or C++ coders), or you need
+to use some pointer arithmetic using C<offsetof> inside your watchers (for
+real programmers):
+
+ #include <stddef.h>
+
+ static void
+ t1_cb (EV_P_ ev_timer *w, int revents)
+ {
+ struct my_biggy big = (struct my_biggy *)
+ (((char *)w) - offsetof (struct my_biggy, t1));
+ }
+
+ static void
+ t2_cb (EV_P_ ev_timer *w, int revents)
+ {
+ struct my_biggy big = (struct my_biggy *)
+ (((char *)w) - offsetof (struct my_biggy, t2));
+ }
+
+=head2 MODEL/NESTED EVENT LOOP INVOCATIONS AND EXIT CONDITIONS
+
+Often (especially in GUI toolkits) there are places where you have
+I<modal> interaction, which is most easily implemented by recursively
+invoking C<ev_run>.
+
+This brings the problem of exiting - a callback might want to finish the
+main C<ev_run> call, but not the nested one (e.g. user clicked "Quit", but
+a modal "Are you sure?" dialog is still waiting), or just the nested one
+and not the main one (e.g. user clocked "Ok" in a modal dialog), or some
+other combination: In these cases, C<ev_break> will not work alone.
+
+The solution is to maintain "break this loop" variable for each C<ev_run>
+invocation, and use a loop around C<ev_run> until the condition is
+triggered, using C<EVRUN_ONCE>:
+
+ // main loop
+ int exit_main_loop = 0;
+
+ while (!exit_main_loop)
+ ev_run (EV_DEFAULT_ EVRUN_ONCE);
+
+ // in a model watcher
+ int exit_nested_loop = 0;
+
+ while (!exit_nested_loop)
+ ev_run (EV_A_ EVRUN_ONCE);
+
+To exit from any of these loops, just set the corresponding exit variable:
+
+ // exit modal loop
+ exit_nested_loop = 1;
+
+ // exit main program, after modal loop is finished
+ exit_main_loop = 1;
+
+ // exit both
+ exit_main_loop = exit_nested_loop = 1;
+
+=head2 THREAD LOCKING EXAMPLE
+
+Here is a fictitious example of how to run an event loop in a different
+thread from where callbacks are being invoked and watchers are
+created/added/removed.
+
+For a real-world example, see the C<EV::Loop::Async> perl module,
+which uses exactly this technique (which is suited for many high-level
+languages).
+
+The example uses a pthread mutex to protect the loop data, a condition
+variable to wait for callback invocations, an async watcher to notify the
+event loop thread and an unspecified mechanism to wake up the main thread.
+
+First, you need to associate some data with the event loop:
+
+ typedef struct {
+ mutex_t lock; /* global loop lock */
+ ev_async async_w;
+ thread_t tid;
+ cond_t invoke_cv;
+ } userdata;
+
+ void prepare_loop (EV_P)
+ {
+ // for simplicity, we use a static userdata struct.
+ static userdata u;
+
+ ev_async_init (&u->async_w, async_cb);
+ ev_async_start (EV_A_ &u->async_w);
+
+ pthread_mutex_init (&u->lock, 0);
+ pthread_cond_init (&u->invoke_cv, 0);
+
+ // now associate this with the loop
+ ev_set_userdata (EV_A_ u);
+ ev_set_invoke_pending_cb (EV_A_ l_invoke);
+ ev_set_loop_release_cb (EV_A_ l_release, l_acquire);
+
+ // then create the thread running ev_loop
+ pthread_create (&u->tid, 0, l_run, EV_A);
+ }
+
+The callback for the C<ev_async> watcher does nothing: the watcher is used
+solely to wake up the event loop so it takes notice of any new watchers
+that might have been added:
+
+ static void
+ async_cb (EV_P_ ev_async *w, int revents)
+ {
+ // just used for the side effects
+ }
+
+The C<l_release> and C<l_acquire> callbacks simply unlock/lock the mutex
+protecting the loop data, respectively.
+
+ static void
+ l_release (EV_P)
+ {
+ userdata *u = ev_userdata (EV_A);
+ pthread_mutex_unlock (&u->lock);
+ }
+
+ static void
+ l_acquire (EV_P)
+ {
+ userdata *u = ev_userdata (EV_A);
+ pthread_mutex_lock (&u->lock);
+ }
+
+The event loop thread first acquires the mutex, and then jumps straight
+into C<ev_run>:
+
+ void *
+ l_run (void *thr_arg)
+ {
+ struct ev_loop *loop = (struct ev_loop *)thr_arg;
+
+ l_acquire (EV_A);
+ pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
+ ev_run (EV_A_ 0);
+ l_release (EV_A);
+
+ return 0;
+ }
+
+Instead of invoking all pending watchers, the C<l_invoke> callback will
+signal the main thread via some unspecified mechanism (signals? pipe
+writes? C<Async::Interrupt>?) and then waits until all pending watchers
+have been called (in a while loop because a) spurious wakeups are possible
+and b) skipping inter-thread-communication when there are no pending
+watchers is very beneficial):
+
+ static void
+ l_invoke (EV_P)
+ {
+ userdata *u = ev_userdata (EV_A);
+
+ while (ev_pending_count (EV_A))
+ {
+ wake_up_other_thread_in_some_magic_or_not_so_magic_way ();
+ pthread_cond_wait (&u->invoke_cv, &u->lock);
+ }
+ }
+
+Now, whenever the main thread gets told to invoke pending watchers, it
+will grab the lock, call C<ev_invoke_pending> and then signal the loop
+thread to continue:
+
+ static void
+ real_invoke_pending (EV_P)
+ {
+ userdata *u = ev_userdata (EV_A);
+
+ pthread_mutex_lock (&u->lock);
+ ev_invoke_pending (EV_A);
+ pthread_cond_signal (&u->invoke_cv);
+ pthread_mutex_unlock (&u->lock);
+ }
+
+Whenever you want to start/stop a watcher or do other modifications to an
+event loop, you will now have to lock:
+
+ ev_timer timeout_watcher;
+ userdata *u = ev_userdata (EV_A);
+
+ ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);
+
+ pthread_mutex_lock (&u->lock);
+ ev_timer_start (EV_A_ &timeout_watcher);
+ ev_async_send (EV_A_ &u->async_w);
+ pthread_mutex_unlock (&u->lock);
+
+Note that sending the C<ev_async> watcher is required because otherwise
+an event loop currently blocking in the kernel will have no knowledge
+about the newly added timer. By waking up the loop it will pick up any new
+watchers in the next event loop iteration.
+
+=head2 THREADS, COROUTINES, CONTINUATIONS, QUEUES... INSTEAD OF CALLBACKS
+
+While the overhead of a callback that e.g. schedules a thread is small, it
+is still an overhead. If you embed libev, and your main usage is with some
+kind of threads or coroutines, you might want to customise libev so that
+doesn't need callbacks anymore.
+
+Imagine you have coroutines that you can switch to using a function
+C<switch_to (coro)>, that libev runs in a coroutine called C<libev_coro>
+and that due to some magic, the currently active coroutine is stored in a
+global called C<current_coro>. Then you can build your own "wait for libev
+event" primitive by changing C<EV_CB_DECLARE> and C<EV_CB_INVOKE> (note
+the differing C<;> conventions):
+
+ #define EV_CB_DECLARE(type) struct my_coro *cb;
+ #define EV_CB_INVOKE(watcher) switch_to ((watcher)->cb)
+
+That means instead of having a C callback function, you store the
+coroutine to switch to in each watcher, and instead of having libev call
+your callback, you instead have it switch to that coroutine.
+
+A coroutine might now wait for an event with a function called
+C<wait_for_event>. (the watcher needs to be started, as always, but it doesn't
+matter when, or whether the watcher is active or not when this function is
+called):
+
+ void
+ wait_for_event (ev_watcher *w)
+ {
+ ev_cb_set (w) = current_coro;
+ switch_to (libev_coro);
+ }
+
+That basically suspends the coroutine inside C<wait_for_event> and
+continues the libev coroutine, which, when appropriate, switches back to
+this or any other coroutine. I am sure if you sue this your own :)
+
+You can do similar tricks if you have, say, threads with an event queue -
+instead of storing a coroutine, you store the queue object and instead of
+switching to a coroutine, you push the watcher onto the queue and notify
+any waiters.
+
+To embed libev, see L<EMBEDDING>, but in short, it's easiest to create two
+files, F<my_ev.h> and F<my_ev.c> that include the respective libev files:
+
+ // my_ev.h
+ #define EV_CB_DECLARE(type) struct my_coro *cb;
+ #define EV_CB_INVOKE(watcher) switch_to ((watcher)->cb);
+ #include "../libev/ev.h"
+
+ // my_ev.c
+ #define EV_H "my_ev.h"
+ #include "../libev/ev.c"
+
+And then use F<my_ev.h> when you would normally use F<ev.h>, and compile
+F<my_ev.c> into your project. When properly specifying include paths, you
+can even use F<ev.h> as header file name directly.
+
+
=head1 LIBEVENT EMULATION
Libev offers a compatibility emulation layer for libevent. It cannot
@@ -3301,6 +3707,11 @@ emulate the internals of libevent, so here are some usage hints:
=over 4
+=item * Only the libevent-1.4.1-beta API is being emulated.
+
+This was the newest libevent version available when libev was implemented,
+and is still mostly unchanged in 2010.
+
=item * Use it by including <event.h>, as usual.
=item * The following members are fully supported: ev_base, ev_callback,
@@ -3315,7 +3726,7 @@ will fail and all watchers will have the same priority, even though there
is an ev_pri field.
=item * In libevent, the last base created gets the signals, in libev, the
-first base created (== the default loop) gets the signals.
+base that registered the signal gets the signals.
=item * Other members are not supported.
@@ -3344,11 +3755,11 @@ classes add (compared to plain C-style watchers) is the event loop pointer
that the watcher is associated with (or no additional members at all if
you disable C<EV_MULTIPLICITY> when embedding libev).
-Currently, functions, and static and non-static member functions can be
-used as callbacks. Other types should be easy to add as long as they only
-need one additional pointer for context. If you need support for other
-types of functors please contact the author (preferably after implementing
-it).
+Currently, functions, static and non-static member functions and classes
+with C<operator ()> can be used as callbacks. Other types should be easy
+to add as long as they only need one additional pointer for context. If
+you need support for other types of functors please contact the author
+(preferably after implementing it).
Here is a list of things available in the C<ev> namespace:
@@ -4226,7 +4637,7 @@ And a F<ev_cpp.C> implementation file that contains libev proper and is compiled
#include "ev_cpp.h"
#include "ev.c"
-=head1 INTERACTION WITH OTHER PROGRAMS OR LIBRARIES
+=head1 INTERACTION WITH OTHER PROGRAMS, LIBRARIES OR THE ENVIRONMENT
=head2 THREADS AND COROUTINES
@@ -4287,143 +4698,7 @@ watcher callback into the event loop interested in the signal.
=back
-=head4 THREAD LOCKING EXAMPLE
-
-Here is a fictitious example of how to run an event loop in a different
-thread than where callbacks are being invoked and watchers are
-created/added/removed.
-
-For a real-world example, see the C<EV::Loop::Async> perl module,
-which uses exactly this technique (which is suited for many high-level
-languages).
-
-The example uses a pthread mutex to protect the loop data, a condition
-variable to wait for callback invocations, an async watcher to notify the
-event loop thread and an unspecified mechanism to wake up the main thread.
-
-First, you need to associate some data with the event loop:
-
- typedef struct {
- mutex_t lock; /* global loop lock */
- ev_async async_w;
- thread_t tid;
- cond_t invoke_cv;
- } userdata;
-
- void prepare_loop (EV_P)
- {
- // for simplicity, we use a static userdata struct.
- static userdata u;
-
- ev_async_init (&u->async_w, async_cb);
- ev_async_start (EV_A_ &u->async_w);
-
- pthread_mutex_init (&u->lock, 0);
- pthread_cond_init (&u->invoke_cv, 0);
-
- // now associate this with the loop
- ev_set_userdata (EV_A_ u);
- ev_set_invoke_pending_cb (EV_A_ l_invoke);
- ev_set_loop_release_cb (EV_A_ l_release, l_acquire);
-
- // then create the thread running ev_loop
- pthread_create (&u->tid, 0, l_run, EV_A);
- }
-
-The callback for the C<ev_async> watcher does nothing: the watcher is used
-solely to wake up the event loop so it takes notice of any new watchers
-that might have been added:
-
- static void
- async_cb (EV_P_ ev_async *w, int revents)
- {
- // just used for the side effects
- }
-
-The C<l_release> and C<l_acquire> callbacks simply unlock/lock the mutex
-protecting the loop data, respectively.
-
- static void
- l_release (EV_P)
- {
- userdata *u = ev_userdata (EV_A);
- pthread_mutex_unlock (&u->lock);
- }
-
- static void
- l_acquire (EV_P)
- {
- userdata *u = ev_userdata (EV_A);
- pthread_mutex_lock (&u->lock);
- }
-
-The event loop thread first acquires the mutex, and then jumps straight
-into C<ev_run>:
-
- void *
- l_run (void *thr_arg)
- {
- struct ev_loop *loop = (struct ev_loop *)thr_arg;
-
- l_acquire (EV_A);
- pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
- ev_run (EV_A_ 0);
- l_release (EV_A);
-
- return 0;
- }
-
-Instead of invoking all pending watchers, the C<l_invoke> callback will
-signal the main thread via some unspecified mechanism (signals? pipe
-writes? C<Async::Interrupt>?) and then waits until all pending watchers
-have been called (in a while loop because a) spurious wakeups are possible
-and b) skipping inter-thread-communication when there are no pending
-watchers is very beneficial):
-
- static void
- l_invoke (EV_P)
- {
- userdata *u = ev_userdata (EV_A);
-
- while (ev_pending_count (EV_A))
- {
- wake_up_other_thread_in_some_magic_or_not_so_magic_way ();
- pthread_cond_wait (&u->invoke_cv, &u->lock);
- }
- }
-
-Now, whenever the main thread gets told to invoke pending watchers, it
-will grab the lock, call C<ev_invoke_pending> and then signal the loop
-thread to continue:
-
- static void
- real_invoke_pending (EV_P)
- {
- userdata *u = ev_userdata (EV_A);
-
- pthread_mutex_lock (&u->lock);
- ev_invoke_pending (EV_A);
- pthread_cond_signal (&u->invoke_cv);
- pthread_mutex_unlock (&u->lock);
- }
-
-Whenever you want to start/stop a watcher or do other modifications to an
-event loop, you will now have to lock:
-
- ev_timer timeout_watcher;
- userdata *u = ev_userdata (EV_A);
-
- ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);
-
- pthread_mutex_lock (&u->lock);
- ev_timer_start (EV_A_ &timeout_watcher);
- ev_async_send (EV_A_ &u->async_w);
- pthread_mutex_unlock (&u->lock);
-
-Note that sending the C<ev_async> watcher is required because otherwise
-an event loop currently blocking in the kernel will have no knowledge
-about the newly added timer. By waking up the loop it will pick up any new
-watchers in the next event loop iteration.
+See also L<THREAD LOCKING EXAMPLE>.
=head3 COROUTINES
@@ -4529,9 +4804,10 @@ OpenGL drivers.
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.
+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 - embedding a socket-only kqueue loop into a select-based one is
+probably going to work well.
=head3 C<poll> is buggy
@@ -4560,19 +4836,21 @@ work on OS/X.
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.
+without C<-D_REENTRANT> in a threaded program, which, of course, isn't
+defined by default. A valid, if stupid, implementation choice.
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.
+The scalable event interface for Solaris is called "event
+ports". Unfortunately, this mechanism is very buggy in all major
+releases. 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 to apply, and afterwards, event ports actually work
+great.
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
@@ -4583,7 +4861,7 @@ C<select> backends.
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.
+with large bitsets on AIX, and AIX is dead anyway.
=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS
@@ -4699,6 +4977,11 @@ assumes that the same (machine) code can be used to call any watcher
callback: The watcher callbacks have different type signatures, but libev
calls them using an C<ev_watcher *> internally.
+=item pointer accesses must be thread-atomic
+
+Accessing a pointer value must be atomic, it must both be readable and
+writable in one piece - this is the case on all current architectures.
+
=item C<sig_atomic_t volatile> must be thread-atomic as well
The type C<sig_atomic_t volatile> (or whatever is defined as
@@ -4814,14 +5097,28 @@ involves iterating over all running async watchers or all signal numbers.
=head1 PORTING FROM LIBEV 3.X TO 4.X
-The major version 4 introduced some minor incompatible changes to the API.
+The major version 4 introduced some 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.
+At the moment, the C<ev.h> header file provides compatibility definitions
+for all changes, so most programs should still compile. The compatibility
+layer might be removed in later versions of libev, so better update to the
+new API early than late.
=over 4
+=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_default_destroy> and C<ev_default_fork> have been removed
+
+These calls can be replaced easily by their C<ev_loop_xxx> counterparts:
+
+ ev_loop_destroy (EV_DEFAULT_UC);
+ ev_loop_fork (EV_DEFAULT);
+
=item function/symbol renames
A number of functions and symbols have been renamed:
@@ -4849,12 +5146,6 @@ 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
@@ -4870,20 +5161,24 @@ and work, but the library code will of course be larger.
=item active
-A watcher is active as long as it has been started (has been attached to
-an event loop) but not yet stopped (disassociated from the event loop).
+A watcher is active as long as it has been started and not yet stopped.
+See L<WATCHER STATES> for details.
=item application
In this document, an application is whatever is using libev.
+=item backend
+
+The part of the code dealing with the operating system interfaces.
+
=item callback
The address of a function that is called when some event has been
detected. Callbacks are being passed the event loop, the watcher that
received the event, and the actual event bitset.
-=item callback invocation
+=item callback/watcher invocation
The act of calling the callback associated with a watcher.
@@ -4912,12 +5207,8 @@ watchers and events.
=item pending
-A watcher is pending as soon as the corresponding event has been detected,
-and stops being pending as soon as the watcher will be invoked or its
-pending status is explicitly cleared by the application.
-
-A watcher can be pending, but not active. Stopping a watcher also clears
-its pending status.
+A watcher is pending as soon as the corresponding event has been
+detected. See L<WATCHER STATES> for details.
=item real time
@@ -4934,13 +5225,10 @@ clock.
A data structure that describes interest in certain events. Watchers need
to be started (attached to an event loop) before they can receive events.
-=item watcher invocation
-
-The act of calling the callback associated with a watcher.
-
=back
=head1 AUTHOR
-Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael Magnusson.
+Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael
+Magnusson and Emanuele Giaquinta.
diff --git a/deps/libev/ev_epoll.c b/deps/libev/ev_epoll.c
index 67a488bddc..d0caa93e7c 100644
--- a/deps/libev/ev_epoll.c
+++ b/deps/libev/ev_epoll.c
@@ -1,7 +1,7 @@
/*
* libev epoll fd activity backend
*
- * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de>
+ * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
@@ -53,7 +53,8 @@
* (such as files). while not critical, no other advanced interface
* seems to share this (rather non-unixy) limitation.
* e) epoll claims to be embeddable, but in practise you never get
- * a ready event for the epoll fd.
+ * a ready event for the epoll fd (broken: <=2.6.26, working: >=2.6.32).
+ * f) epoll_ctl returning EPERM means the fd is always ready.
*
* lots of "weird code" and complication handling in this file is due
* to these design problems with epoll, as we try very hard to avoid
@@ -64,6 +65,8 @@
#include <sys/epoll.h>
+#define EV_EMASK_EPERM 0x80
+
static void
epoll_modify (EV_P_ int fd, int oev, int nev)
{
@@ -112,6 +115,21 @@ epoll_modify (EV_P_ int fd, int oev, int nev)
if (!epoll_ctl (backend_fd, EPOLL_CTL_MOD, fd, &ev))
return;
}
+ else if (expect_true (errno == EPERM))
+ {
+ /* EPERM means the fd is always ready, but epoll is too snobbish */
+ /* to handle it, unlike select or poll. */
+ anfds [fd].emask = EV_EMASK_EPERM;
+
+ /* add fd to epoll_eperms, if not already inside */
+ if (!(oldmask & EV_EMASK_EPERM))
+ {
+ array_needsize (int, epoll_eperms, epoll_epermmax, epoll_epermcnt + 1, EMPTY2);
+ epoll_eperms [epoll_epermcnt++] = fd;
+ }
+
+ return;
+ }
fd_kill (EV_A_ fd);
@@ -125,11 +143,12 @@ epoll_poll (EV_P_ ev_tstamp timeout)
{
int i;
int eventcnt;
-
+
/* epoll wait times cannot be larger than (LONG_MAX - 999UL) / HZ msecs, which is below */
/* the default libev max wait time, however. */
EV_RELEASE_CB;
- eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, (int)ceil (timeout * 1000.));
+ eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax,
+ epoll_epermcnt ? 0 : (int)ceil (timeout * 1000.));
EV_ACQUIRE_CB;
if (expect_false (eventcnt < 0))
@@ -150,6 +169,7 @@ epoll_poll (EV_P_ ev_tstamp timeout)
| (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0);
/* check for spurious notification */
+ /* we assume that fd is always in range, as we never shrink the anfds array */
if (expect_false ((uint32_t)anfds [fd].egen != (uint32_t)(ev->data.u64 >> 32)))
{
/* recreate kernel state */
@@ -185,6 +205,18 @@ epoll_poll (EV_P_ ev_tstamp timeout)
epoll_eventmax = array_nextsize (sizeof (struct epoll_event), epoll_eventmax, epoll_eventmax + 1);
epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax);
}
+
+ /* now synthesize events for all fds where epoll fails, while select works... */
+ for (i = epoll_epermcnt; i--; )
+ {
+ int fd = epoll_eperms [i];
+ unsigned char events = anfds [fd].events & (EV_READ | EV_WRITE);
+
+ if (anfds [fd].emask & EV_EMASK_EPERM && events)
+ fd_event (EV_A_ fd, events);
+ else
+ epoll_eperms [i] = epoll_eperms [--epoll_epermcnt];
+ }
}
int inline_size
@@ -216,6 +248,7 @@ void inline_size
epoll_destroy (EV_P)
{
ev_free (epoll_events);
+ array_free (epoll_eperm, EMPTY);
}
void inline_size
diff --git a/deps/libev/ev_port.c b/deps/libev/ev_port.c
index 801de15cbc..9044ef776f 100644
--- a/deps/libev/ev_port.c
+++ b/deps/libev/ev_port.c
@@ -37,6 +37,17 @@
* either the BSD or the GPL.
*/
+/* useful reading:
+ *
+ * http://bugs.opensolaris.org/view_bug.do?bug_id=6268715 (random results)
+ * http://bugs.opensolaris.org/view_bug.do?bug_id=6455223 (just totally broken)
+ * http://bugs.opensolaris.org/view_bug.do?bug_id=6873782 (manpage ETIME)
+ * http://bugs.opensolaris.org/view_bug.do?bug_id=6874410 (implementation ETIME)
+ * http://www.mail-archive.com/networking-discuss@opensolaris.org/msg11898.html ETIME vs. nget
+ * http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/gen/event_port.c (libc)
+ * http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/fs/portfs/port.c#1325 (kernel)
+ */
+
#include <sys/types.h>
#include <sys/time.h>
#include <poll.h>
@@ -85,18 +96,20 @@ port_poll (EV_P_ ev_tstamp timeout)
struct timespec ts;
uint_t nget = 1;
+ /* we initialise this to something we will skip in the loop, as */
+ /* port_getn can return with nget unchanged, but no indication */
+ /* whether it was the original value or has been updated :/ */
+ port_events [0].portev_source = 0;
+
EV_RELEASE_CB;
EV_TS_SET (ts, timeout);
res = port_getn (backend_fd, port_events, port_eventmax, &nget, &ts);
EV_ACQUIRE_CB;
- if (res == -1)
- {
- if (errno != EINTR && errno != ETIME)
- ev_syserr ("(libev) port_getn (see http://bugs.opensolaris.org/view_bug.do?bug_id=6268715, try LIBEV_FLAGS=3 env variable)");
-
- return;
- }
+ /* port_getn may or may not set nget on error */
+ /* so we rely on port_events [0].portev_source not being updated */
+ if (res == -1 && errno != ETIME && errno != EINTR)
+ ev_syserr ("(libev) port_getn (see http://bugs.opensolaris.org/view_bug.do?bug_id=6268715, try LIBEV_FLAGS=3 env variable)");
for (i = 0; i < nget; ++i)
{
@@ -130,6 +143,8 @@ port_init (EV_P_ int flags)
if ((backend_fd = port_create ()) < 0)
return 0;
+ assert (("libev: PORT_SOURCE_FD must not be zero", PORT_SOURCE_FD));
+
fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
backend_fudge = 1e-3; /* needed to compensate for port_getn returning early */
diff --git a/deps/libev/ev_vars.h b/deps/libev/ev_vars.h
index 808f3c6d6e..17d77c890e 100644
--- a/deps/libev/ev_vars.h
+++ b/deps/libev/ev_vars.h
@@ -1,7 +1,7 @@
/*
* loop member variable declarations
*
- * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de>
+ * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
@@ -102,6 +102,9 @@ VARx(int, pollidxmax)
#if EV_USE_EPOLL || EV_GENWRAP
VARx(struct epoll_event *, epoll_events)
VARx(int, epoll_eventmax)
+VARx(int *, epoll_eperms)
+VARx(int, epoll_epermcnt)
+VARx(int, epoll_epermmax)
#endif
#if EV_USE_KQUEUE || EV_GENWRAP
@@ -117,6 +120,10 @@ VARx(struct port_event *, port_events)
VARx(int, port_eventmax)
#endif
+#if EV_USE_IOCP || EV_GENWRAP
+VARx(HANDLE, iocp)
+#endif
+
VARx(int *, fdchanges)
VARx(int, fdchangemax)
VARx(int, fdchangecnt)
@@ -152,6 +159,12 @@ VARx(int, forkmax)
VARx(int, forkcnt)
#endif
+#if EV_CLEANUP_ENABLE || EV_GENWRAP
+VARx(struct ev_cleanup **, cleanups)
+VARx(int, cleanupmax)
+VARx(int, cleanupcnt)
+#endif
+
#if EV_ASYNC_ENABLE || EV_GENWRAP
VARx(EV_ATOMIC_T, async_pending)
VARx(struct ev_async **, asyncs)
@@ -167,12 +180,15 @@ VAR (fs_hash, ANFS fs_hash [EV_INOTIFY_HASHSIZE])
#endif
VARx(EV_ATOMIC_T, sig_pending)
+VARx(int, nosigmask)
#if EV_USE_SIGNALFD || EV_GENWRAP
VARx(int, sigfd)
VARx(ev_io, sigfd_w)
VARx(sigset_t, sigfd_set)
#endif
+VARx(unsigned int, origflags) /* original loop flags */
+
#if EV_FEATURE_API || EV_GENWRAP
VARx(unsigned int, loop_count) /* total number of loop iterations/blocks */
VARx(unsigned int, loop_depth) /* #ev_run enters - #ev_run leaves */
diff --git a/deps/libev/ev_wrap.h b/deps/libev/ev_wrap.h
index 03b6b8719f..2c195c5db4 100644
--- a/deps/libev/ev_wrap.h
+++ b/deps/libev/ev_wrap.h
@@ -40,6 +40,9 @@
#define pollidxmax ((loop)->pollidxmax)
#define epoll_events ((loop)->epoll_events)
#define epoll_eventmax ((loop)->epoll_eventmax)
+#define epoll_eperms ((loop)->epoll_eperms)
+#define epoll_epermcnt ((loop)->epoll_epermcnt)
+#define epoll_epermmax ((loop)->epoll_epermmax)
#define kqueue_changes ((loop)->kqueue_changes)
#define kqueue_changemax ((loop)->kqueue_changemax)
#define kqueue_changecnt ((loop)->kqueue_changecnt)
@@ -47,6 +50,7 @@
#define kqueue_eventmax ((loop)->kqueue_eventmax)
#define port_events ((loop)->port_events)
#define port_eventmax ((loop)->port_eventmax)
+#define iocp ((loop)->iocp)
#define fdchanges ((loop)->fdchanges)
#define fdchangemax ((loop)->fdchangemax)
#define fdchangecnt ((loop)->fdchangecnt)
@@ -69,6 +73,9 @@
#define forks ((loop)->forks)
#define forkmax ((loop)->forkmax)
#define forkcnt ((loop)->forkcnt)
+#define cleanups ((loop)->cleanups)
+#define cleanupmax ((loop)->cleanupmax)
+#define cleanupcnt ((loop)->cleanupcnt)
#define async_pending ((loop)->async_pending)
#define asyncs ((loop)->asyncs)
#define asyncmax ((loop)->asyncmax)
@@ -78,9 +85,11 @@
#define fs_2625 ((loop)->fs_2625)
#define fs_hash ((loop)->fs_hash)
#define sig_pending ((loop)->sig_pending)
+#define nosigmask ((loop)->nosigmask)
#define sigfd ((loop)->sigfd)
#define sigfd_w ((loop)->sigfd_w)
#define sigfd_set ((loop)->sigfd_set)
+#define origflags ((loop)->origflags)
#define loop_count ((loop)->loop_count)
#define loop_depth ((loop)->loop_depth)
#define userdata ((loop)->userdata)
@@ -128,6 +137,9 @@
#undef pollidxmax
#undef epoll_events
#undef epoll_eventmax
+#undef epoll_eperms
+#undef epoll_epermcnt
+#undef epoll_epermmax
#undef kqueue_changes
#undef kqueue_changemax
#undef kqueue_changecnt
@@ -135,6 +147,7 @@
#undef kqueue_eventmax
#undef port_events
#undef port_eventmax
+#undef iocp
#undef fdchanges
#undef fdchangemax
#undef fdchangecnt
@@ -157,6 +170,9 @@
#undef forks
#undef forkmax
#undef forkcnt
+#undef cleanups
+#undef cleanupmax
+#undef cleanupcnt
#undef async_pending
#undef asyncs
#undef asyncmax
@@ -166,9 +182,11 @@
#undef fs_2625
#undef fs_hash
#undef sig_pending
+#undef nosigmask
#undef sigfd
#undef sigfd_w
#undef sigfd_set
+#undef origflags
#undef loop_count
#undef loop_depth
#undef userdata
diff --git a/deps/libev/event.c b/deps/libev/event.c
index b0eb47588e..579423f18c 100644
--- a/deps/libev/event.c
+++ b/deps/libev/event.c
@@ -110,7 +110,7 @@ void event_base_free (struct event_base *base)
dLOOPbase;
#if EV_MULTIPLICITY
- if (ev_default_loop (EVFLAG_AUTO) != loop)
+ if (!ev_is_default_loop (loop))
ev_loop_destroy (loop);
#endif
}
@@ -298,7 +298,12 @@ int event_pending (struct event *ev, short events, struct timeval *tv)
revents |= EV_TIMEOUT;
if (tv)
- EV_TV_SET (tv, ev_now (EV_A)); /* not sure if this is right :) */
+ {
+ ev_tstamp at = ev_now (EV_A);
+
+ tv->tv_sec = (long)at;
+ tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
+ }
}
return events & revents;
diff --git a/deps/libev/event.h b/deps/libev/event.h
index 358344a0de..6411fb6d0d 100644
--- a/deps/libev/event.h
+++ b/deps/libev/event.h
@@ -95,6 +95,8 @@ struct event
short ev_events;
};
+#define EV_READ EV_READ
+#define EV_WRITE EV_WRITE
#define EV_PERSIST 0x10
#define EVENT_SIGNAL(ev) ((int) (ev)->ev_fd)