… | |
… | |
930 | no warnings; |
930 | no warnings; |
931 | use strict qw(vars subs); |
931 | use strict qw(vars subs); |
932 | |
932 | |
933 | use Carp; |
933 | use Carp; |
934 | |
934 | |
935 | our $VERSION = 4.4; |
935 | our $VERSION = 4.41; |
936 | our $MODEL; |
936 | our $MODEL; |
937 | |
937 | |
938 | our $AUTOLOAD; |
938 | our $AUTOLOAD; |
939 | our @ISA; |
939 | our @ISA; |
940 | |
940 | |
… | |
… | |
1171 | sub AnyEvent::Base::signal::DESTROY { |
1171 | sub AnyEvent::Base::signal::DESTROY { |
1172 | my ($signal, $cb) = @{$_[0]}; |
1172 | my ($signal, $cb) = @{$_[0]}; |
1173 | |
1173 | |
1174 | delete $SIG_CB{$signal}{$cb}; |
1174 | delete $SIG_CB{$signal}{$cb}; |
1175 | |
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. |
1176 | delete $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; |
1179 | undef $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; |
1177 | } |
1180 | } |
1178 | |
1181 | |
1179 | # default implementation for ->child |
1182 | # default implementation for ->child |
1180 | |
1183 | |
1181 | our %PID_CB; |
1184 | our %PID_CB; |
1182 | our $CHLD_W; |
1185 | our $CHLD_W; |
1183 | our $CHLD_DELAY_W; |
1186 | our $CHLD_DELAY_W; |
1184 | our $PID_IDLE; |
|
|
1185 | our $WNOHANG; |
1187 | our $WNOHANG; |
1186 | |
1188 | |
1187 | sub _child_wait { |
1189 | sub _sigchld { |
1188 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
1190 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
1189 | $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
1191 | $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
1190 | (values %{ $PID_CB{0} || {} }); |
1192 | (values %{ $PID_CB{0} || {} }); |
1191 | } |
1193 | } |
1192 | |
|
|
1193 | undef $PID_IDLE; |
|
|
1194 | } |
|
|
1195 | |
|
|
1196 | sub _sigchld { |
|
|
1197 | # make sure we deliver these changes "synchronous" with the event loop. |
|
|
1198 | $CHLD_DELAY_W ||= AnyEvent->timer (after => 0, cb => sub { |
|
|
1199 | undef $CHLD_DELAY_W; |
|
|
1200 | &_child_wait; |
|
|
1201 | }); |
|
|
1202 | } |
1194 | } |
1203 | |
1195 | |
1204 | sub child { |
1196 | sub child { |
1205 | my (undef, %arg) = @_; |
1197 | my (undef, %arg) = @_; |
1206 | |
1198 | |
1207 | defined (my $pid = $arg{pid} + 0) |
1199 | defined (my $pid = $arg{pid} + 0) |
1208 | or Carp::croak "required option 'pid' is missing"; |
1200 | or Carp::croak "required option 'pid' is missing"; |
1209 | |
1201 | |
1210 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1202 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1211 | |
1203 | |
1212 | unless ($WNOHANG) { |
|
|
1213 | $WNOHANG = eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1204 | $WNOHANG ||= eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1214 | } |
|
|
1215 | |
1205 | |
1216 | unless ($CHLD_W) { |
1206 | unless ($CHLD_W) { |
1217 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
1207 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
1218 | # 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 |
1219 | &_sigchld; |
1209 | &_sigchld; |
… | |
… | |
1230 | |
1220 | |
1231 | undef $CHLD_W unless keys %PID_CB; |
1221 | undef $CHLD_W unless keys %PID_CB; |
1232 | } |
1222 | } |
1233 | |
1223 | |
1234 | # idle emulation is done by simply using a timer, regardless |
1224 | # idle emulation is done by simply using a timer, regardless |
1235 | # of whether the proces sis idle or not, and not letting |
1225 | # of whether the process is idle or not, and not letting |
1236 | # the callback use more than 50% of the time. |
1226 | # the callback use more than 50% of the time. |
1237 | sub idle { |
1227 | sub idle { |
1238 | my (undef, %arg) = @_; |
1228 | my (undef, %arg) = @_; |
1239 | |
1229 | |
1240 | my ($cb, $w, $rcb) = $arg{cb}; |
1230 | my ($cb, $w, $rcb) = $arg{cb}; |