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.115 by root, Sat May 10 21:47:28 2008 UTC vs.
Revision 1.120 by root, Sat May 17 21:34:15 2008 UTC

691no warnings; 691no warnings;
692use strict; 692use strict;
693 693
694use Carp; 694use Carp;
695 695
696our $VERSION = '3.4'; 696our $VERSION = '3.5';
697our $MODEL; 697our $MODEL;
698 698
699our $AUTOLOAD; 699our $AUTOLOAD;
700our @ISA; 700our @ISA;
701 701
730 1 730 1
731 } else { 731 } else {
732 push @post_detect, $cb; 732 push @post_detect, $cb;
733 733
734 defined wantarray 734 defined wantarray
735 ? bless \$cb, "AnyEvent::Util::Guard" 735 ? bless \$cb, "AnyEvent::Util::PostDetect"
736 : () 736 : ()
737 } 737 }
738} 738}
739 739
740sub AnyEvent::Util::Guard::DESTROY { 740sub AnyEvent::Util::PostDetect::DESTROY {
741 @post_detect = grep $_ != ${$_[0]}, @post_detect; 741 @post_detect = grep $_ != ${$_[0]}, @post_detect;
742} 742}
743 743
744sub detect() { 744sub detect() {
745 unless ($MODEL) { 745 unless ($MODEL) {
811package AnyEvent::Base; 811package AnyEvent::Base;
812 812
813# default implementation for ->condvar 813# default implementation for ->condvar
814 814
815sub condvar { 815sub condvar {
816 bless {}, "AnyEvent::Base::CondVar" 816 bless {}, AnyEvent::CondVar::
817} 817}
818 818
819# default implementation for ->signal 819# default implementation for ->signal
820 820
821our %SIG_CB; 821our %SIG_CB;
895 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; 895 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} };
896 896
897 undef $CHLD_W unless keys %PID_CB; 897 undef $CHLD_W unless keys %PID_CB;
898} 898}
899 899
900package AnyEvent::Base::CondVar; 900package AnyEvent::CondVar;
901 901
902# wake up the waiter 902our @ISA = AnyEvent::CondVar::Base::;
903
904package AnyEvent::CondVar::Base;
905
903sub _send { 906sub _send {
904 &{ delete $_[0]{_ae_cb} } if $_[0]{_ae_cb}; 907 # nop
905} 908}
906 909
907sub send { 910sub send {
908 my $cv = shift; 911 my $cv = shift;
909 $cv->{_ae_sent} = [@_]; 912 $cv->{_ae_sent} = [@_];
913 (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb};
910 $cv->_send; 914 $cv->_send;
911} 915}
912 916
913sub croak { 917sub croak {
914 $_[0]{_ae_croak} = $_[1]; 918 $_[0]{_ae_croak} = $_[1];
917 921
918sub ready { 922sub ready {
919 $_[0]{_ae_sent} 923 $_[0]{_ae_sent}
920} 924}
921 925
926sub _wait {
927 AnyEvent->one_event while !$_[0]{_ae_sent};
928}
929
922sub recv { 930sub recv {
923 AnyEvent->one_event while !$_[0]{_ae_sent}; 931 $_[0]->_wait;
924 932
925 Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; 933 Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak};
926 wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] 934 wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0]
927} 935}
928 936
941 &{ $_[0]{_ae_end_cb} } if $_[0]{_ae_end_cb}; 949 &{ $_[0]{_ae_end_cb} } if $_[0]{_ae_end_cb};
942} 950}
943 951
944# undocumented/compatibility with pre-3.4 952# undocumented/compatibility with pre-3.4
945*broadcast = \&send; 953*broadcast = \&send;
946*wait = \&recv; 954*wait = \&_wait;
947 955
948=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 956=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
949 957
950This is an advanced topic that you do not normally need to use AnyEvent in 958This is an advanced topic that you do not normally need to use AnyEvent in
951a module. This section is only of use to event loop authors who want to 959a module. This section is only of use to event loop authors who want to
1040 poll => 'r', 1048 poll => 'r',
1041 cb => sub { 1049 cb => sub {
1042 warn "io event <$_[0]>\n"; # will always output <r> 1050 warn "io event <$_[0]>\n"; # will always output <r>
1043 chomp (my $input = <STDIN>); # read a line 1051 chomp (my $input = <STDIN>); # read a line
1044 warn "read: $input\n"; # output what has been read 1052 warn "read: $input\n"; # output what has been read
1045 $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i 1053 $cv->send if $input =~ /^q/i; # quit program if /^q/i
1046 }, 1054 },
1047 ); 1055 );
1048 1056
1049 my $time_watcher; # can only be used once 1057 my $time_watcher; # can only be used once
1050 1058
1055 }); 1063 });
1056 } 1064 }
1057 1065
1058 new_timer; # create first timer 1066 new_timer; # create first timer
1059 1067
1060 $cv->wait; # wait until user enters /^q/i 1068 $cv->recv; # wait until user enters /^q/i
1061 1069
1062=head1 REAL-WORLD EXAMPLE 1070=head1 REAL-WORLD EXAMPLE
1063 1071
1064Consider the L<Net::FCP> module. It features (among others) the following 1072Consider the L<Net::FCP> module. It features (among others) the following
1065API calls, which are to freenet what HTTP GET requests are to http: 1073API calls, which are to freenet what HTTP GET requests are to http:
1121 1129
1122 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; 1130 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf};
1123 1131
1124 if (end-of-file or data complete) { 1132 if (end-of-file or data complete) {
1125 $txn->{result} = $txn->{buf}; 1133 $txn->{result} = $txn->{buf};
1126 $txn->{finished}->broadcast; 1134 $txn->{finished}->send;
1127 $txb->{cb}->($txn) of $txn->{cb}; # also call callback 1135 $txb->{cb}->($txn) of $txn->{cb}; # also call callback
1128 } 1136 }
1129 1137
1130The C<result> method, finally, just waits for the finished signal (if the 1138The C<result> method, finally, just waits for the finished signal (if the
1131request was already finished, it doesn't wait, of course, and returns the 1139request was already finished, it doesn't wait, of course, and returns the
1132data: 1140data:
1133 1141
1134 $txn->{finished}->wait; 1142 $txn->{finished}->recv;
1135 return $txn->{result}; 1143 return $txn->{result};
1136 1144
1137The actual code goes further and collects all errors (C<die>s, exceptions) 1145The actual code goes further and collects all errors (C<die>s, exceptions)
1138that occured during request processing. The C<result> method detects 1146that occured during request processing. The C<result> method detects
1139whether an exception as thrown (it is stored inside the $txn object) 1147whether an exception as thrown (it is stored inside the $txn object)
1174 1182
1175 my $quit = AnyEvent->condvar; 1183 my $quit = AnyEvent->condvar;
1176 1184
1177 $fcp->txn_client_get ($url)->cb (sub { 1185 $fcp->txn_client_get ($url)->cb (sub {
1178 ... 1186 ...
1179 $quit->broadcast; 1187 $quit->send;
1180 }); 1188 });
1181 1189
1182 $quit->wait; 1190 $quit->recv;
1183 1191
1184 1192
1185=head1 BENCHMARKS 1193=head1 BENCHMARKS
1186 1194
1187To give you an idea of the performance and overheads that AnyEvent adds 1195To give you an idea of the performance and overheads that AnyEvent adds
1216all watchers, to avoid adding memory overhead. That means closure creation 1224all watchers, to avoid adding memory overhead. That means closure creation
1217and memory usage is not included in the figures. 1225and memory usage is not included in the figures.
1218 1226
1219I<invoke> is the time, in microseconds, used to invoke a simple 1227I<invoke> is the time, in microseconds, used to invoke a simple
1220callback. The callback simply counts down a Perl variable and after it was 1228callback. The callback simply counts down a Perl variable and after it was
1221invoked "watcher" times, it would C<< ->broadcast >> a condvar once to 1229invoked "watcher" times, it would C<< ->send >> a condvar once to
1222signal the end of this phase. 1230signal the end of this phase.
1223 1231
1224I<destroy> is the time, in microseconds, that it takes to destroy a single 1232I<destroy> is the time, in microseconds, that it takes to destroy a single
1225watcher. 1233watcher.
1226 1234

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines