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.37 by root, Fri Nov 23 10:42:00 2007 UTC

70You can create I/O watcher by calling the C<< AnyEvent->io >> method with 70You can create I/O watcher by calling the C<< AnyEvent->io >> method with
71the following mandatory arguments: 71the following mandatory arguments:
72 72
73C<fh> the Perl I<filehandle> (not filedescriptor) to watch for 73C<fh> the Perl I<filehandle> (not filedescriptor) to watch for
74events. C<poll> must be a string that is either C<r> or C<w>, that creates 74events. C<poll> must be a string that is either C<r> or C<w>, that creates
75a watcher waiting for "r"eadable or "w"ritable events. C<cb> teh callback 75a watcher waiting for "r"eadable or "w"ritable events. C<cb> the callback
76to invoke everytime the filehandle becomes ready. 76to invoke everytime the filehandle becomes ready.
77 77
78Only one io watcher per C<fh> and C<poll> combination is allowed (i.e. on 78Only one io watcher per C<fh> and C<poll> combination is allowed (i.e. on
79a socket you can have one r + one w, not any more (limitation comes from 79a socket you can have one r + one w, not any more (limitation comes from
80Tk - if you are sure you are not using Tk this limitation is gone). 80Tk - if you are sure you are not using Tk this limitation is gone).
109 my $w = AnyEvent->timer (after => 7.7, cb => sub { 109 my $w = AnyEvent->timer (after => 7.7, cb => sub {
110 warn "timeout\n"; 110 warn "timeout\n";
111 }); 111 });
112 112
113 # to cancel the timer: 113 # to cancel the timer:
114 undef $w 114 undef $w;
115 115
116=head2 CONDITION WATCHERS 116=head2 CONDITION WATCHERS
117 117
118Condition watchers can be created by calling the C<< AnyEvent->condvar >> 118Condition watchers can be created by calling the C<< AnyEvent->condvar >>
119method without any arguments. 119method without any arguments.
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 AnyEvent::Impl::CoroEV based on Coro::EV, best choice.
203 AnyEvent::Impl::EV based on EV (an interface to libev, also best choice).
188 AnyEvent::Impl::Coro based on Coro::Event, best choise. 204 AnyEvent::Impl::Coro based on Coro::Event, second best choice.
189 AnyEvent::Impl::Event based on Event, also best choice :) 205 AnyEvent::Impl::Event based on Event, also second best choice :)
190 AnyEvent::Impl::Glib based on Glib, second-best choice. 206 AnyEvent::Impl::Glib based on Glib, second-best choice.
191 AnyEvent::Impl::Tk based on Tk, very bad choice. 207 AnyEvent::Impl::Tk based on Tk, very bad choice.
192 AnyEvent::Impl::Perl pure-perl implementation, inefficient. 208 AnyEvent::Impl::Perl pure-perl implementation, inefficient.
193 209
194=item AnyEvent::detect 210=item AnyEvent::detect
233 249
234package AnyEvent; 250package AnyEvent;
235 251
236no warnings; 252no warnings;
237use strict; 253use strict;
254
238use Carp; 255use Carp;
239 256
240our $VERSION = '2.5'; 257our $VERSION = '2.7';
241our $MODEL; 258our $MODEL;
242 259
243our $AUTOLOAD; 260our $AUTOLOAD;
244our @ISA; 261our @ISA;
245 262
246our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; 263our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1;
247 264
248our @REGISTRY; 265our @REGISTRY;
249 266
250my @models = ( 267my @models = (
268 [Coro::EV:: => AnyEvent::Impl::CoroEV::],
269 [EV:: => AnyEvent::Impl::EV::],
251 [Coro::Event:: => AnyEvent::Impl::Coro::], 270 [Coro::Event:: => AnyEvent::Impl::Coro::],
252 [Event:: => AnyEvent::Impl::Event::], 271 [Event:: => AnyEvent::Impl::Event::],
253 [Glib:: => AnyEvent::Impl::Glib::], 272 [Glib:: => AnyEvent::Impl::Glib::],
254 [Tk:: => AnyEvent::Impl::Tk::], 273 [Tk:: => AnyEvent::Impl::Tk::],
255 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], 274 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
276 unless ($MODEL) { 295 unless ($MODEL) {
277 # try to load a model 296 # try to load a model
278 297
279 for (@REGISTRY, @models) { 298 for (@REGISTRY, @models) {
280 my ($package, $model) = @$_; 299 my ($package, $model) = @$_;
300 if (eval "require $package"
301 and ${"$package\::VERSION"} > 0
281 if (eval "require $model") { 302 and eval "require $model") {
282 $MODEL = $model; 303 $MODEL = $model;
283 warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; 304 warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1;
284 last; 305 last;
285 } 306 }
286 } 307 }
287 308
288 $MODEL 309 $MODEL
289 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Event (or Coro+Event), Glib or Tk."; 310 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV (or Coro+EV), Event (or Coro+Event), Glib or Tk.";
290 } 311 }
291 312
292 unshift @ISA, $MODEL; 313 unshift @ISA, $MODEL;
293 push @{"$MODEL\::ISA"}, "AnyEvent::Base"; 314 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
294 } 315 }
308 $class->$func (@_); 329 $class->$func (@_);
309} 330}
310 331
311package AnyEvent::Base; 332package AnyEvent::Base;
312 333
334# default implementation for ->condvar, ->wait, ->broadcast
335
336sub condvar {
337 bless \my $flag, "AnyEvent::Base::CondVar"
338}
339
340sub AnyEvent::Base::CondVar::broadcast {
341 ${$_[0]}++;
342}
343
344sub AnyEvent::Base::CondVar::wait {
345 AnyEvent->one_event while !${$_[0]};
346}
347
313# default implementation for signal 348# default implementation for ->signal
314 349
315our %SIG_CB; 350our %SIG_CB;
316 351
317sub signal { 352sub signal {
318 my (undef, %arg) = @_; 353 my (undef, %arg) = @_;
319 354
320 my $signal = uc $arg{signal} 355 my $signal = uc $arg{signal}
321 or Carp::croak "required option 'signal' is missing"; 356 or Carp::croak "required option 'signal' is missing";
322 357
323 my $w = bless [$signal, $arg{cb}], "AnyEvent::Base::Signal";
324
325 $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; 358 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
326 $SIG{$signal} ||= sub { 359 $SIG{$signal} ||= sub {
327 $_->() for values %{ $SIG_CB{$signal} }; 360 $_->() for values %{ $SIG_CB{$signal} || {} };
328 }; 361 };
329 362
330 $w 363 bless [$signal, $arg{cb}], "AnyEvent::Base::Signal"
331} 364}
332 365
333sub AnyEvent::Base::Signal::DESTROY { 366sub AnyEvent::Base::Signal::DESTROY {
334 my ($signal, $cb) = @{$_[0]}; 367 my ($signal, $cb) = @{$_[0]};
335 368
336 delete $SIG_CB{$signal}{$cb}; 369 delete $SIG_CB{$signal}{$cb};
337 370
338 $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; 371 $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} };
372}
373
374# default implementation for ->child
375
376our %PID_CB;
377our $CHLD_W;
378our $CHLD_DELAY_W;
379our $PID_IDLE;
380our $WNOHANG;
381
382sub _child_wait {
383 while (0 <= (my $pid = waitpid -1, $WNOHANG)) {
384 $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }),
385 (values %{ $PID_CB{0} || {} });
386 }
387
388 undef $PID_IDLE;
389}
390
391sub _sigchld {
392 # make sure we deliver these changes "synchronous" with the event loop.
393 $CHLD_DELAY_W ||= AnyEvent->timer (after => 0, cb => sub {
394 undef $CHLD_DELAY_W;
395 &_child_wait;
396 });
397}
398
399sub child {
400 my (undef, %arg) = @_;
401
402 defined (my $pid = $arg{pid} + 0)
403 or Carp::croak "required option 'pid' is missing";
404
405 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
406
407 unless ($WNOHANG) {
408 $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1;
409 }
410
411 unless ($CHLD_W) {
412 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld);
413 # child could be a zombie already, so make at least one round
414 &_sigchld;
415 }
416
417 bless [$pid, $arg{cb}], "AnyEvent::Base::Child"
418}
419
420sub AnyEvent::Base::Child::DESTROY {
421 my ($pid, $cb) = @{$_[0]};
422
423 delete $PID_CB{$pid}{$cb};
424 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} };
425
426 undef $CHLD_W unless keys %PID_CB;
339} 427}
340 428
341=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 429=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
342 430
343If you need to support another event library which isn't directly 431If you need to support another event library which isn't directly
368I<rxvt-unicode> distribution. 456I<rxvt-unicode> distribution.
369 457
370I<rxvt-unicode> also cheats a bit by not providing blocking access to 458I<rxvt-unicode> also cheats a bit by not providing blocking access to
371condition variables: code blocking while waiting for a condition will 459condition variables: code blocking while waiting for a condition will
372C<die>. This still works with most modules/usages, and blocking calls must 460C<die>. This still works with most modules/usages, and blocking calls must
373not be in an interactive appliation, so it makes sense. 461not be in an interactive application, so it makes sense.
374 462
375=head1 ENVIRONMENT VARIABLES 463=head1 ENVIRONMENT VARIABLES
376 464
377The following environment variables are used by this module: 465The following environment variables are used by this module:
378 466

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines