… | |
… | |
312 | C<cb>, which specifies a callback to be called when the condition variable |
312 | C<cb>, which specifies a callback to be called when the condition variable |
313 | becomes true. |
313 | becomes true. |
314 | |
314 | |
315 | After creation, the condition variable is "false" until it becomes "true" |
315 | After creation, the condition variable is "false" until it becomes "true" |
316 | by calling the C<send> method (or calling the condition variable as if it |
316 | by calling the C<send> method (or calling the condition variable as if it |
317 | were a callback). |
317 | were a callback, read about the caveats in the description for the C<< |
|
|
318 | ->send >> method). |
318 | |
319 | |
319 | Condition variables are similar to callbacks, except that you can |
320 | Condition variables are similar to callbacks, except that you can |
320 | optionally wait for them. They can also be called merge points - points |
321 | optionally wait for them. They can also be called merge points - points |
321 | in time where multiple outstanding events have been processed. And yet |
322 | in time where multiple outstanding events have been processed. And yet |
322 | another way to call them is transactions - each condition variable can be |
323 | another way to call them is transactions - each condition variable can be |
… | |
… | |
394 | immediately from within send. |
395 | immediately from within send. |
395 | |
396 | |
396 | Any arguments passed to the C<send> call will be returned by all |
397 | Any arguments passed to the C<send> call will be returned by all |
397 | future C<< ->recv >> calls. |
398 | future C<< ->recv >> calls. |
398 | |
399 | |
399 | Condition variables are overloaded so one can call them directly (as a |
400 | Condition variables are overloaded so one can call them directly |
400 | code reference). Calling them directly is the same as calling C<send>. |
401 | (as a code reference). Calling them directly is the same as calling |
|
|
402 | C<send>. Note, however, that many C-based event loops do not handle |
|
|
403 | overloading, so as tempting as it may be, passing a condition variable |
|
|
404 | instead of a callback does not work. Both the pure perl and EV loops |
|
|
405 | support overloading, however, as well as all functions that use perl to |
|
|
406 | invoke a callback (as in L<AnyEvent::Socket> and L<AnyEvent::DNS> for |
|
|
407 | example). |
401 | |
408 | |
402 | =item $cv->croak ($error) |
409 | =item $cv->croak ($error) |
403 | |
410 | |
404 | Similar to send, but causes all call's to C<< ->recv >> to invoke |
411 | Similar to send, but causes all call's to C<< ->recv >> to invoke |
405 | C<Carp::croak> with the given error message/object/scalar. |
412 | C<Carp::croak> with the given error message/object/scalar. |
… | |
… | |
612 | |
619 | |
613 | If it doesn't care, it can just "use AnyEvent" and use it itself, or not |
620 | If it doesn't care, it can just "use AnyEvent" and use it itself, or not |
614 | do anything special (it does not need to be event-based) and let AnyEvent |
621 | do anything special (it does not need to be event-based) and let AnyEvent |
615 | decide which implementation to chose if some module relies on it. |
622 | decide which implementation to chose if some module relies on it. |
616 | |
623 | |
617 | If the main program relies on a specific event model. For example, in |
624 | If the main program relies on a specific event model - for example, in |
618 | Gtk2 programs you have to rely on the Glib module. You should load the |
625 | Gtk2 programs you have to rely on the Glib module - you should load the |
619 | event module before loading AnyEvent or any module that uses it: generally |
626 | event module before loading AnyEvent or any module that uses it: generally |
620 | speaking, you should load it as early as possible. The reason is that |
627 | speaking, you should load it as early as possible. The reason is that |
621 | modules might create watchers when they are loaded, and AnyEvent will |
628 | modules might create watchers when they are loaded, and AnyEvent will |
622 | decide on the event model to use as soon as it creates watchers, and it |
629 | decide on the event model to use as soon as it creates watchers, and it |
623 | might chose the wrong one unless you load the correct one yourself. |
630 | might chose the wrong one unless you load the correct one yourself. |
624 | |
631 | |
625 | You can chose to use a rather inefficient pure-perl implementation by |
632 | You can chose to use a pure-perl implementation by loading the |
626 | loading the C<AnyEvent::Impl::Perl> module, which gives you similar |
633 | C<AnyEvent::Impl::Perl> module, which gives you similar behaviour |
627 | behaviour everywhere, but letting AnyEvent chose is generally better. |
634 | everywhere, but letting AnyEvent chose the model is generally better. |
|
|
635 | |
|
|
636 | =head2 MAINLOOP EMULATION |
|
|
637 | |
|
|
638 | Sometimes (often for short test scripts, or even standalone programs who |
|
|
639 | only want to use AnyEvent), you do not want to run a specific event loop. |
|
|
640 | |
|
|
641 | In that case, you can use a condition variable like this: |
|
|
642 | |
|
|
643 | AnyEvent->condvar->recv; |
|
|
644 | |
|
|
645 | This has the effect of entering the event loop and looping forever. |
|
|
646 | |
|
|
647 | Note that usually your program has some exit condition, in which case |
|
|
648 | it is better to use the "traditional" approach of storing a condition |
|
|
649 | variable somewhere, waiting for it, and sending it when the program should |
|
|
650 | exit cleanly. |
|
|
651 | |
628 | |
652 | |
629 | =head1 OTHER MODULES |
653 | =head1 OTHER MODULES |
630 | |
654 | |
631 | The following is a non-exhaustive list of additional modules that use |
655 | The following is a non-exhaustive list of additional modules that use |
632 | AnyEvent and can therefore be mixed easily with other AnyEvent modules |
656 | AnyEvent and can therefore be mixed easily with other AnyEvent modules |
… | |
… | |
648 | |
672 | |
649 | Provides various utility functions for (internet protocol) sockets, |
673 | Provides various utility functions for (internet protocol) sockets, |
650 | addresses and name resolution. Also functions to create non-blocking tcp |
674 | addresses and name resolution. Also functions to create non-blocking tcp |
651 | connections or tcp servers, with IPv6 and SRV record support and more. |
675 | connections or tcp servers, with IPv6 and SRV record support and more. |
652 | |
676 | |
|
|
677 | =item L<AnyEvent::DNS> |
|
|
678 | |
|
|
679 | Provides rich asynchronous DNS resolver capabilities. |
|
|
680 | |
653 | =item L<AnyEvent::HTTPD> |
681 | =item L<AnyEvent::HTTPD> |
654 | |
682 | |
655 | Provides a simple web application server framework. |
683 | Provides a simple web application server framework. |
656 | |
|
|
657 | =item L<AnyEvent::DNS> |
|
|
658 | |
|
|
659 | Provides rich asynchronous DNS resolver capabilities. |
|
|
660 | |
684 | |
661 | =item L<AnyEvent::FastPing> |
685 | =item L<AnyEvent::FastPing> |
662 | |
686 | |
663 | The fastest ping in the west. |
687 | The fastest ping in the west. |
664 | |
688 | |
… | |
… | |
707 | no warnings; |
731 | no warnings; |
708 | use strict; |
732 | use strict; |
709 | |
733 | |
710 | use Carp; |
734 | use Carp; |
711 | |
735 | |
712 | our $VERSION = '4.03'; |
736 | our $VERSION = '4.05'; |
713 | our $MODEL; |
737 | our $MODEL; |
714 | |
738 | |
715 | our $AUTOLOAD; |
739 | our $AUTOLOAD; |
716 | our @ISA; |
740 | our @ISA; |
717 | |
741 | |
|
|
742 | our @REGISTRY; |
|
|
743 | |
|
|
744 | our $WIN32; |
|
|
745 | |
|
|
746 | BEGIN { |
|
|
747 | my $win32 = ! ! ($^O =~ /mswin32/i); |
|
|
748 | eval "sub WIN32(){ $win32 }"; |
|
|
749 | } |
|
|
750 | |
718 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
751 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
719 | |
752 | |
720 | our @REGISTRY; |
753 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
721 | |
|
|
722 | our %PROTOCOL; # (ipv4|ipv6) => (1|2) |
|
|
723 | |
754 | |
724 | { |
755 | { |
725 | my $idx; |
756 | my $idx; |
726 | $PROTOCOL{$_} = ++$idx |
757 | $PROTOCOL{$_} = ++$idx |
|
|
758 | for reverse split /\s*,\s*/, |
727 | for split /\s*,\s*/, $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
759 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
728 | } |
760 | } |
729 | |
761 | |
730 | my @models = ( |
762 | my @models = ( |
731 | [EV:: => AnyEvent::Impl::EV::], |
763 | [EV:: => AnyEvent::Impl::EV::], |
732 | [Event:: => AnyEvent::Impl::Event::], |
764 | [Event:: => AnyEvent::Impl::Event::], |
733 | [Tk:: => AnyEvent::Impl::Tk::], |
|
|
734 | [Wx:: => AnyEvent::Impl::POE::], |
|
|
735 | [Prima:: => AnyEvent::Impl::POE::], |
|
|
736 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
765 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
737 | # everything below here will not be autoprobed as the pureperl backend should work everywhere |
766 | # everything below here will not be autoprobed |
738 | [Glib:: => AnyEvent::Impl::Glib::], |
767 | # as the pureperl backend should work everywhere |
|
|
768 | # and is usually faster |
|
|
769 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
|
|
770 | [Glib:: => AnyEvent::Impl::Glib::], # becomes extremely slow with many watchers |
739 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
771 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
740 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
772 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
741 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
773 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
|
|
774 | [Wx:: => AnyEvent::Impl::POE::], |
|
|
775 | [Prima:: => AnyEvent::Impl::POE::], |
742 | ); |
776 | ); |
743 | |
777 | |
744 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
778 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
745 | |
779 | |
746 | our @post_detect; |
780 | our @post_detect; |
… | |
… | |
766 | } |
800 | } |
767 | |
801 | |
768 | sub detect() { |
802 | sub detect() { |
769 | unless ($MODEL) { |
803 | unless ($MODEL) { |
770 | no strict 'refs'; |
804 | no strict 'refs'; |
|
|
805 | local $SIG{__DIE__}; |
771 | |
806 | |
772 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
807 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
773 | my $model = "AnyEvent::Impl::$1"; |
808 | my $model = "AnyEvent::Impl::$1"; |
774 | if (eval "require $model") { |
809 | if (eval "require $model") { |
775 | $MODEL = $model; |
810 | $MODEL = $model; |
… | |
… | |
898 | or Carp::croak "required option 'pid' is missing"; |
933 | or Carp::croak "required option 'pid' is missing"; |
899 | |
934 | |
900 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
935 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
901 | |
936 | |
902 | unless ($WNOHANG) { |
937 | unless ($WNOHANG) { |
903 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
938 | $WNOHANG = eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
904 | } |
939 | } |
905 | |
940 | |
906 | unless ($CHLD_W) { |
941 | unless ($CHLD_W) { |
907 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
942 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
908 | # child could be a zombie already, so make at least one round |
943 | # child could be a zombie already, so make at least one round |