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.62 by root, Sun Mar 16 16:38:23 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-03-13" "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
425enabling this flag. 440enabling this flag.
426.Sp 441.Sp
427This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop, 442This 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 443and 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 444iterations 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 445GNU/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 446without 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). 447\&\f(CW\*(C`pthread_atfork\*(C'\fR which is even faster).
433.Sp 448.Sp
434The big advantage of this flag is that you can forget about fork (and 449The 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 450forget about forgetting to tell libev about forking) when you use this
436flag. 451flag.
639.IP "ev_loop_fork (loop)" 4 654.IP "ev_loop_fork (loop)" 4
640.IX Item "ev_loop_fork (loop)" 655.IX Item "ev_loop_fork (loop)"
641Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by 656Like \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 657\&\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. 658after fork, and how you do this is entirely your own problem.
659.IP "int ev_is_default_loop (loop)" 4
660.IX Item "int ev_is_default_loop (loop)"
661Returns true when the given loop actually is the default loop, false otherwise.
644.IP "unsigned int ev_loop_count (loop)" 4 662.IP "unsigned int ev_loop_count (loop)" 4
645.IX Item "unsigned int ev_loop_count (loop)" 663.IX Item "unsigned int ev_loop_count (loop)"
646Returns the count of loop iterations for the loop, which is identical to 664Returns 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 665the number of times libev did poll for new events. It starts at \f(CW0\fR and
648happily wraps around with enough iterations. 666happily wraps around with enough iterations.
914.ie n .IP """EV_FORK""" 4 932.ie n .IP """EV_FORK""" 4
915.el .IP "\f(CWEV_FORK\fR" 4 933.el .IP "\f(CWEV_FORK\fR" 4
916.IX Item "EV_FORK" 934.IX Item "EV_FORK"
917The event loop has been resumed in the child process after fork (see 935The event loop has been resumed in the child process after fork (see
918\&\f(CW\*(C`ev_fork\*(C'\fR). 936\&\f(CW\*(C`ev_fork\*(C'\fR).
937.ie n .IP """EV_ASYNC""" 4
938.el .IP "\f(CWEV_ASYNC\fR" 4
939.IX Item "EV_ASYNC"
940The given async watcher has been asynchronously notified (see \f(CW\*(C`ev_async\*(C'\fR).
919.ie n .IP """EV_ERROR""" 4 941.ie n .IP """EV_ERROR""" 4
920.el .IP "\f(CWEV_ERROR\fR" 4 942.el .IP "\f(CWEV_ERROR\fR" 4
921.IX Item "EV_ERROR" 943.IX Item "EV_ERROR"
922An unspecified error has occured, the watcher has been stopped. This might 944An unspecified error has occured, the watcher has been stopped. This might
923happen because the watcher could not be properly started because libev 945happen because the watcher could not be properly started because libev
1281The timer itself will do a best-effort at avoiding drift, that is, if you 1303The 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 1304configure a timer to trigger every 10 seconds, then it will trigger at
1283exactly 10 second intervals. If, however, your program cannot keep up with 1305exactly 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 1306the timer (because it takes longer than those 10 seconds to do stuff) the
1285timer will not fire more than once per event loop iteration. 1307timer will not fire more than once per event loop iteration.
1286.IP "ev_timer_again (loop)" 4 1308.IP "ev_timer_again (loop, ev_timer *)" 4
1287.IX Item "ev_timer_again (loop)" 1309.IX Item "ev_timer_again (loop, ev_timer *)"
1288This will act as if the timer timed out and restart it again if it is 1310This will act as if the timer timed out and restart it again if it is
1289repeating. The exact semantics are: 1311repeating. The exact semantics are:
1290.Sp 1312.Sp
1291If the timer is pending, its pending status is cleared. 1313If the timer is pending, its pending status is cleared.
1292.Sp 1314.Sp
1402In this configuration the watcher triggers an event at the wallclock time 1424In 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, 1425\&\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 1426that is, if it is to be run at January 1st 2011 then it will run when the
1405system time reaches or surpasses this time. 1427system time reaches or surpasses this time.
1406.IP "\(bu" 4 1428.IP "\(bu" 4
1407non-repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) 1429repeating interval timer (at = offset, interval > 0, reschedule_cb = 0)
1408.Sp 1430.Sp
1409In this mode the watcher will always be scheduled to time out at the next 1431In 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) 1432\&\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. 1433and then repeat, regardless of any time jumps.
1412.Sp 1434.Sp
1550with the kernel (thus it coexists with your own signal handlers as long 1572with 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 1573as 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 1574watcher 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). 1575\&\s-1SIG_DFL\s0 (regardless of what it was set to before).
1554.PP 1576.PP
1577If possible and supported, libev will install its handlers with
1578\&\f(CW\*(C`SA_RESTART\*(C'\fR behaviour enabled, so syscalls should not be unduly
1579interrupted. If you have a problem with syscalls getting interrupted by
1580signals you can block all signals in an \f(CW\*(C`ev_check\*(C'\fR watcher and unblock
1581them in an \f(CW\*(C`ev_prepare\*(C'\fR watcher.
1582.PP
1555\fIWatcher-Specific Functions and Data Members\fR 1583\fIWatcher-Specific Functions and Data Members\fR
1556.IX Subsection "Watcher-Specific Functions and Data Members" 1584.IX Subsection "Watcher-Specific Functions and Data Members"
1557.IP "ev_signal_init (ev_signal *, callback, int signum)" 4 1585.IP "ev_signal_init (ev_signal *, callback, int signum)" 4
1558.IX Item "ev_signal_init (ev_signal *, callback, int signum)" 1586.IX Item "ev_signal_init (ev_signal *, callback, int signum)"
1559.PD 0 1587.PD 0
1563Configures the watcher to trigger on the given signal number (usually one 1591Configures the watcher to trigger on the given signal number (usually one
1564of the \f(CW\*(C`SIGxxx\*(C'\fR constants). 1592of the \f(CW\*(C`SIGxxx\*(C'\fR constants).
1565.IP "int signum [read\-only]" 4 1593.IP "int signum [read\-only]" 4
1566.IX Item "int signum [read-only]" 1594.IX Item "int signum [read-only]"
1567The signal the watcher watches out for. 1595The signal the watcher watches out for.
1596.PP
1597\fIExamples\fR
1598.IX Subsection "Examples"
1599.PP
1600Example: Try to exit cleanly on \s-1SIGINT\s0 and \s-1SIGTERM\s0.
1601.PP
1602.Vb 5
1603\& static void
1604\& sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents)
1605\& {
1606\& ev_unloop (loop, EVUNLOOP_ALL);
1607\& }
1608\&
1609\& struct ev_signal signal_watcher;
1610\& ev_signal_init (&signal_watcher, sigint_cb, SIGINT);
1611\& ev_signal_start (loop, &sigint_cb);
1612.Ve
1568.ie n .Sh """ev_child"" \- watch out for process status changes" 1613.ie n .Sh """ev_child"" \- watch out for process status changes"
1569.el .Sh "\f(CWev_child\fP \- watch out for process status changes" 1614.el .Sh "\f(CWev_child\fP \- watch out for process status changes"
1570.IX Subsection "ev_child - watch out for process status changes" 1615.IX Subsection "ev_child - watch out for process status changes"
1571Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to 1616Child 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). 1617some child status changes (most typically when a child of yours dies). It
1618is permissible to install a child watcher \fIafter\fR the child has been
1619forked (which implies it might have already exited), as long as the event
1620loop isn't entered (or is continued from a watcher).
1621.PP
1622Only the default event loop is capable of handling signals, and therefore
1623you can only rgeister child watchers in the default event loop.
1624.PP
1625\fIProcess Interaction\fR
1626.IX Subsection "Process Interaction"
1627.PP
1628Libev grabs \f(CW\*(C`SIGCHLD\*(C'\fR as soon as the default event loop is
1629initialised. This is necessary to guarantee proper behaviour even if
1630the first child watcher is started after the child exits. The occurance
1631of \f(CW\*(C`SIGCHLD\*(C'\fR is recorded asynchronously, but child reaping is done
1632synchronously as part of the event loop processing. Libev always reaps all
1633children, even ones not watched.
1634.PP
1635\fIOverriding the Built-In Processing\fR
1636.IX Subsection "Overriding the Built-In Processing"
1637.PP
1638Libev offers no special support for overriding the built-in child
1639processing, but if your application collides with libev's default child
1640handler, you can override it easily by installing your own handler for
1641\&\f(CW\*(C`SIGCHLD\*(C'\fR after initialising the default loop, and making sure the
1642default loop never gets destroyed. You are encouraged, however, to use an
1643event-based approach to child reaping and thus use libev's support for
1644that, so other libev users can use \f(CW\*(C`ev_child\*(C'\fR watchers freely.
1573.PP 1645.PP
1574\fIWatcher-Specific Functions and Data Members\fR 1646\fIWatcher-Specific Functions and Data Members\fR
1575.IX Subsection "Watcher-Specific Functions and Data Members" 1647.IX Subsection "Watcher-Specific Functions and Data Members"
1576.IP "ev_child_init (ev_child *, callback, int pid, int trace)" 4 1648.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)" 1649.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). 1671\&\f(CW\*(C`waitpid\*(C'\fR and \f(CW\*(C`sys/wait.h\*(C'\fR documentation for details).
1600.PP 1672.PP
1601\fIExamples\fR 1673\fIExamples\fR
1602.IX Subsection "Examples" 1674.IX Subsection "Examples"
1603.PP 1675.PP
1604Example: Try to exit cleanly on \s-1SIGINT\s0 and \s-1SIGTERM\s0. 1676Example: \f(CW\*(C`fork()\*(C'\fR a new process and install a child handler to wait for
1677its completion.
1605.PP 1678.PP
1606.Vb 5 1679.Vb 1
1680\& ev_child cw;
1681\&
1607\& static void 1682\& static void
1608\& sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) 1683\& child_cb (EV_P_ struct ev_child *w, int revents)
1609\& { 1684\& {
1610\& ev_unloop (loop, EVUNLOOP_ALL); 1685\& ev_child_stop (EV_A_ w);
1686\& printf ("process %d exited with status %x\en", w\->rpid, w\->rstatus);
1611\& } 1687\& }
1612\& 1688\&
1613\& struct ev_signal signal_watcher; 1689\& pid_t pid = fork ();
1614\& ev_signal_init (&signal_watcher, sigint_cb, SIGINT); 1690\&
1615\& ev_signal_start (loop, &sigint_cb); 1691\& if (pid < 0)
1692\& // error
1693\& else if (pid == 0)
1694\& {
1695\& // the forked child executes here
1696\& exit (1);
1697\& }
1698\& else
1699\& {
1700\& ev_child_init (&cw, child_cb, pid, 0);
1701\& ev_child_start (EV_DEFAULT_ &cw);
1702\& }
1616.Ve 1703.Ve
1617.ie n .Sh """ev_stat"" \- did the file attributes just change?" 1704.ie n .Sh """ev_stat"" \- did the file attributes just change?"
1618.el .Sh "\f(CWev_stat\fP \- did the file attributes just change?" 1705.el .Sh "\f(CWev_stat\fP \- did the file attributes just change?"
1619.IX Subsection "ev_stat - did the file attributes just change?" 1706.IX Subsection "ev_stat - did the file attributes just change?"
1620This watches a filesystem path for attribute changes. That is, it calls 1707This watches a filesystem path for attribute changes. That is, it calls
1701path for as long as the watcher is active. 1788path for as long as the watcher is active.
1702.Sp 1789.Sp
1703The callback will be receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected, 1790The 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 1791relative to the attributes at the time the watcher was started (or the
1705last change was detected). 1792last change was detected).
1706.IP "ev_stat_stat (ev_stat *)" 4 1793.IP "ev_stat_stat (loop, ev_stat *)" 4
1707.IX Item "ev_stat_stat (ev_stat *)" 1794.IX Item "ev_stat_stat (loop, ev_stat *)"
1708Updates the stat buffer immediately with new values. If you change the 1795Updates the stat buffer immediately with new values. If you change the
1709watched path in your callback, you could call this fucntion to avoid 1796watched path in your callback, you could call this fucntion to avoid
1710detecting this change (while introducing a race condition). Can also be 1797detecting this change (while introducing a race condition). Can also be
1711useful simply to find out the new values. 1798useful simply to find out the new values.
1712.IP "ev_statdata attr [read\-only]" 4 1799.IP "ev_statdata attr [read\-only]" 4
2173.IP "ev_fork_init (ev_signal *, callback)" 4 2260.IP "ev_fork_init (ev_signal *, callback)" 4
2174.IX Item "ev_fork_init (ev_signal *, callback)" 2261.IX Item "ev_fork_init (ev_signal *, callback)"
2175Initialises and configures the fork watcher \- it has no parameters of any 2262Initialises 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, 2263kind. There is a \f(CW\*(C`ev_fork_set\*(C'\fR macro, but using it is utterly pointless,
2177believe me. 2264believe me.
2265.ie n .Sh """ev_async"" \- how to wake up another event loop"
2266.el .Sh "\f(CWev_async\fP \- how to wake up another event loop"
2267.IX Subsection "ev_async - how to wake up another event loop"
2268In general, you cannot use an \f(CW\*(C`ev_loop\*(C'\fR from multiple threads or other
2269asynchronous sources such as signal handlers (as opposed to multiple event
2270loops \- those are of course safe to use in different threads).
2271.PP
2272Sometimes, however, you need to wake up another event loop you do not
2273control, for example because it belongs to another thread. This is what
2274\&\f(CW\*(C`ev_async\*(C'\fR watchers do: as long as the \f(CW\*(C`ev_async\*(C'\fR watcher is active, you
2275can signal it by calling \f(CW\*(C`ev_async_send\*(C'\fR, which is thread\- and signal
2276safe.
2277.PP
2278This functionality is very similar to \f(CW\*(C`ev_signal\*(C'\fR watchers, as signals,
2279too, are asynchronous in nature, and signals, too, will be compressed
2280(i.e. the number of callback invocations may be less than the number of
2281\&\f(CW\*(C`ev_async_sent\*(C'\fR calls).
2282.PP
2283Unlike \f(CW\*(C`ev_signal\*(C'\fR watchers, \f(CW\*(C`ev_async\*(C'\fR works with any event loop, not
2284just the default loop.
2285.PP
2286\fIQueueing\fR
2287.IX Subsection "Queueing"
2288.PP
2289\&\f(CW\*(C`ev_async\*(C'\fR does not support queueing of data in any way. The reason
2290is that the author does not know of a simple (or any) algorithm for a
2291multiple-writer-single-reader queue that works in all cases and doesn't
2292need elaborate support such as pthreads.
2293.PP
2294That means that if you want to queue data, you have to provide your own
2295queue. But at least I can tell you would implement locking around your
2296queue:
2297.IP "queueing from a signal handler context" 4
2298.IX Item "queueing from a signal handler context"
2299To implement race-free queueing, you simply add to the queue in the signal
2300handler but you block the signal handler in the watcher callback. Here is an example that does that for
2301some fictitiuous \s-1SIGUSR1\s0 handler:
2302.Sp
2303.Vb 1
2304\& static ev_async mysig;
2305\&
2306\& static void
2307\& sigusr1_handler (void)
2308\& {
2309\& sometype data;
2310\&
2311\& // no locking etc.
2312\& queue_put (data);
2313\& ev_async_send (EV_DEFAULT_ &mysig);
2314\& }
2315\&
2316\& static void
2317\& mysig_cb (EV_P_ ev_async *w, int revents)
2318\& {
2319\& sometype data;
2320\& sigset_t block, prev;
2321\&
2322\& sigemptyset (&block);
2323\& sigaddset (&block, SIGUSR1);
2324\& sigprocmask (SIG_BLOCK, &block, &prev);
2325\&
2326\& while (queue_get (&data))
2327\& process (data);
2328\&
2329\& if (sigismember (&prev, SIGUSR1)
2330\& sigprocmask (SIG_UNBLOCK, &block, 0);
2331\& }
2332.Ve
2333.Sp
2334(Note: pthreads in theory requires you to use \f(CW\*(C`pthread_setmask\*(C'\fR
2335instead of \f(CW\*(C`sigprocmask\*(C'\fR when you use threads, but libev doesn't do it
2336either...).
2337.IP "queueing from a thread context" 4
2338.IX Item "queueing from a thread context"
2339The strategy for threads is different, as you cannot (easily) block
2340threads but you can easily preempt them, so to queue safely you need to
2341employ a traditional mutex lock, such as in this pthread example:
2342.Sp
2343.Vb 2
2344\& static ev_async mysig;
2345\& static pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
2346\&
2347\& static void
2348\& otherthread (void)
2349\& {
2350\& // only need to lock the actual queueing operation
2351\& pthread_mutex_lock (&mymutex);
2352\& queue_put (data);
2353\& pthread_mutex_unlock (&mymutex);
2354\&
2355\& ev_async_send (EV_DEFAULT_ &mysig);
2356\& }
2357\&
2358\& static void
2359\& mysig_cb (EV_P_ ev_async *w, int revents)
2360\& {
2361\& pthread_mutex_lock (&mymutex);
2362\&
2363\& while (queue_get (&data))
2364\& process (data);
2365\&
2366\& pthread_mutex_unlock (&mymutex);
2367\& }
2368.Ve
2369.PP
2370\fIWatcher-Specific Functions and Data Members\fR
2371.IX Subsection "Watcher-Specific Functions and Data Members"
2372.IP "ev_async_init (ev_async *, callback)" 4
2373.IX Item "ev_async_init (ev_async *, callback)"
2374Initialises and configures the async watcher \- it has no parameters of any
2375kind. There is a \f(CW\*(C`ev_asynd_set\*(C'\fR macro, but using it is utterly pointless,
2376believe me.
2377.IP "ev_async_send (loop, ev_async *)" 4
2378.IX Item "ev_async_send (loop, ev_async *)"
2379Sends/signals/activates the given \f(CW\*(C`ev_async\*(C'\fR watcher, that is, feeds
2380an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike
2381\&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do in other threads, signal or
2382similar contexts (see the dicusssion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding
2383section below on what exactly this means).
2384.Sp
2385This call incurs the overhead of a syscall only once per loop iteration,
2386so while the overhead might be noticable, it doesn't apply to repeated
2387calls to \f(CW\*(C`ev_async_send\*(C'\fR.
2178.SH "OTHER FUNCTIONS" 2388.SH "OTHER FUNCTIONS"
2179.IX Header "OTHER FUNCTIONS" 2389.IX Header "OTHER FUNCTIONS"
2180There are some other functions of possible interest. Described. Here. Now. 2390There are some other functions of possible interest. Described. Here. Now.
2181.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 2391.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)" 2392.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)"
2406\& 2616\&
2407\& io.start (fd, ev::READ); 2617\& io.start (fd, ev::READ);
2408\& } 2618\& }
2409\& }; 2619\& };
2410.Ve 2620.Ve
2621.SH "OTHER LANGUAGE BINDINGS"
2622.IX Header "OTHER LANGUAGE BINDINGS"
2623Libev does not offer other language bindings itself, but bindings for a
2624numbe rof languages exist in the form of third-party packages. If you know
2625any interesting language binding in addition to the ones listed here, drop
2626me a note.
2627.IP "Perl" 4
2628.IX Item "Perl"
2629The \s-1EV\s0 module implements the full libev \s-1API\s0 and is actually used to test
2630libev. \s-1EV\s0 is developed together with libev. Apart from the \s-1EV\s0 core module,
2631there are additional modules that implement libev-compatible interfaces
2632to \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
2633\&\f(CW\*(C`libglib\*(C'\fR event core (\f(CW\*(C`Glib::EV\*(C'\fR and \f(CW\*(C`EV::Glib\*(C'\fR).
2634.Sp
2635It can be found and installed via \s-1CPAN\s0, its homepage is found at
2636<http://software.schmorp.de/pkg/EV>.
2637.IP "Ruby" 4
2638.IX Item "Ruby"
2639Tony Arcieri has written a ruby extension that offers access to a subset
2640of the libev \s-1API\s0 and adds filehandle abstractions, asynchronous \s-1DNS\s0 and
2641more on top of it. It can be found via gem servers. Its homepage is at
2642<http://rev.rubyforge.org/>.
2643.IP "D" 4
2644.IX Item "D"
2645Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to
2646be found at <http://git.llucax.com.ar/?p=software/ev.d.git;a=summary>.
2411.SH "MACRO MAGIC" 2647.SH "MACRO MAGIC"
2412.IX Header "MACRO MAGIC" 2648.IX Header "MACRO MAGIC"
2413Libev can be compiled with a variety of options, the most fundamantal 2649Libev 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) 2650of 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. 2651functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument.
2668.IP "\s-1EV_USE_INOTIFY\s0" 4 2904.IP "\s-1EV_USE_INOTIFY\s0" 4
2669.IX Item "EV_USE_INOTIFY" 2905.IX Item "EV_USE_INOTIFY"
2670If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify 2906If 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 2907interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will
2672be detected at runtime. 2908be detected at runtime.
2909.IP "\s-1EV_ATOMIC_T\s0" 4
2910.IX Item "EV_ATOMIC_T"
2911Libev requires an integer type (suitable for storing \f(CW0\fR or \f(CW1\fR) whose
2912access is atomic with respect to other threads or signal contexts. No such
2913type is easily found in the C language, so you can provide your own type
2914that you know is safe for your purposes. It is used both for signal handler \*(L"locking\*(R"
2915as well as for signal and thread safety in \f(CW\*(C`ev_async\*(C'\fR watchers.
2916.Sp
2917In the absense of this define, libev will use \f(CW\*(C`sig_atomic_t volatile\*(C'\fR
2918(from \fIsignal.h\fR), which is usually good enough on most platforms.
2673.IP "\s-1EV_H\s0" 4 2919.IP "\s-1EV_H\s0" 4
2674.IX Item "EV_H" 2920.IX Item "EV_H"
2675The name of the \fIev.h\fR header file used to include it. The default if 2921The 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 2922undefined 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. 2923used to virtually rename the \fIev.h\fR header file in case of conflicts.
2735defined to be \f(CW0\fR, then they are not. 2981defined to be \f(CW0\fR, then they are not.
2736.IP "\s-1EV_FORK_ENABLE\s0" 4 2982.IP "\s-1EV_FORK_ENABLE\s0" 4
2737.IX Item "EV_FORK_ENABLE" 2983.IX Item "EV_FORK_ENABLE"
2738If undefined or defined to be \f(CW1\fR, then fork watchers are supported. If 2984If undefined or defined to be \f(CW1\fR, then fork watchers are supported. If
2739defined to be \f(CW0\fR, then they are not. 2985defined to be \f(CW0\fR, then they are not.
2986.IP "\s-1EV_ASYNC_ENABLE\s0" 4
2987.IX Item "EV_ASYNC_ENABLE"
2988If undefined or defined to be \f(CW1\fR, then async watchers are supported. If
2989defined to be \f(CW0\fR, then they are not.
2740.IP "\s-1EV_MINIMAL\s0" 4 2990.IP "\s-1EV_MINIMAL\s0" 4
2741.IX Item "EV_MINIMAL" 2991.IX Item "EV_MINIMAL"
2742If you need to shave off some kilobytes of code at the expense of some 2992If 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 2993speed, define this symbol to \f(CW1\fR. Currently only used for gcc to override
2744some inlining decisions, saves roughly 30% codesize of amd64. 2994some inlining decisions, saves roughly 30% codesize of amd64.
2864have to skip roughly seven (\f(CW\*(C`ld 100\*(C'\fR) of these watchers. 3114have 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 3115.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)" 3116.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 3117That 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. 3118as 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 3119.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)" 3120.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. 3121These just add the watcher into an array or at the head of a list.
2872.IP "Stopping check/prepare/idle watchers: O(1)" 4 3122.IP "Stopping check/prepare/idle/fork/async watchers: O(1)" 4
2873.IX Item "Stopping check/prepare/idle watchers: O(1)" 3123.IX Item "Stopping check/prepare/idle/fork/async watchers: O(1)"
2874.PD 0 3124.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 3125.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))" 3126.IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))"
2877.PD 3127.PD
2878These watchers are stored in lists then need to be walked to find the 3128These watchers are stored in lists then need to be walked to find the
2894.IX Item "Priority handling: O(number_of_priorities)" 3144.IX Item "Priority handling: O(number_of_priorities)"
2895.PD 3145.PD
2896Priorities are implemented by allocating some space for each 3146Priorities are implemented by allocating some space for each
2897priority. When doing priority-based operations, libev usually has to 3147priority. When doing priority-based operations, libev usually has to
2898linearly search all the priorities, but starting/stopping and activating 3148linearly search all the priorities, but starting/stopping and activating
2899watchers becomes O(1) w.r.t. prioritiy handling. 3149watchers becomes O(1) w.r.t. priority handling.
3150.IP "Sending an ev_async: O(1)" 4
3151.IX Item "Sending an ev_async: O(1)"
3152.PD 0
3153.IP "Processing ev_async_send: O(number_of_async_watchers)" 4
3154.IX Item "Processing ev_async_send: O(number_of_async_watchers)"
3155.IP "Processing signals: O(max_signal_number)" 4
3156.IX Item "Processing signals: O(max_signal_number)"
3157.PD
3158Sending involves a syscall \fIiff\fR there were no other \f(CW\*(C`ev_async_send\*(C'\fR
3159calls in the current loop iteration. Checking for async and signal events
3160involves iterating over all running async watchers or all signal numbers.
2900.SH "Win32 platform limitations and workarounds" 3161.SH "Win32 platform limitations and workarounds"
2901.IX Header "Win32 platform limitations and workarounds" 3162.IX Header "Win32 platform limitations and workarounds"
2902Win32 doesn't support any of the standards (e.g. \s-1POSIX\s0) that libev 3163Win32 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 3164requires, and its I/O model is fundamentally incompatible with the \s-1POSIX\s0
2904model. Libev still offers limited functionality on this platform in 3165model. Libev still offers limited functionality on this platform in
2962.IX Header "AUTHOR" 3223.IX Header "AUTHOR"
2963Marc Lehmann <libev@schmorp.de>. 3224Marc Lehmann <libev@schmorp.de>.
2964.SH "POD ERRORS" 3225.SH "POD ERRORS"
2965.IX Header "POD ERRORS" 3226.IX Header "POD ERRORS"
2966Hey! \fBThe above document had some coding errors, which are explained below:\fR 3227Hey! \fBThe above document had some coding errors, which are explained below:\fR
2967.IP "Around line 2686:" 4 3228.IP "Around line 2951:" 4
2968.IX Item "Around line 2686:" 3229.IX Item "Around line 2951:"
2969You forgot a '=back' before '=head2' 3230You forgot a '=back' before '=head2'

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines