… | |
… | |
551 | usually a better approach for this kind of thing. |
551 | usually a better approach for this kind of thing. |
552 | |
552 | |
553 | Here are the gory details of what C<ev_loop> does: |
553 | Here are the gory details of what C<ev_loop> does: |
554 | |
554 | |
555 | - Before the first iteration, call any pending watchers. |
555 | - Before the first iteration, call any pending watchers. |
556 | * If there are no active watchers (reference count is zero), return. |
556 | * If EVFLAG_FORKCHECK was used, check for a fork. |
557 | - Queue all prepare watchers and then call all outstanding watchers. |
557 | - If a fork was detected, queue and call all fork watchers. |
|
|
558 | - Queue and call all prepare watchers. |
558 | - If we have been forked, recreate the kernel state. |
559 | - If we have been forked, recreate the kernel state. |
559 | - Update the kernel state with all outstanding changes. |
560 | - Update the kernel state with all outstanding changes. |
560 | - Update the "event loop time". |
561 | - Update the "event loop time". |
561 | - Calculate for how long to block. |
562 | - Calculate for how long to sleep or block, if at all |
|
|
563 | (active idle watchers, EVLOOP_NONBLOCK or not having |
|
|
564 | any active watchers at all will result in not sleeping). |
|
|
565 | - Sleep if the I/O and timer collect interval say so. |
562 | - Block the process, waiting for any events. |
566 | - Block the process, waiting for any events. |
563 | - Queue all outstanding I/O (fd) events. |
567 | - Queue all outstanding I/O (fd) events. |
564 | - Update the "event loop time" and do time jump handling. |
568 | - Update the "event loop time" and do time jump handling. |
565 | - Queue all outstanding timers. |
569 | - Queue all outstanding timers. |
566 | - Queue all outstanding periodics. |
570 | - Queue all outstanding periodics. |
567 | - If no events are pending now, queue all idle watchers. |
571 | - If no events are pending now, queue all idle watchers. |
568 | - Queue all check watchers. |
572 | - Queue all check watchers. |
569 | - Call all queued watchers in reverse order (i.e. check watchers first). |
573 | - Call all queued watchers in reverse order (i.e. check watchers first). |
570 | Signals and child watchers are implemented as I/O watchers, and will |
574 | Signals and child watchers are implemented as I/O watchers, and will |
571 | be handled here by queueing them when their watcher gets executed. |
575 | be handled here by queueing them when their watcher gets executed. |
572 | - If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
576 | - If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
573 | were used, return, otherwise continue with step *. |
577 | were used, or there are no active watchers, return, otherwise |
|
|
578 | continue with step *. |
574 | |
579 | |
575 | Example: Queue some jobs and then loop until no events are outsanding |
580 | Example: Queue some jobs and then loop until no events are outstanding |
576 | anymore. |
581 | anymore. |
577 | |
582 | |
578 | ... queue jobs here, make sure they register event watchers as long |
583 | ... queue jobs here, make sure they register event watchers as long |
579 | ... as they still have work to do (even an idle watcher will do..) |
584 | ... as they still have work to do (even an idle watcher will do..) |
580 | ev_loop (my_loop, 0); |
585 | ev_loop (my_loop, 0); |
… | |
… | |
584 | |
589 | |
585 | Can be used to make a call to C<ev_loop> return early (but only after it |
590 | Can be used to make a call to C<ev_loop> return early (but only after it |
586 | has processed all outstanding events). The C<how> argument must be either |
591 | has processed all outstanding events). The C<how> argument must be either |
587 | C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or |
592 | C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or |
588 | C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return. |
593 | C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return. |
|
|
594 | |
|
|
595 | This "unloop state" will be cleared when entering C<ev_loop> again. |
589 | |
596 | |
590 | =item ev_ref (loop) |
597 | =item ev_ref (loop) |
591 | |
598 | |
592 | =item ev_unref (loop) |
599 | =item ev_unref (loop) |
593 | |
600 | |
… | |
… | |
598 | returning, ev_unref() after starting, and ev_ref() before stopping it. For |
605 | returning, ev_unref() after starting, and ev_ref() before stopping it. For |
599 | example, libev itself uses this for its internal signal pipe: It is not |
606 | example, libev itself uses this for its internal signal pipe: It is not |
600 | visible to the libev user and should not keep C<ev_loop> from exiting if |
607 | visible to the libev user and should not keep C<ev_loop> from exiting if |
601 | no event watchers registered by it are active. It is also an excellent |
608 | no event watchers registered by it are active. It is also an excellent |
602 | way to do this for generic recurring timers or from within third-party |
609 | way to do this for generic recurring timers or from within third-party |
603 | libraries. Just remember to I<unref after start> and I<ref before stop>. |
610 | libraries. Just remember to I<unref after start> and I<ref before stop> |
|
|
611 | (but only if the watcher wasn't active before, or was active before, |
|
|
612 | respectively). |
604 | |
613 | |
605 | Example: Create a signal watcher, but keep it from keeping C<ev_loop> |
614 | Example: Create a signal watcher, but keep it from keeping C<ev_loop> |
606 | running when nothing else is active. |
615 | running when nothing else is active. |
607 | |
616 | |
608 | struct ev_signal exitsig; |
617 | struct ev_signal exitsig; |