| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # (C) Paul Evans, 2009-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package IO::Async::Signal 0.805; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | use v5.14; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use base qw( IO::Async::Notifier ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | C<IO::Async::Signal> - event callback on receipt of a POSIX signal | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =for highlighter language=perl | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | use IO::Async::Signal; | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | use IO::Async::Loop; | ||||||||||||||||||||
| 25 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | my $signal = IO::Async::Signal->new( | ||||||||||||||||||||
| 28 | name => "HUP", | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | on_receipt => sub { | ||||||||||||||||||||
| 31 | print "I caught SIGHUP\n"; | ||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | $loop->add( $signal ); | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | $loop->run; | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | This subclass of L<IO::Async::Notifier> invokes its callback when a particular | ||||||||||||||||||||
| 42 | POSIX signal is received. | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | Multiple objects can be added to a C<Loop> that all watch for the same signal. | ||||||||||||||||||||
| 45 | The callback functions will all be invoked, in no particular order. | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | =head1 EVENTS | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | The following events are invoked, either using subclass methods or CODE | ||||||||||||||||||||
| 52 | references in parameters: | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =head2 on_receipt | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | Invoked when the signal is received. | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | =head1 PARAMETERS | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | The following named parameters may be passed to C<new> or C<configure>: | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | =head2 name => STRING | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | The name of the signal to watch. This should be a bare name like C<TERM>. Can | ||||||||||||||||||||
| 67 | only be given at construction time. | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =head2 on_receipt => CODE | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | CODE reference for the C<on_receipt> event. | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | Once constructed, the C<Signal> will need to be added to the C<Loop> before it | ||||||||||||||||||||
| 74 | will work. | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | sub _init | ||||||||||||||||||||
| 79 | { | ||||||||||||||||||||
| 80 | 4 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_init: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 81 | 4 | my ( $params ) = @_; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 4 | ▶ | my $name = delete $params->{name} or croak "Expected 'name'"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 4 | $name =~ s/^SIG//; # Trim a leading "SIG" | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 4 | $self->{name} = $name; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 4 | $self->SUPER::_init( $params ); | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | sub configure | ||||||||||||||||||||
| 93 | { | ||||||||||||||||||||
| 94 | 5 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
configure: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 95 | 5 | my %params = @_; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 5 | ▶ | if( exists $params{on_receipt} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 4 | $self->{on_receipt} = delete $params{on_receipt}; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 4 | undef $self->{cb}; # Will be lazily constructed when needed | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 4 | ▶ | if( my $loop = $self->loop ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 1 | $self->_remove_from_loop( $loop ); | |||||||||||||||||||
| 104 | 1 | $self->_add_to_loop( $loop ); | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 5 | ▶ | unless( $self->can_event( 'on_receipt' ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | 0 | croak 'Expected either a on_receipt callback or an ->on_receipt method'; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 5 | $self->SUPER::configure( %params ); | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | sub _add_to_loop | ||||||||||||||||||||
| 116 | { | ||||||||||||||||||||
| 117 | 5 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_add_to_loop: called CC=2 SCAR=7.4
| |||||||||||||||||||||
| 118 | 5 | my ( $loop ) = @_; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 5 | ▶ | $self->{cb} ||= $self->make_event_cb( 'on_receipt' ); | ||||||||||||||||||
|
Condition33%1/3
$self->{"cb"} ||= $self->make_event_cb("on_receipt")
MC/DC0%0/2
$self->{"cb"} ||= $self->make_event_cb("on_receipt")
$self->{"cb"}
$self->make_event_cb("on_receipt")
Truth table33%1/3
A ||= B
A= $self->{"cb"}B= $self->make_event_cb("on_receipt")
| |||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 5 | $self->{id} = $loop->attach_signal( $self->{name}, $self->{cb} ); | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | sub _remove_from_loop | ||||||||||||||||||||
| 126 | { | ||||||||||||||||||||
| 127 | 3 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_remove_from_loop: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 128 | 3 | my ( $loop ) = @_; | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 3 | $loop->detach_signal( $self->{name}, $self->{id} ); | |||||||||||||||||||
| 131 | 3 | undef $self->{id}; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | sub notifier_name | ||||||||||||||||||||
| 135 | { | ||||||||||||||||||||
| 136 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
notifier_name: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 137 | 1 | ▶ | if( length( my $name = $self->SUPER::notifier_name ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 0 | return $name; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 1 | return $self->{name}; | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | =head1 AUTHOR | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 2 | 0x55AA; | |||||||||||||||||||