… | |
… | |
160 | =back |
160 | =back |
161 | |
161 | |
162 | =head2 SIGNAL WATCHERS |
162 | =head2 SIGNAL WATCHERS |
163 | |
163 | |
164 | You can listen for signals using a signal watcher, C<signal> is the signal |
164 | You can listen for signals using a signal watcher, C<signal> is the signal |
165 | I<name> without any C<SIG> prefix. |
165 | I<name> without any C<SIG> prefix. Multiple signals events can be clumped |
|
|
166 | together into one callback invocation, and callback invocation might or |
|
|
167 | might not be asynchronous. |
166 | |
168 | |
167 | These watchers might use C<%SIG>, so programs overwriting those signals |
169 | These watchers might use C<%SIG>, so programs overwriting those signals |
168 | directly will likely not work correctly. |
170 | directly will likely not work correctly. |
169 | |
171 | |
170 | Example: exit on SIGINT |
172 | Example: 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 | |
|
|
178 | You can also listen for the status of a child process specified by the |
|
|
179 | C<pid> argument (or any child if the pid argument is 0). The watcher will |
|
|
180 | trigger as often as status change for the child are received. This works |
|
|
181 | by installing a signal handler for C<SIGCHLD>. The callback will be called with |
|
|
182 | the pid and exit status (as returned by waitpid). |
|
|
183 | |
|
|
184 | Example: 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 | |
… | |
… | |
183 | C<AnyEvent::Impl:xxx> modules, but can be any other class in the case |
197 | C<AnyEvent::Impl:xxx> modules, but can be any other class in the case |
184 | AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). |
198 | AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). |
185 | |
199 | |
186 | The known classes so far are: |
200 | The 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 | |
234 | package AnyEvent; |
250 | package AnyEvent; |
235 | |
251 | |
236 | no warnings; |
252 | no warnings; |
237 | use strict; |
253 | use strict; |
|
|
254 | |
238 | use Carp; |
255 | use Carp; |
239 | |
256 | |
240 | our $VERSION = '2.5'; |
257 | our $VERSION = '2.6'; |
241 | our $MODEL; |
258 | our $MODEL; |
242 | |
259 | |
243 | our $AUTOLOAD; |
260 | our $AUTOLOAD; |
244 | our @ISA; |
261 | our @ISA; |
245 | |
262 | |
246 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
263 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
247 | |
264 | |
248 | our @REGISTRY; |
265 | our @REGISTRY; |
249 | |
266 | |
250 | my @models = ( |
267 | my @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 | } |
… | |
… | |
308 | $class->$func (@_); |
329 | $class->$func (@_); |
309 | } |
330 | } |
310 | |
331 | |
311 | package AnyEvent::Base; |
332 | package AnyEvent::Base; |
312 | |
333 | |
|
|
334 | # default implementation for ->condvar, ->wait, ->broadcast |
|
|
335 | |
|
|
336 | sub condvar { |
|
|
337 | bless \my $flag, "AnyEvent::Base::CondVar" |
|
|
338 | } |
|
|
339 | |
|
|
340 | sub AnyEvent::Base::CondVar::broadcast { |
|
|
341 | ${$_[0]}++; |
|
|
342 | } |
|
|
343 | |
|
|
344 | sub AnyEvent::Base::CondVar::wait { |
|
|
345 | AnyEvent->one_event while !${$_[0]}; |
|
|
346 | } |
|
|
347 | |
313 | # default implementation for signal |
348 | # default implementation for ->signal |
314 | |
349 | |
315 | our %SIG_CB; |
350 | our %SIG_CB; |
316 | |
351 | |
317 | sub signal { |
352 | sub 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 | |
333 | sub AnyEvent::Base::Signal::DESTROY { |
366 | sub 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 | |
|
|
376 | our %PID_CB; |
|
|
377 | our $CHLD_W; |
|
|
378 | our $PID_IDLE; |
|
|
379 | our $WNOHANG; |
|
|
380 | |
|
|
381 | sub _child_wait { |
|
|
382 | while (0 <= (my $pid = waitpid -1, $WNOHANG)) { |
|
|
383 | $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
|
|
384 | (values %{ $PID_CB{0} || {} }); |
|
|
385 | } |
|
|
386 | |
|
|
387 | undef $PID_IDLE; |
|
|
388 | } |
|
|
389 | |
|
|
390 | sub child { |
|
|
391 | my (undef, %arg) = @_; |
|
|
392 | |
|
|
393 | defined (my $pid = $arg{pid} + 0) |
|
|
394 | or Carp::croak "required option 'pid' is missing"; |
|
|
395 | |
|
|
396 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
|
|
397 | |
|
|
398 | unless ($WNOHANG) { |
|
|
399 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
|
|
400 | } |
|
|
401 | |
|
|
402 | unless ($CHLD_W) { |
|
|
403 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait); |
|
|
404 | # child could be a zombie already |
|
|
405 | $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait); |
|
|
406 | } |
|
|
407 | |
|
|
408 | bless [$pid, $arg{cb}], "AnyEvent::Base::Child" |
|
|
409 | } |
|
|
410 | |
|
|
411 | sub AnyEvent::Base::Child::DESTROY { |
|
|
412 | my ($pid, $cb) = @{$_[0]}; |
|
|
413 | |
|
|
414 | delete $PID_CB{$pid}{$cb}; |
|
|
415 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
|
|
416 | |
|
|
417 | undef $CHLD_W unless keys %PID_CB; |
339 | } |
418 | } |
340 | |
419 | |
341 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
420 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
342 | |
421 | |
343 | If you need to support another event library which isn't directly |
422 | If you need to support another event library which isn't directly |
… | |
… | |
368 | I<rxvt-unicode> distribution. |
447 | I<rxvt-unicode> distribution. |
369 | |
448 | |
370 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
449 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
371 | condition variables: code blocking while waiting for a condition will |
450 | condition variables: code blocking while waiting for a condition will |
372 | C<die>. This still works with most modules/usages, and blocking calls must |
451 | C<die>. This still works with most modules/usages, and blocking calls must |
373 | not be in an interactive appliation, so it makes sense. |
452 | not be in an interactive application, so it makes sense. |
374 | |
453 | |
375 | =head1 ENVIRONMENT VARIABLES |
454 | =head1 ENVIRONMENT VARIABLES |
376 | |
455 | |
377 | The following environment variables are used by this module: |
456 | The following environment variables are used by this module: |
378 | |
457 | |