… | |
… | |
399 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
399 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
400 | |
400 | |
401 | =head3 Signal Races, Delays and Workarounds |
401 | =head3 Signal Races, Delays and Workarounds |
402 | |
402 | |
403 | Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching |
403 | Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching |
404 | callbacks to signals in a generic way, which is a pity, as you cannot do |
404 | callbacks to signals in a generic way, which is a pity, as you cannot |
405 | race-free signal handling in perl. AnyEvent will try to do it's best, but |
405 | do race-free signal handling in perl, requiring C libraries for |
|
|
406 | this. AnyEvent will try to do it's best, which means in some cases, |
406 | in some cases, signals will be delayed. The maximum time a signal might |
407 | signals will be delayed. The maximum time a signal might be delayed is |
407 | be delayed is specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10 |
408 | specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10 seconds). This |
408 | seconds). This variable can be changed only before the first signal |
409 | variable can be changed only before the first signal watcher is created, |
409 | watcher is created, and should be left alone otherwise. Higher values |
410 | and should be left alone otherwise. This variable determines how often |
|
|
411 | AnyEvent polls for signals (in case a wake-up was missed). Higher values |
410 | will cause fewer spurious wake-ups, which is better for power and CPU |
412 | will cause fewer spurious wake-ups, which is better for power and CPU |
|
|
413 | saving. |
|
|
414 | |
411 | saving. All these problems can be avoided by installing the optional |
415 | All these problems can be avoided by installing the optional |
412 | L<Async::Interrupt> module. This will not work with inherently broken |
416 | L<Async::Interrupt> module, which works with most event loops. It will not |
413 | event loops such as L<Event> or L<Event::Lib> (and not with L<POE> |
417 | work with inherently broken event loops such as L<Event> or L<Event::Lib> |
414 | currently, as POE does it's own workaround with one-second latency). With |
418 | (and not with L<POE> currently, as POE does it's own workaround with |
415 | those, you just have to suffer the delays. |
419 | one-second latency). For those, you just have to suffer the delays. |
416 | |
420 | |
417 | =head2 CHILD PROCESS WATCHERS |
421 | =head2 CHILD PROCESS WATCHERS |
418 | |
422 | |
419 | $w = AnyEvent->child (pid => <process id>, cb => <callback>); |
423 | $w = AnyEvent->child (pid => <process id>, cb => <callback>); |
420 | |
424 | |
… | |
… | |
786 | =item $cb = $cv->cb ($cb->($cv)) |
790 | =item $cb = $cv->cb ($cb->($cv)) |
787 | |
791 | |
788 | This is a mutator function that returns the callback set and optionally |
792 | This is a mutator function that returns the callback set and optionally |
789 | replaces it before doing so. |
793 | replaces it before doing so. |
790 | |
794 | |
791 | The callback will be called when the condition becomes "true", i.e. when |
795 | The callback will be called when the condition becomes (or already was) |
792 | C<send> or C<croak> are called, with the only argument being the condition |
796 | "true", i.e. when C<send> or C<croak> are called (or were called), with |
793 | variable itself. Calling C<recv> inside the callback or at any later time |
797 | the only argument being the condition variable itself. Calling C<recv> |
794 | is guaranteed not to block. |
798 | inside the callback or at any later time is guaranteed not to block. |
795 | |
799 | |
796 | =back |
800 | =back |
797 | |
801 | |
798 | =head1 SUPPORTED EVENT LOOPS/BACKENDS |
802 | =head1 SUPPORTED EVENT LOOPS/BACKENDS |
799 | |
803 | |
… | |
… | |
1111 | |
1115 | |
1112 | BEGIN { AnyEvent::common_sense } |
1116 | BEGIN { AnyEvent::common_sense } |
1113 | |
1117 | |
1114 | use Carp (); |
1118 | use Carp (); |
1115 | |
1119 | |
1116 | our $VERSION = 4.881; |
1120 | our $VERSION = 4.901; |
1117 | our $MODEL; |
1121 | our $MODEL; |
1118 | |
1122 | |
1119 | our $AUTOLOAD; |
1123 | our $AUTOLOAD; |
1120 | our @ISA; |
1124 | our @ISA; |
1121 | |
1125 | |
… | |
… | |
1630 | Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; |
1634 | Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; |
1631 | wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] |
1635 | wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] |
1632 | } |
1636 | } |
1633 | |
1637 | |
1634 | sub cb { |
1638 | sub cb { |
1635 | $_[0]{_ae_cb} = $_[1] if @_ > 1; |
1639 | my $cv = shift; |
|
|
1640 | |
|
|
1641 | @_ |
|
|
1642 | and $cv->{_ae_cb} = shift |
|
|
1643 | and $cv->{_ae_sent} |
|
|
1644 | and (delete $cv->{_ae_cb})->($cv); |
|
|
1645 | |
1636 | $_[0]{_ae_cb} |
1646 | $cv->{_ae_cb} |
1637 | } |
1647 | } |
1638 | |
1648 | |
1639 | sub begin { |
1649 | sub begin { |
1640 | ++$_[0]{_ae_counter}; |
1650 | ++$_[0]{_ae_counter}; |
1641 | $_[0]{_ae_end_cb} = $_[1] if @_ > 1; |
1651 | $_[0]{_ae_end_cb} = $_[1] if @_ > 1; |
… | |
… | |
1674 | |
1684 | |
1675 | sub idle($) { |
1685 | sub idle($) { |
1676 | AnyEvent->idle (cb => $_[0]); |
1686 | AnyEvent->idle (cb => $_[0]); |
1677 | } |
1687 | } |
1678 | |
1688 | |
1679 | sub cv() { |
1689 | sub cv(;&) { |
1680 | AnyEvent->condvar |
1690 | AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) |
1681 | } |
1691 | } |
1682 | |
1692 | |
1683 | sub now() { |
1693 | sub now() { |
1684 | AnyEvent->now |
1694 | AnyEvent->now |
1685 | } |
1695 | } |