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.14 by root, Mon Oct 30 20:52:24 2006 UTC vs.
Revision 1.22 by root, Sun Dec 31 11:54:43 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
154 # do something such as adding a timer 154 # do something such as adding a timer
155 # or socket watcher the calls $result_ready->broadcast 155 # or socket watcher the calls $result_ready->broadcast
156 # when the "result" is ready. 156 # when the "result" is ready.
157 157
158 $result_ready->wait; 158 $result_ready->wait;
159
160=back
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
186=head1 GLOBALS
187
188=over 4
189
190=item $AnyEvent::MODEL
191
192Contains C<undef> until the first watcher is being created. Then it
193contains the event model that is being used, which is the name of the
194Perl class implementing the model. This class is usually one of the
195C<AnyEvent::Impl:xxx> modules, but can be any other class in the case
196AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>).
197
198The known classes so far are:
199
200 AnyEvent::Impl::Coro based on Coro::Event, best choise.
201 AnyEvent::Impl::Event based on Event, also best choice :)
202 AnyEvent::Impl::Glib based on Glib, second-best choice.
203 AnyEvent::Impl::Tk based on Tk, very bad choice.
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.
159 211
160=back 212=back
161 213
162=head1 WHAT TO DO IN A MODULE 214=head1 WHAT TO DO IN A MODULE
163 215
192=cut 244=cut
193 245
194package AnyEvent; 246package AnyEvent;
195 247
196no warnings; 248no warnings;
197use strict 'vars'; 249use strict;
198use Carp; 250use Carp;
199 251
200our $VERSION = '1.02'; 252our $VERSION = '2.51';
201our $MODEL; 253our $MODEL;
202 254
203our $AUTOLOAD; 255our $AUTOLOAD;
204our @ISA; 256our @ISA;
205 257
206our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; 258our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1;
207 259
208our @REGISTRY; 260our @REGISTRY;
209 261
210my @models = ( 262my @models = (
211 [Coro::Event:: => AnyEvent::Impl::Coro::], 263 [Coro::Event:: => AnyEvent::Impl::Coro::],
212 [Event:: => AnyEvent::Impl::Event::], 264 [Event:: => AnyEvent::Impl::Event::],
213 [Glib:: => AnyEvent::Impl::Glib::], 265 [Glib:: => AnyEvent::Impl::Glib::],
214 [Tk:: => AnyEvent::Impl::Tk::], 266 [Tk:: => AnyEvent::Impl::Tk::],
215 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], 267 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
216); 268);
217 269
218our %method = map +($_ => 1), qw(io timer condvar broadcast wait DESTROY); 270our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY);
219 271
220sub AUTOLOAD { 272sub detect() {
221 $AUTOLOAD =~ s/.*://;
222
223 $method{$AUTOLOAD}
224 or croak "$AUTOLOAD: not a valid method for AnyEvent objects";
225
226 unless ($MODEL) { 273 unless ($MODEL) {
274 no strict 'refs';
275
227 # check for already loaded models 276 # check for already loaded models
228 for (@REGISTRY, @models) { 277 for (@REGISTRY, @models) {
229 my ($package, $model) = @$_; 278 my ($package, $model) = @$_;
230 if (${"$package\::VERSION"} > 0) { 279 if (${"$package\::VERSION"} > 0) {
231 if (eval "require $model") { 280 if (eval "require $model") {
239 unless ($MODEL) { 288 unless ($MODEL) {
240 # try to load a model 289 # try to load a model
241 290
242 for (@REGISTRY, @models) { 291 for (@REGISTRY, @models) {
243 my ($package, $model) = @$_; 292 my ($package, $model) = @$_;
293 if (eval "require $package"
294 and ${"$package\::VERSION"} > 0
244 if (eval "require $model") { 295 and eval "require $model") {
245 $MODEL = $model; 296 $MODEL = $model;
246 warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; 297 warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1;
247 last; 298 last;
248 } 299 }
249 } 300 }
250 301
251 $MODEL 302 $MODEL
252 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; 303 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Event (or Coro+Event), Glib or Tk.";
253 } 304 }
305
306 unshift @ISA, $MODEL;
307 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
254 } 308 }
255 309
256 @ISA = $MODEL; 310 $MODEL
311}
312
313sub AUTOLOAD {
314 (my $func = $AUTOLOAD) =~ s/.*://;
315
316 $method{$func}
317 or croak "$func: not a valid method for AnyEvent objects";
318
319 detect unless $MODEL;
257 320
258 my $class = shift; 321 my $class = shift;
259 $class->$AUTOLOAD (@_); 322 $class->$func (@_);
323}
324
325package AnyEvent::Base;
326
327# default implementation for ->condvar, ->wait, ->broadcast
328
329sub condvar {
330 bless \my $flag, "AnyEvent::Base::CondVar"
331}
332
333sub AnyEvent::Base::CondVar::broadcast {
334 ${$_[0]}++;
335}
336
337sub AnyEvent::Base::CondVar::wait {
338 AnyEvent->one_event while !${$_[0]};
339}
340
341# default implementation for ->signal
342
343our %SIG_CB;
344
345sub signal {
346 my (undef, %arg) = @_;
347
348 my $signal = uc $arg{signal}
349 or Carp::croak "required option 'signal' is missing";
350
351 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
352 $SIG{$signal} ||= sub {
353 $_->() for values %{ $SIG_CB{$signal} || {} };
354 };
355
356 bless [$signal, $arg{cb}], "AnyEvent::Base::Signal"
357}
358
359sub AnyEvent::Base::Signal::DESTROY {
360 my ($signal, $cb) = @{$_[0]};
361
362 delete $SIG_CB{$signal}{$cb};
363
364 $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} };
365}
366
367# default implementation for ->child
368
369our %PID_CB;
370our $CHLD_W;
371our $PID_IDLE;
372our $WNOHANG;
373
374sub _child_wait {
375 while (0 < (my $pid = waitpid -1, $WNOHANG)) {
376 $_->() for values %{ (delete $PID_CB{$pid}) || {} };
377 }
378
379 undef $PID_IDLE;
380}
381
382sub child {
383 my (undef, %arg) = @_;
384
385 my $pid = uc $arg{pid}
386 or Carp::croak "required option 'pid' is missing";
387
388 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
389
390 unless ($WNOHANG) {
391 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait);
392 $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1;
393 }
394
395 # child could be a zombie already
396 $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait);
397
398 bless [$pid, $arg{cb}], "AnyEvent::Base::Child"
399}
400
401sub AnyEvent::Base::Child::DESTROY {
402 my ($pid, $cb) = @{$_[0]};
403
404 delete $PID_CB{$pid}{$cb};
405 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} };
406
407 undef $CHLD_W unless keys %PID_CB;
260} 408}
261 409
262=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 410=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
263 411
264If you need to support another event library which isn't directly 412If you need to support another event library which isn't directly
275This tells AnyEvent to (literally) use the C<urxvt::anyevent::> 423This tells AnyEvent to (literally) use the C<urxvt::anyevent::>
276package/class when it finds the C<urxvt> package/module is loaded. When 424package/class when it finds the C<urxvt> package/module is loaded. When
277AnyEvent is loaded and asked to find a suitable event model, it will 425AnyEvent is loaded and asked to find a suitable event model, it will
278first check for the presence of urxvt. 426first check for the presence of urxvt.
279 427
280The class should prove implementations for all watcher types (see 428The class should provide implementations for all watcher types (see
281L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> 429L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib>
282(Source code) and so on for actual examples, use C<perldoc -m 430(Source code) and so on for actual examples, use C<perldoc -m
283AnyEvent::Impl::Glib> to see the sources). 431AnyEvent::Impl::Glib> to see the sources).
284 432
285The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) 433The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines