blib/lib/IO/Async/Loop/Ppoll.pm

filter: Statementstmt 100% 75 / 75 Branchbran 70.0% 7 / 10 Conditioncond 63.6% 7 / 11 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 90.1% 110 / 122 SCAR 24.9
CC
12
Cov
93%
loop_once
6.1
unwatch_signal
3.0
watch_signal
3.0
CRAP 12.1
↑ Summary Build »
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, 2007-2011 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Loop::Ppoll;
7  
8 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 our $VERSION = '0.09';
12 12 ▶use constant API_VERSION => '0.49';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 ▶use IO::Async::Loop::Poll 0.18;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use base qw( IO::Async::Loop::Poll );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 12 ▶use IO::Ppoll qw( POLLIN POLLOUT POLLHUP );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 12 ▶use POSIX qw( EINTR SIG_BLOCK SIG_UNBLOCK sigprocmask sigaction );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 12 ▶use constant _CAN_WATCHDOG => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 12 ▶use constant WATCHDOG_ENABLE => IO::Async::Loop->WATCHDOG_ENABLE;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =head1 NAME
27  
28 C<IO::Async::Loop::Ppoll> - use C<IO::Async> with C<ppoll(2)>
29  
30 =head1 SYNOPSIS
31  
32 use IO::Async::Loop::Ppoll;
33  
34 my $loop = IO::Async::Loop::Ppoll->new();
35  
36 $loop->add( ... );
37  
38 $loop->add( IO::Async::Signal->new(
39 name =< 'HUP',
40 on_receipt => sub { ... },
41 ) );
42  
43 $loop->loop_forever();
44  
45 =head1 DESCRIPTION
46  
47 This subclass of C<IO::Async::Loop::Poll> uses an C<IO::Ppoll> object instead
48 of a C<IO::Poll> to perform read-ready and write-ready tests so that they can
49 be mixed with signal handling.
50  
51 The C<ppoll()> system call atomically switches the process's signal mask,
52 performs a wait exactly as C<poll()> would, then switches it back. This allows
53 a process to block the signals it cares about, but switch in an empty signal
54 mask during the poll, allowing it to handle file IO and signals concurrently.
55  
56 =head1 CONSTRUCTOR
57  
58 =cut
59  
60 =head2 $loop = IO::Async::Loop::Ppoll->new( %args )
61  
62 This function returns a new instance of a C<IO::Async::Loop::Ppoll> object.
63 It takes the following named arguments:
64  
65 =over 8
66  
67 =item C<poll>
68  
69 The C<IO::Ppoll> object to use for notification. Optional; if a value is not
70 given, a new C<IO::Ppoll> object will be constructed.
71  
72 =back
73  
74 =cut
75  
76 sub new
77 {
78 11 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.1
Pod documented
79 11 my ( %args ) = @_;
80  
81 11 my $poll = delete $args{poll};
82  
83 11 ▶ $poll ||= IO::Ppoll->new();
Condition33%1/3
$poll ||= "IO::Ppoll"->new
l!l&&r!l&&!r
0110
MC/DC0%0/2
$poll ||= "IO::Ppoll"->new
$poll "IO::Ppoll"->new
Truth table33%1/3
A ||= B
A= $pollB= "IO::Ppoll"->new
ABresult
1X1
011
000
84  
85 11 my $self = $class->SUPER::new( %args, poll => $poll );
86  
87 11 $self->{signals} = {};
88  
89 11 return $self;
90 }
91  
92 =head1 METHODS
93  
94 As this is a subclass of L<IO::Async::Loop::Poll>, all of its methods are
95 inherited. Expect where noted below, all of the class's methods behave
96 identically to C<IO::Async::Loop::Poll>.
97  
98 =cut
99  
100 sub DESTROY
101 {
102 11 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=6.9
103  
104 11 foreach my $signal ( keys %{ $self->{signals} } ) {
105 6 $self->unwatch_signal( $signal );
106 }
107 }
108  
109 =head2 $count = $loop->loop_once( $timeout )
110  
111 This method calls the C<poll()> method on the stored C<IO::Ppoll> object,
112 passing in the value of C<$timeout>, and processes the results of that call.
113 It returns the total number of C<IO::Async::Notifier> callbacks invoked, or
114 C<undef> if the underlying C<poll()> method returned an error. If the
115 C<poll()> was interrupted by a signal, then 0 is returned instead.
116  
117 =cut
118  
119 sub loop_once
120 {
121 61 ▶ my $self = shift;
Subroutine loop_once: called CC=6 SCAR=18.0
Pod documented
122 61 my ( $timeout ) = @_;
123  
124 61 $self->_adjust_timeout( \$timeout );
125  
126 61 my $poll = $self->{poll};
127  
128 61 my $pollret = $poll->poll( $timeout );
129  
130 61 ▶ return undef if ( !defined $pollret or $pollret == -1 ) and $! != EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if !defined($pollret) || $pollret == -1 and $! != 4 0 61 61
Condition66%4/6
!defined($pollret) || $pollret == -1 and $! != 4
!ll&&!rl&&r
41200
not defined $pollret or $pollret == -1
l!l&&r!l&&!r
02041
MC/DC0%0/3
!defined($pollret) || $pollret == -1 and $! != 4
not defined $pollret $pollret == -1 $! != 4
Truth table40%2/5
not(A) or B and C
A= not defined $pollretB= $pollret == -1C= $! != 4
ABCresult
00X0
1X00
0100
1X11
0111
131  
132 61 my $count = 0;
133  
134 61 alarm( IO::Async::Loop->WATCHDOG_INTERVAL ) if WATCHDOG_ENABLE;
135  
136 61 my $signals = $self->{signals};
137 61 foreach my $sigslot ( values %$signals ) {
138 21 ▶ if( $sigslot->[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigslot->[1]) 20 1 21
139 20 $sigslot->[0]->();
140 20 $sigslot->[1] = 0;
141 20 $count++;
142 }
143 }
144  
145 61 return $count + $self->post_poll();
146 }
147  
148 # override
149 sub watch_signal
150 {
151 9 ▶ my $self = shift;
Subroutine watch_signal: called CC=3 SCAR=11.0
Pod documented
152 9 my ( $signal, $code ) = @_;
153  
154 9 ▶ exists $SIG{$signal} or croak "Unrecognised signal name $signal";
Branch: 1 branch
BranchTrueFalseTotal
exists $SIG{$signal} or croak("Unrecognised signal name $signal") 1 8 9
155  
156 # We cannot simply set $SIG{$signal} = $code here, because of perl bug
157 # http://rt.perl.org/rt3/Ticket/Display.html?id=82040
158 # Instead, we'll store a tiny piece of code that just sets a flag, and
159 # check the flags on return from the epoll_pwait call.
160  
161 8 $self->{signals}{$signal} = [ $code, 0, $SIG{$signal} ];
162 8 my $pending = \$self->{signals}{$signal}[1];
163  
164 8 my $signum = $self->signame2num( $signal );
165 8 sigprocmask( SIG_BLOCK, POSIX::SigSet->new( $signum ) );
166  
167 # Note this is an unsafe signal handler, and as such it should do as little
168 # as possible.
169 8 ▶ my $sigaction = POSIX::SigAction->new( sub { $$pending = 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
170 8 ▶ sigaction( $signum, $sigaction ) or croak "Unable to sigaction - $!";
Branch: 1 branch
BranchTrueFalseTotal
sigaction($signum, $sigaction) or croak("Unable to sigaction - $!") 0 8 8
171 }
172  
173 # override
174 sub unwatch_signal
175 {
176 8 ▶ my $self = shift;
Subroutine unwatch_signal: called CC=3 SCAR=11.0
Pod documented
177 8 my ( $signal ) = @_;
178  
179 8 ▶ exists $SIG{$signal} or croak "Unrecognised signal name $signal";
Branch: 1 branch
BranchTrueFalseTotal
exists $SIG{$signal} or croak("Unrecognised signal name $signal") 0 8 8
180  
181 # When we saved the original value, we might have got an undef. But %SIG
182 # doesn't like having undef assigned back in, so we need to translate
183 8 ▶ $SIG{$signal} = $self->{signals}{$signal}[2] || 'DEFAULT';
Condition100%2/2
$self->{'signals'}{$signal}[2] || 'DEFAULT'
l!l
26
MC/DC100%1/1
$self->{'signals'}{$signal}[2] || 'DEFAULT'
$self->{'signals'}{$signal}[2]
Truth table100%2/2
A
A= $self->{'signals'}{$signal}[2]
Aresult
11
00
184  
185 8 delete $self->{signals}{$signal};
186
187 8 my $signum = $self->signame2num( $signal );
188  
189 8 sigprocmask( SIG_UNBLOCK, POSIX::SigSet->new( $signum ) );
190 }
191  
192 =head1 SEE ALSO
193  
194 =over 4
195  
196 =item *
197  
198 L<IO::Ppoll> - Object interface to Linux's C<ppoll()> call
199  
200 =item *
201  
202 L<IO::Async::Loop::Poll> - a set using an C<IO::Poll> object
203  
204 =back
205  
206 =head1 AUTHOR
207  
208 Paul Evans <leonerd@leonerd.org.uk>
209  
210 =cut
211  
212 12 0x55AA;
↑ Summary Build »