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.19 by root, Sun Dec 10 23:59:15 2006 UTC vs.
Revision 1.26 by root, Sun Jul 8 08:52:10 2007 UTC

160=back 160=back
161 161
162=head2 SIGNAL WATCHERS 162=head2 SIGNAL WATCHERS
163 163
164You can listen for signals using a signal watcher, C<signal> is the signal 164You can listen for signals using a signal watcher, C<signal> is the signal
165I<name> without any C<SIG> prefix. 165I<name> without any C<SIG> prefix. Multiple signals events can be clumped
166together into one callback invocation, and callback invocation might or
167might not be asynchronous.
166 168
167These watchers might use C<%SIG>, so programs overwriting those signals 169These watchers might use C<%SIG>, so programs overwriting those signals
168directly will likely not work correctly. 170directly will likely not work correctly.
169 171
170Example: exit on SIGINT 172Example: 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
178You can also listen for the status of a child process specified by the
179C<pid> argument. The watcher will only trigger once. This works by
180installing a signal handler for C<SIGCHLD>.
181
182Example: 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
234package AnyEvent; 246package AnyEvent;
235 247
236no warnings; 248no warnings;
237use strict; 249use strict;
250
238use Carp; 251use Carp;
239 252
240our $VERSION = '2.5'; 253our $VERSION = '2.53';
241our $MODEL; 254our $MODEL;
242 255
243our $AUTOLOAD; 256our $AUTOLOAD;
244our @ISA; 257our @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
311package AnyEvent::Base; 326package AnyEvent::Base;
312 327
328# default implementation for ->condvar, ->wait, ->broadcast
329
330sub condvar {
331 bless \my $flag, "AnyEvent::Base::CondVar"
332}
333
334sub AnyEvent::Base::CondVar::broadcast {
335 ${$_[0]}++;
336}
337
338sub AnyEvent::Base::CondVar::wait {
339 AnyEvent->one_event while !${$_[0]};
340}
341
313# default implementation for signal 342# default implementation for ->signal
314 343
315our %SIG_CB; 344our %SIG_CB;
316 345
317sub signal { 346sub 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
333sub AnyEvent::Base::Signal::DESTROY { 360sub 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
370our %PID_CB;
371our $CHLD_W;
372our $PID_IDLE;
373our $WNOHANG;
374
375sub _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
383sub 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
404sub 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
343If you need to support another event library which isn't directly 415If you need to support another event library which isn't directly
368I<rxvt-unicode> distribution. 440I<rxvt-unicode> distribution.
369 441
370I<rxvt-unicode> also cheats a bit by not providing blocking access to 442I<rxvt-unicode> also cheats a bit by not providing blocking access to
371condition variables: code blocking while waiting for a condition will 443condition variables: code blocking while waiting for a condition will
372C<die>. This still works with most modules/usages, and blocking calls must 444C<die>. This still works with most modules/usages, and blocking calls must
373not be in an interactive appliation, so it makes sense. 445not be in an interactive application, so it makes sense.
374 446
375=head1 ENVIRONMENT VARIABLES 447=head1 ENVIRONMENT VARIABLES
376 448
377The following environment variables are used by this module: 449The following environment variables are used by this module:
378 450

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines