line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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-2011 -- leonerd@leonerd.org.uk |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package IO::Async::Signal; |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
92193
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
62
|
|
9
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
62
|
|
10
|
2
|
|
|
2
|
|
11
|
use base qw( IO::Async::Notifier ); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
670
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.79'; |
13
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
14
|
use Carp; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
856
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
C - event callback on receipt of a POSIX signal |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
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 invokes its callback when a particular |
42
|
|
|
|
|
|
|
POSIX signal is received. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Multiple objects can be added to a C 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 or C: |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 name => STRING |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
The name of the signal to watch. This should be a bare name like C. Can |
67
|
|
|
|
|
|
|
only be given at construction time. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 on_receipt => CODE |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
CODE reference for the C event. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Once constructed, the C will need to be added to the C before it |
74
|
|
|
|
|
|
|
will work. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _init |
79
|
|
|
|
|
|
|
{ |
80
|
4
|
|
|
4
|
|
9
|
my $self = shift; |
81
|
4
|
|
|
|
|
10
|
my ( $params ) = @_; |
82
|
|
|
|
|
|
|
|
83
|
4
|
50
|
|
|
|
20
|
my $name = delete $params->{name} or croak "Expected 'name'"; |
84
|
|
|
|
|
|
|
|
85
|
4
|
|
|
|
|
12
|
$name =~ s/^SIG//; # Trim a leading "SIG" |
86
|
|
|
|
|
|
|
|
87
|
4
|
|
|
|
|
20
|
$self->{name} = $name; |
88
|
|
|
|
|
|
|
|
89
|
4
|
|
|
|
|
21
|
$self->SUPER::_init( $params ); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub configure |
93
|
|
|
|
|
|
|
{ |
94
|
5
|
|
|
5
|
1
|
985
|
my $self = shift; |
95
|
5
|
|
|
|
|
15
|
my %params = @_; |
96
|
|
|
|
|
|
|
|
97
|
5
|
100
|
|
|
|
15
|
if( exists $params{on_receipt} ) { |
98
|
4
|
|
|
|
|
12
|
$self->{on_receipt} = delete $params{on_receipt}; |
99
|
|
|
|
|
|
|
|
100
|
4
|
|
|
|
|
10
|
undef $self->{cb}; # Will be lazily constructed when needed |
101
|
|
|
|
|
|
|
|
102
|
4
|
100
|
|
|
|
18
|
if( my $loop = $self->loop ) { |
103
|
1
|
|
|
|
|
5
|
$self->_remove_from_loop( $loop ); |
104
|
1
|
|
|
|
|
4
|
$self->_add_to_loop( $loop ); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
5
|
50
|
|
|
|
26
|
unless( $self->can_event( 'on_receipt' ) ) { |
109
|
0
|
|
|
|
|
0
|
croak 'Expected either a on_receipt callback or an ->on_receipt method'; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
5
|
|
|
|
|
21
|
$self->SUPER::configure( %params ); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub _add_to_loop |
116
|
|
|
|
|
|
|
{ |
117
|
5
|
|
|
5
|
|
11
|
my $self = shift; |
118
|
5
|
|
|
|
|
12
|
my ( $loop ) = @_; |
119
|
|
|
|
|
|
|
|
120
|
5
|
|
33
|
|
|
31
|
$self->{cb} ||= $self->make_event_cb( 'on_receipt' ); |
121
|
|
|
|
|
|
|
|
122
|
5
|
|
|
|
|
23
|
$self->{id} = $loop->attach_signal( $self->{name}, $self->{cb} ); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub _remove_from_loop |
126
|
|
|
|
|
|
|
{ |
127
|
3
|
|
|
3
|
|
4
|
my $self = shift; |
128
|
3
|
|
|
|
|
7
|
my ( $loop ) = @_; |
129
|
|
|
|
|
|
|
|
130
|
3
|
|
|
|
|
14
|
$loop->detach_signal( $self->{name}, $self->{id} ); |
131
|
3
|
|
|
|
|
13
|
undef $self->{id}; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub notifier_name |
135
|
|
|
|
|
|
|
{ |
136
|
1
|
|
|
1
|
1
|
1232
|
my $self = shift; |
137
|
1
|
50
|
|
|
|
8
|
if( length( my $name = $self->SUPER::notifier_name ) ) { |
138
|
0
|
|
|
|
|
0
|
return $name; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
1
|
|
|
|
|
7
|
return $self->{name}; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 AUTHOR |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Paul Evans |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=cut |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
0x55AA; |