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.133 by root, Sun May 25 03:44:03 2008 UTC vs.
Revision 1.141 by root, Mon May 26 17:45:05 2008 UTC

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 (or calling the condition variable as if it 316by calling the C<send> method (or calling the condition variable as if it
317were a callback). 317were a callback, read about the caveats in the description for the C<<
318->send >> method).
318 319
319Condition variables are similar to callbacks, except that you can 320Condition variables are similar to callbacks, except that you can
320optionally wait for them. They can also be called merge points - points 321optionally wait for them. They can also be called merge points - points
321in time where multiple outstanding events have been processed. And yet 322in time where multiple outstanding events have been processed. And yet
322another way to call them is transactions - each condition variable can be 323another way to call them is transactions - each condition variable can be
394immediately from within send. 395immediately from within send.
395 396
396Any 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
397future C<< ->recv >> calls. 398future C<< ->recv >> calls.
398 399
399Condition variables are overloaded so one can call them directly (as a 400Condition variables are overloaded so one can call them directly
400code reference). Calling them directly is the same as calling C<send>. 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).
401 408
402=item $cv->croak ($error) 409=item $cv->croak ($error)
403 410
404Similar 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
405C<Carp::croak> with the given error message/object/scalar. 412C<Carp::croak> with the given error message/object/scalar.
519The callback will be called when the condition becomes "true", i.e. when 526The callback will be called when the condition becomes "true", i.e. when
520C<send> or C<croak> are called. Calling C<recv> inside the callback 527C<send> or C<croak> are called. Calling C<recv> inside the callback
521or at any later time is guaranteed not to block. 528or at any later time is guaranteed not to block.
522 529
523=back 530=back
524
525=head3 MAINLOOP EMULATION
526
527Sometimes (often for short test scripts, or even standalone programs
528who only want to use AnyEvent), you I<do> want your program to block
529indefinitely in some event loop.
530
531In that case, you cna use a condition variable like this:
532
533 AnyEvent->condvar->recv;
534
535This has the effect of entering the event loop and looping forever.
536
537Note that usually your program has some exit condition, in which case
538it is better to use the "traditional" approach of storing a condition
539variable, waiting for it, and sending it when the program should exit
540cleanly.
541
542 531
543=head1 GLOBAL VARIABLES AND FUNCTIONS 532=head1 GLOBAL VARIABLES AND FUNCTIONS
544 533
545=over 4 534=over 4
546 535
630 619
631If 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
632do 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
633decide which implementation to chose if some module relies on it. 622decide which implementation to chose if some module relies on it.
634 623
635If 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
636Gtk2 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
637event module before loading AnyEvent or any module that uses it: generally 626event module before loading AnyEvent or any module that uses it: generally
638speaking, 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
639modules might create watchers when they are loaded, and AnyEvent will 628modules might create watchers when they are loaded, and AnyEvent will
640decide 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
641might chose the wrong one unless you load the correct one yourself. 630might chose the wrong one unless you load the correct one yourself.
642 631
643You can chose to use a rather inefficient pure-perl implementation by 632You can chose to use a pure-perl implementation by loading the
644loading the C<AnyEvent::Impl::Perl> module, which gives you similar 633C<AnyEvent::Impl::Perl> module, which gives you similar behaviour
645behaviour 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
646 652
647=head1 OTHER MODULES 653=head1 OTHER MODULES
648 654
649The following is a non-exhaustive list of additional modules that use 655The following is a non-exhaustive list of additional modules that use
650AnyEvent and can therefore be mixed easily with other AnyEvent modules 656AnyEvent and can therefore be mixed easily with other AnyEvent modules
666 672
667Provides various utility functions for (internet protocol) sockets, 673Provides various utility functions for (internet protocol) sockets,
668addresses and name resolution. Also functions to create non-blocking tcp 674addresses and name resolution. Also functions to create non-blocking tcp
669connections or tcp servers, with IPv6 and SRV record support and more. 675connections or tcp servers, with IPv6 and SRV record support and more.
670 676
677=item L<AnyEvent::DNS>
678
679Provides rich asynchronous DNS resolver capabilities.
680
671=item L<AnyEvent::HTTPD> 681=item L<AnyEvent::HTTPD>
672 682
673Provides a simple web application server framework. 683Provides a simple web application server framework.
674
675=item L<AnyEvent::DNS>
676
677Provides rich asynchronous DNS resolver capabilities.
678 684
679=item L<AnyEvent::FastPing> 685=item L<AnyEvent::FastPing>
680 686
681The fastest ping in the west. 687The fastest ping in the west.
682 688
725no warnings; 731no warnings;
726use strict; 732use strict;
727 733
728use Carp; 734use Carp;
729 735
730our $VERSION = '4.03'; 736our $VERSION = '4.05';
731our $MODEL; 737our $MODEL;
732 738
733our $AUTOLOAD; 739our $AUTOLOAD;
734our @ISA; 740our @ISA;
735 741
742our @REGISTRY;
743
744our $WIN32;
745
746BEGIN {
747 my $win32 = ! ! ($^O =~ /mswin32/i);
748 eval "sub WIN32(){ $win32 }";
749}
750
736our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; 751our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1;
737 752
738our @REGISTRY; 753our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred
739
740our %PROTOCOL; # (ipv4|ipv6) => (1|2)
741 754
742{ 755{
743 my $idx; 756 my $idx;
744 $PROTOCOL{$_} = ++$idx 757 $PROTOCOL{$_} = ++$idx
758 for reverse split /\s*,\s*/,
745 for split /\s*,\s*/, $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; 759 $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6";
746} 760}
747 761
748my @models = ( 762my @models = (
749 [EV:: => AnyEvent::Impl::EV::], 763 [EV:: => AnyEvent::Impl::EV::],
750 [Event:: => AnyEvent::Impl::Event::], 764 [Event:: => AnyEvent::Impl::Event::],
751 [Tk:: => AnyEvent::Impl::Tk::],
752 [Wx:: => AnyEvent::Impl::POE::],
753 [Prima:: => AnyEvent::Impl::POE::],
754 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], 765 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
755 # everything below here will not be autoprobed as the pureperl backend should work everywhere 766 # everything below here will not be autoprobed
756 [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
757 [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy 771 [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy
758 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program 772 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program
759 [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::],
760); 776);
761 777
762our %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);
763 779
764our @post_detect; 780our @post_detect;
784} 800}
785 801
786sub detect() { 802sub detect() {
787 unless ($MODEL) { 803 unless ($MODEL) {
788 no strict 'refs'; 804 no strict 'refs';
805 local $SIG{__DIE__};
789 806
790 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { 807 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) {
791 my $model = "AnyEvent::Impl::$1"; 808 my $model = "AnyEvent::Impl::$1";
792 if (eval "require $model") { 809 if (eval "require $model") {
793 $MODEL = $model; 810 $MODEL = $model;
916 or Carp::croak "required option 'pid' is missing"; 933 or Carp::croak "required option 'pid' is missing";
917 934
918 $PID_CB{$pid}{$arg{cb}} = $arg{cb}; 935 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
919 936
920 unless ($WNOHANG) { 937 unless ($WNOHANG) {
921 $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; 938 $WNOHANG = eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1;
922 } 939 }
923 940
924 unless ($CHLD_W) { 941 unless ($CHLD_W) {
925 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); 942 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld);
926 # 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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines