… | |
… | |
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>. |
|
|
182 | |
|
|
183 | Example: wait for pid 1333 |
|
|
184 | |
|
|
185 | my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); |
173 | |
186 | |
174 | =head1 GLOBALS |
187 | =head1 GLOBALS |
175 | |
188 | |
176 | =over 4 |
189 | =over 4 |
177 | |
190 | |
… | |
… | |
183 | C<AnyEvent::Impl:xxx> modules, but can be any other class in the case |
196 | 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>). |
197 | AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). |
185 | |
198 | |
186 | The known classes so far are: |
199 | The known classes so far are: |
187 | |
200 | |
|
|
201 | EV::AnyEvent based on EV (an interface to libev, best choice) |
188 | AnyEvent::Impl::Coro based on Coro::Event, best choise. |
202 | AnyEvent::Impl::Coro based on Coro::Event, second best choice. |
189 | AnyEvent::Impl::Event based on Event, also best choice :) |
203 | AnyEvent::Impl::Event based on Event, also second best choice :) |
190 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
204 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
191 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
205 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
192 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
206 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
193 | |
207 | |
194 | =item AnyEvent::detect |
208 | =item AnyEvent::detect |
… | |
… | |
233 | |
247 | |
234 | package AnyEvent; |
248 | package AnyEvent; |
235 | |
249 | |
236 | no warnings; |
250 | no warnings; |
237 | use strict; |
251 | use strict; |
|
|
252 | |
238 | use Carp; |
253 | use Carp; |
239 | |
254 | |
240 | our $VERSION = '2.5'; |
255 | our $VERSION = '2.55'; |
241 | our $MODEL; |
256 | our $MODEL; |
242 | |
257 | |
243 | our $AUTOLOAD; |
258 | our $AUTOLOAD; |
244 | our @ISA; |
259 | our @ISA; |
245 | |
260 | |
… | |
… | |
247 | |
262 | |
248 | our @REGISTRY; |
263 | our @REGISTRY; |
249 | |
264 | |
250 | my @models = ( |
265 | my @models = ( |
251 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
266 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
|
|
267 | [EV:: => EV::AnyEvent::], |
252 | [Event:: => AnyEvent::Impl::Event::], |
268 | [Event:: => AnyEvent::Impl::Event::], |
253 | [Glib:: => AnyEvent::Impl::Glib::], |
269 | [Glib:: => AnyEvent::Impl::Glib::], |
254 | [Tk:: => AnyEvent::Impl::Tk::], |
270 | [Tk:: => AnyEvent::Impl::Tk::], |
255 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
271 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
256 | ); |
272 | ); |
… | |
… | |
276 | unless ($MODEL) { |
292 | unless ($MODEL) { |
277 | # try to load a model |
293 | # try to load a model |
278 | |
294 | |
279 | for (@REGISTRY, @models) { |
295 | for (@REGISTRY, @models) { |
280 | my ($package, $model) = @$_; |
296 | my ($package, $model) = @$_; |
|
|
297 | if (eval "require $package" |
|
|
298 | and ${"$package\::VERSION"} > 0 |
281 | if (eval "require $model") { |
299 | and eval "require $model") { |
282 | $MODEL = $model; |
300 | $MODEL = $model; |
283 | 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; |
284 | last; |
302 | last; |
285 | } |
303 | } |
286 | } |
304 | } |
… | |
… | |
308 | $class->$func (@_); |
326 | $class->$func (@_); |
309 | } |
327 | } |
310 | |
328 | |
311 | package AnyEvent::Base; |
329 | package AnyEvent::Base; |
312 | |
330 | |
|
|
331 | # default implementation for ->condvar, ->wait, ->broadcast |
|
|
332 | |
|
|
333 | sub condvar { |
|
|
334 | bless \my $flag, "AnyEvent::Base::CondVar" |
|
|
335 | } |
|
|
336 | |
|
|
337 | sub AnyEvent::Base::CondVar::broadcast { |
|
|
338 | ${$_[0]}++; |
|
|
339 | } |
|
|
340 | |
|
|
341 | sub AnyEvent::Base::CondVar::wait { |
|
|
342 | AnyEvent->one_event while !${$_[0]}; |
|
|
343 | } |
|
|
344 | |
313 | # default implementation for signal |
345 | # default implementation for ->signal |
314 | |
346 | |
315 | our %SIG_CB; |
347 | our %SIG_CB; |
316 | |
348 | |
317 | sub signal { |
349 | sub signal { |
318 | my (undef, %arg) = @_; |
350 | my (undef, %arg) = @_; |
319 | |
351 | |
320 | my $signal = uc $arg{signal} |
352 | my $signal = uc $arg{signal} |
321 | or Carp::croak "required option 'signal' is missing"; |
353 | or Carp::croak "required option 'signal' is missing"; |
322 | |
354 | |
323 | my $w = bless [$signal, $arg{cb}], "AnyEvent::Base::Signal"; |
|
|
324 | |
|
|
325 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
355 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
326 | $SIG{$signal} ||= sub { |
356 | $SIG{$signal} ||= sub { |
327 | $_->() for values %{ $SIG_CB{$signal} }; |
357 | $_->() for values %{ $SIG_CB{$signal} || {} }; |
328 | }; |
358 | }; |
329 | |
359 | |
330 | $w |
360 | bless [$signal, $arg{cb}], "AnyEvent::Base::Signal" |
331 | } |
361 | } |
332 | |
362 | |
333 | sub AnyEvent::Base::Signal::DESTROY { |
363 | sub AnyEvent::Base::Signal::DESTROY { |
334 | my ($signal, $cb) = @{$_[0]}; |
364 | my ($signal, $cb) = @{$_[0]}; |
335 | |
365 | |
336 | delete $SIG_CB{$signal}{$cb}; |
366 | delete $SIG_CB{$signal}{$cb}; |
337 | |
367 | |
338 | $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; |
368 | $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; |
|
|
369 | } |
|
|
370 | |
|
|
371 | # default implementation for ->child |
|
|
372 | |
|
|
373 | our %PID_CB; |
|
|
374 | our $CHLD_W; |
|
|
375 | our $PID_IDLE; |
|
|
376 | our $WNOHANG; |
|
|
377 | |
|
|
378 | sub _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 | |
|
|
387 | sub 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 | |
|
|
408 | sub 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; |
339 | } |
415 | } |
340 | |
416 | |
341 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
417 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
342 | |
418 | |
343 | If you need to support another event library which isn't directly |
419 | If you need to support another event library which isn't directly |
… | |
… | |
368 | I<rxvt-unicode> distribution. |
444 | I<rxvt-unicode> distribution. |
369 | |
445 | |
370 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
446 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
371 | condition variables: code blocking while waiting for a condition will |
447 | condition variables: code blocking while waiting for a condition will |
372 | C<die>. This still works with most modules/usages, and blocking calls must |
448 | C<die>. This still works with most modules/usages, and blocking calls must |
373 | not be in an interactive appliation, so it makes sense. |
449 | not be in an interactive application, so it makes sense. |
374 | |
450 | |
375 | =head1 ENVIRONMENT VARIABLES |
451 | =head1 ENVIRONMENT VARIABLES |
376 | |
452 | |
377 | The following environment variables are used by this module: |
453 | The following environment variables are used by this module: |
378 | |
454 | |