X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/09c28a9820828432b5ced0a8f2b05c56213690fc..b3c85cba31a5669ce3e1995d8660542354957a53:/ev.3?ds=sidebyside diff --git a/ev.3 b/ev.3 index 5e213b8..a7805ec 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-19" "perl v5.8.8" "User Contributed Perl Documentation" .SH "NAME" libev \- a high performance full\-featured event loop written in C .SH "SYNOPSIS" @@ -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 @@ -261,14 +263,17 @@ you actually want to know. .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 @@ -554,8 +559,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 +Not 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 @@ -636,9 +650,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". @@ -1063,6 +1078,31 @@ 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 +\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 +1165,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 +1279,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 +1300,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 +1327,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 +1341,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 +1375,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 +1392,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 +1455,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 +1474,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 +1551,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 +1642,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 +1709,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 @@ -1892,6 +1978,9 @@ create it, and if that fails, use the normal loop for everything: \& else \& loop_lo = loop_hi; .Ve +.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 @@ -1921,6 +2010,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 +2216,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 +2261,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: @@ -2539,6 +2631,36 @@ definition and a statement, respectively. See the \fIev.v\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 comamnd 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