X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/09c28a9820828432b5ced0a8f2b05c56213690fc..a2417dbac4f1991fb09fe2e0ba96dabdf69c72aa:/ev.3 diff --git a/ev.3 b/ev.3 index 5e213b8..7689fc0 100644 --- a/ev.3 +++ b/ev.3 @@ -128,8 +128,8 @@ .rm #[ #] #H #V #F C .\" ======================================================================== .\" -.IX Title """ 1" -.TH "" 1 "2007-12-08" "perl v5.8.8" "User Contributed Perl Documentation" +.IX Title "EV 1" +.TH EV 1 "2007-12-22" "perl v5.8.8" "User Contributed Perl Documentation" .SH "NAME" libev \- a high performance full\-featured event loop written in C .SH "SYNOPSIS" @@ -203,7 +203,7 @@ web page you might find easier to navigate when reading it for the first time: . .PP Libev is an event loop: you register interest in certain events (such as a -file descriptor being readable or a timeout occuring), and it will manage +file descriptor being readable or a timeout occurring), and it will manage these event sources and provide your program with events. .PP To do this, it must take more or less complete control over your process @@ -245,7 +245,9 @@ Libev represents time as a single floating point number, representing the 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, and when you need to do any calculations on -it, you should treat it as such. +it, you should treat it as some floatingpoint value. Unlike the name +component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for time differences +throughout libev. .SH "GLOBAL FUNCTIONS" .IX Header "GLOBAL FUNCTIONS" These functions can be called anytime, even before initialising the @@ -255,20 +257,28 @@ library in any way. 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. +.IP "void ev_sleep (ev_tstamp interval)" 4 +.IX Item "void ev_sleep (ev_tstamp interval)" +Sleep for the given interval: The current thread will be blocked until +either it is interrupted or the given time interval has passed. Basically +this is a subsecond-resolution \f(CW\*(C`sleep ()\*(C'\fR. .IP "int ev_version_major ()" 4 .IX Item "int ev_version_major ()" .PD 0 .IP "int ev_version_minor ()" 4 .IX Item "int ev_version_minor ()" .PD -You can find out the major and minor version numbers of the library +You can find out the major and minor \s-1ABI\s0 version numbers of the library you linked against by calling the functions \f(CW\*(C`ev_version_major\*(C'\fR and \&\f(CW\*(C`ev_version_minor\*(C'\fR. If you want, you can compare against the global symbols \f(CW\*(C`EV_VERSION_MAJOR\*(C'\fR and \f(CW\*(C`EV_VERSION_MINOR\*(C'\fR, which specify the version of the library your program was compiled against. .Sp +These version numbers refer to the \s-1ABI\s0 version of the library, not the +release version. +.Sp Usually, it's a good idea to terminate if the major versions mismatch, -as this indicates an incompatible change. Minor versions are usually +as this indicates an incompatible change. Minor versions are usually compatible to older versions, so a larger minor version alone is usually not a problem. .Sp @@ -456,15 +466,18 @@ lot of inactive fds). It scales similarly to select, i.e. O(total_fds). .el .IP "\f(CWEVBACKEND_EPOLL\fR (value 4, Linux)" 4 .IX Item "EVBACKEND_EPOLL (value 4, Linux)" For few fds, this backend is a bit little slower than poll and select, -but it scales phenomenally better. While poll and select usually scale like -O(total_fds) where n is the total number of fds (or the highest fd), epoll scales -either O(1) or O(active_fds). -.Sp -While stopping and starting an I/O watcher in the same iteration will -result in some caching, there is still a syscall per such incident +but it scales phenomenally better. While poll and select usually scale +like O(total_fds) where n is the total number of fds (or the highest fd), +epoll scales either O(1) or O(active_fds). The epoll design has a number +of shortcomings, such as silently dropping events in some hard-to-detect +cases and rewiring a syscall per fd change, no fork support and bad +support for dup: +.Sp +While stopping, setting and starting an I/O watcher in the same iteration +will result in some caching, there is still a syscall per such incident (because the fd could point to a different file description now), so its -best to avoid that. Also, \fIdup()\fRed file descriptors might not work very -well if you register events for both fds. +best to avoid that. Also, \f(CW\*(C`dup ()\*(C'\fR'ed file descriptors might not work +very well if you register events for both fds. .Sp Please note that epoll sometimes generates spurious notifications, so you need to use non-blocking I/O or other means to avoid blocking when no data @@ -473,17 +486,20 @@ need to use non-blocking I/O or other means to avoid blocking when no data .el .IP "\f(CWEVBACKEND_KQUEUE\fR (value 8, most \s-1BSD\s0 clones)" 4 .IX Item "EVBACKEND_KQUEUE (value 8, most BSD clones)" Kqueue deserves special mention, as at the time of this writing, it -was broken on all BSDs except NetBSD (usually it doesn't work with -anything but sockets and pipes, except on Darwin, where of course its -completely useless). For this reason its not being \*(L"autodetected\*(R" +was broken on \fIall\fR BSDs (usually it doesn't work with anything but +sockets and pipes, except on Darwin, where of course it's completely +useless. On NetBSD, it seems to work for all the \s-1FD\s0 types I tested, so it +is used by default there). For this reason it's not being \*(L"autodetected\*(R" unless you explicitly specify it explicitly in the flags (i.e. using -\&\f(CW\*(C`EVBACKEND_KQUEUE\*(C'\fR). +\&\f(CW\*(C`EVBACKEND_KQUEUE\*(C'\fR) or libev was compiled on a known-to-be-good (\-enough) +system like NetBSD. .Sp It scales in the same way as the epoll backend, but the interface to the -kernel is more efficient (which says nothing about its actual speed, of -course). While starting and stopping an I/O watcher does not cause an -extra syscall as with epoll, it still adds up to four event changes per -incident, so its best to avoid that. +kernel is more efficient (which says nothing about its actual speed, +of course). While stopping, setting and starting an I/O watcher does +never cause an extra syscall as with epoll, it still adds up to two event +changes per incident, support for \f(CW\*(C`fork ()\*(C'\fR is very bad and it drops fds +silently in similarly hard-to-detetc cases. .ie n .IP """EVBACKEND_DEVPOLL"" (value 16, Solaris 8)" 4 .el .IP "\f(CWEVBACKEND_DEVPOLL\fR (value 16, Solaris 8)" 4 .IX Item "EVBACKEND_DEVPOLL (value 16, Solaris 8)" @@ -491,10 +507,10 @@ This is not implemented yet (and might never be). .ie n .IP """EVBACKEND_PORT"" (value 32, Solaris 10)" 4 .el .IP "\f(CWEVBACKEND_PORT\fR (value 32, Solaris 10)" 4 .IX Item "EVBACKEND_PORT (value 32, Solaris 10)" -This uses the Solaris 10 port mechanism. As with everything on Solaris, +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 ports can result in a lot of spurious +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. .ie n .IP """EVBACKEND_ALL""" 4 @@ -554,8 +570,17 @@ 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 yoursef \fIbefore\fR calling this function, or cope with the fact afterwards (which is usually -the easiest thing, youc na just ignore the watchers and/or \f(CW\*(C`free ()\*(C'\fR them +the easiest thing, you can just ignore the watchers and/or \f(CW\*(C`free ()\*(C'\fR them for example). +.Sp +Note that certain global state, such as signal state, 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 @@ -606,7 +631,7 @@ Returns the current \*(L"event loop time\*(R", which is the time the event loop received events and started processing them. This timestamp does not change as long as callbacks are being processed, and this is also the base time used for relative timers. You can treat it as the timestamp of the -event occuring (or more correctly, libev finding out about it). +event occurring (or more correctly, libev finding out about it). .IP "ev_loop (loop, int flags)" 4 .IX Item "ev_loop (loop, int flags)" Finally, this is it, the event handler. This function usually is called @@ -636,9 +661,10 @@ 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: .Sp -.Vb 18 +.Vb 19 +\& - Before the first iteration, call any pending watchers. \& * If there are no active watchers (reference count is zero), return. -\& - Queue prepare watchers and then call all outstanding watchers. +\& - Queue all prepare watchers and then call all outstanding watchers. \& - If we have been forked, recreate the kernel state. \& - Update the kernel state with all outstanding changes. \& - Update the "event loop time". @@ -705,6 +731,41 @@ Example: For some weird reason, unregister the above signal handler again. \& ev_ref (loop); \& ev_signal_stop (loop, &exitsig); .Ve +.IP "ev_set_io_collect_interval (ev_tstamp interval)" 4 +.IX Item "ev_set_io_collect_interval (ev_tstamp interval)" +.PD 0 +.IP "ev_set_timeout_collect_interval (ev_tstamp interval)" 4 +.IX Item "ev_set_timeout_collect_interval (ev_tstamp interval)" +.PD +These advanced functions influence the time that libev will spend waiting +for events. Both are by default \f(CW0\fR, meaning that libev will try to +invoke timer/periodic callbacks and I/O callbacks with minimum latency. +.Sp +Setting these to a higher value (the \f(CW\*(C`interval\*(C'\fR \fImust\fR be >= \f(CW0\fR) +allows libev to delay invocation of I/O and timer/periodic callbacks to +increase efficiency of loop iterations. +.Sp +The background is that sometimes your program runs just fast enough to +handle one (or very few) event(s) per loop iteration. While this makes +the program responsive, it also wastes a lot of \s-1CPU\s0 time to poll for new +events, especially with backends like \f(CW\*(C`select ()\*(C'\fR which have a high +overhead for the actual polling but can deliver many events at once. +.Sp +By setting a higher \fIio collect interval\fR you allow libev to spend more +time collecting I/O events, so you can handle more events per iteration, +at the cost of increasing latency. Timeouts (both \f(CW\*(C`ev_periodic\*(C'\fR and +\&\f(CW\*(C`ev_timer\*(C'\fR) will be not affected. +.Sp +Likewise, by setting a higher \fItimeout collect interval\fR you allow libev +to spend more time collecting timeouts, at the expense of increased +latency (the watcher callback will be called later). \f(CW\*(C`ev_io\*(C'\fR watchers +will not be affected. +.Sp +Many programs can usually benefit by setting the io collect interval to +a value near \f(CW0.1\fR or so, which is often enough for interactive servers +(of course not for games), likewise for timeouts. It usually doesn't make +much sense to set it to a lower value than \f(CW0.01\fR, as this approsaches +the timing granularity of most systems. .SH "ANATOMY OF A WATCHER" .IX Header "ANATOMY OF A WATCHER" A watcher is a structure that you create and register to record your @@ -1063,6 +1124,55 @@ play around with an Xlib connection), then you have to seperately 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). +.PP +\fIThe special problem of disappearing file descriptors\fR +.IX Subsection "The special problem of disappearing file descriptors" +.PP +Some backends (e.g. kqueue, epoll) need to be told about closing a file +descriptor (either by calling \f(CW\*(C`close\*(C'\fR explicitly or by any other means, +such as \f(CW\*(C`dup\*(C'\fR). The reason is that you register interest in some file +descriptor, but when it goes away, the operating system will silently drop +this interest. If another file descriptor with the same number then is +registered with libev, there is no efficient way to see that this is, in +fact, a different file descriptor. +.PP +To avoid having to explicitly tell libev about such cases, libev follows +the following policy: Each time \f(CW\*(C`ev_io_set\*(C'\fR is being called, libev +will assume that this is potentially a new file descriptor, otherwise +it is assumed that the file descriptor stays the same. That means that +you \fIhave\fR to call \f(CW\*(C`ev_io_set\*(C'\fR (or \f(CW\*(C`ev_io_init\*(C'\fR) when you change the +descriptor even if the file descriptor number itself did not change. +.PP +This is how one would do it normally anyway, the important point is that +the libev application should not optimise around libev but should leave +optimisations to libev. +.PP +\fIThe special problem of dup'ed file descriptors\fR +.IX Subsection "The special problem of dup'ed file descriptors" +.PP +Some backends (e.g. epoll), cannot register events for file descriptors, +but only events for the underlying file descriptions. That menas when you +have \f(CW\*(C`dup ()\*(C'\fR'ed file descriptors and register events for them, only one +file descriptor might actually receive events. +.PP +There is no workaorund 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 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. +.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. +.PP +\fIWatcher-Specific Functions\fR +.IX Subsection "Watcher-Specific Functions" .IP "ev_io_init (ev_io *, callback, int fd, int events)" 4 .IX Item "ev_io_init (ev_io *, callback, int fd, int events)" .PD 0 @@ -1125,6 +1235,9 @@ on the current time, use something like this to adjust for this: The callback is guarenteed to be invoked only when its timeout has passed, but if multiple timers become ready during the same loop iteration then order of execution is undefined. +.PP +\fIWatcher-Specific Functions and Data Members\fR +.IX Subsection "Watcher-Specific Functions and Data Members" .IP "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" 4 .IX Item "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" .PD 0 @@ -1236,15 +1349,18 @@ to trigger \*(L"at\*(R" some specific point in time. For example, if you tell a periodic watcher to trigger in 10 seconds (by specifiying e.g. \f(CW\*(C`ev_now () + 10.\*(C'\fR) and then reset your system clock to the last year, then it will take a year to trigger the event (unlike an \f(CW\*(C`ev_timer\*(C'\fR, which would trigger -roughly 10 seconds later and of course not if you reset your system time -again). +roughly 10 seconds later). .PP They can also be used to implement vastly more complex timers, such as -triggering an event on eahc midnight, local time. +triggering an event on each midnight, local time or other, complicated, +rules. .PP As with timers, the callback is guarenteed to be invoked only when the time (\f(CW\*(C`at\*(C'\fR) has been passed, but if multiple periodic timers become ready during the same loop iteration then order of execution is undefined. +.PP +\fIWatcher-Specific Functions and Data Members\fR +.IX Subsection "Watcher-Specific Functions and Data Members" .IP "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" 4 .IX Item "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" .PD 0 @@ -1254,17 +1370,17 @@ during the same loop iteration then order of execution is undefined. Lots of arguments, lets sort it out... There are basically three modes of operation, and we will explain them from simplest to complex: .RS 4 -.IP "* absolute timer (interval = reschedule_cb = 0)" 4 -.IX Item "absolute timer (interval = reschedule_cb = 0)" +.IP "* absolute timer (at = time, interval = reschedule_cb = 0)" 4 +.IX Item "absolute timer (at = time, interval = reschedule_cb = 0)" In this configuration the watcher triggers an event at the wallclock time \&\f(CW\*(C`at\*(C'\fR and doesn't repeat. It will not adjust when a time jump occurs, that is, if it is to be run at January 1st 2011 then it will run when the system time reaches or surpasses this time. -.IP "* non-repeating interval timer (interval > 0, reschedule_cb = 0)" 4 -.IX Item "non-repeating interval timer (interval > 0, reschedule_cb = 0)" +.IP "* non-repeating interval timer (at = offset, interval > 0, reschedule_cb = 0)" 4 +.IX Item "non-repeating interval timer (at = offset, interval > 0, reschedule_cb = 0)" In this mode the watcher will always be scheduled to time out at the next -\&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N) and then repeat, regardless -of any time jumps. +\&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative) +and then repeat, regardless of any time jumps. .Sp This can be used to create timers that do not drift with respect to system time: @@ -1281,8 +1397,12 @@ by 3600. Another way to think about it (for the mathematically inclined) is that \&\f(CW\*(C`ev_periodic\*(C'\fR will try to run the callback in this mode at the next possible time where \f(CW\*(C`time = at (mod interval)\*(C'\fR, regardless of any time jumps. -.IP "* manual reschedule mode (reschedule_cb = callback)" 4 -.IX Item "manual reschedule mode (reschedule_cb = callback)" +.Sp +For numerical stability it is preferable that the \f(CW\*(C`at\*(C'\fR value is near +\&\f(CW\*(C`ev_now ()\*(C'\fR (the current time), but there is no range requirement for +this value. +.IP "* manual reschedule mode (at and interval ignored, reschedule_cb = callback)" 4 +.IX Item "manual reschedule mode (at and interval ignored, reschedule_cb = callback)" In this mode the values for \f(CW\*(C`interval\*(C'\fR and \f(CW\*(C`at\*(C'\fR are both being ignored. Instead, each time the periodic watcher gets scheduled, the reschedule callback will be called with the watcher as first, and the @@ -1291,7 +1411,7 @@ current time as second argument. \&\s-1NOTE:\s0 \fIThis callback \s-1MUST\s0 \s-1NOT\s0 stop or destroy any periodic watcher, ever, or make any event loop modifications\fR. If you need to stop it, return \f(CW\*(C`now + 1e30\*(C'\fR (or so, fudge fudge) and stop it afterwards (e.g. by -starting a prepare watcher). +starting an \f(CW\*(C`ev_prepare\*(C'\fR watcher, which is legal). .Sp Its prototype is \f(CW\*(C`ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now)\*(C'\fR, e.g.: @@ -1325,6 +1445,13 @@ Simply stops and restarts the periodic watcher again. This is only useful when you changed some parameters or the reschedule callback would return a different time than the last time it was called (e.g. in a crond like program when the crontabs have changed). +.IP "ev_tstamp offset [read\-write]" 4 +.IX Item "ev_tstamp offset [read-write]" +When repeating, this contains the offset value, otherwise this is the +absolute point in time (the \f(CW\*(C`at\*(C'\fR value passed to \f(CW\*(C`ev_periodic_set\*(C'\fR). +.Sp +Can be modified any time, but changes only take effect when the periodic +timer fires or \f(CW\*(C`ev_periodic_again\*(C'\fR is being called. .IP "ev_tstamp interval [read\-write]" 4 .IX Item "ev_tstamp interval [read-write]" The current interval value. Can be modified any time, but changes only @@ -1335,6 +1462,10 @@ called. The current reschedule callback, or \f(CW0\fR, if this functionality is switched off. Can be changed any time, but changes only take effect when the periodic timer fires or \f(CW\*(C`ev_periodic_again\*(C'\fR is being called. +.IP "ev_tstamp at [read\-only]" 4 +.IX Item "ev_tstamp at [read-only]" +When active, contains the absolute time that the watcher is supposed to +trigger next. .PP Example: Call a callback every hour, or, more precisely, whenever the system clock is divisible by 3600. The callback invocation times have @@ -1394,6 +1525,9 @@ with the kernel (thus it coexists with your own signal handlers as long as you don't register any with libev). Similarly, when the last signal watcher for a signal is stopped libev will reset the signal handler to \&\s-1SIG_DFL\s0 (regardless of what it was set to before). +.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 .IX Item "ev_signal_init (ev_signal *, callback, int signum)" .PD 0 @@ -1410,6 +1544,9 @@ The signal the watcher watches out for. .IX Subsection "ev_child - watch out for process status changes" Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to some child status changes (most typically when a child of yours dies). +.PP +\fIWatcher-Specific Functions and Data Members\fR +.IX Subsection "Watcher-Specific Functions and Data Members" .IP "ev_child_init (ev_child *, callback, int pid)" 4 .IX Item "ev_child_init (ev_child *, callback, int pid)" .PD 0 @@ -1484,6 +1621,9 @@ semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev sometime to fall back to regular polling again even with inotify, but changes are usually detected immediately, and if the file exists there will be no polling. +.PP +\fIWatcher-Specific Functions and Data Members\fR +.IX Subsection "Watcher-Specific Functions and Data Members" .IP "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" 4 .IX Item "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" .PD 0 @@ -1572,6 +1712,9 @@ Apart from keeping your process non-blocking (which is a useful effect on its own sometimes), idle watchers are a good place to do \&\*(L"pseudo\-background processing\*(R", or delay processing stuff to after the event loop has handled all outstanding events. +.PP +\fIWatcher-Specific Functions and Data Members\fR +.IX Subsection "Watcher-Specific Functions and Data Members" .IP "ev_idle_init (ev_signal *, callback)" 4 .IX Item "ev_idle_init (ev_signal *, callback)" Initialises and configures the idle watcher \- it has no parameters of any @@ -1636,6 +1779,19 @@ with priority higher than or equal to the event loop and one coroutine of lower priority, but only once, using idle watchers to keep the event loop from blocking if lower-priority coroutines are active, thus mapping low-priority coroutines to idle/background tasks). +.PP +It is recommended to give \f(CW\*(C`ev_check\*(C'\fR watchers highest (\f(CW\*(C`EV_MAXPRI\*(C'\fR) +priority, to ensure that they are being run before any other watchers +after the poll. Also, \f(CW\*(C`ev_check\*(C'\fR watchers (and \f(CW\*(C`ev_prepare\*(C'\fR watchers, +too) should not activate (\*(L"feed\*(R") events into libev. While libev fully +supports this, they will be called before other \f(CW\*(C`ev_check\*(C'\fR watchers did +their job. As \f(CW\*(C`ev_check\*(C'\fR watchers are often used to embed other event +loops those other event loops might be in an unusable state until their +\&\f(CW\*(C`ev_check\*(C'\fR watcher ran (always remind yourself to coexist peacefully with +others). +.PP +\fIWatcher-Specific Functions and Data Members\fR +.IX Subsection "Watcher-Specific Functions and Data Members" .IP "ev_prepare_init (ev_prepare *, callback)" 4 .IX Item "ev_prepare_init (ev_prepare *, callback)" .PD 0 @@ -1822,7 +1978,7 @@ this. This is a rather advanced watcher type that lets you embed one event loop into another (currently only \f(CW\*(C`ev_io\*(C'\fR events are supported in the embedded loop, other types of watchers might be handled in a delayed or incorrect -fashion and must not be used). +fashion and must not be used). (See portability notes, below). .PP There are primarily two reasons you would want that: work around bugs and prioritise I/O. @@ -1892,6 +2048,24 @@ create it, and if that fails, use the normal loop for everything: \& else \& loop_lo = loop_hi; .Ve +.Sh "Portability notes" +.IX Subsection "Portability notes" +Kqueue is nominally embeddable, but this is broken on all BSDs that I +tried, in various ways. Usually the embedded event loop will simply never +receive events, sometimes it will only trigger a few times, sometimes in a +loop. Epoll is also nominally embeddable, but many Linux kernel versions +will always eport the epoll fd as ready, even when no events are pending. +.PP +While libev allows embedding these backends (they are contained in +\&\f(CW\*(C`ev_embeddable_backends ()\*(C'\fR), take extreme care that it will actually +work. +.PP +When in doubt, create a dynamic event loop forced to use sockets (this +usually works) and possibly another thread and a pipe or so to report to +your main event loop. +.PP +\fIWatcher-Specific Functions and Data Members\fR +.IX Subsection "Watcher-Specific Functions and Data Members" .IP "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" 4 .IX Item "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" .PD 0 @@ -1908,8 +2082,8 @@ if you do not want thta, you need to temporarily stop the embed watcher). 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 apropriate way for embedded loops. -.IP "struct ev_loop *loop [read\-only]" 4 -.IX Item "struct ev_loop *loop [read-only]" +.IP "struct ev_loop *other [read\-only]" 4 +.IX Item "struct ev_loop *other [read-only]" The embedded event loop. .ie n .Sh """ev_fork"" \- the audacity to resume the event loop after a fork" .el .Sh "\f(CWev_fork\fP \- the audacity to resume the event loop after a fork" @@ -1921,6 +2095,9 @@ event loop blocks next and before \f(CW\*(C`ev_check\*(C'\fR watchers are being and only in the child after the fork. If whoever good citizen calling \&\f(CW\*(C`ev_default_fork\*(C'\fR cheats and calls it in the wrong process, the fork handlers will be invoked, too, of course. +.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)" Initialises and configures the fork watcher \- it has no parameters of any @@ -2124,18 +2301,18 @@ constructor already stores the event loop. .IP "w\->stop ()" 4 .IX Item "w->stop ()" Stops the watcher if it is active. Again, no \f(CW\*(C`loop\*(C'\fR argument. -.ie n .IP "w\->again () ""ev::timer""\fR, \f(CW""ev::periodic"" only" 4 -.el .IP "w\->again () \f(CWev::timer\fR, \f(CWev::periodic\fR only" 4 -.IX Item "w->again () ev::timer, ev::periodic only" +.ie n .IP "w\->again () (""ev::timer""\fR, \f(CW""ev::periodic"" only)" 4 +.el .IP "w\->again () (\f(CWev::timer\fR, \f(CWev::periodic\fR only)" 4 +.IX Item "w->again () (ev::timer, ev::periodic only)" For \f(CW\*(C`ev::timer\*(C'\fR and \f(CW\*(C`ev::periodic\*(C'\fR, this invokes the corresponding \&\f(CW\*(C`ev_TYPE_again\*(C'\fR function. -.ie n .IP "w\->sweep () ""ev::embed"" only" 4 -.el .IP "w\->sweep () \f(CWev::embed\fR only" 4 -.IX Item "w->sweep () ev::embed only" +.ie n .IP "w\->sweep () (""ev::embed"" only)" 4 +.el .IP "w\->sweep () (\f(CWev::embed\fR only)" 4 +.IX Item "w->sweep () (ev::embed only)" Invokes \f(CW\*(C`ev_embed_sweep\*(C'\fR. -.ie n .IP "w\->update () ""ev::stat"" only" 4 -.el .IP "w\->update () \f(CWev::stat\fR only" 4 -.IX Item "w->update () ev::stat only" +.ie n .IP "w\->update () (""ev::stat"" only)" 4 +.el .IP "w\->update () (\f(CWev::stat\fR only)" 4 +.IX Item "w->update () (ev::stat only)" Invokes \f(CW\*(C`ev_stat_stat\*(C'\fR. .RE .RS 4 @@ -2169,9 +2346,9 @@ the constructor. .Ve .SH "MACRO MAGIC" .IX Header "MACRO MAGIC" -Libev can be compiled with a variety of options, the most fundemantal is -\&\f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most) functions and -callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument. +Libev can be compiled with a variety of options, the most fundamantal +of which is \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most) +functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument. .PP To make it easier to write programs that cope with either variant, the following macros are defined: @@ -2240,7 +2417,7 @@ applications. Examples of applications that embed it include the Deliantra Game Server, the \s-1EV\s0 perl module, the \s-1GNU\s0 Virtual Private Ethernet (gvpe) and rxvt\-unicode. .PP -The goal is to enable you to just copy the neecssary files into your +The goal is to enable you to just copy the necessary files into your source directory without having to change even a single line in them, so you can easily upgrade by simply copying (or having a checked-out copy of libev somewhere in your source tree). @@ -2355,7 +2532,7 @@ If defined to be \f(CW1\fR, libev will try to detect the availability of the monotonic clock option at both compiletime and runtime. Otherwise no use of the monotonic clock option will be attempted. If you enable this, you usually have to link against librt or something similar. Enabling it when -the functionality isn't available is safe, though, althoguh you have +the functionality isn't available is safe, though, although you have to make sure you link against any libraries where the \f(CW\*(C`clock_gettime\*(C'\fR function is hiding in (often \fI\-lrt\fR). .IP "\s-1EV_USE_REALTIME\s0" 4 @@ -2364,8 +2541,12 @@ If defined to be \f(CW1\fR, libev will try to detect the availability of the realtime clock option at compiletime (and assume its availability at runtime if successful). Otherwise no use of the realtime clock option will be attempted. This effectively replaces \f(CW\*(C`gettimeofday\*(C'\fR by \f(CW\*(C`clock_get -(CLOCK_REALTIME, ...)\*(C'\fR and will not normally affect correctness. See tzhe note about libraries -in the description of \f(CW\*(C`EV_USE_MONOTONIC\*(C'\fR, though. +(CLOCK_REALTIME, ...)\*(C'\fR and will not normally affect correctness. See the +note about libraries in the description of \f(CW\*(C`EV_USE_MONOTONIC\*(C'\fR, though. +.IP "\s-1EV_USE_NANOSLEEP\s0" 4 +.IX Item "EV_USE_NANOSLEEP" +If defined to be \f(CW1\fR, libev will assume that \f(CW\*(C`nanosleep ()\*(C'\fR is available +and will use it for delays. Otherwise it will use \f(CW\*(C`select ()\*(C'\fR. .IP "\s-1EV_USE_SELECT\s0" 4 .IX Item "EV_USE_SELECT" If undefined or defined to be \f(CW1\fR, libev will compile in support for the @@ -2535,10 +2716,40 @@ For example, the perl \s-1EV\s0 module uses something like this: .PD Can be used to change the callback member declaration in each watcher, and the way callbacks are invoked and set. Must expand to a struct member -definition and a statement, respectively. See the \fIev.v\fR header file for +definition and a statement, respectively. See the \fIev.h\fR header file for their default definitions. One possible use for overriding these is to avoid the \f(CW\*(C`struct ev_loop *\*(C'\fR as first argument in all cases, or to use method calls instead of plain function calls in \*(C+. +.Sh "\s-1EXPORTED\s0 \s-1API\s0 \s-1SYMBOLS\s0" +.IX Subsection "EXPORTED API SYMBOLS" +If you need to re-export the \s-1API\s0 (e.g. via a dll) and you need a list of +exported symbols, you can use the provided \fISymbol.*\fR files which list +all public symbols, one per line: +.Sp +.Vb 2 +\& Symbols.ev for libev proper +\& Symbols.event for the libevent emulation +.Ve +.Sp +This can also be used to rename all public symbols to avoid clashes with +multiple versions of libev linked together (which is obviously bad in +itself, but sometimes it is inconvinient to avoid this). +.Sp +A sed command like this will create wrapper \f(CW\*(C`#define\*(C'\fR's that you need to +include before including \fIev.h\fR: +.Sp +.Vb 1 +\& wrap.h +.Ve +.Sp +This would create a file \fIwrap.h\fR which essentially looks like this: +.Sp +.Vb 4 +\& #define ev_backend myprefix_ev_backend +\& #define ev_check_start myprefix_ev_check_start +\& #define ev_check_stop myprefix_ev_check_stop +\& ... +.Ve .Sh "\s-1EXAMPLES\s0" .IX Subsection "EXAMPLES" For a real-world example of a program the includes libev