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

Comparing libev/ev.pod (file contents):
Revision 1.166 by root, Tue Jun 3 03:48:10 2008 UTC vs.
Revision 1.172 by root, Wed Aug 6 07:01:25 2008 UTC

604 604
605Here are the gory details of what C<ev_loop> does: 605Here are the gory details of what C<ev_loop> does:
606 606
607 - Before the first iteration, call any pending watchers. 607 - Before the first iteration, call any pending watchers.
608 * If EVFLAG_FORKCHECK was used, check for a fork. 608 * If EVFLAG_FORKCHECK was used, check for a fork.
609 - If a fork was detected, queue and call all fork watchers. 609 - If a fork was detected (by any means), queue and call all fork watchers.
610 - Queue and call all prepare watchers. 610 - Queue and call all prepare watchers.
611 - If we have been forked, recreate the kernel state. 611 - If we have been forked, detach and recreate the kernel state
612 as to not disturb the other process.
612 - Update the kernel state with all outstanding changes. 613 - Update the kernel state with all outstanding changes.
613 - Update the "event loop time". 614 - Update the "event loop time" (ev_now ()).
614 - Calculate for how long to sleep or block, if at all 615 - Calculate for how long to sleep or block, if at all
615 (active idle watchers, EVLOOP_NONBLOCK or not having 616 (active idle watchers, EVLOOP_NONBLOCK or not having
616 any active watchers at all will result in not sleeping). 617 any active watchers at all will result in not sleeping).
617 - Sleep if the I/O and timer collect interval say so. 618 - Sleep if the I/O and timer collect interval say so.
618 - Block the process, waiting for any events. 619 - Block the process, waiting for any events.
619 - Queue all outstanding I/O (fd) events. 620 - Queue all outstanding I/O (fd) events.
620 - Update the "event loop time" and do time jump handling. 621 - Update the "event loop time" (ev_now ()), and do time jump adjustments.
621 - Queue all outstanding timers. 622 - Queue all outstanding timers.
622 - Queue all outstanding periodics. 623 - Queue all outstanding periodics.
623 - If no events are pending now, queue all idle watchers. 624 - Unless any events are pending now, queue all idle watchers.
624 - Queue all check watchers. 625 - Queue all check watchers.
625 - Call all queued watchers in reverse order (i.e. check watchers first). 626 - 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 627 Signals and child watchers are implemented as I/O watchers, and will
627 be handled here by queueing them when their watcher gets executed. 628 be handled here by queueing them when their watcher gets executed.
628 - If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK 629 - If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK
633anymore. 634anymore.
634 635
635 ... queue jobs here, make sure they register event watchers as long 636 ... 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..) 637 ... as they still have work to do (even an idle watcher will do..)
637 ev_loop (my_loop, 0); 638 ev_loop (my_loop, 0);
638 ... jobs done. yeah! 639 ... jobs done or somebody called unloop. yeah!
639 640
640=item ev_unloop (loop, how) 641=item ev_unloop (loop, how)
641 642
642Can be used to make a call to C<ev_loop> return early (but only after it 643Can 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 644has processed all outstanding events). The C<how> argument must be either
679=item ev_set_io_collect_interval (loop, ev_tstamp interval) 680=item ev_set_io_collect_interval (loop, ev_tstamp interval)
680 681
681=item ev_set_timeout_collect_interval (loop, ev_tstamp interval) 682=item ev_set_timeout_collect_interval (loop, ev_tstamp interval)
682 683
683These advanced functions influence the time that libev will spend waiting 684These advanced functions influence the time that libev will spend waiting
684for events. Both are by default C<0>, meaning that libev will try to 685for events. Both time intervals are by default C<0>, meaning that libev
685invoke timer/periodic callbacks and I/O callbacks with minimum latency. 686will try to invoke timer/periodic callbacks and I/O callbacks with minimum
687latency.
686 688
687Setting these to a higher value (the C<interval> I<must> be >= C<0>) 689Setting 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 690allows libev to delay invocation of I/O and timer/periodic callbacks
689increase efficiency of loop iterations. 691to increase efficiency of loop iterations (or to increase power-saving
692opportunities).
690 693
691The background is that sometimes your program runs just fast enough to 694The background is that sometimes your program runs just fast enough to
692handle one (or very few) event(s) per loop iteration. While this makes 695handle 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 696the 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 697events, especially with backends like C<select ()> which have a high
709Many (busy) programs can usually benefit by setting the I/O collect 712Many (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 713interval 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 714interactive 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>, 715usually 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. 716as this approaches the timing granularity of most systems.
717
718Setting the I<timeout collect interval> can improve the opportunity for
719saving power, as the program will "bundle" timer callback invocations that
720are "near" in time together, by delaying some, thus reducing the number of
721times the process sleeps and wakes up again. Another useful technique to
722reduce iterations/wake-ups is to use C<ev_periodic> watchers and make sure
723they fire on, say, one-second boundaries only.
714 724
715=item ev_loop_verify (loop) 725=item ev_loop_verify (loop)
716 726
717This function only does something when C<EV_VERIFY> support has been 727This function only does something when C<EV_VERIFY> support has been
718compiled in. It tries to go through all internal structures and checks 728compiled in. It tries to go through all internal structures and checks
1662will be no polling. 1672will be no polling.
1663 1673
1664=head3 ABI Issues (Largefile Support) 1674=head3 ABI Issues (Largefile Support)
1665 1675
1666Libev by default (unless the user overrides this) uses the default 1676Libev by default (unless the user overrides this) uses the default
1667compilation environment, which means that on systems with optionally 1677compilation environment, which means that on systems with large file
1668disabled large file support, you get the 32 bit version of the stat 1678support disabled by default, you get the 32 bit version of the stat
1669structure. When using the library from programs that change the ABI to 1679structure. 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 1680use 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 1681compile 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 1682obviously the case with any flags that change the ABI, but the problem is
1673most noticeably with ev_stat and large file support. 1683most noticeably disabled with ev_stat and large file support.
1684
1685The solution for this is to lobby your distribution maker to make large
1686file interfaces available by default (as e.g. FreeBSD does) and not
1687optional. Libev cannot simply switch on large file support because it has
1688to exchange stat structures with application programs compiled using the
1689default compilation environment.
1674 1690
1675=head3 Inotify 1691=head3 Inotify
1676 1692
1677When C<inotify (7)> support has been compiled into libev (generally only 1693When 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 1694available on Linux) and present at runtime, it will be used to speed up
2648L<http://rev.rubyforge.org/>. 2664L<http://rev.rubyforge.org/>.
2649 2665
2650=item D 2666=item D
2651 2667
2652Leandro Lucarella has written a D language binding (F<ev.d>) for libev, to 2668Leandro Lucarella has written a D language binding (F<ev.d>) for libev, to
2653be found at L<http://git.llucax.com.ar/?p=software/ev.d.git;a=summary>. 2669be found at L<http://proj.llucax.com.ar/wiki/evd>.
2654 2670
2655=back 2671=back
2656 2672
2657 2673
2658=head1 MACRO MAGIC 2674=head1 MACRO MAGIC
3182parallel from multiple threads, calls with the same loop parameter must be 3198parallel from multiple threads, calls with the same loop parameter must be
3183done serially (but can be done from different threads, as long as only one 3199done serially (but can be done from different threads, as long as only one
3184thread ever is inside a call at any point in time, e.g. by using a mutex 3200thread ever is inside a call at any point in time, e.g. by using a mutex
3185per loop). 3201per loop).
3186 3202
3187If you want to know which design is best for your problem, then I cannot 3203If you want to know which design (one loop, locking, or multiple loops
3188help you but by giving some generic advice: 3204without or something else still) is best for your problem, then I cannot
3205help you. I can give some generic advice however:
3189 3206
3190=over 4 3207=over 4
3191 3208
3192=item * most applications have a main thread: use the default libev loop 3209=item * most applications have a main thread: use the default libev loop
3193in that thread, or create a separate thread running only the default loop. 3210in that thread, or create a separate thread running only the default loop.
3326more than a hundred or so sockets, then likely it needs to use a totally 3343more than a hundred or so sockets, then likely it needs to use a totally
3327different implementation for windows, as libev offers the POSIX readiness 3344different implementation for windows, as libev offers the POSIX readiness
3328notification model, which cannot be implemented efficiently on windows 3345notification model, which cannot be implemented efficiently on windows
3329(Microsoft monopoly games). 3346(Microsoft monopoly games).
3330 3347
3348A typical way to use libev under windows is to embed it (see the embedding
3349section for details) and use the following F<evwrap.h> header file instead
3350of F<ev.h>:
3351
3352 #define EV_STANDALONE /* keeps ev from requiring config.h */
3353 #define EV_SELECT_IS_WINSOCKET 1 /* configure libev for windows select */
3354
3355 #include "ev.h"
3356
3357And compile the following F<evwrap.c> file into your project (make sure
3358you do I<not> compile the F<ev.c> or any other embedded soruce files!):
3359
3360 #include "evwrap.h"
3361 #include "ev.c"
3362
3331=over 4 3363=over 4
3332 3364
3333=item The winsocket select function 3365=item The winsocket select function
3334 3366
3335The winsocket C<select> function doesn't follow POSIX in that it 3367The winsocket C<select> function doesn't follow POSIX in that it
3336requires socket I<handles> and not socket I<file descriptors> (it is 3368requires socket I<handles> and not socket I<file descriptors> (it is
3337also extremely buggy). This makes select very inefficient, and also 3369also extremely buggy). This makes select very inefficient, and also
3338requires a mapping from file descriptors to socket handles. See the 3370requires a mapping from file descriptors to socket handles (the Microsoft
3371C runtime provides the function C<_open_osfhandle> for this). See the
3339discussion of the C<EV_SELECT_USE_FD_SET>, C<EV_SELECT_IS_WINSOCKET> and 3372discussion of the C<EV_SELECT_USE_FD_SET>, C<EV_SELECT_IS_WINSOCKET> and
3340C<EV_FD_TO_WIN32_HANDLE> preprocessor symbols for more info. 3373C<EV_FD_TO_WIN32_HANDLE> preprocessor symbols for more info.
3341 3374
3342The configuration for a "naked" win32 using the Microsoft runtime 3375The configuration for a "naked" win32 using the Microsoft runtime
3343libraries and raw winsocket select is: 3376libraries and raw winsocket select is:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines