… | |
… | |
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: |
… | |
… | |
168 | =head2 I/O WATCHERS |
175 | =head2 I/O WATCHERS |
169 | |
176 | |
170 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
177 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
171 | with the following mandatory key-value pairs as arguments: |
178 | with the following mandatory key-value pairs as arguments: |
172 | |
179 | |
173 | C<fh> the Perl I<file handle> (I<not> file descriptor) to watch for events |
180 | C<fh> is the Perl I<file handle> (I<not> file descriptor) to watch |
174 | (AnyEvent might or might not keep a reference to this file handle). C<poll> |
181 | for events (AnyEvent might or might not keep a reference to this file |
|
|
182 | handle). Note that only file handles pointing to things for which |
|
|
183 | non-blocking operation makes sense are allowed. This includes sockets, |
|
|
184 | most character devices, pipes, fifos and so on, but not for example files |
|
|
185 | or block devices. |
|
|
186 | |
175 | must be a string that is either C<r> or C<w>, which creates a watcher |
187 | C<poll> must be a string that is either C<r> or C<w>, which creates a |
176 | waiting for "r"eadable or "w"ritable events, respectively. C<cb> is the |
188 | watcher waiting for "r"eadable or "w"ritable events, respectively. |
|
|
189 | |
177 | callback to invoke each time the file handle becomes ready. |
190 | C<cb> is the callback to invoke each time the file handle becomes ready. |
178 | |
191 | |
179 | Although the callback might get passed parameters, their value and |
192 | Although the callback might get passed parameters, their value and |
180 | presence is undefined and you cannot rely on them. Portable AnyEvent |
193 | presence is undefined and you cannot rely on them. Portable AnyEvent |
181 | callbacks cannot use arguments passed to I/O watcher callbacks. |
194 | callbacks cannot use arguments passed to I/O watcher callbacks. |
182 | |
195 | |
… | |
… | |
313 | |
326 | |
314 | In either case, if you care (and in most cases, you don't), then you |
327 | In either case, if you care (and in most cases, you don't), then you |
315 | can get whatever behaviour you want with any event loop, by taking the |
328 | can get whatever behaviour you want with any event loop, by taking the |
316 | difference between C<< AnyEvent->time >> and C<< AnyEvent->now >> into |
329 | difference between C<< AnyEvent->time >> and C<< AnyEvent->now >> into |
317 | account. |
330 | account. |
|
|
331 | |
|
|
332 | =item AnyEvent->now_update |
|
|
333 | |
|
|
334 | Some event loops (such as L<EV> or L<AnyEvent::Impl::Perl>) cache |
|
|
335 | the current time for each loop iteration (see the discussion of L<< |
|
|
336 | AnyEvent->now >>, above). |
|
|
337 | |
|
|
338 | When a callback runs for a long time (or when the process sleeps), then |
|
|
339 | this "current" time will differ substantially from the real time, which |
|
|
340 | might affect timers and time-outs. |
|
|
341 | |
|
|
342 | When this is the case, you can call this method, which will update the |
|
|
343 | event loop's idea of "current time". |
|
|
344 | |
|
|
345 | Note that updating the time I<might> cause some events to be handled. |
318 | |
346 | |
319 | =back |
347 | =back |
320 | |
348 | |
321 | =head2 SIGNAL WATCHERS |
349 | =head2 SIGNAL WATCHERS |
322 | |
350 | |
… | |
… | |
389 | ); |
417 | ); |
390 | |
418 | |
391 | # do something else, then wait for process exit |
419 | # do something else, then wait for process exit |
392 | $done->recv; |
420 | $done->recv; |
393 | |
421 | |
|
|
422 | =head2 IDLE WATCHERS |
|
|
423 | |
|
|
424 | Sometimes there is a need to do something, but it is not so important |
|
|
425 | to 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 |
|
|
427 | attention by the event loop". |
|
|
428 | |
|
|
429 | Idle watchers ideally get invoked when the event loop has nothing |
|
|
430 | better to do, just before it would block the process to wait for new |
|
|
431 | events. Instead of blocking, the idle watcher is invoked. |
|
|
432 | |
|
|
433 | Most event loops unfortunately do not really support idle watchers (only |
|
|
434 | EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent |
|
|
435 | will simply call the callback "from time to time". |
|
|
436 | |
|
|
437 | Example: read lines from STDIN, but only process them when the |
|
|
438 | program 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 | |
394 | =head2 CONDITION VARIABLES |
457 | =head2 CONDITION VARIABLES |
395 | |
458 | |
396 | If you are familiar with some event loops you will know that all of them |
459 | If you are familiar with some event loops you will know that all of them |
397 | require you to run some blocking "loop", "run" or similar function that |
460 | require you to run some blocking "loop", "run" or similar function that |
398 | will actively watch for new events and call your callbacks. |
461 | will actively watch for new events and call your callbacks. |
… | |
… | |
867 | no warnings; |
930 | no warnings; |
868 | use strict qw(vars subs); |
931 | use strict qw(vars subs); |
869 | |
932 | |
870 | use Carp; |
933 | use Carp; |
871 | |
934 | |
872 | our $VERSION = 4.341; |
935 | our $VERSION = 4.41; |
873 | our $MODEL; |
936 | our $MODEL; |
874 | |
937 | |
875 | our $AUTOLOAD; |
938 | our $AUTOLOAD; |
876 | our @ISA; |
939 | our @ISA; |
877 | |
940 | |
… | |
… | |
909 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
972 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
910 | [Wx:: => AnyEvent::Impl::POE::], |
973 | [Wx:: => AnyEvent::Impl::POE::], |
911 | [Prima:: => AnyEvent::Impl::POE::], |
974 | [Prima:: => AnyEvent::Impl::POE::], |
912 | ); |
975 | ); |
913 | |
976 | |
914 | our %method = map +($_ => 1), qw(io timer time now signal child condvar one_event DESTROY); |
977 | our %method = map +($_ => 1), |
|
|
978 | qw(io timer time now now_update signal child idle condvar one_event DESTROY); |
915 | |
979 | |
916 | our @post_detect; |
980 | our @post_detect; |
917 | |
981 | |
918 | sub post_detect(&) { |
982 | sub post_detect(&) { |
919 | my ($cb) = @_; |
983 | my ($cb) = @_; |
… | |
… | |
924 | 1 |
988 | 1 |
925 | } else { |
989 | } else { |
926 | push @post_detect, $cb; |
990 | push @post_detect, $cb; |
927 | |
991 | |
928 | defined wantarray |
992 | defined wantarray |
929 | ? bless \$cb, "AnyEvent::Util::PostDetect" |
993 | ? bless \$cb, "AnyEvent::Util::postdetect" |
930 | : () |
994 | : () |
931 | } |
995 | } |
932 | } |
996 | } |
933 | |
997 | |
934 | sub AnyEvent::Util::PostDetect::DESTROY { |
998 | sub AnyEvent::Util::postdetect::DESTROY { |
935 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
999 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
936 | } |
1000 | } |
937 | |
1001 | |
938 | sub detect() { |
1002 | sub detect() { |
939 | unless ($MODEL) { |
1003 | unless ($MODEL) { |
… | |
… | |
976 | last; |
1040 | last; |
977 | } |
1041 | } |
978 | } |
1042 | } |
979 | |
1043 | |
980 | $MODEL |
1044 | $MODEL |
981 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib."; |
1045 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib.\n"; |
982 | } |
1046 | } |
983 | } |
1047 | } |
984 | |
1048 | |
985 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1049 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
986 | |
1050 | |
… | |
… | |
1016 | my ($rw, $mode) = $poll eq "r" ? ($r, "<") |
1080 | my ($rw, $mode) = $poll eq "r" ? ($r, "<") |
1017 | : $poll eq "w" ? ($w, ">") |
1081 | : $poll eq "w" ? ($w, ">") |
1018 | : Carp::croak "AnyEvent->io requires poll set to either 'r' or 'w'"; |
1082 | : Carp::croak "AnyEvent->io requires poll set to either 'r' or 'w'"; |
1019 | |
1083 | |
1020 | open my $fh2, "$mode&" . fileno $fh |
1084 | open my $fh2, "$mode&" . fileno $fh |
1021 | or die "cannot dup() filehandle: $!"; |
1085 | or die "cannot dup() filehandle: $!,"; |
1022 | |
1086 | |
1023 | # we assume CLOEXEC is already set by perl in all important cases |
1087 | # we assume CLOEXEC is already set by perl in all important cases |
1024 | |
1088 | |
1025 | ($fh2, $rw) |
1089 | ($fh2, $rw) |
1026 | } |
1090 | } |
1027 | |
1091 | |
1028 | package AnyEvent::Base; |
1092 | package AnyEvent::Base; |
1029 | |
1093 | |
1030 | # default implementation for now and time |
1094 | # default implementations for many methods |
1031 | |
1095 | |
1032 | BEGIN { |
1096 | BEGIN { |
1033 | if (eval "use Time::HiRes (); time (); 1") { |
1097 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1034 | *_time = \&Time::HiRes::time; |
1098 | *_time = \&Time::HiRes::time; |
1035 | # if (eval "use POSIX (); (POSIX::times())... |
1099 | # if (eval "use POSIX (); (POSIX::times())... |
1036 | } else { |
1100 | } else { |
1037 | *_time = sub { time }; # epic fail |
1101 | *_time = sub { time }; # epic fail |
1038 | } |
1102 | } |
1039 | } |
1103 | } |
1040 | |
1104 | |
1041 | sub time { _time } |
1105 | sub time { _time } |
1042 | sub now { _time } |
1106 | sub now { _time } |
|
|
1107 | sub now_update { } |
1043 | |
1108 | |
1044 | # default implementation for ->condvar |
1109 | # default implementation for ->condvar |
1045 | |
1110 | |
1046 | sub condvar { |
1111 | sub condvar { |
1047 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: |
1112 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
1048 | } |
1113 | } |
1049 | |
1114 | |
1050 | # default implementation for ->signal |
1115 | # default implementation for ->signal |
1051 | |
1116 | |
1052 | our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); |
1117 | our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); |
1053 | |
1118 | |
1054 | sub _signal_exec { |
1119 | sub _signal_exec { |
|
|
1120 | sysread $SIGPIPE_R, my $dummy, 4; |
|
|
1121 | |
1055 | while (%SIG_EV) { |
1122 | while (%SIG_EV) { |
1056 | sysread $SIGPIPE_R, my $dummy, 4; |
|
|
1057 | for (keys %SIG_EV) { |
1123 | for (keys %SIG_EV) { |
1058 | delete $SIG_EV{$_}; |
1124 | delete $SIG_EV{$_}; |
1059 | $_->() for values %{ $SIG_CB{$_} || {} }; |
1125 | $_->() for values %{ $SIG_CB{$_} || {} }; |
1060 | } |
1126 | } |
1061 | } |
1127 | } |
… | |
… | |
1063 | |
1129 | |
1064 | sub signal { |
1130 | sub signal { |
1065 | my (undef, %arg) = @_; |
1131 | my (undef, %arg) = @_; |
1066 | |
1132 | |
1067 | unless ($SIGPIPE_R) { |
1133 | unless ($SIGPIPE_R) { |
|
|
1134 | require Fcntl; |
|
|
1135 | |
1068 | if (AnyEvent::WIN32) { |
1136 | if (AnyEvent::WIN32) { |
|
|
1137 | require AnyEvent::Util; |
|
|
1138 | |
1069 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
1139 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
1070 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_R) if $SIGPIPE_R; |
1140 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_R) if $SIGPIPE_R; |
1071 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_W) if $SIGPIPE_W; # just in case |
1141 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_W) if $SIGPIPE_W; # just in case |
1072 | } else { |
1142 | } else { |
1073 | pipe $SIGPIPE_R, $SIGPIPE_W; |
1143 | pipe $SIGPIPE_R, $SIGPIPE_W; |
1074 | require Fcntl; |
|
|
1075 | fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R; |
1144 | fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R; |
1076 | fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case |
1145 | fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case |
1077 | } |
1146 | } |
1078 | |
1147 | |
1079 | $SIGPIPE_R |
1148 | $SIGPIPE_R |
1080 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1149 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1081 | |
1150 | |
|
|
1151 | # not strictly required, as $^F is normally 2, but let's make sure... |
|
|
1152 | fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
|
|
1153 | fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
|
|
1154 | |
1082 | $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec); |
1155 | $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec); |
1083 | } |
1156 | } |
1084 | |
1157 | |
1085 | my $signal = uc $arg{signal} |
1158 | my $signal = uc $arg{signal} |
1086 | or Carp::croak "required option 'signal' is missing"; |
1159 | or Carp::croak "required option 'signal' is missing"; |
1087 | |
1160 | |
1088 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1161 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1089 | $SIG{$signal} ||= sub { |
1162 | $SIG{$signal} ||= sub { |
|
|
1163 | local $!; |
1090 | syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; |
1164 | syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; |
1091 | undef $SIG_EV{$signal}; |
1165 | undef $SIG_EV{$signal}; |
1092 | }; |
1166 | }; |
1093 | |
1167 | |
1094 | bless [$signal, $arg{cb}], "AnyEvent::Base::Signal" |
1168 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
1095 | } |
1169 | } |
1096 | |
1170 | |
1097 | sub AnyEvent::Base::Signal::DESTROY { |
1171 | sub AnyEvent::Base::signal::DESTROY { |
1098 | my ($signal, $cb) = @{$_[0]}; |
1172 | my ($signal, $cb) = @{$_[0]}; |
1099 | |
1173 | |
1100 | delete $SIG_CB{$signal}{$cb}; |
1174 | delete $SIG_CB{$signal}{$cb}; |
1101 | |
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. |
1102 | delete $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; |
1179 | undef $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; |
1103 | } |
1180 | } |
1104 | |
1181 | |
1105 | # default implementation for ->child |
1182 | # default implementation for ->child |
1106 | |
1183 | |
1107 | our %PID_CB; |
1184 | our %PID_CB; |
1108 | our $CHLD_W; |
1185 | our $CHLD_W; |
1109 | our $CHLD_DELAY_W; |
1186 | our $CHLD_DELAY_W; |
1110 | our $PID_IDLE; |
|
|
1111 | our $WNOHANG; |
1187 | our $WNOHANG; |
1112 | |
1188 | |
1113 | sub _child_wait { |
1189 | sub _sigchld { |
1114 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
1190 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
1115 | $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
1191 | $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
1116 | (values %{ $PID_CB{0} || {} }); |
1192 | (values %{ $PID_CB{0} || {} }); |
1117 | } |
1193 | } |
1118 | |
|
|
1119 | undef $PID_IDLE; |
|
|
1120 | } |
|
|
1121 | |
|
|
1122 | sub _sigchld { |
|
|
1123 | # make sure we deliver these changes "synchronous" with the event loop. |
|
|
1124 | $CHLD_DELAY_W ||= AnyEvent->timer (after => 0, cb => sub { |
|
|
1125 | undef $CHLD_DELAY_W; |
|
|
1126 | &_child_wait; |
|
|
1127 | }); |
|
|
1128 | } |
1194 | } |
1129 | |
1195 | |
1130 | sub child { |
1196 | sub child { |
1131 | my (undef, %arg) = @_; |
1197 | my (undef, %arg) = @_; |
1132 | |
1198 | |
1133 | defined (my $pid = $arg{pid} + 0) |
1199 | defined (my $pid = $arg{pid} + 0) |
1134 | or Carp::croak "required option 'pid' is missing"; |
1200 | or Carp::croak "required option 'pid' is missing"; |
1135 | |
1201 | |
1136 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1202 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1137 | |
1203 | |
1138 | unless ($WNOHANG) { |
|
|
1139 | $WNOHANG = eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1204 | $WNOHANG ||= eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1140 | } |
|
|
1141 | |
1205 | |
1142 | unless ($CHLD_W) { |
1206 | unless ($CHLD_W) { |
1143 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
1207 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
1144 | # 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 |
1145 | &_sigchld; |
1209 | &_sigchld; |
1146 | } |
1210 | } |
1147 | |
1211 | |
1148 | bless [$pid, $arg{cb}], "AnyEvent::Base::Child" |
1212 | bless [$pid, $arg{cb}], "AnyEvent::Base::child" |
1149 | } |
1213 | } |
1150 | |
1214 | |
1151 | sub AnyEvent::Base::Child::DESTROY { |
1215 | sub AnyEvent::Base::child::DESTROY { |
1152 | my ($pid, $cb) = @{$_[0]}; |
1216 | my ($pid, $cb) = @{$_[0]}; |
1153 | |
1217 | |
1154 | delete $PID_CB{$pid}{$cb}; |
1218 | delete $PID_CB{$pid}{$cb}; |
1155 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
1219 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
1156 | |
1220 | |
1157 | 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. |
|
|
1227 | sub 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 | |
|
|
1256 | sub AnyEvent::Base::idle::DESTROY { |
|
|
1257 | undef $${$_[0]}; |
1158 | } |
1258 | } |
1159 | |
1259 | |
1160 | package AnyEvent::CondVar; |
1260 | package AnyEvent::CondVar; |
1161 | |
1261 | |
1162 | our @ISA = AnyEvent::CondVar::Base::; |
1262 | our @ISA = AnyEvent::CondVar::Base::; |