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

Comparing libev/ev.pod (file contents):
Revision 1.170 by root, Sat Jul 5 02:25:40 2008 UTC vs.
Revision 1.178 by root, Sat Sep 13 18:25:50 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
984 { 1006 {
985 struct ev_io io; 1007 struct ev_io io;
986 int otherfd; 1008 int otherfd;
987 void *somedata; 1009 void *somedata;
988 struct whatever *mostinteresting; 1010 struct whatever *mostinteresting;
989 } 1011 };
1012
1013 ...
1014 struct my_io w;
1015 ev_io_init (&w.io, my_cb, fd, EV_READ);
990 1016
991And since your callback will be called with a pointer to the watcher, you 1017And since your callback will be called with a pointer to the watcher, you
992can cast it back to your own type: 1018can cast it back to your own type:
993 1019
994 static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents) 1020 static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents)
998 } 1024 }
999 1025
1000More interesting and less C-conformant ways of casting your callback type 1026More interesting and less C-conformant ways of casting your callback type
1001instead have been omitted. 1027instead have been omitted.
1002 1028
1003Another common scenario is having some data structure with multiple 1029Another common scenario is to use some data structure with multiple
1004watchers: 1030embedded watchers:
1005 1031
1006 struct my_biggy 1032 struct my_biggy
1007 { 1033 {
1008 int some_data; 1034 int some_data;
1009 ev_timer t1; 1035 ev_timer t1;
1010 ev_timer t2; 1036 ev_timer t2;
1011 } 1037 }
1012 1038
1013In this case getting the pointer to C<my_biggy> is a bit more complicated, 1039In this case getting the pointer to C<my_biggy> is a bit more
1014you need to use C<offsetof>: 1040complicated: Either you store the address of your C<my_biggy> struct
1041in the C<data> member of the watcher, or you need to use some pointer
1042arithmetic using C<offsetof> inside your watchers:
1015 1043
1016 #include <stddef.h> 1044 #include <stddef.h>
1017 1045
1018 static void 1046 static void
1019 t1_cb (EV_P_ struct ev_timer *w, int revents) 1047 t1_cb (EV_P_ struct ev_timer *w, int revents)
1124C<EVBACKEND_POLL>. 1152C<EVBACKEND_POLL>.
1125 1153
1126=head3 The special problem of SIGPIPE 1154=head3 The special problem of SIGPIPE
1127 1155
1128While not really specific to libev, it is easy to forget about SIGPIPE: 1156While 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 1157when 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 1158send a SIGPIPE, which, by default, aborts your program. For most programs
1131programs this is sensible behaviour, for daemons, this is usually 1159this is sensible behaviour, for daemons, this is usually undesirable.
1132undesirable.
1133 1160
1134So when you encounter spurious, unexplained daemon exits, make sure you 1161So when you encounter spurious, unexplained daemon exits, make sure you
1135ignore SIGPIPE (and maybe make sure you log the exit status of your daemon 1162ignore SIGPIPE (and maybe make sure you log the exit status of your daemon
1136somewhere, as that would have given you a big clue). 1163somewhere, as that would have given you a big clue).
1137 1164
1188times out after an hour and you reset your system clock to January last 1215times 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 1216year, it will still time out after (roughly) and hour. "Roughly" because
1190detecting time jumps is hard, and some inaccuracies are unavoidable (the 1217detecting time jumps is hard, and some inaccuracies are unavoidable (the
1191monotonic clock option helps a lot here). 1218monotonic clock option helps a lot here).
1192 1219
1220The callback is guaranteed to be invoked only after its timeout has passed,
1221but if multiple timers become ready during the same loop iteration then
1222order of execution is undefined.
1223
1224=head3 The special problem of time updates
1225
1226Establishing the current time is a costly operation (it usually takes at
1227least two system calls): EV therefore updates its idea of the current
1228time only before and after C<ev_loop> polls for new events, which causes
1229a growing difference between C<ev_now ()> and C<ev_time ()> when handling
1230lots of events.
1231
1193The relative timeouts are calculated relative to the C<ev_now ()> 1232The relative timeouts are calculated relative to the C<ev_now ()>
1194time. This is usually the right thing as this timestamp refers to the time 1233time. This is usually the right thing as this timestamp refers to the time
1195of the event triggering whatever timeout you are modifying/starting. If 1234of 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 1235you 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: 1236timeout on the current time, use something like this to adjust for this:
1198 1237
1199 ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); 1238 ev_timer_set (&timer, after + ev_now () - ev_time (), 0.);
1200 1239
1201The callback is guaranteed to be invoked only after its timeout has passed, 1240If 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 1241update of the time returned by C<ev_now ()> by calling C<ev_now_update
1203order of execution is undefined. 1242()>.
1204 1243
1205=head3 Watcher-Specific Functions and Data Members 1244=head3 Watcher-Specific Functions and Data Members
1206 1245
1207=over 4 1246=over 4
1208 1247
1559handler, you can override it easily by installing your own handler for 1598handler, you can override it easily by installing your own handler for
1560C<SIGCHLD> after initialising the default loop, and making sure the 1599C<SIGCHLD> after initialising the default loop, and making sure the
1561default loop never gets destroyed. You are encouraged, however, to use an 1600default loop never gets destroyed. You are encouraged, however, to use an
1562event-based approach to child reaping and thus use libev's support for 1601event-based approach to child reaping and thus use libev's support for
1563that, so other libev users can use C<ev_child> watchers freely. 1602that, so other libev users can use C<ev_child> watchers freely.
1603
1604=head3 Stopping the Child Watcher
1605
1606Currently, the child watcher never gets stopped, even when the
1607child terminates, so normally one needs to stop the watcher in the
1608callback. Future versions of libev might stop the watcher automatically
1609when a child exit is detected.
1564 1610
1565=head3 Watcher-Specific Functions and Data Members 1611=head3 Watcher-Specific Functions and Data Members
1566 1612
1567=over 4 1613=over 4
1568 1614
2654L<http://rev.rubyforge.org/>. 2700L<http://rev.rubyforge.org/>.
2655 2701
2656=item D 2702=item D
2657 2703
2658Leandro Lucarella has written a D language binding (F<ev.d>) for libev, to 2704Leandro Lucarella has written a D language binding (F<ev.d>) for libev, to
2659be found at L<http://git.llucax.com.ar/?p=software/ev.d.git;a=summary>. 2705be found at L<http://proj.llucax.com.ar/wiki/evd>.
2660 2706
2661=back 2707=back
2662 2708
2663 2709
2664=head1 MACRO MAGIC 2710=head1 MACRO MAGIC

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines