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.16 by root, Sun Nov 5 01:08:16 2006 UTC vs.
Revision 1.31 by root, Fri Nov 2 19:20:36 2007 UTC

89 chomp (my $input = <STDIN>); 89 chomp (my $input = <STDIN>);
90 warn "read: $input\n"; 90 warn "read: $input\n";
91 undef $w; 91 undef $w;
92 }); 92 });
93 93
94=head2 TIMER WATCHERS 94=head2 TIME WATCHERS
95 95
96You can create a timer watcher by calling the C<< AnyEvent->timer >> 96You can create a time watcher by calling the C<< AnyEvent->timer >>
97method with the following mandatory arguments: 97method with the following mandatory arguments:
98 98
99C<after> after how many seconds (fractions are supported) should the timer 99C<after> after how many seconds (fractions are supported) should the timer
100activate. C<cb> the callback to invoke. 100activate. C<cb> the callback to invoke.
101 101
157 157
158 $result_ready->wait; 158 $result_ready->wait;
159 159
160=back 160=back
161 161
162=head2 SIGNAL WATCHERS
163
164You can listen for signals using a signal watcher, C<signal> is the signal
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.
168
169These watchers might use C<%SIG>, so programs overwriting those signals
170directly will likely not work correctly.
171
172Example: exit on SIGINT
173
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>.
182
183Example: wait for pid 1333
184
185 my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" });
186
162=head1 GLOBALS 187=head1 GLOBALS
163 188
164=over 4 189=over 4
165 190
166=item $AnyEvent::MODEL 191=item $AnyEvent::MODEL
171C<AnyEvent::Impl:xxx> modules, but can be any other class in the case 196C<AnyEvent::Impl:xxx> modules, but can be any other class in the case
172AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). 197AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>).
173 198
174The known classes so far are: 199The known classes so far are:
175 200
201 EV::AnyEvent based on EV (an interface to libev, best choice)
176 AnyEvent::Impl::Coro based on Coro::Event, best choise. 202 AnyEvent::Impl::Coro based on Coro::Event, second best choice.
177 AnyEvent::Impl::Event based on Event, also best choice :) 203 AnyEvent::Impl::Event based on Event, also second best choice :)
178 AnyEvent::Impl::Glib based on Glib, second-best choice. 204 AnyEvent::Impl::Glib based on Glib, second-best choice.
179 AnyEvent::Impl::Tk based on Tk, very bad choice. 205 AnyEvent::Impl::Tk based on Tk, very bad choice.
180 AnyEvent::Impl::Perl pure-perl implementation, inefficient. 206 AnyEvent::Impl::Perl pure-perl implementation, inefficient.
207
208=item AnyEvent::detect
209
210Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model if
211necessary. You should only call this function right before you would have
212created an AnyEvent watcher anyway, that is, very late at runtime.
181 213
182=back 214=back
183 215
184=head1 WHAT TO DO IN A MODULE 216=head1 WHAT TO DO IN A MODULE
185 217
214=cut 246=cut
215 247
216package AnyEvent; 248package AnyEvent;
217 249
218no warnings; 250no warnings;
219use strict 'vars'; 251use strict;
252
220use Carp; 253use Carp;
221 254
222our $VERSION = '2.0'; 255our $VERSION = '2.55';
223our $MODEL; 256our $MODEL;
224 257
225our $AUTOLOAD; 258our $AUTOLOAD;
226our @ISA; 259our @ISA;
227 260
228our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; 261our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1;
229 262
230our @REGISTRY; 263our @REGISTRY;
231 264
232my @models = ( 265my @models = (
233 [Coro::Event:: => AnyEvent::Impl::Coro::], 266 [Coro::Event:: => AnyEvent::Impl::Coro::],
267 [EV:: => EV::AnyEvent::],
234 [Event:: => AnyEvent::Impl::Event::], 268 [Event:: => AnyEvent::Impl::Event::],
235 [Glib:: => AnyEvent::Impl::Glib::], 269 [Glib:: => AnyEvent::Impl::Glib::],
236 [Tk:: => AnyEvent::Impl::Tk::], 270 [Tk:: => AnyEvent::Impl::Tk::],
237 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], 271 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
238); 272);
239 273
240our %method = map +($_ => 1), qw(io timer condvar broadcast wait DESTROY); 274our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY);
241 275
242sub AUTOLOAD { 276sub detect() {
243 $AUTOLOAD =~ s/.*://;
244
245 $method{$AUTOLOAD}
246 or croak "$AUTOLOAD: not a valid method for AnyEvent objects";
247
248 unless ($MODEL) { 277 unless ($MODEL) {
278 no strict 'refs';
279
249 # check for already loaded models 280 # check for already loaded models
250 for (@REGISTRY, @models) { 281 for (@REGISTRY, @models) {
251 my ($package, $model) = @$_; 282 my ($package, $model) = @$_;
252 if (${"$package\::VERSION"} > 0) { 283 if (${"$package\::VERSION"} > 0) {
253 if (eval "require $model") { 284 if (eval "require $model") {
261 unless ($MODEL) { 292 unless ($MODEL) {
262 # try to load a model 293 # try to load a model
263 294
264 for (@REGISTRY, @models) { 295 for (@REGISTRY, @models) {
265 my ($package, $model) = @$_; 296 my ($package, $model) = @$_;
297 if (eval "require $package"
298 and ${"$package\::VERSION"} > 0
266 if (eval "require $model") { 299 and eval "require $model") {
267 $MODEL = $model; 300 $MODEL = $model;
268 warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; 301 warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1;
269 last; 302 last;
270 } 303 }
271 } 304 }
272 305
273 $MODEL 306 $MODEL
274 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; 307 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Event (or Coro+Event), Glib or Tk.";
275 } 308 }
309
310 unshift @ISA, $MODEL;
311 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
276 } 312 }
277 313
278 @ISA = $MODEL; 314 $MODEL
315}
316
317sub AUTOLOAD {
318 (my $func = $AUTOLOAD) =~ s/.*://;
319
320 $method{$func}
321 or croak "$func: not a valid method for AnyEvent objects";
322
323 detect unless $MODEL;
279 324
280 my $class = shift; 325 my $class = shift;
281 $class->$AUTOLOAD (@_); 326 $class->$func (@_);
327}
328
329package AnyEvent::Base;
330
331# default implementation for ->condvar, ->wait, ->broadcast
332
333sub condvar {
334 bless \my $flag, "AnyEvent::Base::CondVar"
335}
336
337sub AnyEvent::Base::CondVar::broadcast {
338 ${$_[0]}++;
339}
340
341sub AnyEvent::Base::CondVar::wait {
342 AnyEvent->one_event while !${$_[0]};
343}
344
345# default implementation for ->signal
346
347our %SIG_CB;
348
349sub signal {
350 my (undef, %arg) = @_;
351
352 my $signal = uc $arg{signal}
353 or Carp::croak "required option 'signal' is missing";
354
355 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
356 $SIG{$signal} ||= sub {
357 $_->() for values %{ $SIG_CB{$signal} || {} };
358 };
359
360 bless [$signal, $arg{cb}], "AnyEvent::Base::Signal"
361}
362
363sub AnyEvent::Base::Signal::DESTROY {
364 my ($signal, $cb) = @{$_[0]};
365
366 delete $SIG_CB{$signal}{$cb};
367
368 $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} };
369}
370
371# default implementation for ->child
372
373our %PID_CB;
374our $CHLD_W;
375our $PID_IDLE;
376our $WNOHANG;
377
378sub _child_wait {
379 while (0 < (my $pid = waitpid -1, $WNOHANG)) {
380 $_->() for (values %{ $PID_CB{$pid} || {} }),
381 (values %{ $PID_CB{0} || {} });
382 }
383
384 undef $PID_IDLE;
385}
386
387sub child {
388 my (undef, %arg) = @_;
389
390 defined (my $pid = $arg{pid} + 0)
391 or Carp::croak "required option 'pid' is missing";
392
393 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
394
395 unless ($WNOHANG) {
396 $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1;
397 }
398
399 unless ($CHLD_W) {
400 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait);
401 # child could be a zombie already
402 $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait);
403 }
404
405 bless [$pid, $arg{cb}], "AnyEvent::Base::Child"
406}
407
408sub AnyEvent::Base::Child::DESTROY {
409 my ($pid, $cb) = @{$_[0]};
410
411 delete $PID_CB{$pid}{$cb};
412 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} };
413
414 undef $CHLD_W unless keys %PID_CB;
282} 415}
283 416
284=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 417=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
285 418
286If you need to support another event library which isn't directly 419If you need to support another event library which isn't directly
297This tells AnyEvent to (literally) use the C<urxvt::anyevent::> 430This tells AnyEvent to (literally) use the C<urxvt::anyevent::>
298package/class when it finds the C<urxvt> package/module is loaded. When 431package/class when it finds the C<urxvt> package/module is loaded. When
299AnyEvent is loaded and asked to find a suitable event model, it will 432AnyEvent is loaded and asked to find a suitable event model, it will
300first check for the presence of urxvt. 433first check for the presence of urxvt.
301 434
302The class should prove implementations for all watcher types (see 435The class should provide implementations for all watcher types (see
303L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> 436L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib>
304(Source code) and so on for actual examples, use C<perldoc -m 437(Source code) and so on for actual examples, use C<perldoc -m
305AnyEvent::Impl::Glib> to see the sources). 438AnyEvent::Impl::Glib> to see the sources).
306 439
307The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) 440The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt)
311I<rxvt-unicode> distribution. 444I<rxvt-unicode> distribution.
312 445
313I<rxvt-unicode> also cheats a bit by not providing blocking access to 446I<rxvt-unicode> also cheats a bit by not providing blocking access to
314condition variables: code blocking while waiting for a condition will 447condition variables: code blocking while waiting for a condition will
315C<die>. This still works with most modules/usages, and blocking calls must 448C<die>. This still works with most modules/usages, and blocking calls must
316not be in an interactive appliation, so it makes sense. 449not be in an interactive application, so it makes sense.
317 450
318=head1 ENVIRONMENT VARIABLES 451=head1 ENVIRONMENT VARIABLES
319 452
320The following environment variables are used by this module: 453The following environment variables are used by this module:
321 454

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines