… | |
… | |
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-08" "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" |
192 | The newest version of this document is also available as a html-formatted |
206 | The newest version of this document is also available as an html-formatted |
193 | web page you might find easier to navigate when reading it for the first |
207 | web page you might find easier to navigate when reading it for the first |
194 | time: <http://cvs.schmorp.de/libev/ev.html>. |
208 | time: <http://cvs.schmorp.de/libev/ev.html>. |
195 | .PP |
209 | .PP |
196 | Libev is an event loop: you register interest in certain events (such as a |
210 | Libev is an event loop: you register interest in certain events (such as a |
197 | file descriptor being readable or a timeout occurring), and it will manage |
211 | file descriptor being readable or a timeout occurring), and it will manage |
… | |
… | |
221 | It also is quite fast (see this |
235 | It also is quite fast (see this |
222 | benchmark comparing it to libevent |
236 | benchmark comparing it to libevent |
223 | for example). |
237 | for example). |
224 | .Sh "\s-1CONVENTIONS\s0" |
238 | .Sh "\s-1CONVENTIONS\s0" |
225 | .IX Subsection "CONVENTIONS" |
239 | .IX Subsection "CONVENTIONS" |
226 | Libev is very configurable. In this manual the default configuration will |
240 | Libev is very configurable. In this manual the default (and most common) |
227 | be described, which supports multiple event loops. For more info about |
241 | configuration will be described, which supports multiple event loops. For |
228 | various configuration options please have a look at \fB\s-1EMBED\s0\fR section in |
242 | more info about various configuration options please have a look at |
229 | this 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 |
230 | loops, then all functions taking an initial argument of name \f(CW\*(C`loop\*(C'\fR |
244 | for 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. |
245 | name \f(CW\*(C`loop\*(C'\fR (which is always of type \f(CW\*(C`struct ev_loop *\*(C'\fR) will not have |
|
|
246 | this 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" |
234 | Libev represents time as a single floating point number, representing the |
249 | Libev 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 |
236 | the beginning of 1970, details are complicated, don't ask). This type is |
251 | the beginning of 1970, details are complicated, don't ask). This type is |
… | |
… | |
425 | enabling this flag. |
440 | enabling this flag. |
426 | .Sp |
441 | .Sp |
427 | This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop, |
442 | This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop, |
428 | and thus this might slow down your event loop if you do a lot of loop |
443 | and thus this might slow down your event loop if you do a lot of loop |
429 | iterations and little real work, but is usually not noticeable (on my |
444 | iterations and little real work, but is usually not noticeable (on my |
430 | Linux system for example, \f(CW\*(C`getpid\*(C'\fR is actually a simple 5\-insn sequence |
445 | GNU/Linux system for example, \f(CW\*(C`getpid\*(C'\fR is actually a simple 5\-insn sequence |
431 | without a syscall and thus \fIvery\fR fast, but my Linux system also has |
446 | without 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 |
434 | The big advantage of this flag is that you can forget about fork (and |
449 | The big advantage of this flag is that you can forget about fork (and |
435 | forget about forgetting to tell libev about forking) when you use this |
450 | forget about forgetting to tell libev about forking) when you use this |
436 | flag. |
451 | flag. |
… | |
… | |
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)" |
641 | Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by |
656 | Like \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 |
643 | after fork, and how you do this is entirely your own problem. |
658 | after 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)" |
|
|
661 | Returns 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)" |
646 | Returns the count of loop iterations for the loop, which is identical to |
664 | Returns the count of loop iterations for the loop, which is identical to |
647 | the number of times libev did poll for new events. It starts at \f(CW0\fR and |
665 | the number of times libev did poll for new events. It starts at \f(CW0\fR and |
648 | happily wraps around with enough iterations. |
666 | happily 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" |
917 | The event loop has been resumed in the child process after fork (see |
935 | The 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" |
|
|
940 | The 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" |
922 | An unspecified error has occured, the watcher has been stopped. This might |
944 | An unspecified error has occured, the watcher has been stopped. This might |
923 | happen because the watcher could not be properly started because libev |
945 | happen because the watcher could not be properly started because libev |
… | |
… | |
1281 | The timer itself will do a best-effort at avoiding drift, that is, if you |
1303 | The timer itself will do a best-effort at avoiding drift, that is, if you |
1282 | configure a timer to trigger every 10 seconds, then it will trigger at |
1304 | configure a timer to trigger every 10 seconds, then it will trigger at |
1283 | exactly 10 second intervals. If, however, your program cannot keep up with |
1305 | exactly 10 second intervals. If, however, your program cannot keep up with |
1284 | the timer (because it takes longer than those 10 seconds to do stuff) the |
1306 | the timer (because it takes longer than those 10 seconds to do stuff) the |
1285 | timer will not fire more than once per event loop iteration. |
1307 | timer 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 *)" |
1288 | This will act as if the timer timed out and restart it again if it is |
1310 | This will act as if the timer timed out and restart it again if it is |
1289 | repeating. The exact semantics are: |
1311 | repeating. The exact semantics are: |
1290 | .Sp |
1312 | .Sp |
1291 | If the timer is pending, its pending status is cleared. |
1313 | If the timer is pending, its pending status is cleared. |
1292 | .Sp |
1314 | .Sp |
… | |
… | |
1402 | In this configuration the watcher triggers an event at the wallclock time |
1424 | In 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, |
1404 | that is, if it is to be run at January 1st 2011 then it will run when the |
1426 | that is, if it is to be run at January 1st 2011 then it will run when the |
1405 | system time reaches or surpasses this time. |
1427 | system time reaches or surpasses this time. |
1406 | .IP "\(bu" 4 |
1428 | .IP "\(bu" 4 |
1407 | non-repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) |
1429 | repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) |
1408 | .Sp |
1430 | .Sp |
1409 | In this mode the watcher will always be scheduled to time out at the next |
1431 | In 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) |
1411 | and then repeat, regardless of any time jumps. |
1433 | and then repeat, regardless of any time jumps. |
1412 | .Sp |
1434 | .Sp |
… | |
… | |
1550 | with the kernel (thus it coexists with your own signal handlers as long |
1572 | with the kernel (thus it coexists with your own signal handlers as long |
1551 | as you don't register any with libev). Similarly, when the last signal |
1573 | as you don't register any with libev). Similarly, when the last signal |
1552 | watcher for a signal is stopped libev will reset the signal handler to |
1574 | watcher 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 |
|
|
1577 | If 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 |
|
|
1579 | interrupted. If you have a problem with syscalls getting interrupted by |
|
|
1580 | signals you can block all signals in an \f(CW\*(C`ev_check\*(C'\fR watcher and unblock |
|
|
1581 | them 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 |
… | |
… | |
1563 | Configures the watcher to trigger on the given signal number (usually one |
1591 | Configures the watcher to trigger on the given signal number (usually one |
1564 | of the \f(CW\*(C`SIGxxx\*(C'\fR constants). |
1592 | of 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]" |
1567 | The signal the watcher watches out for. |
1595 | The signal the watcher watches out for. |
|
|
1596 | .PP |
|
|
1597 | \fIExamples\fR |
|
|
1598 | .IX Subsection "Examples" |
|
|
1599 | .PP |
|
|
1600 | Example: 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" |
1571 | Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to |
1616 | Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to |
1572 | some child status changes (most typically when a child of yours dies). |
1617 | some child status changes (most typically when a child of yours dies). It |
|
|
1618 | is permissible to install a child watcher \fIafter\fR the child has been |
|
|
1619 | forked (which implies it might have already exited), as long as the event |
|
|
1620 | loop isn't entered (or is continued from a watcher). |
|
|
1621 | .PP |
|
|
1622 | Only the default event loop is capable of handling signals, and therefore |
|
|
1623 | you can only rgeister child watchers in the default event loop. |
|
|
1624 | .PP |
|
|
1625 | \fIProcess Interaction\fR |
|
|
1626 | .IX Subsection "Process Interaction" |
|
|
1627 | .PP |
|
|
1628 | Libev grabs \f(CW\*(C`SIGCHLD\*(C'\fR as soon as the default event loop is |
|
|
1629 | initialised. This is necessary to guarantee proper behaviour even if |
|
|
1630 | the first child watcher is started after the child exits. The occurance |
|
|
1631 | of \f(CW\*(C`SIGCHLD\*(C'\fR is recorded asynchronously, but child reaping is done |
|
|
1632 | synchronously as part of the event loop processing. Libev always reaps all |
|
|
1633 | children, even ones not watched. |
|
|
1634 | .PP |
|
|
1635 | \fIOverriding the Built-In Processing\fR |
|
|
1636 | .IX Subsection "Overriding the Built-In Processing" |
|
|
1637 | .PP |
|
|
1638 | Libev offers no special support for overriding the built-in child |
|
|
1639 | processing, but if your application collides with libev's default child |
|
|
1640 | handler, 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 |
|
|
1642 | default loop never gets destroyed. You are encouraged, however, to use an |
|
|
1643 | event-based approach to child reaping and thus use libev's support for |
|
|
1644 | that, 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 |
1604 | Example: Try to exit cleanly on \s-1SIGINT\s0 and \s-1SIGTERM\s0. |
1676 | Example: \f(CW\*(C`fork()\*(C'\fR a new process and install a child handler to wait for |
|
|
1677 | its 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?" |
1620 | This watches a filesystem path for attribute changes. That is, it calls |
1707 | This watches a filesystem path for attribute changes. That is, it calls |
… | |
… | |
1701 | path for as long as the watcher is active. |
1788 | path for as long as the watcher is active. |
1702 | .Sp |
1789 | .Sp |
1703 | The callback will be receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected, |
1790 | The callback will be receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected, |
1704 | relative to the attributes at the time the watcher was started (or the |
1791 | relative to the attributes at the time the watcher was started (or the |
1705 | last change was detected). |
1792 | last 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 *)" |
1708 | Updates the stat buffer immediately with new values. If you change the |
1795 | Updates the stat buffer immediately with new values. If you change the |
1709 | watched path in your callback, you could call this fucntion to avoid |
1796 | watched path in your callback, you could call this fucntion to avoid |
1710 | detecting this change (while introducing a race condition). Can also be |
1797 | detecting this change (while introducing a race condition). Can also be |
1711 | useful simply to find out the new values. |
1798 | useful 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)" |
2175 | Initialises and configures the fork watcher \- it has no parameters of any |
2262 | Initialises and configures the fork watcher \- it has no parameters of any |
2176 | kind. There is a \f(CW\*(C`ev_fork_set\*(C'\fR macro, but using it is utterly pointless, |
2263 | kind. There is a \f(CW\*(C`ev_fork_set\*(C'\fR macro, but using it is utterly pointless, |
2177 | believe me. |
2264 | believe 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" |
|
|
2268 | In general, you cannot use an \f(CW\*(C`ev_loop\*(C'\fR from multiple threads or other |
|
|
2269 | asynchronous sources such as signal handlers (as opposed to multiple event |
|
|
2270 | loops \- those are of course safe to use in different threads). |
|
|
2271 | .PP |
|
|
2272 | Sometimes, however, you need to wake up another event loop you do not |
|
|
2273 | control, 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 |
|
|
2275 | can signal it by calling \f(CW\*(C`ev_async_send\*(C'\fR, which is thread\- and signal |
|
|
2276 | safe. |
|
|
2277 | .PP |
|
|
2278 | This functionality is very similar to \f(CW\*(C`ev_signal\*(C'\fR watchers, as signals, |
|
|
2279 | too, 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 |
|
|
2283 | Unlike \f(CW\*(C`ev_signal\*(C'\fR watchers, \f(CW\*(C`ev_async\*(C'\fR works with any event loop, not |
|
|
2284 | just 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 |
|
|
2290 | is that the author does not know of a simple (or any) algorithm for a |
|
|
2291 | multiple-writer-single-reader queue that works in all cases and doesn't |
|
|
2292 | need elaborate support such as pthreads. |
|
|
2293 | .PP |
|
|
2294 | That means that if you want to queue data, you have to provide your own |
|
|
2295 | queue. But at least I can tell you would implement locking around your |
|
|
2296 | queue: |
|
|
2297 | .IP "queueing from a signal handler context" 4 |
|
|
2298 | .IX Item "queueing from a signal handler context" |
|
|
2299 | To implement race-free queueing, you simply add to the queue in the signal |
|
|
2300 | handler but you block the signal handler in the watcher callback. Here is an example that does that for |
|
|
2301 | some 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 |
|
|
2335 | instead of \f(CW\*(C`sigprocmask\*(C'\fR when you use threads, but libev doesn't do it |
|
|
2336 | either...). |
|
|
2337 | .IP "queueing from a thread context" 4 |
|
|
2338 | .IX Item "queueing from a thread context" |
|
|
2339 | The strategy for threads is different, as you cannot (easily) block |
|
|
2340 | threads but you can easily preempt them, so to queue safely you need to |
|
|
2341 | employ 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)" |
|
|
2374 | Initialises and configures the async watcher \- it has no parameters of any |
|
|
2375 | kind. There is a \f(CW\*(C`ev_asynd_set\*(C'\fR macro, but using it is utterly pointless, |
|
|
2376 | believe me. |
|
|
2377 | .IP "ev_async_send (loop, ev_async *)" 4 |
|
|
2378 | .IX Item "ev_async_send (loop, ev_async *)" |
|
|
2379 | Sends/signals/activates the given \f(CW\*(C`ev_async\*(C'\fR watcher, that is, feeds |
|
|
2380 | an \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 |
|
|
2382 | similar contexts (see the dicusssion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding |
|
|
2383 | section below on what exactly this means). |
|
|
2384 | .Sp |
|
|
2385 | This call incurs the overhead of a syscall only once per loop iteration, |
|
|
2386 | so while the overhead might be noticable, it doesn't apply to repeated |
|
|
2387 | calls 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" |
2180 | There are some other functions of possible interest. Described. Here. Now. |
2390 | There 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)" |
… | |
… | |
2668 | .IP "\s-1EV_USE_INOTIFY\s0" 4 |
2878 | .IP "\s-1EV_USE_INOTIFY\s0" 4 |
2669 | .IX Item "EV_USE_INOTIFY" |
2879 | .IX Item "EV_USE_INOTIFY" |
2670 | If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify |
2880 | If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify |
2671 | interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will |
2881 | interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will |
2672 | be detected at runtime. |
2882 | be detected at runtime. |
|
|
2883 | .IP "\s-1EV_ATOMIC_T\s0" 4 |
|
|
2884 | .IX Item "EV_ATOMIC_T" |
|
|
2885 | Libev requires an integer type (suitable for storing \f(CW0\fR or \f(CW1\fR) whose |
|
|
2886 | access is atomic with respect to other threads or signal contexts. No such |
|
|
2887 | type is easily found in the C language, so you can provide your own type |
|
|
2888 | that you know is safe for your purposes. It is used both for signal handler \*(L"locking\*(R" |
|
|
2889 | as well as for signal and thread safety in \f(CW\*(C`ev_async\*(C'\fR watchers. |
|
|
2890 | .Sp |
|
|
2891 | In the absense of this define, libev will use \f(CW\*(C`sig_atomic_t volatile\*(C'\fR |
|
|
2892 | (from \fIsignal.h\fR), which is usually good enough on most platforms. |
2673 | .IP "\s-1EV_H\s0" 4 |
2893 | .IP "\s-1EV_H\s0" 4 |
2674 | .IX Item "EV_H" |
2894 | .IX Item "EV_H" |
2675 | The name of the \fIev.h\fR header file used to include it. The default if |
2895 | The name of the \fIev.h\fR header file used to include it. The default if |
2676 | undefined is \f(CW"ev.h"\fR in \fIevent.h\fR, \fIev.c\fR and \fIev++.h\fR. This can be |
2896 | undefined is \f(CW"ev.h"\fR in \fIevent.h\fR, \fIev.c\fR and \fIev++.h\fR. This can be |
2677 | used to virtually rename the \fIev.h\fR header file in case of conflicts. |
2897 | used to virtually rename the \fIev.h\fR header file in case of conflicts. |
… | |
… | |
2735 | defined to be \f(CW0\fR, then they are not. |
2955 | defined to be \f(CW0\fR, then they are not. |
2736 | .IP "\s-1EV_FORK_ENABLE\s0" 4 |
2956 | .IP "\s-1EV_FORK_ENABLE\s0" 4 |
2737 | .IX Item "EV_FORK_ENABLE" |
2957 | .IX Item "EV_FORK_ENABLE" |
2738 | If undefined or defined to be \f(CW1\fR, then fork watchers are supported. If |
2958 | If undefined or defined to be \f(CW1\fR, then fork watchers are supported. If |
2739 | defined to be \f(CW0\fR, then they are not. |
2959 | defined to be \f(CW0\fR, then they are not. |
|
|
2960 | .IP "\s-1EV_ASYNC_ENABLE\s0" 4 |
|
|
2961 | .IX Item "EV_ASYNC_ENABLE" |
|
|
2962 | If undefined or defined to be \f(CW1\fR, then async watchers are supported. If |
|
|
2963 | defined to be \f(CW0\fR, then they are not. |
2740 | .IP "\s-1EV_MINIMAL\s0" 4 |
2964 | .IP "\s-1EV_MINIMAL\s0" 4 |
2741 | .IX Item "EV_MINIMAL" |
2965 | .IX Item "EV_MINIMAL" |
2742 | If you need to shave off some kilobytes of code at the expense of some |
2966 | If you need to shave off some kilobytes of code at the expense of some |
2743 | speed, define this symbol to \f(CW1\fR. Currently only used for gcc to override |
2967 | speed, define this symbol to \f(CW1\fR. Currently only used for gcc to override |
2744 | some inlining decisions, saves roughly 30% codesize of amd64. |
2968 | some inlining decisions, saves roughly 30% codesize of amd64. |
… | |
… | |
2864 | have to skip roughly seven (\f(CW\*(C`ld 100\*(C'\fR) of these watchers. |
3088 | have 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 |
3089 | .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)" |
3090 | .IX Item "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" |
2867 | That means that changing a timer costs less than removing/adding them |
3091 | That means that changing a timer costs less than removing/adding them |
2868 | as only the relative motion in the event queue has to be paid for. |
3092 | as 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 |
3093 | .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)" |
3094 | .IX Item "Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)" |
2871 | These just add the watcher into an array or at the head of a list. |
3095 | These just add the watcher into an array or at the head of a list. |
2872 | .IP "Stopping check/prepare/idle watchers: O(1)" 4 |
3096 | .IP "Stopping check/prepare/idle/fork/async watchers: O(1)" 4 |
2873 | .IX Item "Stopping check/prepare/idle watchers: O(1)" |
3097 | .IX Item "Stopping check/prepare/idle/fork/async watchers: O(1)" |
2874 | .PD 0 |
3098 | .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 |
3099 | .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))" |
3100 | .IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))" |
2877 | .PD |
3101 | .PD |
2878 | These watchers are stored in lists then need to be walked to find the |
3102 | These watchers are stored in lists then need to be walked to find the |
… | |
… | |
2894 | .IX Item "Priority handling: O(number_of_priorities)" |
3118 | .IX Item "Priority handling: O(number_of_priorities)" |
2895 | .PD |
3119 | .PD |
2896 | Priorities are implemented by allocating some space for each |
3120 | Priorities are implemented by allocating some space for each |
2897 | priority. When doing priority-based operations, libev usually has to |
3121 | priority. When doing priority-based operations, libev usually has to |
2898 | linearly search all the priorities, but starting/stopping and activating |
3122 | linearly search all the priorities, but starting/stopping and activating |
2899 | watchers becomes O(1) w.r.t. prioritiy handling. |
3123 | watchers becomes O(1) w.r.t. priority handling. |
|
|
3124 | .IP "Sending an ev_async: O(1)" 4 |
|
|
3125 | .IX Item "Sending an ev_async: O(1)" |
|
|
3126 | .PD 0 |
|
|
3127 | .IP "Processing ev_async_send: O(number_of_async_watchers)" 4 |
|
|
3128 | .IX Item "Processing ev_async_send: O(number_of_async_watchers)" |
|
|
3129 | .IP "Processing signals: O(max_signal_number)" 4 |
|
|
3130 | .IX Item "Processing signals: O(max_signal_number)" |
|
|
3131 | .PD |
|
|
3132 | Sending involves a syscall \fIiff\fR there were no other \f(CW\*(C`ev_async_send\*(C'\fR |
|
|
3133 | calls in the current loop iteration. Checking for async and signal events |
|
|
3134 | involves iterating over all running async watchers or all signal numbers. |
2900 | .SH "Win32 platform limitations and workarounds" |
3135 | .SH "Win32 platform limitations and workarounds" |
2901 | .IX Header "Win32 platform limitations and workarounds" |
3136 | .IX Header "Win32 platform limitations and workarounds" |
2902 | Win32 doesn't support any of the standards (e.g. \s-1POSIX\s0) that libev |
3137 | Win32 doesn't support any of the standards (e.g. \s-1POSIX\s0) that libev |
2903 | requires, and its I/O model is fundamentally incompatible with the \s-1POSIX\s0 |
3138 | requires, and its I/O model is fundamentally incompatible with the \s-1POSIX\s0 |
2904 | model. Libev still offers limited functionality on this platform in |
3139 | model. Libev still offers limited functionality on this platform in |
… | |
… | |
2962 | .IX Header "AUTHOR" |
3197 | .IX Header "AUTHOR" |
2963 | Marc Lehmann <libev@schmorp.de>. |
3198 | Marc Lehmann <libev@schmorp.de>. |
2964 | .SH "POD ERRORS" |
3199 | .SH "POD ERRORS" |
2965 | .IX Header "POD ERRORS" |
3200 | .IX Header "POD ERRORS" |
2966 | Hey! \fBThe above document had some coding errors, which are explained below:\fR |
3201 | Hey! \fBThe above document had some coding errors, which are explained below:\fR |
2967 | .IP "Around line 2686:" 4 |
3202 | .IP "Around line 2916:" 4 |
2968 | .IX Item "Around line 2686:" |
3203 | .IX Item "Around line 2916:" |
2969 | You forgot a '=back' before '=head2' |
3204 | You forgot a '=back' before '=head2' |