… | |
… | |
160 | =back |
160 | =back |
161 | |
161 | |
162 | =head2 SIGNAL WATCHERS |
162 | =head2 SIGNAL WATCHERS |
163 | |
163 | |
164 | You can listen for signals using a signal watcher, C<signal> is the signal |
164 | You can listen for signals using a signal watcher, C<signal> is the signal |
165 | I<name> without any C<SIG> prefix. |
165 | I<name> without any C<SIG> prefix. Multiple signals events can be clumped |
|
|
166 | together into one callback invocation, and callback invocation might or |
|
|
167 | might not be asynchronous. |
166 | |
168 | |
167 | These watchers might use C<%SIG>, so programs overwriting those signals |
169 | These watchers might use C<%SIG>, so programs overwriting those signals |
168 | directly will likely not work correctly. |
170 | directly will likely not work correctly. |
169 | |
171 | |
170 | Example: exit on SIGINT |
172 | Example: exit on SIGINT |
171 | |
173 | |
172 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
174 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
|
|
175 | |
|
|
176 | =head2 CHILD PROCESS WATCHERS |
|
|
177 | |
|
|
178 | You can also listen for the status of a child process specified by the |
|
|
179 | C<pid> argument. The watcher will only trigger once. This works by |
|
|
180 | installing a signal handler for C<SIGCHLD>. |
|
|
181 | |
|
|
182 | Example: wait for pid 1333 |
|
|
183 | |
|
|
184 | my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); |
173 | |
185 | |
174 | =head1 GLOBALS |
186 | =head1 GLOBALS |
175 | |
187 | |
176 | =over 4 |
188 | =over 4 |
177 | |
189 | |
… | |
… | |
233 | |
245 | |
234 | package AnyEvent; |
246 | package AnyEvent; |
235 | |
247 | |
236 | no warnings; |
248 | no warnings; |
237 | use strict; |
249 | use strict; |
|
|
250 | |
238 | use Carp; |
251 | use Carp; |
239 | |
252 | |
240 | our $VERSION = '2.5'; |
253 | our $VERSION = '2.53'; |
241 | our $MODEL; |
254 | our $MODEL; |
242 | |
255 | |
243 | our $AUTOLOAD; |
256 | our $AUTOLOAD; |
244 | our @ISA; |
257 | our @ISA; |
245 | |
258 | |
… | |
… | |
276 | unless ($MODEL) { |
289 | unless ($MODEL) { |
277 | # try to load a model |
290 | # try to load a model |
278 | |
291 | |
279 | for (@REGISTRY, @models) { |
292 | for (@REGISTRY, @models) { |
280 | my ($package, $model) = @$_; |
293 | my ($package, $model) = @$_; |
|
|
294 | if (eval "require $package" |
|
|
295 | and ${"$package\::VERSION"} > 0 |
281 | if (eval "require $model") { |
296 | and eval "require $model") { |
282 | $MODEL = $model; |
297 | $MODEL = $model; |
283 | warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; |
298 | warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; |
284 | last; |
299 | last; |
285 | } |
300 | } |
286 | } |
301 | } |
… | |
… | |
308 | $class->$func (@_); |
323 | $class->$func (@_); |
309 | } |
324 | } |
310 | |
325 | |
311 | package AnyEvent::Base; |
326 | package AnyEvent::Base; |
312 | |
327 | |
|
|
328 | # default implementation for ->condvar, ->wait, ->broadcast |
|
|
329 | |
|
|
330 | sub condvar { |
|
|
331 | bless \my $flag, "AnyEvent::Base::CondVar" |
|
|
332 | } |
|
|
333 | |
|
|
334 | sub AnyEvent::Base::CondVar::broadcast { |
|
|
335 | ${$_[0]}++; |
|
|
336 | } |
|
|
337 | |
|
|
338 | sub AnyEvent::Base::CondVar::wait { |
|
|
339 | AnyEvent->one_event while !${$_[0]}; |
|
|
340 | } |
|
|
341 | |
313 | # default implementation for signal |
342 | # default implementation for ->signal |
314 | |
343 | |
315 | our %SIG_CB; |
344 | our %SIG_CB; |
316 | |
345 | |
317 | sub signal { |
346 | sub signal { |
318 | my (undef, %arg) = @_; |
347 | my (undef, %arg) = @_; |
319 | |
348 | |
320 | my $signal = uc $arg{signal} |
349 | my $signal = uc $arg{signal} |
321 | or Carp::croak "required option 'signal' is missing"; |
350 | or Carp::croak "required option 'signal' is missing"; |
322 | |
351 | |
323 | my $w = bless [$signal, $arg{cb}], "AnyEvent::Base::Signal"; |
|
|
324 | |
|
|
325 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
352 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
326 | $SIG{$signal} ||= sub { |
353 | $SIG{$signal} ||= sub { |
327 | $_->() for values %{ $SIG_CB{$signal} }; |
354 | $_->() for values %{ $SIG_CB{$signal} || {} }; |
328 | }; |
355 | }; |
329 | |
356 | |
330 | $w |
357 | bless [$signal, $arg{cb}], "AnyEvent::Base::Signal" |
331 | } |
358 | } |
332 | |
359 | |
333 | sub AnyEvent::Base::Signal::DESTROY { |
360 | sub AnyEvent::Base::Signal::DESTROY { |
334 | my ($signal, $cb) = @{$_[0]}; |
361 | my ($signal, $cb) = @{$_[0]}; |
335 | |
362 | |
336 | delete $SIG_CB{$signal}{$cb}; |
363 | delete $SIG_CB{$signal}{$cb}; |
337 | |
364 | |
338 | $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; |
365 | $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; |
|
|
366 | } |
|
|
367 | |
|
|
368 | # default implementation for ->child |
|
|
369 | |
|
|
370 | our %PID_CB; |
|
|
371 | our $CHLD_W; |
|
|
372 | our $PID_IDLE; |
|
|
373 | our $WNOHANG; |
|
|
374 | |
|
|
375 | sub _child_wait { |
|
|
376 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
|
|
377 | $_->() for values %{ (delete $PID_CB{$pid}) || {} }; |
|
|
378 | } |
|
|
379 | |
|
|
380 | undef $PID_IDLE; |
|
|
381 | } |
|
|
382 | |
|
|
383 | sub child { |
|
|
384 | my (undef, %arg) = @_; |
|
|
385 | |
|
|
386 | my $pid = uc $arg{pid} |
|
|
387 | or Carp::croak "required option 'pid' is missing"; |
|
|
388 | |
|
|
389 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
|
|
390 | |
|
|
391 | unless ($WNOHANG) { |
|
|
392 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
|
|
393 | } |
|
|
394 | |
|
|
395 | unless ($CHLD_W) { |
|
|
396 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait); |
|
|
397 | # child could be a zombie already |
|
|
398 | $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait); |
|
|
399 | } |
|
|
400 | |
|
|
401 | bless [$pid, $arg{cb}], "AnyEvent::Base::Child" |
|
|
402 | } |
|
|
403 | |
|
|
404 | sub AnyEvent::Base::Child::DESTROY { |
|
|
405 | my ($pid, $cb) = @{$_[0]}; |
|
|
406 | |
|
|
407 | delete $PID_CB{$pid}{$cb}; |
|
|
408 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
|
|
409 | |
|
|
410 | undef $CHLD_W unless keys %PID_CB; |
339 | } |
411 | } |
340 | |
412 | |
341 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
413 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
342 | |
414 | |
343 | If you need to support another event library which isn't directly |
415 | If you need to support another event library which isn't directly |
… | |
… | |
368 | I<rxvt-unicode> distribution. |
440 | I<rxvt-unicode> distribution. |
369 | |
441 | |
370 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
442 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
371 | condition variables: code blocking while waiting for a condition will |
443 | condition variables: code blocking while waiting for a condition will |
372 | C<die>. This still works with most modules/usages, and blocking calls must |
444 | C<die>. This still works with most modules/usages, and blocking calls must |
373 | not be in an interactive appliation, so it makes sense. |
445 | not be in an interactive application, so it makes sense. |
374 | |
446 | |
375 | =head1 ENVIRONMENT VARIABLES |
447 | =head1 ENVIRONMENT VARIABLES |
376 | |
448 | |
377 | The following environment variables are used by this module: |
449 | The following environment variables are used by this module: |
378 | |
450 | |