… | |
… | |
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. |
… | |
… | |
730 | our $MODEL; |
737 | our $MODEL; |
731 | |
738 | |
732 | our $AUTOLOAD; |
739 | our $AUTOLOAD; |
733 | our @ISA; |
740 | our @ISA; |
734 | |
741 | |
|
|
742 | our @REGISTRY; |
|
|
743 | |
|
|
744 | our $WIN32; |
|
|
745 | |
|
|
746 | BEGIN { |
|
|
747 | my $win32 = ! ! ($^O =~ /mswin32/i); |
|
|
748 | eval "sub WIN32(){ $win32 }"; |
|
|
749 | } |
|
|
750 | |
735 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
751 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
736 | |
752 | |
737 | our @REGISTRY; |
753 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
738 | |
|
|
739 | our %PROTOCOL; # (ipv4|ipv6) => (1|2) |
|
|
740 | |
754 | |
741 | { |
755 | { |
742 | my $idx; |
756 | my $idx; |
743 | $PROTOCOL{$_} = ++$idx |
757 | $PROTOCOL{$_} = ++$idx |
|
|
758 | for reverse split /\s*,\s*/, |
744 | for split /\s*,\s*/, $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
759 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
|
|
760 | } |
|
|
761 | |
|
|
762 | sub import { |
|
|
763 | shift; |
|
|
764 | return unless @_; |
|
|
765 | |
|
|
766 | my $pkg = caller; |
|
|
767 | |
|
|
768 | no strict 'refs'; |
|
|
769 | |
|
|
770 | for (@_) { |
|
|
771 | *{"$pkg\::WIN32"} = *WIN32 if $_ eq "WIN32"; |
|
|
772 | } |
745 | } |
773 | } |
746 | |
774 | |
747 | my @models = ( |
775 | my @models = ( |
748 | [EV:: => AnyEvent::Impl::EV::], |
776 | [EV:: => AnyEvent::Impl::EV::], |
749 | [Event:: => AnyEvent::Impl::Event::], |
777 | [Event:: => AnyEvent::Impl::Event::], |
750 | [Tk:: => AnyEvent::Impl::Tk::], |
|
|
751 | [Wx:: => AnyEvent::Impl::POE::], |
|
|
752 | [Prima:: => AnyEvent::Impl::POE::], |
|
|
753 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
778 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
754 | # everything below here will not be autoprobed as the pureperl backend should work everywhere |
779 | # everything below here will not be autoprobed |
755 | [Glib:: => AnyEvent::Impl::Glib::], |
780 | # as the pureperl backend should work everywhere |
|
|
781 | # and is usually faster |
|
|
782 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
|
|
783 | [Glib:: => AnyEvent::Impl::Glib::], # becomes extremely slow with many watchers |
756 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
784 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
757 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
785 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
758 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
786 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
|
|
787 | [Wx:: => AnyEvent::Impl::POE::], |
|
|
788 | [Prima:: => AnyEvent::Impl::POE::], |
759 | ); |
789 | ); |
760 | |
790 | |
761 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
791 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
762 | |
792 | |
763 | our @post_detect; |
793 | our @post_detect; |
… | |
… | |
783 | } |
813 | } |
784 | |
814 | |
785 | sub detect() { |
815 | sub detect() { |
786 | unless ($MODEL) { |
816 | unless ($MODEL) { |
787 | no strict 'refs'; |
817 | no strict 'refs'; |
|
|
818 | local $SIG{__DIE__}; |
788 | |
819 | |
789 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
820 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
790 | my $model = "AnyEvent::Impl::$1"; |
821 | my $model = "AnyEvent::Impl::$1"; |
791 | if (eval "require $model") { |
822 | if (eval "require $model") { |
792 | $MODEL = $model; |
823 | $MODEL = $model; |
… | |
… | |
915 | or Carp::croak "required option 'pid' is missing"; |
946 | or Carp::croak "required option 'pid' is missing"; |
916 | |
947 | |
917 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
948 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
918 | |
949 | |
919 | unless ($WNOHANG) { |
950 | unless ($WNOHANG) { |
920 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
951 | $WNOHANG = eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
921 | } |
952 | } |
922 | |
953 | |
923 | unless ($CHLD_W) { |
954 | unless ($CHLD_W) { |
924 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
955 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
925 | # child could be a zombie already, so make at least one round |
956 | # child could be a zombie already, so make at least one round |