… | |
… | |
311 | >> method, usually without arguments. The only argument pair allowed is |
311 | >> method, usually without arguments. The only argument pair allowed is |
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. |
316 | by calling the C<send> method (or calling the condition variable as if it |
|
|
317 | were a callback, read about the caveats in the description for the C<< |
|
|
318 | ->send >> method). |
317 | |
319 | |
318 | Condition variables are similar to callbacks, except that you can |
320 | Condition variables are similar to callbacks, except that you can |
319 | 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 |
320 | in time where multiple outstanding events have been processed. And yet |
322 | in time where multiple outstanding events have been processed. And yet |
321 | 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 |
… | |
… | |
347 | |
349 | |
348 | There are two "sides" to a condition variable - the "producer side" which |
350 | There are two "sides" to a condition variable - the "producer side" which |
349 | eventually calls C<< -> send >>, and the "consumer side", which waits |
351 | eventually calls C<< -> send >>, and the "consumer side", which waits |
350 | for the send to occur. |
352 | for the send to occur. |
351 | |
353 | |
352 | Example: |
354 | Example: wait for a timer. |
353 | |
355 | |
354 | # wait till the result is ready |
356 | # wait till the result is ready |
355 | my $result_ready = AnyEvent->condvar; |
357 | my $result_ready = AnyEvent->condvar; |
356 | |
358 | |
357 | # do something such as adding a timer |
359 | # do something such as adding a timer |
… | |
… | |
365 | |
367 | |
366 | # this "blocks" (while handling events) till the callback |
368 | # this "blocks" (while handling events) till the callback |
367 | # calls send |
369 | # calls send |
368 | $result_ready->recv; |
370 | $result_ready->recv; |
369 | |
371 | |
|
|
372 | Example: wait for a timer, but take advantage of the fact that |
|
|
373 | condition variables are also code references. |
|
|
374 | |
|
|
375 | my $done = AnyEvent->condvar; |
|
|
376 | my $delay = AnyEvent->timer (after => 5, cb => $done); |
|
|
377 | $done->recv; |
|
|
378 | |
370 | =head3 METHODS FOR PRODUCERS |
379 | =head3 METHODS FOR PRODUCERS |
371 | |
380 | |
372 | These methods should only be used by the producing side, i.e. the |
381 | These methods should only be used by the producing side, i.e. the |
373 | code/module that eventually sends the signal. Note that it is also |
382 | code/module that eventually sends the signal. Note that it is also |
374 | the producer side which creates the condvar in most cases, but it isn't |
383 | the producer side which creates the condvar in most cases, but it isn't |
… | |
… | |
385 | If a callback has been set on the condition variable, it is called |
394 | If a callback has been set on the condition variable, it is called |
386 | immediately from within send. |
395 | immediately from within send. |
387 | |
396 | |
388 | 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 |
389 | future C<< ->recv >> calls. |
398 | future C<< ->recv >> calls. |
|
|
399 | |
|
|
400 | Condition variables are overloaded so one can call them directly |
|
|
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). |
390 | |
408 | |
391 | =item $cv->croak ($error) |
409 | =item $cv->croak ($error) |
392 | |
410 | |
393 | 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 |
394 | C<Carp::croak> with the given error message/object/scalar. |
412 | C<Carp::croak> with the given error message/object/scalar. |
… | |
… | |
601 | |
619 | |
602 | 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 |
603 | 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 |
604 | decide which implementation to chose if some module relies on it. |
622 | decide which implementation to chose if some module relies on it. |
605 | |
623 | |
606 | 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 |
607 | 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 |
608 | 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 |
609 | 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 |
610 | modules might create watchers when they are loaded, and AnyEvent will |
628 | modules might create watchers when they are loaded, and AnyEvent will |
611 | 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 |
612 | 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. |
613 | |
631 | |
614 | 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 |
615 | loading the C<AnyEvent::Impl::Perl> module, which gives you similar |
633 | C<AnyEvent::Impl::Perl> module, which gives you similar behaviour |
616 | 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 | |
617 | |
652 | |
618 | =head1 OTHER MODULES |
653 | =head1 OTHER MODULES |
619 | |
654 | |
620 | 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 |
621 | AnyEvent and can therefore be mixed easily with other AnyEvent modules |
656 | AnyEvent and can therefore be mixed easily with other AnyEvent modules |
… | |
… | |
637 | |
672 | |
638 | Provides various utility functions for (internet protocol) sockets, |
673 | Provides various utility functions for (internet protocol) sockets, |
639 | addresses and name resolution. Also functions to create non-blocking tcp |
674 | addresses and name resolution. Also functions to create non-blocking tcp |
640 | 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. |
641 | |
676 | |
|
|
677 | =item L<AnyEvent::DNS> |
|
|
678 | |
|
|
679 | Provides rich asynchronous DNS resolver capabilities. |
|
|
680 | |
642 | =item L<AnyEvent::HTTPD> |
681 | =item L<AnyEvent::HTTPD> |
643 | |
682 | |
644 | Provides a simple web application server framework. |
683 | Provides a simple web application server framework. |
645 | |
|
|
646 | =item L<AnyEvent::DNS> |
|
|
647 | |
|
|
648 | Provides rich asynchronous DNS resolver capabilities. |
|
|
649 | |
684 | |
650 | =item L<AnyEvent::FastPing> |
685 | =item L<AnyEvent::FastPing> |
651 | |
686 | |
652 | The fastest ping in the west. |
687 | The fastest ping in the west. |
653 | |
688 | |
… | |
… | |
696 | no warnings; |
731 | no warnings; |
697 | use strict; |
732 | use strict; |
698 | |
733 | |
699 | use Carp; |
734 | use Carp; |
700 | |
735 | |
701 | our $VERSION = '3.6'; |
736 | our $VERSION = '4.03'; |
702 | our $MODEL; |
737 | our $MODEL; |
703 | |
738 | |
704 | our $AUTOLOAD; |
739 | our $AUTOLOAD; |
705 | our @ISA; |
740 | our @ISA; |
706 | |
741 | |
|
|
742 | our @REGISTRY; |
|
|
743 | |
707 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
744 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
708 | |
745 | |
709 | our @REGISTRY; |
746 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
710 | |
|
|
711 | our %PROTOCOL; # (ipv4|ipv6) => (1|2) |
|
|
712 | |
747 | |
713 | { |
748 | { |
714 | my $idx; |
749 | my $idx; |
715 | $PROTOCOL{$_} = ++$idx |
750 | $PROTOCOL{$_} = ++$idx |
|
|
751 | for reverse split /\s*,\s*/, |
716 | for split /\s*,\s*/, $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
752 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
717 | } |
753 | } |
718 | |
754 | |
719 | my @models = ( |
755 | my @models = ( |
720 | [EV:: => AnyEvent::Impl::EV::], |
756 | [EV:: => AnyEvent::Impl::EV::], |
721 | [Event:: => AnyEvent::Impl::Event::], |
757 | [Event:: => AnyEvent::Impl::Event::], |
722 | [Tk:: => AnyEvent::Impl::Tk::], |
|
|
723 | [Wx:: => AnyEvent::Impl::POE::], |
|
|
724 | [Prima:: => AnyEvent::Impl::POE::], |
|
|
725 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
758 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
726 | # everything below here will not be autoprobed as the pureperl backend should work everywhere |
759 | # everything below here will not be autoprobed |
727 | [Glib:: => AnyEvent::Impl::Glib::], |
760 | # as the pureperl backend should work everywhere |
|
|
761 | # and is usually faster |
|
|
762 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
|
|
763 | [Glib:: => AnyEvent::Impl::Glib::], # becomes extremely slow with many watchers |
728 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
764 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
729 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
765 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
730 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
766 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
|
|
767 | [Wx:: => AnyEvent::Impl::POE::], |
|
|
768 | [Prima:: => AnyEvent::Impl::POE::], |
731 | ); |
769 | ); |
732 | |
770 | |
733 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
771 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
734 | |
772 | |
735 | our @post_detect; |
773 | our @post_detect; |
… | |
… | |
755 | } |
793 | } |
756 | |
794 | |
757 | sub detect() { |
795 | sub detect() { |
758 | unless ($MODEL) { |
796 | unless ($MODEL) { |
759 | no strict 'refs'; |
797 | no strict 'refs'; |
|
|
798 | local $SIG{__DIE__}; |
760 | |
799 | |
761 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
800 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
762 | my $model = "AnyEvent::Impl::$1"; |
801 | my $model = "AnyEvent::Impl::$1"; |
763 | if (eval "require $model") { |
802 | if (eval "require $model") { |
764 | $MODEL = $model; |
803 | $MODEL = $model; |
… | |
… | |
887 | or Carp::croak "required option 'pid' is missing"; |
926 | or Carp::croak "required option 'pid' is missing"; |
888 | |
927 | |
889 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
928 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
890 | |
929 | |
891 | unless ($WNOHANG) { |
930 | unless ($WNOHANG) { |
892 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
931 | $WNOHANG = eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
893 | } |
932 | } |
894 | |
933 | |
895 | unless ($CHLD_W) { |
934 | unless ($CHLD_W) { |
896 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
935 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
897 | # child could be a zombie already, so make at least one round |
936 | # child could be a zombie already, so make at least one round |
… | |
… | |
913 | package AnyEvent::CondVar; |
952 | package AnyEvent::CondVar; |
914 | |
953 | |
915 | our @ISA = AnyEvent::CondVar::Base::; |
954 | our @ISA = AnyEvent::CondVar::Base::; |
916 | |
955 | |
917 | package AnyEvent::CondVar::Base; |
956 | package AnyEvent::CondVar::Base; |
|
|
957 | |
|
|
958 | use overload |
|
|
959 | '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, |
|
|
960 | fallback => 1; |
918 | |
961 | |
919 | sub _send { |
962 | sub _send { |
920 | # nop |
963 | # nop |
921 | } |
964 | } |
922 | |
965 | |