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.205 by root, Sun Apr 19 12:09:46 2009 UTC vs.
Revision 1.210 by root, Wed May 13 15:19:43 2009 UTC

6 6
7=head1 SYNOPSIS 7=head1 SYNOPSIS
8 8
9 use AnyEvent; 9 use AnyEvent;
10 10
11 # file descriptor readable
11 my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { ... }); 12 my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... });
12 13
14 # one-shot or repeating timers
13 my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); 15 my $w = AnyEvent->timer (after => $seconds, cb => sub { ... });
14 my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... 16 my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ...
15 17
16 print AnyEvent->now; # prints current event loop time 18 print AnyEvent->now; # prints current event loop time
17 print AnyEvent->time; # think Time::HiRes::time or simply CORE::time. 19 print AnyEvent->time; # think Time::HiRes::time or simply CORE::time.
18 20
21 # POSIX signal
19 my $w = AnyEvent->signal (signal => "TERM", cb => sub { ... }); 22 my $w = AnyEvent->signal (signal => "TERM", cb => sub { ... });
20 23
24 # child process exit
21 my $w = AnyEvent->child (pid => $pid, cb => sub { 25 my $w = AnyEvent->child (pid => $pid, cb => sub {
22 my ($pid, $status) = @_; 26 my ($pid, $status) = @_;
23 ... 27 ...
24 }); 28 });
29
30 # called when event loop idle (if applicable)
31 my $w = AnyEvent->idle (cb => sub { ... });
25 32
26 my $w = AnyEvent->condvar; # stores whether a condition was flagged 33 my $w = AnyEvent->condvar; # stores whether a condition was flagged
27 $w->send; # wake up current and all future recv's 34 $w->send; # wake up current and all future recv's
28 $w->recv; # enters "main loop" till $condvar gets ->send 35 $w->recv; # enters "main loop" till $condvar gets ->send
29 # use a condvar in callback mode: 36 # use a condvar in callback mode:
410 ); 417 );
411 418
412 # do something else, then wait for process exit 419 # do something else, then wait for process exit
413 $done->recv; 420 $done->recv;
414 421
422=head2 IDLE WATCHERS
423
424Sometimes there is a need to do something, but it is not so important
425to do it instantly, but only when there is nothing better to do. This
426"nothing better to do" is usually defined to be "no other events need
427attention by the event loop".
428
429Idle watchers ideally get invoked when the event loop has nothing
430better to do, just before it would block the process to wait for new
431events. Instead of blocking, the idle watcher is invoked.
432
433Most event loops unfortunately do not really support idle watchers (only
434EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent
435will simply call the callback "from time to time".
436
437Example: read lines from STDIN, but only process them when the
438program is otherwise idle:
439
440 my @lines; # read data
441 my $idle_w;
442 my $io_w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub {
443 push @lines, scalar <STDIN>;
444
445 # start an idle watcher, if not already done
446 $idle_w ||= AnyEvent->idle (cb => sub {
447 # handle only one line, when there are lines left
448 if (my $line = shift @lines) {
449 print "handled when idle: $line";
450 } else {
451 # otherwise disable the idle watcher again
452 undef $idle_w;
453 }
454 });
455 });
456
415=head2 CONDITION VARIABLES 457=head2 CONDITION VARIABLES
416 458
417If you are familiar with some event loops you will know that all of them 459If you are familiar with some event loops you will know that all of them
418require you to run some blocking "loop", "run" or similar function that 460require you to run some blocking "loop", "run" or similar function that
419will actively watch for new events and call your callbacks. 461will actively watch for new events and call your callbacks.
888no warnings; 930no warnings;
889use strict qw(vars subs); 931use strict qw(vars subs);
890 932
891use Carp; 933use Carp;
892 934
893our $VERSION = 4.351; 935our $VERSION = 4.41;
894our $MODEL; 936our $MODEL;
895 937
896our $AUTOLOAD; 938our $AUTOLOAD;
897our @ISA; 939our @ISA;
898 940
931 [Wx:: => AnyEvent::Impl::POE::], 973 [Wx:: => AnyEvent::Impl::POE::],
932 [Prima:: => AnyEvent::Impl::POE::], 974 [Prima:: => AnyEvent::Impl::POE::],
933); 975);
934 976
935our %method = map +($_ => 1), 977our %method = map +($_ => 1),
936 qw(io timer time now now_update signal child condvar one_event DESTROY); 978 qw(io timer time now now_update signal child idle condvar one_event DESTROY);
937 979
938our @post_detect; 980our @post_detect;
939 981
940sub post_detect(&) { 982sub post_detect(&) {
941 my ($cb) = @_; 983 my ($cb) = @_;
946 1 988 1
947 } else { 989 } else {
948 push @post_detect, $cb; 990 push @post_detect, $cb;
949 991
950 defined wantarray 992 defined wantarray
951 ? bless \$cb, "AnyEvent::Util::PostDetect" 993 ? bless \$cb, "AnyEvent::Util::postdetect"
952 : () 994 : ()
953 } 995 }
954} 996}
955 997
956sub AnyEvent::Util::PostDetect::DESTROY { 998sub AnyEvent::Util::postdetect::DESTROY {
957 @post_detect = grep $_ != ${$_[0]}, @post_detect; 999 @post_detect = grep $_ != ${$_[0]}, @post_detect;
958} 1000}
959 1001
960sub detect() { 1002sub detect() {
961 unless ($MODEL) { 1003 unless ($MODEL) {
1050package AnyEvent::Base; 1092package AnyEvent::Base;
1051 1093
1052# default implementations for many methods 1094# default implementations for many methods
1053 1095
1054BEGIN { 1096BEGIN {
1055 if (eval "use Time::HiRes (); time (); 1") { 1097 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") {
1056 *_time = \&Time::HiRes::time; 1098 *_time = \&Time::HiRes::time;
1057 # if (eval "use POSIX (); (POSIX::times())... 1099 # if (eval "use POSIX (); (POSIX::times())...
1058 } else { 1100 } else {
1059 *_time = sub { time }; # epic fail 1101 *_time = sub { time }; # epic fail
1060 } 1102 }
1065sub now_update { } 1107sub now_update { }
1066 1108
1067# default implementation for ->condvar 1109# default implementation for ->condvar
1068 1110
1069sub condvar { 1111sub condvar {
1070 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: 1112 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar"
1071} 1113}
1072 1114
1073# default implementation for ->signal 1115# default implementation for ->signal
1074 1116
1075our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); 1117our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO);
1121 local $!; 1163 local $!;
1122 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; 1164 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV;
1123 undef $SIG_EV{$signal}; 1165 undef $SIG_EV{$signal};
1124 }; 1166 };
1125 1167
1126 bless [$signal, $arg{cb}], "AnyEvent::Base::Signal" 1168 bless [$signal, $arg{cb}], "AnyEvent::Base::signal"
1127} 1169}
1128 1170
1129sub AnyEvent::Base::Signal::DESTROY { 1171sub AnyEvent::Base::signal::DESTROY {
1130 my ($signal, $cb) = @{$_[0]}; 1172 my ($signal, $cb) = @{$_[0]};
1131 1173
1132 delete $SIG_CB{$signal}{$cb}; 1174 delete $SIG_CB{$signal}{$cb};
1133 1175
1176 # delete doesn't work with older perls - they then
1177 # print weird messages, or just unconditionally exit
1178 # instead of getting the default action.
1134 delete $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; 1179 undef $SIG{$signal} unless keys %{ $SIG_CB{$signal} };
1135} 1180}
1136 1181
1137# default implementation for ->child 1182# default implementation for ->child
1138 1183
1139our %PID_CB; 1184our %PID_CB;
1140our $CHLD_W; 1185our $CHLD_W;
1141our $CHLD_DELAY_W; 1186our $CHLD_DELAY_W;
1142our $PID_IDLE;
1143our $WNOHANG; 1187our $WNOHANG;
1144 1188
1145sub _child_wait { 1189sub _sigchld {
1146 while (0 < (my $pid = waitpid -1, $WNOHANG)) { 1190 while (0 < (my $pid = waitpid -1, $WNOHANG)) {
1147 $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), 1191 $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }),
1148 (values %{ $PID_CB{0} || {} }); 1192 (values %{ $PID_CB{0} || {} });
1149 } 1193 }
1150
1151 undef $PID_IDLE;
1152}
1153
1154sub _sigchld {
1155 # make sure we deliver these changes "synchronous" with the event loop.
1156 $CHLD_DELAY_W ||= AnyEvent->timer (after => 0, cb => sub {
1157 undef $CHLD_DELAY_W;
1158 &_child_wait;
1159 });
1160} 1194}
1161 1195
1162sub child { 1196sub child {
1163 my (undef, %arg) = @_; 1197 my (undef, %arg) = @_;
1164 1198
1165 defined (my $pid = $arg{pid} + 0) 1199 defined (my $pid = $arg{pid} + 0)
1166 or Carp::croak "required option 'pid' is missing"; 1200 or Carp::croak "required option 'pid' is missing";
1167 1201
1168 $PID_CB{$pid}{$arg{cb}} = $arg{cb}; 1202 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
1169 1203
1170 unless ($WNOHANG) {
1171 $WNOHANG = eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; 1204 $WNOHANG ||= eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1;
1172 }
1173 1205
1174 unless ($CHLD_W) { 1206 unless ($CHLD_W) {
1175 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); 1207 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld);
1176 # child could be a zombie already, so make at least one round 1208 # child could be a zombie already, so make at least one round
1177 &_sigchld; 1209 &_sigchld;
1178 } 1210 }
1179 1211
1180 bless [$pid, $arg{cb}], "AnyEvent::Base::Child" 1212 bless [$pid, $arg{cb}], "AnyEvent::Base::child"
1181} 1213}
1182 1214
1183sub AnyEvent::Base::Child::DESTROY { 1215sub AnyEvent::Base::child::DESTROY {
1184 my ($pid, $cb) = @{$_[0]}; 1216 my ($pid, $cb) = @{$_[0]};
1185 1217
1186 delete $PID_CB{$pid}{$cb}; 1218 delete $PID_CB{$pid}{$cb};
1187 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; 1219 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} };
1188 1220
1189 undef $CHLD_W unless keys %PID_CB; 1221 undef $CHLD_W unless keys %PID_CB;
1222}
1223
1224# idle emulation is done by simply using a timer, regardless
1225# of whether the process is idle or not, and not letting
1226# the callback use more than 50% of the time.
1227sub idle {
1228 my (undef, %arg) = @_;
1229
1230 my ($cb, $w, $rcb) = $arg{cb};
1231
1232 $rcb = sub {
1233 if ($cb) {
1234 $w = _time;
1235 &$cb;
1236 $w = _time - $w;
1237
1238 # never use more then 50% of the time for the idle watcher,
1239 # within some limits
1240 $w = 0.0001 if $w < 0.0001;
1241 $w = 5 if $w > 5;
1242
1243 $w = AnyEvent->timer (after => $w, cb => $rcb);
1244 } else {
1245 # clean up...
1246 undef $w;
1247 undef $rcb;
1248 }
1249 };
1250
1251 $w = AnyEvent->timer (after => 0.05, cb => $rcb);
1252
1253 bless \\$cb, "AnyEvent::Base::idle"
1254}
1255
1256sub AnyEvent::Base::idle::DESTROY {
1257 undef $${$_[0]};
1190} 1258}
1191 1259
1192package AnyEvent::CondVar; 1260package AnyEvent::CondVar;
1193 1261
1194our @ISA = AnyEvent::CondVar::Base::; 1262our @ISA = AnyEvent::CondVar::Base::;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines