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.18 by root, Sun Dec 10 21:33:33 2006 UTC vs.
Revision 1.28 by root, Sat Oct 27 15:10:09 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. 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 $?" });
185
162=head1 GLOBALS 186=head1 GLOBALS
163 187
164=over 4 188=over 4
165 189
166=item $AnyEvent::MODEL 190=item $AnyEvent::MODEL
171C<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
172AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). 196AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>).
173 197
174The known classes so far are: 198The known classes so far are:
175 199
176 AnyEvent::Impl::Coro based on Coro::Event, best choise. 200 AnyEvent::Impl::Coro based on Coro::Event, best choice.
201 EV::AnyEvent based on EV (an interface to libevent)
177 AnyEvent::Impl::Event based on Event, also best choice :) 202 AnyEvent::Impl::Event based on Event, also best choice :)
178 AnyEvent::Impl::Glib based on Glib, second-best choice. 203 AnyEvent::Impl::Glib based on Glib, second-best choice.
179 AnyEvent::Impl::Tk based on Tk, very bad choice. 204 AnyEvent::Impl::Tk based on Tk, very bad choice.
180 AnyEvent::Impl::Perl pure-perl implementation, inefficient. 205 AnyEvent::Impl::Perl pure-perl implementation, inefficient.
206
207=item AnyEvent::detect
208
209Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model if
210necessary. You should only call this function right before you would have
211created an AnyEvent watcher anyway, that is, very late at runtime.
181 212
182=back 213=back
183 214
184=head1 WHAT TO DO IN A MODULE 215=head1 WHAT TO DO IN A MODULE
185 216
214=cut 245=cut
215 246
216package AnyEvent; 247package AnyEvent;
217 248
218no warnings; 249no warnings;
219use strict 'vars'; 250use strict;
251
220use Carp; 252use Carp;
221 253
222our $VERSION = '2.1'; 254our $VERSION = '2.55';
223our $MODEL; 255our $MODEL;
224 256
225our $AUTOLOAD; 257our $AUTOLOAD;
226our @ISA; 258our @ISA;
227 259
229 261
230our @REGISTRY; 262our @REGISTRY;
231 263
232my @models = ( 264my @models = (
233 [Coro::Event:: => AnyEvent::Impl::Coro::], 265 [Coro::Event:: => AnyEvent::Impl::Coro::],
266 [EV:: => EV::AnyEvent::],
234 [Event:: => AnyEvent::Impl::Event::], 267 [Event:: => AnyEvent::Impl::Event::],
235 [Glib:: => AnyEvent::Impl::Glib::], 268 [Glib:: => AnyEvent::Impl::Glib::],
236 [Tk:: => AnyEvent::Impl::Tk::], 269 [Tk:: => AnyEvent::Impl::Tk::],
237 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], 270 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
238); 271);
239 272
240our %method = map +($_ => 1), qw(io timer condvar broadcast wait DESTROY); 273our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY);
241 274
242sub AUTOLOAD { 275sub detect() {
243 (my $func = $AUTOLOAD) =~ s/.*://;
244
245 $method{$func}
246 or croak "$func: not a valid method for AnyEvent objects";
247
248 unless ($MODEL) { 276 unless ($MODEL) {
277 no strict 'refs';
278
249 # check for already loaded models 279 # check for already loaded models
250 for (@REGISTRY, @models) { 280 for (@REGISTRY, @models) {
251 my ($package, $model) = @$_; 281 my ($package, $model) = @$_;
252 if (${"$package\::VERSION"} > 0) { 282 if (${"$package\::VERSION"} > 0) {
253 if (eval "require $model") { 283 if (eval "require $model") {
261 unless ($MODEL) { 291 unless ($MODEL) {
262 # try to load a model 292 # try to load a model
263 293
264 for (@REGISTRY, @models) { 294 for (@REGISTRY, @models) {
265 my ($package, $model) = @$_; 295 my ($package, $model) = @$_;
296 if (eval "require $package"
297 and ${"$package\::VERSION"} > 0
266 if (eval "require $model") { 298 and eval "require $model") {
267 $MODEL = $model; 299 $MODEL = $model;
268 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;
269 last; 301 last;
270 } 302 }
271 } 303 }
272 304
273 $MODEL 305 $MODEL
274 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; 306 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 } 307 }
308
309 unshift @ISA, $MODEL;
310 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
276 } 311 }
277 312
278 @ISA = $MODEL; 313 $MODEL
314}
315
316sub AUTOLOAD {
317 (my $func = $AUTOLOAD) =~ s/.*://;
318
319 $method{$func}
320 or croak "$func: not a valid method for AnyEvent objects";
321
322 detect unless $MODEL;
279 323
280 my $class = shift; 324 my $class = shift;
281 $class->$func (@_); 325 $class->$func (@_);
326}
327
328package AnyEvent::Base;
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
344# default implementation for ->signal
345
346our %SIG_CB;
347
348sub signal {
349 my (undef, %arg) = @_;
350
351 my $signal = uc $arg{signal}
352 or Carp::croak "required option 'signal' is missing";
353
354 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
355 $SIG{$signal} ||= sub {
356 $_->() for values %{ $SIG_CB{$signal} || {} };
357 };
358
359 bless [$signal, $arg{cb}], "AnyEvent::Base::Signal"
360}
361
362sub AnyEvent::Base::Signal::DESTROY {
363 my ($signal, $cb) = @{$_[0]};
364
365 delete $SIG_CB{$signal}{$cb};
366
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 %{ (delete $PID_CB{$pid}) || {} };
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;
282} 413}
283 414
284=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 415=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
285 416
286If you need to support another event library which isn't directly 417If you need to support another event library which isn't directly
297This tells AnyEvent to (literally) use the C<urxvt::anyevent::> 428This tells AnyEvent to (literally) use the C<urxvt::anyevent::>
298package/class when it finds the C<urxvt> package/module is loaded. When 429package/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 430AnyEvent is loaded and asked to find a suitable event model, it will
300first check for the presence of urxvt. 431first check for the presence of urxvt.
301 432
302The class should prove implementations for all watcher types (see 433The class should provide implementations for all watcher types (see
303L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> 434L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib>
304(Source code) and so on for actual examples, use C<perldoc -m 435(Source code) and so on for actual examples, use C<perldoc -m
305AnyEvent::Impl::Glib> to see the sources). 436AnyEvent::Impl::Glib> to see the sources).
306 437
307The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) 438The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt)
311I<rxvt-unicode> distribution. 442I<rxvt-unicode> distribution.
312 443
313I<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
314condition variables: code blocking while waiting for a condition will 445condition variables: code blocking while waiting for a condition will
315C<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
316not be in an interactive appliation, so it makes sense. 447not be in an interactive application, so it makes sense.
317 448
318=head1 ENVIRONMENT VARIABLES 449=head1 ENVIRONMENT VARIABLES
319 450
320The following environment variables are used by this module: 451The following environment variables are used by this module:
321 452

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines