ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.3
(Generate patch)

Comparing libev/ev.3 (file contents):
Revision 1.60 by root, Mon Jan 28 12:23:02 2008 UTC vs.
Revision 1.63 by root, Wed Apr 2 15:23:11 2008 UTC

130.\} 130.\}
131.rm #[ #] #H #V #F C 131.rm #[ #] #H #V #F C
132.\" ======================================================================== 132.\" ========================================================================
133.\" 133.\"
134.IX Title "EV 1" 134.IX Title "EV 1"
135.TH EV 1 "2008-01-28" "perl v5.10.0" "User Contributed Perl Documentation" 135.TH EV 1 "2008-04-02" "perl v5.10.0" "User Contributed Perl Documentation"
136.\" For nroff, turn off justification. Always turn off hyphenation; it makes 136.\" For nroff, turn off justification. Always turn off hyphenation; it makes
137.\" way too many mistakes in technical documents. 137.\" way too many mistakes in technical documents.
138.if n .ad l 138.if n .ad l
139.nh 139.nh
140.SH "NAME" 140.SH "NAME"
144.Vb 1 144.Vb 1
145\& #include <ev.h> 145\& #include <ev.h>
146.Ve 146.Ve
147.Sh "\s-1EXAMPLE\s0 \s-1PROGRAM\s0" 147.Sh "\s-1EXAMPLE\s0 \s-1PROGRAM\s0"
148.IX Subsection "EXAMPLE PROGRAM" 148.IX Subsection "EXAMPLE PROGRAM"
149.Vb 1 149.Vb 2
150\& // a single header file is required
150\& #include <ev.h> 151\& #include <ev.h>
151\& 152\&
153\& // every watcher type has its own typedef\*(Aqd struct
154\& // with the name ev_<type>
152\& ev_io stdin_watcher; 155\& ev_io stdin_watcher;
153\& ev_timer timeout_watcher; 156\& ev_timer timeout_watcher;
154\& 157\&
158\& // all watcher callbacks have a similar signature
155\& /* called when data readable on stdin */ 159\& // this callback is called when data is readable on stdin
156\& static void 160\& static void
157\& stdin_cb (EV_P_ struct ev_io *w, int revents) 161\& stdin_cb (EV_P_ struct ev_io *w, int revents)
158\& { 162\& {
159\& /* puts ("stdin ready"); */ 163\& puts ("stdin ready");
160\& ev_io_stop (EV_A_ w); /* just a syntax example */ 164\& // for one\-shot events, one must manually stop the watcher
161\& ev_unloop (EV_A_ EVUNLOOP_ALL); /* leave all loop calls */ 165\& // with its corresponding stop function.
166\& ev_io_stop (EV_A_ w);
167\&
168\& // this causes all nested ev_loop\*(Aqs to stop iterating
169\& ev_unloop (EV_A_ EVUNLOOP_ALL);
162\& } 170\& }
163\& 171\&
172\& // another callback, this time for a time\-out
164\& static void 173\& static void
165\& timeout_cb (EV_P_ struct ev_timer *w, int revents) 174\& timeout_cb (EV_P_ struct ev_timer *w, int revents)
166\& { 175\& {
167\& /* puts ("timeout"); */ 176\& puts ("timeout");
168\& ev_unloop (EV_A_ EVUNLOOP_ONE); /* leave one loop call */ 177\& // this causes the innermost ev_loop to stop iterating
178\& ev_unloop (EV_A_ EVUNLOOP_ONE);
169\& } 179\& }
170\& 180\&
171\& int 181\& int
172\& main (void) 182\& main (void)
173\& { 183\& {
184\& // use the default event loop unless you have special needs
174\& struct ev_loop *loop = ev_default_loop (0); 185\& struct ev_loop *loop = ev_default_loop (0);
175\& 186\&
176\& /* initialise an io watcher, then start it */ 187\& // initialise an io watcher, then start it
188\& // this one will watch for stdin to become readable
177\& ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); 189\& ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ);
178\& ev_io_start (loop, &stdin_watcher); 190\& ev_io_start (loop, &stdin_watcher);
179\& 191\&
192\& // initialise a timer watcher, then start it
180\& /* simple non\-repeating 5.5 second timeout */ 193\& // simple non\-repeating 5.5 second timeout
181\& ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); 194\& ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);
182\& ev_timer_start (loop, &timeout_watcher); 195\& ev_timer_start (loop, &timeout_watcher);
183\& 196\&
184\& /* loop till timeout or data ready */ 197\& // now wait for events to arrive
185\& ev_loop (loop, 0); 198\& ev_loop (loop, 0);
186\& 199\&
200\& // unloop was called, so exit
187\& return 0; 201\& return 0;
188\& } 202\& }
189.Ve 203.Ve
190.SH "DESCRIPTION" 204.SH "DESCRIPTION"
191.IX Header "DESCRIPTION" 205.IX Header "DESCRIPTION"
192The newest version of this document is also available as a html-formatted 206The newest version of this document is also available as an html-formatted
193web page you might find easier to navigate when reading it for the first 207web page you might find easier to navigate when reading it for the first
194time: <http://cvs.schmorp.de/libev/ev.html>. 208time: <http://cvs.schmorp.de/libev/ev.html>.
195.PP 209.PP
196Libev is an event loop: you register interest in certain events (such as a 210Libev is an event loop: you register interest in certain events (such as a
197file descriptor being readable or a timeout occurring), and it will manage 211file descriptor being readable or a timeout occurring), and it will manage
221It also is quite fast (see this 235It also is quite fast (see this
222benchmark comparing it to libevent 236benchmark comparing it to libevent
223for example). 237for example).
224.Sh "\s-1CONVENTIONS\s0" 238.Sh "\s-1CONVENTIONS\s0"
225.IX Subsection "CONVENTIONS" 239.IX Subsection "CONVENTIONS"
226Libev is very configurable. In this manual the default configuration will 240Libev is very configurable. In this manual the default (and most common)
227be described, which supports multiple event loops. For more info about 241configuration will be described, which supports multiple event loops. For
228various configuration options please have a look at \fB\s-1EMBED\s0\fR section in 242more info about various configuration options please have a look at
229this manual. If libev was configured without support for multiple event 243\&\fB\s-1EMBED\s0\fR section in this manual. If libev was configured without support
230loops, then all functions taking an initial argument of name \f(CW\*(C`loop\*(C'\fR 244for multiple event loops, then all functions taking an initial argument of
231(which is always of type \f(CW\*(C`struct ev_loop *\*(C'\fR) will not have this argument. 245name \f(CW\*(C`loop\*(C'\fR (which is always of type \f(CW\*(C`struct ev_loop *\*(C'\fR) will not have
246this argument.
232.Sh "\s-1TIME\s0 \s-1REPRESENTATION\s0" 247.Sh "\s-1TIME\s0 \s-1REPRESENTATION\s0"
233.IX Subsection "TIME REPRESENTATION" 248.IX Subsection "TIME REPRESENTATION"
234Libev represents time as a single floating point number, representing the 249Libev represents time as a single floating point number, representing the
235(fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near 250(fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near
236the beginning of 1970, details are complicated, don't ask). This type is 251the beginning of 1970, details are complicated, don't ask). This type is
389flags. If that is troubling you, check \f(CW\*(C`ev_backend ()\*(C'\fR afterwards). 404flags. If that is troubling you, check \f(CW\*(C`ev_backend ()\*(C'\fR afterwards).
390.Sp 405.Sp
391If you don't know what event loop to use, use the one returned from this 406If you don't know what event loop to use, use the one returned from this
392function. 407function.
393.Sp 408.Sp
409Note that this function is \fInot\fR thread-safe, so if you want to use it
410from multiple threads, you have to lock (note also that this is unlikely,
411as loops cannot bes hared easily between threads anyway).
412.Sp
394The default loop is the only loop that can handle \f(CW\*(C`ev_signal\*(C'\fR and 413The default loop is the only loop that can handle \f(CW\*(C`ev_signal\*(C'\fR and
395\&\f(CW\*(C`ev_child\*(C'\fR watchers, and to do this, it always registers a handler 414\&\f(CW\*(C`ev_child\*(C'\fR watchers, and to do this, it always registers a handler
396for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is a problem for your app you can either 415for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is a problem for your app you can either
397create a dynamic loop with \f(CW\*(C`ev_loop_new\*(C'\fR that doesn't do that, or you 416create a dynamic loop with \f(CW\*(C`ev_loop_new\*(C'\fR that doesn't do that, or you
398can simply overwrite the \f(CW\*(C`SIGCHLD\*(C'\fR signal handler \fIafter\fR calling 417can simply overwrite the \f(CW\*(C`SIGCHLD\*(C'\fR signal handler \fIafter\fR calling
425enabling this flag. 444enabling this flag.
426.Sp 445.Sp
427This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop, 446This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop,
428and thus this might slow down your event loop if you do a lot of loop 447and thus this might slow down your event loop if you do a lot of loop
429iterations and little real work, but is usually not noticeable (on my 448iterations and little real work, but is usually not noticeable (on my
430Linux system for example, \f(CW\*(C`getpid\*(C'\fR is actually a simple 5\-insn sequence 449GNU/Linux system for example, \f(CW\*(C`getpid\*(C'\fR is actually a simple 5\-insn sequence
431without a syscall and thus \fIvery\fR fast, but my Linux system also has 450without a syscall and thus \fIvery\fR fast, but my GNU/Linux system also has
432\&\f(CW\*(C`pthread_atfork\*(C'\fR which is even faster). 451\&\f(CW\*(C`pthread_atfork\*(C'\fR which is even faster).
433.Sp 452.Sp
434The big advantage of this flag is that you can forget about fork (and 453The big advantage of this flag is that you can forget about fork (and
435forget about forgetting to tell libev about forking) when you use this 454forget about forgetting to tell libev about forking) when you use this
436flag. 455flag.
585Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is 604Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is
586always distinct from the default loop. Unlike the default loop, it cannot 605always distinct from the default loop. Unlike the default loop, it cannot
587handle signal and child watchers, and attempts to do so will be greeted by 606handle signal and child watchers, and attempts to do so will be greeted by
588undefined behaviour (or a failed assertion if assertions are enabled). 607undefined behaviour (or a failed assertion if assertions are enabled).
589.Sp 608.Sp
609Note that this function \fIis\fR thread-safe, and the recommended way to use
610libev with threads is indeed to create one loop per thread, and using the
611default loop in the \*(L"main\*(R" or \*(L"initial\*(R" thread.
612.Sp
590Example: Try to create a event loop that uses epoll and nothing else. 613Example: Try to create a event loop that uses epoll and nothing else.
591.Sp 614.Sp
592.Vb 3 615.Vb 3
593\& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); 616\& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV);
594\& if (!epoller) 617\& if (!epoller)
639.IP "ev_loop_fork (loop)" 4 662.IP "ev_loop_fork (loop)" 4
640.IX Item "ev_loop_fork (loop)" 663.IX Item "ev_loop_fork (loop)"
641Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by 664Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by
642\&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop 665\&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop
643after fork, and how you do this is entirely your own problem. 666after fork, and how you do this is entirely your own problem.
667.IP "int ev_is_default_loop (loop)" 4
668.IX Item "int ev_is_default_loop (loop)"
669Returns true when the given loop actually is the default loop, false otherwise.
644.IP "unsigned int ev_loop_count (loop)" 4 670.IP "unsigned int ev_loop_count (loop)" 4
645.IX Item "unsigned int ev_loop_count (loop)" 671.IX Item "unsigned int ev_loop_count (loop)"
646Returns the count of loop iterations for the loop, which is identical to 672Returns the count of loop iterations for the loop, which is identical to
647the number of times libev did poll for new events. It starts at \f(CW0\fR and 673the number of times libev did poll for new events. It starts at \f(CW0\fR and
648happily wraps around with enough iterations. 674happily wraps around with enough iterations.
914.ie n .IP """EV_FORK""" 4 940.ie n .IP """EV_FORK""" 4
915.el .IP "\f(CWEV_FORK\fR" 4 941.el .IP "\f(CWEV_FORK\fR" 4
916.IX Item "EV_FORK" 942.IX Item "EV_FORK"
917The event loop has been resumed in the child process after fork (see 943The event loop has been resumed in the child process after fork (see
918\&\f(CW\*(C`ev_fork\*(C'\fR). 944\&\f(CW\*(C`ev_fork\*(C'\fR).
945.ie n .IP """EV_ASYNC""" 4
946.el .IP "\f(CWEV_ASYNC\fR" 4
947.IX Item "EV_ASYNC"
948The given async watcher has been asynchronously notified (see \f(CW\*(C`ev_async\*(C'\fR).
919.ie n .IP """EV_ERROR""" 4 949.ie n .IP """EV_ERROR""" 4
920.el .IP "\f(CWEV_ERROR\fR" 4 950.el .IP "\f(CWEV_ERROR\fR" 4
921.IX Item "EV_ERROR" 951.IX Item "EV_ERROR"
922An unspecified error has occured, the watcher has been stopped. This might 952An unspecified error has occured, the watcher has been stopped. This might
923happen because the watcher could not be properly started because libev 953happen because the watcher could not be properly started because libev
1197To support fork in your programs, you either have to call 1227To support fork in your programs, you either have to call
1198\&\f(CW\*(C`ev_default_fork ()\*(C'\fR or \f(CW\*(C`ev_loop_fork ()\*(C'\fR after a fork in the child, 1228\&\f(CW\*(C`ev_default_fork ()\*(C'\fR or \f(CW\*(C`ev_loop_fork ()\*(C'\fR after a fork in the child,
1199enable \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR, or resort to \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or 1229enable \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR, or resort to \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or
1200\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. 1230\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR.
1201.PP 1231.PP
1232\fIThe special problem of \s-1SIGPIPE\s0\fR
1233.IX Subsection "The special problem of SIGPIPE"
1234.PP
1235While not really specific to libev, it is easy to forget about \s-1SIGPIPE:\s0
1236when reading from a pipe whose other end has been closed, your program
1237gets send a \s-1SIGPIPE\s0, which, by default, aborts your program. For most
1238programs this is sensible behaviour, for daemons, this is usually
1239undesirable.
1240.PP
1241So when you encounter spurious, unexplained daemon exits, make sure you
1242ignore \s-1SIGPIPE\s0 (and maybe make sure you log the exit status of your daemon
1243somewhere, as that would have given you a big clue).
1244.PP
1202\fIWatcher-Specific Functions\fR 1245\fIWatcher-Specific Functions\fR
1203.IX Subsection "Watcher-Specific Functions" 1246.IX Subsection "Watcher-Specific Functions"
1204.IP "ev_io_init (ev_io *, callback, int fd, int events)" 4 1247.IP "ev_io_init (ev_io *, callback, int fd, int events)" 4
1205.IX Item "ev_io_init (ev_io *, callback, int fd, int events)" 1248.IX Item "ev_io_init (ev_io *, callback, int fd, int events)"
1206.PD 0 1249.PD 0
1281The timer itself will do a best-effort at avoiding drift, that is, if you 1324The timer itself will do a best-effort at avoiding drift, that is, if you
1282configure a timer to trigger every 10 seconds, then it will trigger at 1325configure a timer to trigger every 10 seconds, then it will trigger at
1283exactly 10 second intervals. If, however, your program cannot keep up with 1326exactly 10 second intervals. If, however, your program cannot keep up with
1284the timer (because it takes longer than those 10 seconds to do stuff) the 1327the timer (because it takes longer than those 10 seconds to do stuff) the
1285timer will not fire more than once per event loop iteration. 1328timer will not fire more than once per event loop iteration.
1286.IP "ev_timer_again (loop)" 4 1329.IP "ev_timer_again (loop, ev_timer *)" 4
1287.IX Item "ev_timer_again (loop)" 1330.IX Item "ev_timer_again (loop, ev_timer *)"
1288This will act as if the timer timed out and restart it again if it is 1331This will act as if the timer timed out and restart it again if it is
1289repeating. The exact semantics are: 1332repeating. The exact semantics are:
1290.Sp 1333.Sp
1291If the timer is pending, its pending status is cleared. 1334If the timer is pending, its pending status is cleared.
1292.Sp 1335.Sp
1402In this configuration the watcher triggers an event at the wallclock time 1445In this configuration the watcher triggers an event at the wallclock time
1403\&\f(CW\*(C`at\*(C'\fR and doesn't repeat. It will not adjust when a time jump occurs, 1446\&\f(CW\*(C`at\*(C'\fR and doesn't repeat. It will not adjust when a time jump occurs,
1404that is, if it is to be run at January 1st 2011 then it will run when the 1447that is, if it is to be run at January 1st 2011 then it will run when the
1405system time reaches or surpasses this time. 1448system time reaches or surpasses this time.
1406.IP "\(bu" 4 1449.IP "\(bu" 4
1407non-repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) 1450repeating interval timer (at = offset, interval > 0, reschedule_cb = 0)
1408.Sp 1451.Sp
1409In this mode the watcher will always be scheduled to time out at the next 1452In this mode the watcher will always be scheduled to time out at the next
1410\&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative) 1453\&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative)
1411and then repeat, regardless of any time jumps. 1454and then repeat, regardless of any time jumps.
1412.Sp 1455.Sp
1550with the kernel (thus it coexists with your own signal handlers as long 1593with the kernel (thus it coexists with your own signal handlers as long
1551as you don't register any with libev). Similarly, when the last signal 1594as you don't register any with libev). Similarly, when the last signal
1552watcher for a signal is stopped libev will reset the signal handler to 1595watcher for a signal is stopped libev will reset the signal handler to
1553\&\s-1SIG_DFL\s0 (regardless of what it was set to before). 1596\&\s-1SIG_DFL\s0 (regardless of what it was set to before).
1554.PP 1597.PP
1598If possible and supported, libev will install its handlers with
1599\&\f(CW\*(C`SA_RESTART\*(C'\fR behaviour enabled, so syscalls should not be unduly
1600interrupted. If you have a problem with syscalls getting interrupted by
1601signals you can block all signals in an \f(CW\*(C`ev_check\*(C'\fR watcher and unblock
1602them in an \f(CW\*(C`ev_prepare\*(C'\fR watcher.
1603.PP
1555\fIWatcher-Specific Functions and Data Members\fR 1604\fIWatcher-Specific Functions and Data Members\fR
1556.IX Subsection "Watcher-Specific Functions and Data Members" 1605.IX Subsection "Watcher-Specific Functions and Data Members"
1557.IP "ev_signal_init (ev_signal *, callback, int signum)" 4 1606.IP "ev_signal_init (ev_signal *, callback, int signum)" 4
1558.IX Item "ev_signal_init (ev_signal *, callback, int signum)" 1607.IX Item "ev_signal_init (ev_signal *, callback, int signum)"
1559.PD 0 1608.PD 0
1563Configures the watcher to trigger on the given signal number (usually one 1612Configures the watcher to trigger on the given signal number (usually one
1564of the \f(CW\*(C`SIGxxx\*(C'\fR constants). 1613of the \f(CW\*(C`SIGxxx\*(C'\fR constants).
1565.IP "int signum [read\-only]" 4 1614.IP "int signum [read\-only]" 4
1566.IX Item "int signum [read-only]" 1615.IX Item "int signum [read-only]"
1567The signal the watcher watches out for. 1616The signal the watcher watches out for.
1617.PP
1618\fIExamples\fR
1619.IX Subsection "Examples"
1620.PP
1621Example: Try to exit cleanly on \s-1SIGINT\s0 and \s-1SIGTERM\s0.
1622.PP
1623.Vb 5
1624\& static void
1625\& sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents)
1626\& {
1627\& ev_unloop (loop, EVUNLOOP_ALL);
1628\& }
1629\&
1630\& struct ev_signal signal_watcher;
1631\& ev_signal_init (&signal_watcher, sigint_cb, SIGINT);
1632\& ev_signal_start (loop, &sigint_cb);
1633.Ve
1568.ie n .Sh """ev_child"" \- watch out for process status changes" 1634.ie n .Sh """ev_child"" \- watch out for process status changes"
1569.el .Sh "\f(CWev_child\fP \- watch out for process status changes" 1635.el .Sh "\f(CWev_child\fP \- watch out for process status changes"
1570.IX Subsection "ev_child - watch out for process status changes" 1636.IX Subsection "ev_child - watch out for process status changes"
1571Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to 1637Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to
1572some child status changes (most typically when a child of yours dies). 1638some child status changes (most typically when a child of yours dies). It
1639is permissible to install a child watcher \fIafter\fR the child has been
1640forked (which implies it might have already exited), as long as the event
1641loop isn't entered (or is continued from a watcher).
1642.PP
1643Only the default event loop is capable of handling signals, and therefore
1644you can only rgeister child watchers in the default event loop.
1645.PP
1646\fIProcess Interaction\fR
1647.IX Subsection "Process Interaction"
1648.PP
1649Libev grabs \f(CW\*(C`SIGCHLD\*(C'\fR as soon as the default event loop is
1650initialised. This is necessary to guarantee proper behaviour even if
1651the first child watcher is started after the child exits. The occurance
1652of \f(CW\*(C`SIGCHLD\*(C'\fR is recorded asynchronously, but child reaping is done
1653synchronously as part of the event loop processing. Libev always reaps all
1654children, even ones not watched.
1655.PP
1656\fIOverriding the Built-In Processing\fR
1657.IX Subsection "Overriding the Built-In Processing"
1658.PP
1659Libev offers no special support for overriding the built-in child
1660processing, but if your application collides with libev's default child
1661handler, you can override it easily by installing your own handler for
1662\&\f(CW\*(C`SIGCHLD\*(C'\fR after initialising the default loop, and making sure the
1663default loop never gets destroyed. You are encouraged, however, to use an
1664event-based approach to child reaping and thus use libev's support for
1665that, so other libev users can use \f(CW\*(C`ev_child\*(C'\fR watchers freely.
1573.PP 1666.PP
1574\fIWatcher-Specific Functions and Data Members\fR 1667\fIWatcher-Specific Functions and Data Members\fR
1575.IX Subsection "Watcher-Specific Functions and Data Members" 1668.IX Subsection "Watcher-Specific Functions and Data Members"
1576.IP "ev_child_init (ev_child *, callback, int pid, int trace)" 4 1669.IP "ev_child_init (ev_child *, callback, int pid, int trace)" 4
1577.IX Item "ev_child_init (ev_child *, callback, int pid, int trace)" 1670.IX Item "ev_child_init (ev_child *, callback, int pid, int trace)"
1599\&\f(CW\*(C`waitpid\*(C'\fR and \f(CW\*(C`sys/wait.h\*(C'\fR documentation for details). 1692\&\f(CW\*(C`waitpid\*(C'\fR and \f(CW\*(C`sys/wait.h\*(C'\fR documentation for details).
1600.PP 1693.PP
1601\fIExamples\fR 1694\fIExamples\fR
1602.IX Subsection "Examples" 1695.IX Subsection "Examples"
1603.PP 1696.PP
1604Example: Try to exit cleanly on \s-1SIGINT\s0 and \s-1SIGTERM\s0. 1697Example: \f(CW\*(C`fork()\*(C'\fR a new process and install a child handler to wait for
1698its completion.
1605.PP 1699.PP
1606.Vb 5 1700.Vb 1
1701\& ev_child cw;
1702\&
1607\& static void 1703\& static void
1608\& sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) 1704\& child_cb (EV_P_ struct ev_child *w, int revents)
1609\& { 1705\& {
1610\& ev_unloop (loop, EVUNLOOP_ALL); 1706\& ev_child_stop (EV_A_ w);
1707\& printf ("process %d exited with status %x\en", w\->rpid, w\->rstatus);
1611\& } 1708\& }
1612\& 1709\&
1613\& struct ev_signal signal_watcher; 1710\& pid_t pid = fork ();
1614\& ev_signal_init (&signal_watcher, sigint_cb, SIGINT); 1711\&
1615\& ev_signal_start (loop, &sigint_cb); 1712\& if (pid < 0)
1713\& // error
1714\& else if (pid == 0)
1715\& {
1716\& // the forked child executes here
1717\& exit (1);
1718\& }
1719\& else
1720\& {
1721\& ev_child_init (&cw, child_cb, pid, 0);
1722\& ev_child_start (EV_DEFAULT_ &cw);
1723\& }
1616.Ve 1724.Ve
1617.ie n .Sh """ev_stat"" \- did the file attributes just change?" 1725.ie n .Sh """ev_stat"" \- did the file attributes just change?"
1618.el .Sh "\f(CWev_stat\fP \- did the file attributes just change?" 1726.el .Sh "\f(CWev_stat\fP \- did the file attributes just change?"
1619.IX Subsection "ev_stat - did the file attributes just change?" 1727.IX Subsection "ev_stat - did the file attributes just change?"
1620This watches a filesystem path for attribute changes. That is, it calls 1728This watches a filesystem path for attribute changes. That is, it calls
1648reader). Inotify will be used to give hints only and should not change the 1756reader). Inotify will be used to give hints only and should not change the
1649semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev sometimes needs 1757semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev sometimes needs
1650to fall back to regular polling again even with inotify, but changes are 1758to fall back to regular polling again even with inotify, but changes are
1651usually detected immediately, and if the file exists there will be no 1759usually detected immediately, and if the file exists there will be no
1652polling. 1760polling.
1761.PP
1762\fI\s-1ABI\s0 Issues (Largefile Support)\fR
1763.IX Subsection "ABI Issues (Largefile Support)"
1764.PP
1765Libev by default (unless the user overrides this) uses the default
1766compilation environment, which means that on systems with optionally
1767disabled large file support, you get the 32 bit version of the stat
1768structure. When using the library from programs that change the \s-1ABI\s0 to
1769use 64 bit file offsets the programs will fail. In that case you have to
1770compile libev with the same flags to get binary compatibility. This is
1771obviously the case with any flags that change the \s-1ABI\s0, but the problem is
1772most noticably with ev_stat and largefile support.
1653.PP 1773.PP
1654\fIInotify\fR 1774\fIInotify\fR
1655.IX Subsection "Inotify" 1775.IX Subsection "Inotify"
1656.PP 1776.PP
1657When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only 1777When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only
1701path for as long as the watcher is active. 1821path for as long as the watcher is active.
1702.Sp 1822.Sp
1703The callback will be receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected, 1823The callback will be receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected,
1704relative to the attributes at the time the watcher was started (or the 1824relative to the attributes at the time the watcher was started (or the
1705last change was detected). 1825last change was detected).
1706.IP "ev_stat_stat (ev_stat *)" 4 1826.IP "ev_stat_stat (loop, ev_stat *)" 4
1707.IX Item "ev_stat_stat (ev_stat *)" 1827.IX Item "ev_stat_stat (loop, ev_stat *)"
1708Updates the stat buffer immediately with new values. If you change the 1828Updates the stat buffer immediately with new values. If you change the
1709watched path in your callback, you could call this fucntion to avoid 1829watched path in your callback, you could call this fucntion to avoid
1710detecting this change (while introducing a race condition). Can also be 1830detecting this change (while introducing a race condition). Can also be
1711useful simply to find out the new values. 1831useful simply to find out the new values.
1712.IP "ev_statdata attr [read\-only]" 4 1832.IP "ev_statdata attr [read\-only]" 4
2173.IP "ev_fork_init (ev_signal *, callback)" 4 2293.IP "ev_fork_init (ev_signal *, callback)" 4
2174.IX Item "ev_fork_init (ev_signal *, callback)" 2294.IX Item "ev_fork_init (ev_signal *, callback)"
2175Initialises and configures the fork watcher \- it has no parameters of any 2295Initialises and configures the fork watcher \- it has no parameters of any
2176kind. There is a \f(CW\*(C`ev_fork_set\*(C'\fR macro, but using it is utterly pointless, 2296kind. There is a \f(CW\*(C`ev_fork_set\*(C'\fR macro, but using it is utterly pointless,
2177believe me. 2297believe me.
2298.ie n .Sh """ev_async"" \- how to wake up another event loop"
2299.el .Sh "\f(CWev_async\fP \- how to wake up another event loop"
2300.IX Subsection "ev_async - how to wake up another event loop"
2301In general, you cannot use an \f(CW\*(C`ev_loop\*(C'\fR from multiple threads or other
2302asynchronous sources such as signal handlers (as opposed to multiple event
2303loops \- those are of course safe to use in different threads).
2304.PP
2305Sometimes, however, you need to wake up another event loop you do not
2306control, for example because it belongs to another thread. This is what
2307\&\f(CW\*(C`ev_async\*(C'\fR watchers do: as long as the \f(CW\*(C`ev_async\*(C'\fR watcher is active, you
2308can signal it by calling \f(CW\*(C`ev_async_send\*(C'\fR, which is thread\- and signal
2309safe.
2310.PP
2311This functionality is very similar to \f(CW\*(C`ev_signal\*(C'\fR watchers, as signals,
2312too, are asynchronous in nature, and signals, too, will be compressed
2313(i.e. the number of callback invocations may be less than the number of
2314\&\f(CW\*(C`ev_async_sent\*(C'\fR calls).
2315.PP
2316Unlike \f(CW\*(C`ev_signal\*(C'\fR watchers, \f(CW\*(C`ev_async\*(C'\fR works with any event loop, not
2317just the default loop.
2318.PP
2319\fIQueueing\fR
2320.IX Subsection "Queueing"
2321.PP
2322\&\f(CW\*(C`ev_async\*(C'\fR does not support queueing of data in any way. The reason
2323is that the author does not know of a simple (or any) algorithm for a
2324multiple-writer-single-reader queue that works in all cases and doesn't
2325need elaborate support such as pthreads.
2326.PP
2327That means that if you want to queue data, you have to provide your own
2328queue. But at least I can tell you would implement locking around your
2329queue:
2330.IP "queueing from a signal handler context" 4
2331.IX Item "queueing from a signal handler context"
2332To implement race-free queueing, you simply add to the queue in the signal
2333handler but you block the signal handler in the watcher callback. Here is an example that does that for
2334some fictitiuous \s-1SIGUSR1\s0 handler:
2335.Sp
2336.Vb 1
2337\& static ev_async mysig;
2338\&
2339\& static void
2340\& sigusr1_handler (void)
2341\& {
2342\& sometype data;
2343\&
2344\& // no locking etc.
2345\& queue_put (data);
2346\& ev_async_send (EV_DEFAULT_ &mysig);
2347\& }
2348\&
2349\& static void
2350\& mysig_cb (EV_P_ ev_async *w, int revents)
2351\& {
2352\& sometype data;
2353\& sigset_t block, prev;
2354\&
2355\& sigemptyset (&block);
2356\& sigaddset (&block, SIGUSR1);
2357\& sigprocmask (SIG_BLOCK, &block, &prev);
2358\&
2359\& while (queue_get (&data))
2360\& process (data);
2361\&
2362\& if (sigismember (&prev, SIGUSR1)
2363\& sigprocmask (SIG_UNBLOCK, &block, 0);
2364\& }
2365.Ve
2366.Sp
2367(Note: pthreads in theory requires you to use \f(CW\*(C`pthread_setmask\*(C'\fR
2368instead of \f(CW\*(C`sigprocmask\*(C'\fR when you use threads, but libev doesn't do it
2369either...).
2370.IP "queueing from a thread context" 4
2371.IX Item "queueing from a thread context"
2372The strategy for threads is different, as you cannot (easily) block
2373threads but you can easily preempt them, so to queue safely you need to
2374employ a traditional mutex lock, such as in this pthread example:
2375.Sp
2376.Vb 2
2377\& static ev_async mysig;
2378\& static pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
2379\&
2380\& static void
2381\& otherthread (void)
2382\& {
2383\& // only need to lock the actual queueing operation
2384\& pthread_mutex_lock (&mymutex);
2385\& queue_put (data);
2386\& pthread_mutex_unlock (&mymutex);
2387\&
2388\& ev_async_send (EV_DEFAULT_ &mysig);
2389\& }
2390\&
2391\& static void
2392\& mysig_cb (EV_P_ ev_async *w, int revents)
2393\& {
2394\& pthread_mutex_lock (&mymutex);
2395\&
2396\& while (queue_get (&data))
2397\& process (data);
2398\&
2399\& pthread_mutex_unlock (&mymutex);
2400\& }
2401.Ve
2402.PP
2403\fIWatcher-Specific Functions and Data Members\fR
2404.IX Subsection "Watcher-Specific Functions and Data Members"
2405.IP "ev_async_init (ev_async *, callback)" 4
2406.IX Item "ev_async_init (ev_async *, callback)"
2407Initialises and configures the async watcher \- it has no parameters of any
2408kind. There is a \f(CW\*(C`ev_asynd_set\*(C'\fR macro, but using it is utterly pointless,
2409believe me.
2410.IP "ev_async_send (loop, ev_async *)" 4
2411.IX Item "ev_async_send (loop, ev_async *)"
2412Sends/signals/activates the given \f(CW\*(C`ev_async\*(C'\fR watcher, that is, feeds
2413an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike
2414\&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do in other threads, signal or
2415similar contexts (see the dicusssion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding
2416section below on what exactly this means).
2417.Sp
2418This call incurs the overhead of a syscall only once per loop iteration,
2419so while the overhead might be noticable, it doesn't apply to repeated
2420calls to \f(CW\*(C`ev_async_send\*(C'\fR.
2421.IP "bool = ev_async_pending (ev_async *)" 4
2422.IX Item "bool = ev_async_pending (ev_async *)"
2423Returns a non-zero value when \f(CW\*(C`ev_async_send\*(C'\fR has been called on the
2424watcher but the event has not yet been processed (or even noted) by the
2425event loop.
2426.Sp
2427\&\f(CW\*(C`ev_async_send\*(C'\fR sets a flag in the watcher and wakes up the loop. When
2428the loop iterates next and checks for the watcher to have become active,
2429it will reset the flag again. \f(CW\*(C`ev_async_pending\*(C'\fR can be used to very
2430quickly check wether invoking the loop might be a good idea.
2431.Sp
2432Not that this does \fInot\fR check wether the watcher itself is pending, only
2433wether it has been requested to make this watcher pending.
2178.SH "OTHER FUNCTIONS" 2434.SH "OTHER FUNCTIONS"
2179.IX Header "OTHER FUNCTIONS" 2435.IX Header "OTHER FUNCTIONS"
2180There are some other functions of possible interest. Described. Here. Now. 2436There are some other functions of possible interest. Described. Here. Now.
2181.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 2437.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4
2182.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 2438.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)"
2406\& 2662\&
2407\& io.start (fd, ev::READ); 2663\& io.start (fd, ev::READ);
2408\& } 2664\& }
2409\& }; 2665\& };
2410.Ve 2666.Ve
2667.SH "OTHER LANGUAGE BINDINGS"
2668.IX Header "OTHER LANGUAGE BINDINGS"
2669Libev does not offer other language bindings itself, but bindings for a
2670numbe rof languages exist in the form of third-party packages. If you know
2671any interesting language binding in addition to the ones listed here, drop
2672me a note.
2673.IP "Perl" 4
2674.IX Item "Perl"
2675The \s-1EV\s0 module implements the full libev \s-1API\s0 and is actually used to test
2676libev. \s-1EV\s0 is developed together with libev. Apart from the \s-1EV\s0 core module,
2677there are additional modules that implement libev-compatible interfaces
2678to \f(CW\*(C`libadns\*(C'\fR (\f(CW\*(C`EV::ADNS\*(C'\fR), \f(CW\*(C`Net::SNMP\*(C'\fR (\f(CW\*(C`Net::SNMP::EV\*(C'\fR) and the
2679\&\f(CW\*(C`libglib\*(C'\fR event core (\f(CW\*(C`Glib::EV\*(C'\fR and \f(CW\*(C`EV::Glib\*(C'\fR).
2680.Sp
2681It can be found and installed via \s-1CPAN\s0, its homepage is found at
2682<http://software.schmorp.de/pkg/EV>.
2683.IP "Ruby" 4
2684.IX Item "Ruby"
2685Tony Arcieri has written a ruby extension that offers access to a subset
2686of the libev \s-1API\s0 and adds filehandle abstractions, asynchronous \s-1DNS\s0 and
2687more on top of it. It can be found via gem servers. Its homepage is at
2688<http://rev.rubyforge.org/>.
2689.IP "D" 4
2690.IX Item "D"
2691Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to
2692be found at <http://git.llucax.com.ar/?p=software/ev.d.git;a=summary>.
2411.SH "MACRO MAGIC" 2693.SH "MACRO MAGIC"
2412.IX Header "MACRO MAGIC" 2694.IX Header "MACRO MAGIC"
2413Libev can be compiled with a variety of options, the most fundamantal 2695Libev can be compiled with a variety of options, the most fundamantal
2414of which is \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most) 2696of which is \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most)
2415functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument. 2697functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument.
2668.IP "\s-1EV_USE_INOTIFY\s0" 4 2950.IP "\s-1EV_USE_INOTIFY\s0" 4
2669.IX Item "EV_USE_INOTIFY" 2951.IX Item "EV_USE_INOTIFY"
2670If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify 2952If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify
2671interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will 2953interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will
2672be detected at runtime. 2954be detected at runtime.
2955.IP "\s-1EV_ATOMIC_T\s0" 4
2956.IX Item "EV_ATOMIC_T"
2957Libev requires an integer type (suitable for storing \f(CW0\fR or \f(CW1\fR) whose
2958access is atomic with respect to other threads or signal contexts. No such
2959type is easily found in the C language, so you can provide your own type
2960that you know is safe for your purposes. It is used both for signal handler \*(L"locking\*(R"
2961as well as for signal and thread safety in \f(CW\*(C`ev_async\*(C'\fR watchers.
2962.Sp
2963In the absense of this define, libev will use \f(CW\*(C`sig_atomic_t volatile\*(C'\fR
2964(from \fIsignal.h\fR), which is usually good enough on most platforms.
2673.IP "\s-1EV_H\s0" 4 2965.IP "\s-1EV_H\s0" 4
2674.IX Item "EV_H" 2966.IX Item "EV_H"
2675The name of the \fIev.h\fR header file used to include it. The default if 2967The name of the \fIev.h\fR header file used to include it. The default if
2676undefined is \f(CW"ev.h"\fR in \fIevent.h\fR, \fIev.c\fR and \fIev++.h\fR. This can be 2968undefined is \f(CW"ev.h"\fR in \fIevent.h\fR, \fIev.c\fR and \fIev++.h\fR. This can be
2677used to virtually rename the \fIev.h\fR header file in case of conflicts. 2969used to virtually rename the \fIev.h\fR header file in case of conflicts.
2735defined to be \f(CW0\fR, then they are not. 3027defined to be \f(CW0\fR, then they are not.
2736.IP "\s-1EV_FORK_ENABLE\s0" 4 3028.IP "\s-1EV_FORK_ENABLE\s0" 4
2737.IX Item "EV_FORK_ENABLE" 3029.IX Item "EV_FORK_ENABLE"
2738If undefined or defined to be \f(CW1\fR, then fork watchers are supported. If 3030If undefined or defined to be \f(CW1\fR, then fork watchers are supported. If
2739defined to be \f(CW0\fR, then they are not. 3031defined to be \f(CW0\fR, then they are not.
3032.IP "\s-1EV_ASYNC_ENABLE\s0" 4
3033.IX Item "EV_ASYNC_ENABLE"
3034If undefined or defined to be \f(CW1\fR, then async watchers are supported. If
3035defined to be \f(CW0\fR, then they are not.
2740.IP "\s-1EV_MINIMAL\s0" 4 3036.IP "\s-1EV_MINIMAL\s0" 4
2741.IX Item "EV_MINIMAL" 3037.IX Item "EV_MINIMAL"
2742If you need to shave off some kilobytes of code at the expense of some 3038If you need to shave off some kilobytes of code at the expense of some
2743speed, define this symbol to \f(CW1\fR. Currently only used for gcc to override 3039speed, define this symbol to \f(CW1\fR. Currently only used for gcc to override
2744some inlining decisions, saves roughly 30% codesize of amd64. 3040some inlining decisions, saves roughly 30% codesize of amd64.
2864have to skip roughly seven (\f(CW\*(C`ld 100\*(C'\fR) of these watchers. 3160have to skip roughly seven (\f(CW\*(C`ld 100\*(C'\fR) of these watchers.
2865.IP "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" 4 3161.IP "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" 4
2866.IX Item "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" 3162.IX Item "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)"
2867That means that changing a timer costs less than removing/adding them 3163That means that changing a timer costs less than removing/adding them
2868as only the relative motion in the event queue has to be paid for. 3164as only the relative motion in the event queue has to be paid for.
2869.IP "Starting io/check/prepare/idle/signal/child watchers: O(1)" 4 3165.IP "Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)" 4
2870.IX Item "Starting io/check/prepare/idle/signal/child watchers: O(1)" 3166.IX Item "Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)"
2871These just add the watcher into an array or at the head of a list. 3167These just add the watcher into an array or at the head of a list.
2872.IP "Stopping check/prepare/idle watchers: O(1)" 4 3168.IP "Stopping check/prepare/idle/fork/async watchers: O(1)" 4
2873.IX Item "Stopping check/prepare/idle watchers: O(1)" 3169.IX Item "Stopping check/prepare/idle/fork/async watchers: O(1)"
2874.PD 0 3170.PD 0
2875.IP "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % \s-1EV_PID_HASHSIZE\s0))" 4 3171.IP "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % \s-1EV_PID_HASHSIZE\s0))" 4
2876.IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))" 3172.IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))"
2877.PD 3173.PD
2878These watchers are stored in lists then need to be walked to find the 3174These watchers are stored in lists then need to be walked to find the
2894.IX Item "Priority handling: O(number_of_priorities)" 3190.IX Item "Priority handling: O(number_of_priorities)"
2895.PD 3191.PD
2896Priorities are implemented by allocating some space for each 3192Priorities are implemented by allocating some space for each
2897priority. When doing priority-based operations, libev usually has to 3193priority. When doing priority-based operations, libev usually has to
2898linearly search all the priorities, but starting/stopping and activating 3194linearly search all the priorities, but starting/stopping and activating
2899watchers becomes O(1) w.r.t. prioritiy handling. 3195watchers becomes O(1) w.r.t. priority handling.
3196.IP "Sending an ev_async: O(1)" 4
3197.IX Item "Sending an ev_async: O(1)"
3198.PD 0
3199.IP "Processing ev_async_send: O(number_of_async_watchers)" 4
3200.IX Item "Processing ev_async_send: O(number_of_async_watchers)"
3201.IP "Processing signals: O(max_signal_number)" 4
3202.IX Item "Processing signals: O(max_signal_number)"
3203.PD
3204Sending involves a syscall \fIiff\fR there were no other \f(CW\*(C`ev_async_send\*(C'\fR
3205calls in the current loop iteration. Checking for async and signal events
3206involves iterating over all running async watchers or all signal numbers.
2900.SH "Win32 platform limitations and workarounds" 3207.SH "Win32 platform limitations and workarounds"
2901.IX Header "Win32 platform limitations and workarounds" 3208.IX Header "Win32 platform limitations and workarounds"
2902Win32 doesn't support any of the standards (e.g. \s-1POSIX\s0) that libev 3209Win32 doesn't support any of the standards (e.g. \s-1POSIX\s0) that libev
2903requires, and its I/O model is fundamentally incompatible with the \s-1POSIX\s0 3210requires, and its I/O model is fundamentally incompatible with the \s-1POSIX\s0
2904model. Libev still offers limited functionality on this platform in 3211model. Libev still offers limited functionality on this platform in
2962.IX Header "AUTHOR" 3269.IX Header "AUTHOR"
2963Marc Lehmann <libev@schmorp.de>. 3270Marc Lehmann <libev@schmorp.de>.
2964.SH "POD ERRORS" 3271.SH "POD ERRORS"
2965.IX Header "POD ERRORS" 3272.IX Header "POD ERRORS"
2966Hey! \fBThe above document had some coding errors, which are explained below:\fR 3273Hey! \fBThe above document had some coding errors, which are explained below:\fR
2967.IP "Around line 2686:" 4 3274.IP "Around line 2996:" 4
2968.IX Item "Around line 2686:" 3275.IX Item "Around line 2996:"
2969You forgot a '=back' before '=head2' 3276You forgot a '=back' before '=head2'

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines