ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.129 by elmex, Sat May 24 15:19:30 2008 UTC vs.
Revision 1.140 by root, Mon May 26 06:18:53 2008 UTC

311>> method, usually without arguments. The only argument pair allowed is 311>> method, usually without arguments. The only argument pair allowed is
312C<cb>, which specifies a callback to be called when the condition variable 312C<cb>, which specifies a callback to be called when the condition variable
313becomes true. 313becomes true.
314 314
315After creation, the condition variable is "false" until it becomes "true" 315After creation, the condition variable is "false" until it becomes "true"
316by calling the C<send> method. 316by calling the C<send> method (or calling the condition variable as if it
317were a callback, read about the caveats in the description for the C<<
318->send >> method).
317 319
318Condition variables are similar to callbacks, except that you can 320Condition variables are similar to callbacks, except that you can
319optionally wait for them. They can also be called merge points - points 321optionally wait for them. They can also be called merge points - points
320in time where multiple outstanding events have been processed. And yet 322in time where multiple outstanding events have been processed. And yet
321another way to call them is transactions - each condition variable can be 323another way to call them is transactions - each condition variable can be
347 349
348There are two "sides" to a condition variable - the "producer side" which 350There are two "sides" to a condition variable - the "producer side" which
349eventually calls C<< -> send >>, and the "consumer side", which waits 351eventually calls C<< -> send >>, and the "consumer side", which waits
350for the send to occur. 352for the send to occur.
351 353
352Example: 354Example: 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
372Example: wait for a timer, but take advantage of the fact that
373condition 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
372These methods should only be used by the producing side, i.e. the 381These methods should only be used by the producing side, i.e. the
373code/module that eventually sends the signal. Note that it is also 382code/module that eventually sends the signal. Note that it is also
374the producer side which creates the condvar in most cases, but it isn't 383the producer side which creates the condvar in most cases, but it isn't
385If a callback has been set on the condition variable, it is called 394If a callback has been set on the condition variable, it is called
386immediately from within send. 395immediately from within send.
387 396
388Any arguments passed to the C<send> call will be returned by all 397Any arguments passed to the C<send> call will be returned by all
389future C<< ->recv >> calls. 398future C<< ->recv >> calls.
399
400Condition variables are overloaded so one can call them directly
401(as a code reference). Calling them directly is the same as calling
402C<send>. Note, however, that many C-based event loops do not handle
403overloading, so as tempting as it may be, passing a condition variable
404instead of a callback does not work. Both the pure perl and EV loops
405support overloading, however, as well as all functions that use perl to
406invoke a callback (as in L<AnyEvent::Socket> and L<AnyEvent::DNS> for
407example).
390 408
391=item $cv->croak ($error) 409=item $cv->croak ($error)
392 410
393Similar to send, but causes all call's to C<< ->recv >> to invoke 411Similar to send, but causes all call's to C<< ->recv >> to invoke
394C<Carp::croak> with the given error message/object/scalar. 412C<Carp::croak> with the given error message/object/scalar.
601 619
602If it doesn't care, it can just "use AnyEvent" and use it itself, or not 620If it doesn't care, it can just "use AnyEvent" and use it itself, or not
603do anything special (it does not need to be event-based) and let AnyEvent 621do anything special (it does not need to be event-based) and let AnyEvent
604decide which implementation to chose if some module relies on it. 622decide which implementation to chose if some module relies on it.
605 623
606If the main program relies on a specific event model. For example, in 624If the main program relies on a specific event model - for example, in
607Gtk2 programs you have to rely on the Glib module. You should load the 625Gtk2 programs you have to rely on the Glib module - you should load the
608event module before loading AnyEvent or any module that uses it: generally 626event module before loading AnyEvent or any module that uses it: generally
609speaking, you should load it as early as possible. The reason is that 627speaking, you should load it as early as possible. The reason is that
610modules might create watchers when they are loaded, and AnyEvent will 628modules might create watchers when they are loaded, and AnyEvent will
611decide on the event model to use as soon as it creates watchers, and it 629decide on the event model to use as soon as it creates watchers, and it
612might chose the wrong one unless you load the correct one yourself. 630might chose the wrong one unless you load the correct one yourself.
613 631
614You can chose to use a rather inefficient pure-perl implementation by 632You can chose to use a pure-perl implementation by loading the
615loading the C<AnyEvent::Impl::Perl> module, which gives you similar 633C<AnyEvent::Impl::Perl> module, which gives you similar behaviour
616behaviour everywhere, but letting AnyEvent chose is generally better. 634everywhere, but letting AnyEvent chose the model is generally better.
635
636=head2 MAINLOOP EMULATION
637
638Sometimes (often for short test scripts, or even standalone programs who
639only want to use AnyEvent), you do not want to run a specific event loop.
640
641In that case, you can use a condition variable like this:
642
643 AnyEvent->condvar->recv;
644
645This has the effect of entering the event loop and looping forever.
646
647Note that usually your program has some exit condition, in which case
648it is better to use the "traditional" approach of storing a condition
649variable somewhere, waiting for it, and sending it when the program should
650exit cleanly.
651
617 652
618=head1 OTHER MODULES 653=head1 OTHER MODULES
619 654
620The following is a non-exhaustive list of additional modules that use 655The following is a non-exhaustive list of additional modules that use
621AnyEvent and can therefore be mixed easily with other AnyEvent modules 656AnyEvent and can therefore be mixed easily with other AnyEvent modules
637 672
638Provides various utility functions for (internet protocol) sockets, 673Provides various utility functions for (internet protocol) sockets,
639addresses and name resolution. Also functions to create non-blocking tcp 674addresses and name resolution. Also functions to create non-blocking tcp
640connections or tcp servers, with IPv6 and SRV record support and more. 675connections or tcp servers, with IPv6 and SRV record support and more.
641 676
677=item L<AnyEvent::DNS>
678
679Provides rich asynchronous DNS resolver capabilities.
680
642=item L<AnyEvent::HTTPD> 681=item L<AnyEvent::HTTPD>
643 682
644Provides a simple web application server framework. 683Provides a simple web application server framework.
645
646=item L<AnyEvent::DNS>
647
648Provides rich asynchronous DNS resolver capabilities.
649 684
650=item L<AnyEvent::FastPing> 685=item L<AnyEvent::FastPing>
651 686
652The fastest ping in the west. 687The fastest ping in the west.
653 688
696no warnings; 731no warnings;
697use strict; 732use strict;
698 733
699use Carp; 734use Carp;
700 735
701our $VERSION = '3.6'; 736our $VERSION = '4.04';
702our $MODEL; 737our $MODEL;
703 738
704our $AUTOLOAD; 739our $AUTOLOAD;
705our @ISA; 740our @ISA;
706 741
742our @REGISTRY;
743
744our $WIN32;
745
746BEGIN {
747 my $win32 = ! ! ($^O =~ /mswin32/i);
748 eval "sub WIN32(){ $win32 }";
749}
750
707our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; 751our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1;
708 752
709our @REGISTRY; 753our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred
710
711our %PROTOCOL; # (ipv4|ipv6) => (1|2)
712 754
713{ 755{
714 my $idx; 756 my $idx;
715 $PROTOCOL{$_} = ++$idx 757 $PROTOCOL{$_} = ++$idx
758 for reverse split /\s*,\s*/,
716 for split /\s*,\s*/, $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; 759 $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6";
717} 760}
718 761
719my @models = ( 762my @models = (
720 [EV:: => AnyEvent::Impl::EV::], 763 [EV:: => AnyEvent::Impl::EV::],
721 [Event:: => AnyEvent::Impl::Event::], 764 [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::], 765 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
726 # everything below here will not be autoprobed as the pureperl backend should work everywhere 766 # everything below here will not be autoprobed
727 [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
728 [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy 771 [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy
729 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program 772 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program
730 [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::],
731); 776);
732 777
733our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); 778our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY);
734 779
735our @post_detect; 780our @post_detect;
755} 800}
756 801
757sub detect() { 802sub detect() {
758 unless ($MODEL) { 803 unless ($MODEL) {
759 no strict 'refs'; 804 no strict 'refs';
805 local $SIG{__DIE__};
760 806
761 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { 807 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) {
762 my $model = "AnyEvent::Impl::$1"; 808 my $model = "AnyEvent::Impl::$1";
763 if (eval "require $model") { 809 if (eval "require $model") {
764 $MODEL = $model; 810 $MODEL = $model;
887 or Carp::croak "required option 'pid' is missing"; 933 or Carp::croak "required option 'pid' is missing";
888 934
889 $PID_CB{$pid}{$arg{cb}} = $arg{cb}; 935 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
890 936
891 unless ($WNOHANG) { 937 unless ($WNOHANG) {
892 $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; 938 $WNOHANG = eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1;
893 } 939 }
894 940
895 unless ($CHLD_W) { 941 unless ($CHLD_W) {
896 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); 942 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld);
897 # 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
913package AnyEvent::CondVar; 959package AnyEvent::CondVar;
914 960
915our @ISA = AnyEvent::CondVar::Base::; 961our @ISA = AnyEvent::CondVar::Base::;
916 962
917package AnyEvent::CondVar::Base; 963package AnyEvent::CondVar::Base;
964
965use overload
966 '&{}' => sub { my $self = shift; sub { $self->send (@_) } },
967 fallback => 1;
918 968
919sub _send { 969sub _send {
920 # nop 970 # nop
921} 971}
922 972

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines