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.32 by root, Sat Nov 3 09:29:51 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 (or any child if the pid argument is 0). The watcher will
180trigger as often as status change for the child are received. This works
181by installing a signal handler for C<SIGCHLD>. The callback will be called with
182the pid and exit status (as returned by waitpid).
183
184Example: wait for pid 1333
185
186 my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" });
173 187
174=head1 GLOBALS 188=head1 GLOBALS
175 189
176=over 4 190=over 4
177 191
183C<AnyEvent::Impl:xxx> modules, but can be any other class in the case 197C<AnyEvent::Impl:xxx> modules, but can be any other class in the case
184AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). 198AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>).
185 199
186The known classes so far are: 200The known classes so far are:
187 201
202 EV::AnyEvent based on EV (an interface to libev, best choice)
188 AnyEvent::Impl::Coro based on Coro::Event, best choise. 203 AnyEvent::Impl::Coro based on Coro::Event, second best choice.
189 AnyEvent::Impl::Event based on Event, also best choice :) 204 AnyEvent::Impl::Event based on Event, also second best choice :)
190 AnyEvent::Impl::Glib based on Glib, second-best choice. 205 AnyEvent::Impl::Glib based on Glib, second-best choice.
191 AnyEvent::Impl::Tk based on Tk, very bad choice. 206 AnyEvent::Impl::Tk based on Tk, very bad choice.
192 AnyEvent::Impl::Perl pure-perl implementation, inefficient. 207 AnyEvent::Impl::Perl pure-perl implementation, inefficient.
193 208
194=item AnyEvent::detect 209=item AnyEvent::detect
233 248
234package AnyEvent; 249package AnyEvent;
235 250
236no warnings; 251no warnings;
237use strict; 252use strict;
253
238use Carp; 254use Carp;
239 255
240our $VERSION = '2.5'; 256our $VERSION = '2.55';
241our $MODEL; 257our $MODEL;
242 258
243our $AUTOLOAD; 259our $AUTOLOAD;
244our @ISA; 260our @ISA;
245 261
247 263
248our @REGISTRY; 264our @REGISTRY;
249 265
250my @models = ( 266my @models = (
251 [Coro::Event:: => AnyEvent::Impl::Coro::], 267 [Coro::Event:: => AnyEvent::Impl::Coro::],
268 [EV:: => EV::AnyEvent::],
252 [Event:: => AnyEvent::Impl::Event::], 269 [Event:: => AnyEvent::Impl::Event::],
253 [Glib:: => AnyEvent::Impl::Glib::], 270 [Glib:: => AnyEvent::Impl::Glib::],
254 [Tk:: => AnyEvent::Impl::Tk::], 271 [Tk:: => AnyEvent::Impl::Tk::],
255 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], 272 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
256); 273);
276 unless ($MODEL) { 293 unless ($MODEL) {
277 # try to load a model 294 # try to load a model
278 295
279 for (@REGISTRY, @models) { 296 for (@REGISTRY, @models) {
280 my ($package, $model) = @$_; 297 my ($package, $model) = @$_;
298 if (eval "require $package"
299 and ${"$package\::VERSION"} > 0
281 if (eval "require $model") { 300 and eval "require $model") {
282 $MODEL = $model; 301 $MODEL = $model;
283 warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; 302 warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1;
284 last; 303 last;
285 } 304 }
286 } 305 }
308 $class->$func (@_); 327 $class->$func (@_);
309} 328}
310 329
311package AnyEvent::Base; 330package AnyEvent::Base;
312 331
332# default implementation for ->condvar, ->wait, ->broadcast
333
334sub condvar {
335 bless \my $flag, "AnyEvent::Base::CondVar"
336}
337
338sub AnyEvent::Base::CondVar::broadcast {
339 ${$_[0]}++;
340}
341
342sub AnyEvent::Base::CondVar::wait {
343 AnyEvent->one_event while !${$_[0]};
344}
345
313# default implementation for signal 346# default implementation for ->signal
314 347
315our %SIG_CB; 348our %SIG_CB;
316 349
317sub signal { 350sub signal {
318 my (undef, %arg) = @_; 351 my (undef, %arg) = @_;
319 352
320 my $signal = uc $arg{signal} 353 my $signal = uc $arg{signal}
321 or Carp::croak "required option 'signal' is missing"; 354 or Carp::croak "required option 'signal' is missing";
322 355
323 my $w = bless [$signal, $arg{cb}], "AnyEvent::Base::Signal";
324
325 $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; 356 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
326 $SIG{$signal} ||= sub { 357 $SIG{$signal} ||= sub {
327 $_->() for values %{ $SIG_CB{$signal} }; 358 $_->() for values %{ $SIG_CB{$signal} || {} };
328 }; 359 };
329 360
330 $w 361 bless [$signal, $arg{cb}], "AnyEvent::Base::Signal"
331} 362}
332 363
333sub AnyEvent::Base::Signal::DESTROY { 364sub AnyEvent::Base::Signal::DESTROY {
334 my ($signal, $cb) = @{$_[0]}; 365 my ($signal, $cb) = @{$_[0]};
335 366
336 delete $SIG_CB{$signal}{$cb}; 367 delete $SIG_CB{$signal}{$cb};
337 368
338 $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; 369 $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} };
370}
371
372# default implementation for ->child
373
374our %PID_CB;
375our $CHLD_W;
376our $PID_IDLE;
377our $WNOHANG;
378
379sub _child_wait {
380 while (0 <= (my $pid = waitpid -1, $WNOHANG)) {
381 $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }),
382 (values %{ $PID_CB{0} || {} });
383 }
384
385 undef $PID_IDLE;
386}
387
388sub child {
389 my (undef, %arg) = @_;
390
391 defined (my $pid = $arg{pid} + 0)
392 or Carp::croak "required option 'pid' is missing";
393
394 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
395
396 unless ($WNOHANG) {
397 $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1;
398 }
399
400 unless ($CHLD_W) {
401 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait);
402 # child could be a zombie already
403 $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait);
404 }
405
406 bless [$pid, $arg{cb}], "AnyEvent::Base::Child"
407}
408
409sub AnyEvent::Base::Child::DESTROY {
410 my ($pid, $cb) = @{$_[0]};
411
412 delete $PID_CB{$pid}{$cb};
413 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} };
414
415 undef $CHLD_W unless keys %PID_CB;
339} 416}
340 417
341=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 418=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
342 419
343If you need to support another event library which isn't directly 420If you need to support another event library which isn't directly
368I<rxvt-unicode> distribution. 445I<rxvt-unicode> distribution.
369 446
370I<rxvt-unicode> also cheats a bit by not providing blocking access to 447I<rxvt-unicode> also cheats a bit by not providing blocking access to
371condition variables: code blocking while waiting for a condition will 448condition variables: code blocking while waiting for a condition will
372C<die>. This still works with most modules/usages, and blocking calls must 449C<die>. This still works with most modules/usages, and blocking calls must
373not be in an interactive appliation, so it makes sense. 450not be in an interactive application, so it makes sense.
374 451
375=head1 ENVIRONMENT VARIABLES 452=head1 ENVIRONMENT VARIABLES
376 453
377The following environment variables are used by this module: 454The following environment variables are used by this module:
378 455

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines