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.20 by root, Mon Dec 11 01:16:09 2006 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 callbakc 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
176 AnyEvent::Impl::Coro based on Coro::Event, best choise. 200 AnyEvent::Impl::Coro based on Coro::Event, best choise.
177 AnyEvent::Impl::Event based on Event, also best choice :) 201 AnyEvent::Impl::Event based on Event, also best choice :)
178 AnyEvent::Impl::Glib based on Glib, second-best choice. 202 AnyEvent::Impl::Glib based on Glib, second-best choice.
179 AnyEvent::Impl::Tk based on Tk, very bad choice. 203 AnyEvent::Impl::Tk based on Tk, very bad choice.
180 AnyEvent::Impl::Perl pure-perl implementation, inefficient. 204 AnyEvent::Impl::Perl pure-perl implementation, inefficient.
205
206=item AnyEvent::detect
207
208Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model if
209necessary. You should only call this function right before you would have
210created an AnyEvent watcher anyway, that is, very late at runtime.
181 211
182=back 212=back
183 213
184=head1 WHAT TO DO IN A MODULE 214=head1 WHAT TO DO IN A MODULE
185 215
214=cut 244=cut
215 245
216package AnyEvent; 246package AnyEvent;
217 247
218no warnings; 248no warnings;
219use strict 'vars'; 249use strict;
220use Carp; 250use Carp;
221 251
222our $VERSION = '2.0'; 252our $VERSION = '2.5';
223our $MODEL; 253our $MODEL;
224 254
225our $AUTOLOAD; 255our $AUTOLOAD;
226our @ISA; 256our @ISA;
227 257
228our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; 258our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1;
229 259
230our @REGISTRY; 260our @REGISTRY;
231 261
232my @models = ( 262my @models = (
233 [Coro::Event:: => AnyEvent::Impl::Coro::], 263 [Coro::Event:: => AnyEvent::Impl::Coro::],
234 [Event:: => AnyEvent::Impl::Event::], 264 [Event:: => AnyEvent::Impl::Event::],
235 [Glib:: => AnyEvent::Impl::Glib::], 265 [Glib:: => AnyEvent::Impl::Glib::],
236 [Tk:: => AnyEvent::Impl::Tk::], 266 [Tk:: => AnyEvent::Impl::Tk::],
237 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], 267 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
238); 268);
239 269
240our %method = map +($_ => 1), qw(io timer condvar broadcast wait DESTROY); 270our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY);
241 271
242sub AUTOLOAD { 272sub detect() {
243 $AUTOLOAD =~ s/.*://;
244
245 $method{$AUTOLOAD}
246 or croak "$AUTOLOAD: not a valid method for AnyEvent objects";
247
248 unless ($MODEL) { 273 unless ($MODEL) {
274 no strict 'refs';
275
249 # check for already loaded models 276 # check for already loaded models
250 for (@REGISTRY, @models) { 277 for (@REGISTRY, @models) {
251 my ($package, $model) = @$_; 278 my ($package, $model) = @$_;
252 if (${"$package\::VERSION"} > 0) { 279 if (${"$package\::VERSION"} > 0) {
253 if (eval "require $model") { 280 if (eval "require $model") {
269 last; 296 last;
270 } 297 }
271 } 298 }
272 299
273 $MODEL 300 $MODEL
274 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; 301 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 } 302 }
303
304 unshift @ISA, $MODEL;
305 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
276 } 306 }
277 307
278 @ISA = $MODEL; 308 $MODEL
309}
310
311sub AUTOLOAD {
312 (my $func = $AUTOLOAD) =~ s/.*://;
313
314 $method{$func}
315 or croak "$func: not a valid method for AnyEvent objects";
316
317 detect unless $MODEL;
279 318
280 my $class = shift; 319 my $class = shift;
281 $class->$AUTOLOAD (@_); 320 $class->$func (@_);
321}
322
323package AnyEvent::Base;
324
325# default implementation for ->condvar, ->wait, ->broadcast
326
327sub condvar {
328 bless \my $flag, "AnyEvent::Base::CondVar"
329}
330
331sub AnyEvent::Base::CondVar::broadcast {
332 ${$_[0]}++;
333}
334
335sub AnyEvent::Base::CondVar::wait {
336 AnyEvent->one_event while !${$_[0]};
337}
338
339# default implementation for ->signal
340
341our %SIG_CB;
342
343sub signal {
344 my (undef, %arg) = @_;
345
346 my $signal = uc $arg{signal}
347 or Carp::croak "required option 'signal' is missing";
348
349 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
350 $SIG{$signal} ||= sub {
351 $_->() for values %{ $SIG_CB{$signal} || {} };
352 };
353
354 bless [$signal, $arg{cb}], "AnyEvent::Base::Signal"
355}
356
357sub AnyEvent::Base::Signal::DESTROY {
358 my ($signal, $cb) = @{$_[0]};
359
360 delete $SIG_CB{$signal}{$cb};
361
362 $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} };
363}
364
365# default implementation for ->child
366
367our %PID_CB;
368our $CHLD_W;
369our $PID_IDLE;
370our $WNOHANG;
371
372sub _child_wait {
373 while (0 < (my $pid = waitpid -1, $WNOHANG)) {
374 $_->() for values %{ (delete $PID_CB{$pid}) || {} };
375 }
376
377 undef $PID_IDLE;
378}
379
380sub child {
381 my (undef, %arg) = @_;
382
383 my $pid = uc $arg{pid}
384 or Carp::croak "required option 'pid' is missing";
385
386 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
387
388 unless ($WNOHANG) {
389 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait);
390 $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1;
391 }
392
393 # child could be a zombie already
394 $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait);
395
396 bless [$pid, $arg{cb}], "AnyEvent::Base::Child"
397}
398
399sub AnyEvent::Base::Child::DESTROY {
400 my ($pid, $cb) = @{$_[0]};
401
402 delete $PID_CB{$pid}{$cb};
403 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} };
404
405 undef $CHLD_W unless keys %PID_CB;
282} 406}
283 407
284=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 408=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
285 409
286If you need to support another event library which isn't directly 410If you need to support another event library which isn't directly
297This tells AnyEvent to (literally) use the C<urxvt::anyevent::> 421This tells AnyEvent to (literally) use the C<urxvt::anyevent::>
298package/class when it finds the C<urxvt> package/module is loaded. When 422package/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 423AnyEvent is loaded and asked to find a suitable event model, it will
300first check for the presence of urxvt. 424first check for the presence of urxvt.
301 425
302The class should prove implementations for all watcher types (see 426The class should provide implementations for all watcher types (see
303L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> 427L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib>
304(Source code) and so on for actual examples, use C<perldoc -m 428(Source code) and so on for actual examples, use C<perldoc -m
305AnyEvent::Impl::Glib> to see the sources). 429AnyEvent::Impl::Glib> to see the sources).
306 430
307The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) 431The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines