… | |
… | |
604 | |
604 | |
605 | Here are the gory details of what C<ev_loop> does: |
605 | Here 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 |
… | |
… | |
633 | anymore. |
634 | anymore. |
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 | |
642 | Can be used to make a call to C<ev_loop> return early (but only after it |
643 | Can be used to make a call to C<ev_loop> return early (but only after it |
643 | has processed all outstanding events). The C<how> argument must be either |
644 | has 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 | |
683 | These advanced functions influence the time that libev will spend waiting |
684 | These advanced functions influence the time that libev will spend waiting |
684 | for events. Both are by default C<0>, meaning that libev will try to |
685 | for events. Both time intervals are by default C<0>, meaning that libev |
685 | invoke timer/periodic callbacks and I/O callbacks with minimum latency. |
686 | will try to invoke timer/periodic callbacks and I/O callbacks with minimum |
|
|
687 | latency. |
686 | |
688 | |
687 | Setting these to a higher value (the C<interval> I<must> be >= C<0>) |
689 | Setting these to a higher value (the C<interval> I<must> be >= C<0>) |
688 | allows libev to delay invocation of I/O and timer/periodic callbacks to |
690 | allows libev to delay invocation of I/O and timer/periodic callbacks |
689 | increase efficiency of loop iterations. |
691 | to increase efficiency of loop iterations (or to increase power-saving |
|
|
692 | opportunities). |
690 | |
693 | |
691 | The background is that sometimes your program runs just fast enough to |
694 | The background is that sometimes your program runs just fast enough to |
692 | handle one (or very few) event(s) per loop iteration. While this makes |
695 | handle one (or very few) event(s) per loop iteration. While this makes |
693 | the program responsive, it also wastes a lot of CPU time to poll for new |
696 | the program responsive, it also wastes a lot of CPU time to poll for new |
694 | events, especially with backends like C<select ()> which have a high |
697 | events, especially with backends like C<select ()> which have a high |
… | |
… | |
709 | Many (busy) programs can usually benefit by setting the I/O collect |
712 | Many (busy) programs can usually benefit by setting the I/O collect |
710 | interval to a value near C<0.1> or so, which is often enough for |
713 | interval to a value near C<0.1> or so, which is often enough for |
711 | interactive servers (of course not for games), likewise for timeouts. It |
714 | interactive servers (of course not for games), likewise for timeouts. It |
712 | usually doesn't make much sense to set it to a lower value than C<0.01>, |
715 | usually doesn't make much sense to set it to a lower value than C<0.01>, |
713 | as this approaches the timing granularity of most systems. |
716 | as this approaches the timing granularity of most systems. |
|
|
717 | |
|
|
718 | Setting the I<timeout collect interval> can improve the opportunity for |
|
|
719 | saving power, as the program will "bundle" timer callback invocations that |
|
|
720 | are "near" in time together, by delaying some, thus reducing the number of |
|
|
721 | times the process sleeps and wakes up again. Another useful technique to |
|
|
722 | reduce iterations/wake-ups is to use C<ev_periodic> watchers and make sure |
|
|
723 | they fire on, say, one-second boundaries only. |
714 | |
724 | |
715 | =item ev_loop_verify (loop) |
725 | =item ev_loop_verify (loop) |
716 | |
726 | |
717 | This function only does something when C<EV_VERIFY> support has been |
727 | This function only does something when C<EV_VERIFY> support has been |
718 | compiled in. It tries to go through all internal structures and checks |
728 | compiled in. It tries to go through all internal structures and checks |
… | |
… | |
1559 | handler, you can override it easily by installing your own handler for |
1569 | handler, you can override it easily by installing your own handler for |
1560 | C<SIGCHLD> after initialising the default loop, and making sure the |
1570 | C<SIGCHLD> after initialising the default loop, and making sure the |
1561 | default loop never gets destroyed. You are encouraged, however, to use an |
1571 | default loop never gets destroyed. You are encouraged, however, to use an |
1562 | event-based approach to child reaping and thus use libev's support for |
1572 | event-based approach to child reaping and thus use libev's support for |
1563 | that, so other libev users can use C<ev_child> watchers freely. |
1573 | that, so other libev users can use C<ev_child> watchers freely. |
|
|
1574 | |
|
|
1575 | =head3 Stopping the Child Watcher |
|
|
1576 | |
|
|
1577 | Currently, the child watcher never gets stopped, even when the |
|
|
1578 | child terminates, so normally one needs to stop the watcher in the |
|
|
1579 | callback. Future versions of libev might stop the watcher automatically |
|
|
1580 | when a child exit is detected. |
1564 | |
1581 | |
1565 | =head3 Watcher-Specific Functions and Data Members |
1582 | =head3 Watcher-Specific Functions and Data Members |
1566 | |
1583 | |
1567 | =over 4 |
1584 | =over 4 |
1568 | |
1585 | |
… | |
… | |
2654 | L<http://rev.rubyforge.org/>. |
2671 | L<http://rev.rubyforge.org/>. |
2655 | |
2672 | |
2656 | =item D |
2673 | =item D |
2657 | |
2674 | |
2658 | Leandro Lucarella has written a D language binding (F<ev.d>) for libev, to |
2675 | Leandro Lucarella has written a D language binding (F<ev.d>) for libev, to |
2659 | be found at L<http://git.llucax.com.ar/?p=software/ev.d.git;a=summary>. |
2676 | be found at L<http://proj.llucax.com.ar/wiki/evd>. |
2660 | |
2677 | |
2661 | =back |
2678 | =back |
2662 | |
2679 | |
2663 | |
2680 | |
2664 | =head1 MACRO MAGIC |
2681 | =head1 MACRO MAGIC |
… | |
… | |
3188 | parallel from multiple threads, calls with the same loop parameter must be |
3205 | parallel from multiple threads, calls with the same loop parameter must be |
3189 | done serially (but can be done from different threads, as long as only one |
3206 | done serially (but can be done from different threads, as long as only one |
3190 | thread ever is inside a call at any point in time, e.g. by using a mutex |
3207 | thread ever is inside a call at any point in time, e.g. by using a mutex |
3191 | per loop). |
3208 | per loop). |
3192 | |
3209 | |
3193 | If you want to know which design is best for your problem, then I cannot |
3210 | If you want to know which design (one loop, locking, or multiple loops |
3194 | help you but by giving some generic advice: |
3211 | without or something else still) is best for your problem, then I cannot |
|
|
3212 | help you. I can give some generic advice however: |
3195 | |
3213 | |
3196 | =over 4 |
3214 | =over 4 |
3197 | |
3215 | |
3198 | =item * most applications have a main thread: use the default libev loop |
3216 | =item * most applications have a main thread: use the default libev loop |
3199 | in that thread, or create a separate thread running only the default loop. |
3217 | in that thread, or create a separate thread running only the default loop. |