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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines