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.114 by root, Sat May 10 21:12:49 2008 UTC vs.
Revision 1.118 by root, Sun May 11 18:08:36 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.41';
697our $MODEL; 697our $MODEL;
698 698
699our $AUTOLOAD; 699our $AUTOLOAD;
700our @ISA; 700our @ISA;
701 701
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 &{ $_[0]{_ae_cb} } if $_[0]{_ae_cb}; 907 # nop
905} 908}
906 909
907sub send { 910sub send {
911 my $cv = shift;
908 $_[0]{_ae_sent} = [@_]; 912 $cv->{_ae_sent} = [@_];
913 (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb};
909 $_[0]->_send; 914 $cv->_send;
910} 915}
911 916
912sub croak { 917sub croak {
913 $_[0]{_ae_croak} = $_[0]; 918 $_[0]{_ae_croak} = $_[1];
914 $_[0]->send; 919 $_[0]->send;
915} 920}
916 921
917sub ready { 922sub ready {
918 $_[0]{_ae_sent} 923 $_[0]{_ae_sent}
919} 924}
920 925
926sub _wait {
927 AnyEvent->one_event while !$_[0]{_ae_sent};
928}
929
921sub recv { 930sub recv {
922 AnyEvent->one_event while !$_[0]{_ae_sent}; 931 $_[0]->_wait;
923 932
924 Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; 933 Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak};
925 wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] 934 wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0]
926} 935}
927 936
940 &{ $_[0]{_ae_end_cb} } if $_[0]{_ae_end_cb}; 949 &{ $_[0]{_ae_end_cb} } if $_[0]{_ae_end_cb};
941} 950}
942 951
943# undocumented/compatibility with pre-3.4 952# undocumented/compatibility with pre-3.4
944*broadcast = \&send; 953*broadcast = \&send;
945*wait = \&recv; 954*wait = \&_wait;
946 955
947=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 956=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
948 957
949This 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
950a 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
1039 poll => 'r', 1048 poll => 'r',
1040 cb => sub { 1049 cb => sub {
1041 warn "io event <$_[0]>\n"; # will always output <r> 1050 warn "io event <$_[0]>\n"; # will always output <r>
1042 chomp (my $input = <STDIN>); # read a line 1051 chomp (my $input = <STDIN>); # read a line
1043 warn "read: $input\n"; # output what has been read 1052 warn "read: $input\n"; # output what has been read
1044 $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i 1053 $cv->send if $input =~ /^q/i; # quit program if /^q/i
1045 }, 1054 },
1046 ); 1055 );
1047 1056
1048 my $time_watcher; # can only be used once 1057 my $time_watcher; # can only be used once
1049 1058
1054 }); 1063 });
1055 } 1064 }
1056 1065
1057 new_timer; # create first timer 1066 new_timer; # create first timer
1058 1067
1059 $cv->wait; # wait until user enters /^q/i 1068 $cv->recv; # wait until user enters /^q/i
1060 1069
1061=head1 REAL-WORLD EXAMPLE 1070=head1 REAL-WORLD EXAMPLE
1062 1071
1063Consider the L<Net::FCP> module. It features (among others) the following 1072Consider the L<Net::FCP> module. It features (among others) the following
1064API 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:
1120 1129
1121 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; 1130 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf};
1122 1131
1123 if (end-of-file or data complete) { 1132 if (end-of-file or data complete) {
1124 $txn->{result} = $txn->{buf}; 1133 $txn->{result} = $txn->{buf};
1125 $txn->{finished}->broadcast; 1134 $txn->{finished}->send;
1126 $txb->{cb}->($txn) of $txn->{cb}; # also call callback 1135 $txb->{cb}->($txn) of $txn->{cb}; # also call callback
1127 } 1136 }
1128 1137
1129The 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
1130request 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
1131data: 1140data:
1132 1141
1133 $txn->{finished}->wait; 1142 $txn->{finished}->recv;
1134 return $txn->{result}; 1143 return $txn->{result};
1135 1144
1136The 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)
1137that occured during request processing. The C<result> method detects 1146that occured during request processing. The C<result> method detects
1138whether an exception as thrown (it is stored inside the $txn object) 1147whether an exception as thrown (it is stored inside the $txn object)
1173 1182
1174 my $quit = AnyEvent->condvar; 1183 my $quit = AnyEvent->condvar;
1175 1184
1176 $fcp->txn_client_get ($url)->cb (sub { 1185 $fcp->txn_client_get ($url)->cb (sub {
1177 ... 1186 ...
1178 $quit->broadcast; 1187 $quit->send;
1179 }); 1188 });
1180 1189
1181 $quit->wait; 1190 $quit->recv;
1182 1191
1183 1192
1184=head1 BENCHMARKS 1193=head1 BENCHMARKS
1185 1194
1186To 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
1215all watchers, to avoid adding memory overhead. That means closure creation 1224all watchers, to avoid adding memory overhead. That means closure creation
1216and memory usage is not included in the figures. 1225and memory usage is not included in the figures.
1217 1226
1218I<invoke> is the time, in microseconds, used to invoke a simple 1227I<invoke> is the time, in microseconds, used to invoke a simple
1219callback. 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
1220invoked "watcher" times, it would C<< ->broadcast >> a condvar once to 1229invoked "watcher" times, it would C<< ->send >> a condvar once to
1221signal the end of this phase. 1230signal the end of this phase.
1222 1231
1223I<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
1224watcher. 1233watcher.
1225 1234

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines