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

Comparing libev/ev.pod (file contents):
Revision 1.164 by root, Sat May 31 23:22:23 2008 UTC vs.
Revision 1.177 by root, Mon Sep 8 17:27:42 2008 UTC

573received events and started processing them. This timestamp does not 573received events and started processing them. This timestamp does not
574change as long as callbacks are being processed, and this is also the base 574change as long as callbacks are being processed, and this is also the base
575time used for relative timers. You can treat it as the timestamp of the 575time used for relative timers. You can treat it as the timestamp of the
576event occurring (or more correctly, libev finding out about it). 576event occurring (or more correctly, libev finding out about it).
577 577
578=item ev_now_update (loop)
579
580Establishes the current time by querying the kernel, updating the time
581returned by C<ev_now ()> in the progress. This is a costly operation and
582is usually done automatically within C<ev_loop ()>.
583
584This function is rarely useful, but when some event callback runs for a
585very long time without entering the event loop, updating libev's idea of
586the current time is a good idea.
587
588See also "The special problem of time updates" in the C<ev_timer> section.
589
578=item ev_loop (loop, int flags) 590=item ev_loop (loop, int flags)
579 591
580Finally, this is it, the event handler. This function usually is called 592Finally, this is it, the event handler. This function usually is called
581after you initialised all your watchers and you want to start handling 593after you initialised all your watchers and you want to start handling
582events. 594events.
604 616
605Here are the gory details of what C<ev_loop> does: 617Here are the gory details of what C<ev_loop> does:
606 618
607 - Before the first iteration, call any pending watchers. 619 - Before the first iteration, call any pending watchers.
608 * If EVFLAG_FORKCHECK was used, check for a fork. 620 * If EVFLAG_FORKCHECK was used, check for a fork.
609 - If a fork was detected, queue and call all fork watchers. 621 - If a fork was detected (by any means), queue and call all fork watchers.
610 - Queue and call all prepare watchers. 622 - Queue and call all prepare watchers.
611 - If we have been forked, recreate the kernel state. 623 - If we have been forked, detach and recreate the kernel state
624 as to not disturb the other process.
612 - Update the kernel state with all outstanding changes. 625 - Update the kernel state with all outstanding changes.
613 - Update the "event loop time". 626 - Update the "event loop time" (ev_now ()).
614 - Calculate for how long to sleep or block, if at all 627 - Calculate for how long to sleep or block, if at all
615 (active idle watchers, EVLOOP_NONBLOCK or not having 628 (active idle watchers, EVLOOP_NONBLOCK or not having
616 any active watchers at all will result in not sleeping). 629 any active watchers at all will result in not sleeping).
617 - Sleep if the I/O and timer collect interval say so. 630 - Sleep if the I/O and timer collect interval say so.
618 - Block the process, waiting for any events. 631 - Block the process, waiting for any events.
619 - Queue all outstanding I/O (fd) events. 632 - Queue all outstanding I/O (fd) events.
620 - Update the "event loop time" and do time jump handling. 633 - Update the "event loop time" (ev_now ()), and do time jump adjustments.
621 - Queue all outstanding timers. 634 - Queue all outstanding timers.
622 - Queue all outstanding periodics. 635 - Queue all outstanding periodics.
623 - If no events are pending now, queue all idle watchers. 636 - Unless any events are pending now, queue all idle watchers.
624 - Queue all check watchers. 637 - Queue all check watchers.
625 - Call all queued watchers in reverse order (i.e. check watchers first). 638 - Call all queued watchers in reverse order (i.e. check watchers first).
626 Signals and child watchers are implemented as I/O watchers, and will 639 Signals and child watchers are implemented as I/O watchers, and will
627 be handled here by queueing them when their watcher gets executed. 640 be handled here by queueing them when their watcher gets executed.
628 - If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK 641 - If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK
633anymore. 646anymore.
634 647
635 ... queue jobs here, make sure they register event watchers as long 648 ... queue jobs here, make sure they register event watchers as long
636 ... as they still have work to do (even an idle watcher will do..) 649 ... as they still have work to do (even an idle watcher will do..)
637 ev_loop (my_loop, 0); 650 ev_loop (my_loop, 0);
638 ... jobs done. yeah! 651 ... jobs done or somebody called unloop. yeah!
639 652
640=item ev_unloop (loop, how) 653=item ev_unloop (loop, how)
641 654
642Can be used to make a call to C<ev_loop> return early (but only after it 655Can be used to make a call to C<ev_loop> return early (but only after it
643has processed all outstanding events). The C<how> argument must be either 656has processed all outstanding events). The C<how> argument must be either
679=item ev_set_io_collect_interval (loop, ev_tstamp interval) 692=item ev_set_io_collect_interval (loop, ev_tstamp interval)
680 693
681=item ev_set_timeout_collect_interval (loop, ev_tstamp interval) 694=item ev_set_timeout_collect_interval (loop, ev_tstamp interval)
682 695
683These advanced functions influence the time that libev will spend waiting 696These advanced functions influence the time that libev will spend waiting
684for events. Both are by default C<0>, meaning that libev will try to 697for events. Both time intervals are by default C<0>, meaning that libev
685invoke timer/periodic callbacks and I/O callbacks with minimum latency. 698will try to invoke timer/periodic callbacks and I/O callbacks with minimum
699latency.
686 700
687Setting these to a higher value (the C<interval> I<must> be >= C<0>) 701Setting these to a higher value (the C<interval> I<must> be >= C<0>)
688allows libev to delay invocation of I/O and timer/periodic callbacks to 702allows libev to delay invocation of I/O and timer/periodic callbacks
689increase efficiency of loop iterations. 703to increase efficiency of loop iterations (or to increase power-saving
704opportunities).
690 705
691The background is that sometimes your program runs just fast enough to 706The background is that sometimes your program runs just fast enough to
692handle one (or very few) event(s) per loop iteration. While this makes 707handle one (or very few) event(s) per loop iteration. While this makes
693the program responsive, it also wastes a lot of CPU time to poll for new 708the program responsive, it also wastes a lot of CPU time to poll for new
694events, especially with backends like C<select ()> which have a high 709events, especially with backends like C<select ()> which have a high
709Many (busy) programs can usually benefit by setting the I/O collect 724Many (busy) programs can usually benefit by setting the I/O collect
710interval to a value near C<0.1> or so, which is often enough for 725interval to a value near C<0.1> or so, which is often enough for
711interactive servers (of course not for games), likewise for timeouts. It 726interactive servers (of course not for games), likewise for timeouts. It
712usually doesn't make much sense to set it to a lower value than C<0.01>, 727usually doesn't make much sense to set it to a lower value than C<0.01>,
713as this approaches the timing granularity of most systems. 728as this approaches the timing granularity of most systems.
729
730Setting the I<timeout collect interval> can improve the opportunity for
731saving power, as the program will "bundle" timer callback invocations that
732are "near" in time together, by delaying some, thus reducing the number of
733times the process sleeps and wakes up again. Another useful technique to
734reduce iterations/wake-ups is to use C<ev_periodic> watchers and make sure
735they fire on, say, one-second boundaries only.
714 736
715=item ev_loop_verify (loop) 737=item ev_loop_verify (loop)
716 738
717This function only does something when C<EV_VERIFY> support has been 739This function only does something when C<EV_VERIFY> support has been
718compiled in. It tries to go through all internal structures and checks 740compiled in. It tries to go through all internal structures and checks
1124C<EVBACKEND_POLL>. 1146C<EVBACKEND_POLL>.
1125 1147
1126=head3 The special problem of SIGPIPE 1148=head3 The special problem of SIGPIPE
1127 1149
1128While not really specific to libev, it is easy to forget about SIGPIPE: 1150While not really specific to libev, it is easy to forget about SIGPIPE:
1129when reading from a pipe whose other end has been closed, your program 1151when writing to a pipe whose other end has been closed, your program gets
1130gets send a SIGPIPE, which, by default, aborts your program. For most 1152send a SIGPIPE, which, by default, aborts your program. For most programs
1131programs this is sensible behaviour, for daemons, this is usually 1153this is sensible behaviour, for daemons, this is usually undesirable.
1132undesirable.
1133 1154
1134So when you encounter spurious, unexplained daemon exits, make sure you 1155So when you encounter spurious, unexplained daemon exits, make sure you
1135ignore SIGPIPE (and maybe make sure you log the exit status of your daemon 1156ignore SIGPIPE (and maybe make sure you log the exit status of your daemon
1136somewhere, as that would have given you a big clue). 1157somewhere, as that would have given you a big clue).
1137 1158
1188times out after an hour and you reset your system clock to January last 1209times out after an hour and you reset your system clock to January last
1189year, it will still time out after (roughly) and hour. "Roughly" because 1210year, it will still time out after (roughly) and hour. "Roughly" because
1190detecting time jumps is hard, and some inaccuracies are unavoidable (the 1211detecting time jumps is hard, and some inaccuracies are unavoidable (the
1191monotonic clock option helps a lot here). 1212monotonic clock option helps a lot here).
1192 1213
1214The callback is guaranteed to be invoked only after its timeout has passed,
1215but if multiple timers become ready during the same loop iteration then
1216order of execution is undefined.
1217
1218=head3 The special problem of time updates
1219
1220Establishing the current time is a costly operation (it usually takes at
1221least two system calls): EV therefore updates its idea of the current
1222time only before and after C<ev_loop> polls for new events, which causes
1223a growing difference between C<ev_now ()> and C<ev_time ()> when handling
1224lots of events.
1225
1193The relative timeouts are calculated relative to the C<ev_now ()> 1226The relative timeouts are calculated relative to the C<ev_now ()>
1194time. This is usually the right thing as this timestamp refers to the time 1227time. This is usually the right thing as this timestamp refers to the time
1195of the event triggering whatever timeout you are modifying/starting. If 1228of the event triggering whatever timeout you are modifying/starting. If
1196you suspect event processing to be delayed and you I<need> to base the timeout 1229you suspect event processing to be delayed and you I<need> to base the
1197on the current time, use something like this to adjust for this: 1230timeout on the current time, use something like this to adjust for this:
1198 1231
1199 ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); 1232 ev_timer_set (&timer, after + ev_now () - ev_time (), 0.);
1200 1233
1201The callback is guaranteed to be invoked only after its timeout has passed, 1234If the event loop is suspended for a long time, you can also force an
1202but if multiple timers become ready during the same loop iteration then 1235update of the time returned by C<ev_now ()> by calling C<ev_now_update
1203order of execution is undefined. 1236()>.
1204 1237
1205=head3 Watcher-Specific Functions and Data Members 1238=head3 Watcher-Specific Functions and Data Members
1206 1239
1207=over 4 1240=over 4
1208 1241
1559handler, you can override it easily by installing your own handler for 1592handler, you can override it easily by installing your own handler for
1560C<SIGCHLD> after initialising the default loop, and making sure the 1593C<SIGCHLD> after initialising the default loop, and making sure the
1561default loop never gets destroyed. You are encouraged, however, to use an 1594default loop never gets destroyed. You are encouraged, however, to use an
1562event-based approach to child reaping and thus use libev's support for 1595event-based approach to child reaping and thus use libev's support for
1563that, so other libev users can use C<ev_child> watchers freely. 1596that, so other libev users can use C<ev_child> watchers freely.
1597
1598=head3 Stopping the Child Watcher
1599
1600Currently, the child watcher never gets stopped, even when the
1601child terminates, so normally one needs to stop the watcher in the
1602callback. Future versions of libev might stop the watcher automatically
1603when a child exit is detected.
1564 1604
1565=head3 Watcher-Specific Functions and Data Members 1605=head3 Watcher-Specific Functions and Data Members
1566 1606
1567=over 4 1607=over 4
1568 1608
1662will be no polling. 1702will be no polling.
1663 1703
1664=head3 ABI Issues (Largefile Support) 1704=head3 ABI Issues (Largefile Support)
1665 1705
1666Libev by default (unless the user overrides this) uses the default 1706Libev by default (unless the user overrides this) uses the default
1667compilation environment, which means that on systems with optionally 1707compilation environment, which means that on systems with large file
1668disabled large file support, you get the 32 bit version of the stat 1708support disabled by default, you get the 32 bit version of the stat
1669structure. When using the library from programs that change the ABI to 1709structure. When using the library from programs that change the ABI to
1670use 64 bit file offsets the programs will fail. In that case you have to 1710use 64 bit file offsets the programs will fail. In that case you have to
1671compile libev with the same flags to get binary compatibility. This is 1711compile libev with the same flags to get binary compatibility. This is
1672obviously the case with any flags that change the ABI, but the problem is 1712obviously the case with any flags that change the ABI, but the problem is
1673most noticeably with ev_stat and large file support. 1713most noticeably disabled with ev_stat and large file support.
1714
1715The solution for this is to lobby your distribution maker to make large
1716file interfaces available by default (as e.g. FreeBSD does) and not
1717optional. Libev cannot simply switch on large file support because it has
1718to exchange stat structures with application programs compiled using the
1719default compilation environment.
1674 1720
1675=head3 Inotify 1721=head3 Inotify
1676 1722
1677When C<inotify (7)> support has been compiled into libev (generally only 1723When C<inotify (7)> support has been compiled into libev (generally only
1678available on Linux) and present at runtime, it will be used to speed up 1724available on Linux) and present at runtime, it will be used to speed up
2626libev. EV is developed together with libev. Apart from the EV core module, 2672libev. EV is developed together with libev. Apart from the EV core module,
2627there are additional modules that implement libev-compatible interfaces 2673there are additional modules that implement libev-compatible interfaces
2628to C<libadns> (C<EV::ADNS>), C<Net::SNMP> (C<Net::SNMP::EV>) and the 2674to C<libadns> (C<EV::ADNS>), C<Net::SNMP> (C<Net::SNMP::EV>) and the
2629C<libglib> event core (C<Glib::EV> and C<EV::Glib>). 2675C<libglib> event core (C<Glib::EV> and C<EV::Glib>).
2630 2676
2631It can be found and installed via CPAN, its homepage is found at 2677It can be found and installed via CPAN, its homepage is at
2632L<http://software.schmorp.de/pkg/EV>. 2678L<http://software.schmorp.de/pkg/EV>.
2679
2680=item Python
2681
2682Python bindings can be found at L<http://code.google.com/p/pyev/>. It
2683seems to be quite complete and well-documented. Note, however, that the
2684patch they require for libev is outright dangerous as it breaks the ABI
2685for everybody else, and therefore, should never be applied in an installed
2686libev (if python requires an incompatible ABI then it needs to embed
2687libev).
2633 2688
2634=item Ruby 2689=item Ruby
2635 2690
2636Tony Arcieri has written a ruby extension that offers access to a subset 2691Tony Arcieri has written a ruby extension that offers access to a subset
2637of the libev API and adds file handle abstractions, asynchronous DNS and 2692of the libev API and adds file handle abstractions, asynchronous DNS and
2639L<http://rev.rubyforge.org/>. 2694L<http://rev.rubyforge.org/>.
2640 2695
2641=item D 2696=item D
2642 2697
2643Leandro Lucarella has written a D language binding (F<ev.d>) for libev, to 2698Leandro Lucarella has written a D language binding (F<ev.d>) for libev, to
2644be found at L<http://git.llucax.com.ar/?p=software/ev.d.git;a=summary>. 2699be found at L<http://proj.llucax.com.ar/wiki/evd>.
2645 2700
2646=back 2701=back
2647 2702
2648 2703
2649=head1 MACRO MAGIC 2704=head1 MACRO MAGIC
3173parallel from multiple threads, calls with the same loop parameter must be 3228parallel from multiple threads, calls with the same loop parameter must be
3174done serially (but can be done from different threads, as long as only one 3229done serially (but can be done from different threads, as long as only one
3175thread ever is inside a call at any point in time, e.g. by using a mutex 3230thread ever is inside a call at any point in time, e.g. by using a mutex
3176per loop). 3231per loop).
3177 3232
3178If you want to know which design is best for your problem, then I cannot 3233If you want to know which design (one loop, locking, or multiple loops
3179help you but by giving some generic advice: 3234without or something else still) is best for your problem, then I cannot
3235help you. I can give some generic advice however:
3180 3236
3181=over 4 3237=over 4
3182 3238
3183=item * most applications have a main thread: use the default libev loop 3239=item * most applications have a main thread: use the default libev loop
3184in that thread, or create a separate thread running only the default loop. 3240in that thread, or create a separate thread running only the default loop.
3317more than a hundred or so sockets, then likely it needs to use a totally 3373more than a hundred or so sockets, then likely it needs to use a totally
3318different implementation for windows, as libev offers the POSIX readiness 3374different implementation for windows, as libev offers the POSIX readiness
3319notification model, which cannot be implemented efficiently on windows 3375notification model, which cannot be implemented efficiently on windows
3320(Microsoft monopoly games). 3376(Microsoft monopoly games).
3321 3377
3378A typical way to use libev under windows is to embed it (see the embedding
3379section for details) and use the following F<evwrap.h> header file instead
3380of F<ev.h>:
3381
3382 #define EV_STANDALONE /* keeps ev from requiring config.h */
3383 #define EV_SELECT_IS_WINSOCKET 1 /* configure libev for windows select */
3384
3385 #include "ev.h"
3386
3387And compile the following F<evwrap.c> file into your project (make sure
3388you do I<not> compile the F<ev.c> or any other embedded soruce files!):
3389
3390 #include "evwrap.h"
3391 #include "ev.c"
3392
3322=over 4 3393=over 4
3323 3394
3324=item The winsocket select function 3395=item The winsocket select function
3325 3396
3326The winsocket C<select> function doesn't follow POSIX in that it 3397The winsocket C<select> function doesn't follow POSIX in that it
3327requires socket I<handles> and not socket I<file descriptors> (it is 3398requires socket I<handles> and not socket I<file descriptors> (it is
3328also extremely buggy). This makes select very inefficient, and also 3399also extremely buggy). This makes select very inefficient, and also
3329requires a mapping from file descriptors to socket handles. See the 3400requires a mapping from file descriptors to socket handles (the Microsoft
3401C runtime provides the function C<_open_osfhandle> for this). See the
3330discussion of the C<EV_SELECT_USE_FD_SET>, C<EV_SELECT_IS_WINSOCKET> and 3402discussion of the C<EV_SELECT_USE_FD_SET>, C<EV_SELECT_IS_WINSOCKET> and
3331C<EV_FD_TO_WIN32_HANDLE> preprocessor symbols for more info. 3403C<EV_FD_TO_WIN32_HANDLE> preprocessor symbols for more info.
3332 3404
3333The configuration for a "naked" win32 using the Microsoft runtime 3405The configuration for a "naked" win32 using the Microsoft runtime
3334libraries and raw winsocket select is: 3406libraries and raw winsocket select is:
3374In addition to a working ISO-C implementation, libev relies on a few 3446In addition to a working ISO-C implementation, libev relies on a few
3375additional extensions: 3447additional extensions:
3376 3448
3377=over 4 3449=over 4
3378 3450
3451=item C<void (*)(ev_watcher_type *, int revents)> must have compatible
3452calling conventions regardless of C<ev_watcher_type *>.
3453
3454Libev assumes not only that all watcher pointers have the same internal
3455structure (guaranteed by POSIX but not by ISO C for example), but it also
3456assumes that the same (machine) code can be used to call any watcher
3457callback: The watcher callbacks have different type signatures, but libev
3458calls them using an C<ev_watcher *> internally.
3459
3379=item C<sig_atomic_t volatile> must be thread-atomic as well 3460=item C<sig_atomic_t volatile> must be thread-atomic as well
3380 3461
3381The type C<sig_atomic_t volatile> (or whatever is defined as 3462The type C<sig_atomic_t volatile> (or whatever is defined as
3382C<EV_ATOMIC_T>) must be atomic w.r.t. accesses from different 3463C<EV_ATOMIC_T>) must be atomic w.r.t. accesses from different
3383threads. This is not part of the specification for C<sig_atomic_t>, but is 3464threads. This is not part of the specification for C<sig_atomic_t>, but is

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines