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

filter: Statementstmt 97.5% 197 / 202 Branchbran 69.3% 79 / 114 Conditioncond 87.8% 29 / 33 MC/DCmcdc 61.1% 11 / 18 Subroutinesub 100% 24 / 24 Podpod 87.5% 7 / 8 totaltotal 86.9% 347 / 399 CC 74 SCAR 25.4
CC
74
Cov
87%
loop_once
21.1
watch_io
18.2
unwatch_io
17.6
CRAP 12.7
↑ Summary
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, 2008-2021 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Loop::Epoll 0.23;
7  
8 17 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 17 ▶use constant API_VERSION => '0.76';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Only Linux is known always to be able to report EOF conditions on
14 # filehandles using EPOLLHUP
15 # This is probably redundant as epoll is probably Linux-only also, but it
16 # doesn't harm anything to test specially.
17 17 ▶use constant _CAN_ON_HANGUP => ( $^O eq "linux" );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 17 ▶use base qw( IO::Async::Loop );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 17 ▶use Linux::Epoll 0.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 17 ▶use POSIX qw( EINTR EPERM SIG_BLOCK SIG_UNBLOCK sigprocmask sigaction ceil );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 17 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 17 ▶use constant _CAN_WATCHDOG => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 17 ▶use constant WATCHDOG_ENABLE => IO::Async::Loop->WATCHDOG_ENABLE;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 17 ▶use Struct::Dumb;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 17 struct SignalWatch => [qw( code pending orig )];
34  
35 =head1 NAME
36  
37 C<IO::Async::Loop::Epoll> - use C<IO::Async> with C<epoll> on Linux
38  
39 =head1 SYNOPSIS
40  
41 =for highlighter language=perl
42  
43 use IO::Async::Loop::Epoll;
44  
45 use IO::Async::Stream;
46 use IO::Async::Signal;
47  
48 my $loop = IO::Async::Loop::Epoll->new;
49  
50 $loop->add( IO::Async::Stream->new(
51 read_handle => \*STDIN,
52 on_read => sub {
53 my ( $self, $buffref ) = @_;
54 while( $$buffref =~ s/^(.*)\r?\n// ) {
55 print "You said: $1\n";
56 }
57 },
58 ) );
59  
60 $loop->add( IO::Async::Signal->new(
61 name => 'INT',
62 on_receipt => sub {
63 print "SIGINT, will now quit\n";
64 $loop->stop;
65 },
66 ) );
67  
68 $loop->run;
69  
70 =head1 DESCRIPTION
71  
72 This subclass of L<IO::Async::Loop> uses C<epoll(7)> on Linux to perform
73 read-ready and write-ready tests so that the OZ<>(1) high-performance
74 multiplexing of Linux's C<epoll_pwait(2)> syscall can be used.
75  
76 The C<epoll> Linux subsystem uses a persistent registration system, meaning
77 that better performance can be achieved in programs using a large number of
78 filehandles. Each C<epoll_pwait(2)> syscall only has an overhead proportional
79 to the number of ready filehandles, rather than the total number being
80 watched. For more detail, see the C<epoll(7)> manpage.
81  
82 This class uses the C<epoll_pwait(2)> system call, which atomically switches
83 the process's signal mask, performs a wait exactly as C<epoll_wait(2)> would,
84 then switches it back. This allows a process to block the signals it cares
85 about, but switch in an empty signal mask during the poll, allowing it to
86 handle file IO and signals concurrently.
87  
88 =cut
89  
90 =head1 CONSTRUCTOR
91  
92 =cut
93  
94 =head2 new
95  
96 $loop = IO::Async::Loop::Epoll->new();
97  
98 This function returns a new instance of a C<IO::Async::Loop::Epoll> object.
99  
100 =cut
101  
102 sub new
103 16 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod documented
104 16 my $class = shift;
105 16 my ( %args ) = @_;
106  
107 16 my $epoll = Linux::Epoll->new;
108 16 ▶ defined $epoll or croak "Cannot create epoll handle - $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $epoll or croak("Cannot create epoll handle - $!") 16 0 16
109  
110 16 my $self = $class->SUPER::__new( %args );
111  
112 16 $self->{epoll} = $epoll;
113 16 $self->{sigmask} = POSIX::SigSet->new();
114 16 $self->{maxevents} = 8;
115  
116 16 $self->{fakeevents} = {};
117  
118 16 $self->{signals} = {}; # {$name} => SignalWatch
119 16 $self->{masks} = {};
120  
121 16 $self->{pid} = $$;
122  
123 # epoll gets very upset if applications close() filehandles without telling
124 # it, and then try to add that mask a second time. We can attempt to detect
125 # this by storing the mapping from fileno to refaddr($fh)
126 16 $self->{refaddr_for_fileno} = {};
127  
128 16 return $self;
129 }
130  
131 # Some bits to keep track of in {masks}
132 17 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
133 17 WATCH_READ => 0x01,
134 WATCH_WRITE => 0x02,
135 WATCH_HUP => 0x04,
136 17 };
137  
138 =head1 METHODS
139  
140 As this is a subclass of L<IO::Async::Loop>, all of its methods are inherited.
141 Expect where noted below, all of the class's methods behave identically to
142 C<IO::Async::Loop>.
143  
144 =cut
145  
146 sub DESTROY
147 11 ▶{
Subroutine DESTROY: called CC=2 SCAR=6.9
148 11 my $self = shift;
149  
150 12 foreach my $signal ( keys %{ $self->{signals} } ) {
151 6 $self->unwatch_signal( $signal );
152 }
153 }
154  
155 sub is_running
156 1 ▶{
Subroutine is_running: called CC=1 SCAR=0.0
Pod undocumented
157 1 my $self = shift;
158 1 return $self->{running};
159 }
160  
161 =head2 loop_once
162  
163 $count = $loop->loop_once( $timeout );
164  
165 This method calls C<epoll_pwait(2)>, and processes the results of that call.
166 It returns the total number of C<IO::Async::Notifier> callbacks invoked, or
167 C<undef> if the underlying C<epoll_pwait()> method returned an error. If the
168 C<epoll_pwait()> was interrupted by a signal, then 0 is returned instead.
169  
170 =cut
171  
172 sub loop_once
173 68 ▶{
Subroutine loop_once: called CC=18 SCAR=30.5
Pod documented
174 68 my $self = shift;
175 68 my ( $timeout ) = @_;
176  
177 68 ▶ $self->post_fork if $self->{pid} != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pid"} != $$ 0 68 68
178  
179 68 $self->_adjust_timeout( \$timeout );
180  
181 68 ▶ $timeout = 0 if keys %{ $self->{fakeevents} };
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"fakeevents"};} 1 67 68
182  
183 # Round up to next milisecond to avoid zero timeouts
184 68 ▶ my $msec = defined $timeout ? ceil( $timeout * 1000 ) : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 68 0 68
185  
186 68 $self->pre_wait;
187 68 my $ret = $self->{epoll}->wait( $self->{maxevents}, $msec / 1000, $self->{sigmask} );
188 68 $self->post_wait;
189  
190 68 ▶ return undef if !defined $ret and $! != EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ret and $! != 4 0 68 68
Condition66%2/3
not defined $ret and $! != 4
!ll&&!rl&&r
46220
MC/DC0%0/2
not defined $ret and $! != 4
not defined $ret $! != 4
Truth table66%2/3
not(A) and B
A= not defined $retB= $! != 4
ABresult
0X0
100
111
191  
192 68 ▶ my $count = $ret || 0;
Condition100%2/2
$ret || 0
l!l
1850
MC/DC100%1/1
$ret || 0
$ret
Truth table100%2/2
A
A= $ret
Aresult
11
00
193  
194 68 if( WATCHDOG_ENABLE and !$self->{alarmed} ) {
195 alarm( IO::Async::Loop->WATCHDOG_INTERVAL );
196 $self->{alarmed}++;
197 }
198  
199 68 my $iowatches = $self->{iowatches};
200  
201 68 my $fakeevents = $self->{fakeevents};
202 68 my @fakeevents = map { [ $_ => $fakeevents->{$_} ] } keys %$fakeevents;
203  
204 68 foreach my $ev ( @fakeevents ) {
205 1 my ( $fd, $bits ) = @$ev;
206  
207 1 my $watch = $iowatches->{$fd};
208  
209 1 ▶ if( $bits & WATCH_READ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits & 1) 1 0 1
210 1 ▶ $watch->[1]->() if $watch->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $watch->[1] 1 0 1
211 1 $count++;
212 }
213  
214 1 ▶ if( $bits & WATCH_WRITE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits & 2) 1 0 1
215 1 ▶ $watch->[2]->() if $watch->[2];
Branch: 1 branch
BranchTrueFalseTotal
if $watch->[2] 1 0 1
216 1 $count++;
217 }
218  
219 1 ▶ if( $bits & WATCH_HUP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits & 4) 0 1 1
220 0 ▶ $watch->[3]->() if $watch->[3];
Branch: 1 branch
BranchTrueFalseTotal
if $watch->[3] 0 0 0
221 0 $count++;
222 }
223 }
224  
225 68 my $signals = $self->{signals};
226 68 foreach my $sigslot ( values %$signals ) {
227 23 ▶ if( $sigslot->pending ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigslot->pending) 22 1 23
228 22 $sigslot->pending = 0;
229 22 $sigslot->code->();
230 22 $count++;
231 }
232 }
233  
234 68 $count += $self->_manage_queues;
235  
236 # If we entirely filled the event buffer this time, we may have missed some
237 # Lets get a bigger buffer next time
238 62 ▶ $self->{maxevents} *= 2 if defined $ret and $ret == $self->{maxevents};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ret and $ret == $self->{"maxevents"} 0 68 68
Condition66%2/3
defined $ret and $ret == $self->{"maxevents"}
!ll&&!rl&&r
22460
MC/DC0%0/2
defined $ret and $ret == $self->{"maxevents"}
defined $ret $ret == $self->{"maxevents"}
Truth table66%2/3
A and B
A= defined $retB= $ret == $self->{"maxevents"}
ABresult
0X0
100
111
239  
240 57 alarm( 0 ), undef $self->{alarmed} if WATCHDOG_ENABLE;
241  
242 68 return $count;
243 }
244  
245 sub watch_io
246 19 ▶{
Subroutine watch_io: called CC=18 SCAR=29.0
Pod documented
247 19 my $self = shift;
248 19 my %params = @_;
249  
250 19 ▶ $self->post_fork if $self->{pid} != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pid"} != $$ 3 16 19
251  
252 19 my $epoll = $self->{epoll};
253  
254 19 $self->__watch_io( %params );
255  
256 19 my $handle = $params{handle};
257 19 my $fd = $handle->fileno;
258  
259 19 my $watch = $self->{iowatches}->{$fd};
260  
261 19 my $alarmed = \$self->{alarmed};
262  
263 19 ▶ my $curmask = $self->{masks}->{$fd} || 0;
Condition100%2/2
$self->{'masks'}{$fd} || 0
l!l
316
MC/DC100%1/1
$self->{'masks'}{$fd} || 0
$self->{'masks'}{$fd}
Truth table100%2/2
A
A= $self->{'masks'}{$fd}
Aresult
11
00
264 19 ▶ my $cb = $self->{callbacks}->{$fd} ||= sub {
Subroutine __ANON__: called CC=12 SCAR=24.8
265 19 my ( $events ) = @_;
266  
267 19 if( WATCHDOG_ENABLE and !$$alarmed ) {
268 alarm( IO::Async::Loop->WATCHDOG_INTERVAL );
269 $$alarmed = 1;
270 }
271  
272 19 ▶ if( $events->{in} or $events->{hup} or $events->{err} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($events->{"in"} or $events->{"hup"} or $events->{"err"}) 16 3 19
Condition100%6/6
$events->{"in"} or $events->{"hup"} or $events->{"err"}
l!l&&r!l&&!r
1423
$events->{"in"} or $events->{"hup"}
l!l&&r!l&&!r
1225
MC/DC100%3/3
$events->{"in"} or $events->{"hup"} or $events->{"err"}
$events->{"in"} $events->{"hup"} $events->{"err"}
Truth table100%4/4
A or B or C
A= $events->{"in"}B= $events->{"hup"}C= $events->{"err"}
ABCresult
1XX1
01X1
0011
0000
273 16 ▶ $watch->[1]->() if $watch->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $watch->[1] 14 2 16
274 }
275  
276 19 ▶ if( $events->{out} or $events->{hup} or $events->{err} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($events->{"out"} or $events->{"hup"} or $events->{"err"}) 13 6 19
Condition100%6/6
$events->{"out"} or $events->{"hup"} or $events->{"err"}
l!l&&r!l&&!r
1126
$events->{"out"} or $events->{"hup"}
l!l&&r!l&&!r
838
MC/DC100%3/3
$events->{"out"} or $events->{"hup"} or $events->{"err"}
$events->{"out"} $events->{"hup"} $events->{"err"}
Truth table100%4/4
A or B or C
A= $events->{"out"}B= $events->{"hup"}C= $events->{"err"}
ABCresult
1XX1
01X1
0011
0000
277 13 ▶ $watch->[2]->() if $watch->[2];
Branch: 1 branch
BranchTrueFalseTotal
if $watch->[2] 6 7 13
278 }
279  
280 19 ▶ if( $events->{hup} or $events->{err} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($events->{"hup"} or $events->{"err"}) 6 13 19
Condition100%3/3
$events->{"hup"} or $events->{"err"}
l!l&&r!l&&!r
4213
MC/DC100%2/2
$events->{"hup"} or $events->{"err"}
$events->{"hup"} $events->{"err"}
Truth table100%3/3
A or B
A= $events->{"hup"}B= $events->{"err"}
ABresult
1X1
011
000
281 6 ▶ $watch->[3]->() if $watch->[3];
Branch: 1 branch
BranchTrueFalseTotal
if $watch->[3] 2 4 6
282 }
283 19 ▶ };
Condition66%2/3
$self->{"callbacks"}{$fd} ||= sub { my($events) = @_; '???'; if ($events->{"in"} or $events->{"hup"} or $events->{"err"}) { $watch->[1]->() if $watch->[1]; } ; if ($events->{"out"} or $events->{"hup"} or $events->{"err"}) { $watch->[2]->() if $watch->[2]; } ; if ($events->{"hup"} or $events->{"err"}) { $watch->[3]->() if $watch->[3]; } ; }
l!l&&r!l&&!r
3160
MC/DC0%0/2
$self->{"callbacks"}{$fd} ||= sub { my($events) = @_; '???'; if ($events->{"in"} or $events->{"hup"} or $events->{"err"}) { $watch->[1]->() if $watch->[1]; } ; if ($events->{"out"} or $events->{"hup"} or $events->{"err"}) { $watch->[2]->() if $watch->[2]; } ; if ($events->{"hup"} or $events->{"err"}) { $watch->[3]->() if $watch->[3]; } ; }
$self->{"callbacks"}{$fd} sub { my($events) = @_; '???'; if ($events->{"in"} or $events->{"hup"} or $events->{"err"}) { $watch->[1]->() if $watch->[1]; } ; if ($events->{"out"} or $events->{"hup"} or $events->{"err"}) { $watch->[2]->() if $watch->[2]; } ; if ($events->{"hup"} or $events->{"err"}) { $watch->[3]->() if $watch->[3]; } ; }
Truth table66%2/3
A ||= B
A= $self->{"callbacks"}{$fd}B= sub { my($events) = @_; '???'; if ($events->{"in"} or $events->{"hup"} or $events->{"err"}) { $watch->[1]->() if $watch->[1]; } ; if ($events->{"out"} or $events->{"hup"} or $events->{"err"}) { $watch->[2]->() if $watch->[2]; } ; if ($events->{"hup"} or $events->{"err"}) { $watch->[3]->() if $watch->[3]; } ; }
ABresult
1X1
011
000
284  
285 19 my $mask = $curmask;
286 19 ▶ $params{on_read_ready} and $mask |= WATCH_READ;
Branch: 1 branch
BranchTrueFalseTotal
$params{"on_read_ready"} and $mask |= 1 12 7 19
287 19 ▶ $params{on_write_ready} and $mask |= WATCH_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
$params{"on_write_ready"} and $mask |= 2 7 12 19
288 19 ▶ $params{on_hangup} and $mask |= WATCH_HUP;
Branch: 1 branch
BranchTrueFalseTotal
$params{"on_hangup"} and $mask |= 4 2 17 19
289  
290 19 my @bits;
291 19 ▶ push @bits, 'in' if $mask & WATCH_READ;
Branch: 1 branch
BranchTrueFalseTotal
if $mask & 1 14 5 19
292 19 ▶ push @bits, 'out' if $mask & WATCH_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if $mask & 2 8 11 19
293 19 ▶ push @bits, 'hup' if $mask & WATCH_HUP;
Branch: 1 branch
BranchTrueFalseTotal
if $mask & 4 2 17 19
294  
295 19 my $fakeevents = $self->{fakeevents};
296  
297 19 ▶ if( !$curmask ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $curmask) { } 16 3 19
elsif ($mask != $curmask) { } 3 0 3
298 16 ▶ defined $self->{refaddr_for_fileno}->{$fd} and
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"refaddr_for_fileno"}{$fd} and croak("Epoll has already seen this filehandle; cannot add it a second time") 0 16 16
299 croak "Epoll has already seen this filehandle; cannot add it a second time";
300 16 $self->{refaddr_for_fileno}->{$fd} = refaddr $handle;
301  
302 16 ▶ if( defined $epoll->add( $handle, \@bits, $cb ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $epoll->add($handle, \@bits, $cb)) { } 15 1 16
elsif ($! == 1) { } 1 0 1
303 # All OK
304 }
305 elsif( $! == EPERM ) {
306 # The filehandle isn't epoll'able. This means kernel thinks it should
307 # always be ready.
308 1 $fakeevents->{$fd} = $mask;
309 }
310 else {
311 0 croak "Cannot EPOLL_CTL_ADD($fd,$mask) - $!";
312 }
313  
314 16 $self->{masks}->{$fd} = $mask;
315 }
316 elsif( $mask != $curmask ) {
317 3 ▶ $self->{refaddr_for_fileno}->{$fd} == refaddr $handle or
Branch: 1 branch
BranchTrueFalseTotal
$self->{"refaddr_for_fileno"}{$fd} == builtin::refaddr($handle) or croak("Epoll cannot cope with fd $fd changing handle under it") 3 0 3
318 croak "Epoll cannot cope with fd $fd changing handle under it";
319  
320 3 ▶ if( exists $fakeevents->{$fd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fakeevents->{$fd}) { } 0 3 3
321 0 $fakeevents->{$fd} = $mask;
322 }
323 else {
324 3 ▶ defined $epoll->modify( $handle, \@bits, $cb )
Branch: 1 branch
BranchTrueFalseTotal
defined $epoll->modify($handle, \@bits, $cb) or croak("Cannot EPOLL_CTL_MOD($fd,$mask) - $!") 3 0 3
325 or croak "Cannot EPOLL_CTL_MOD($fd,$mask) - $!";
326 }
327  
328 3 $self->{masks}->{$fd} = $mask;
329 }
330 }
331  
332 sub unwatch_io
333 13 ▶{
Subroutine unwatch_io: called CC=16 SCAR=28.7
Pod documented
334 13 my $self = shift;
335 13 my %params = @_;
336  
337 13 ▶ $self->post_fork if $self->{pid} != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pid"} != $$ 0 13 13
338  
339 13 $self->__unwatch_io( %params );
340  
341 13 my $epoll = $self->{epoll};
342  
343 13 my $handle = $params{handle};
344 13 my $fd = $handle->fileno;
345  
346 13 ▶ my $curmask = $self->{masks}->{$fd} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $curmask = $self->{"masks"}{$fd} or return 13 0 13
347 13 ▶ my $cb = $self->{callbacks}->{$fd} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $self->{"callbacks"}{$fd} or return 13 0 13
348  
349 13 my $mask = $curmask;
350 13 ▶ $params{on_read_ready} and $mask &= ~WATCH_READ;
Branch: 1 branch
BranchTrueFalseTotal
$params{"on_read_ready"} and $mask &= '18446744073709551614' 8 5 13
351 13 ▶ $params{on_write_ready} and $mask &= ~WATCH_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
$params{"on_write_ready"} and $mask &= '18446744073709551613' 7 6 13
352 13 ▶ $params{on_hangup} and $mask &= ~WATCH_HUP;
Branch: 1 branch
BranchTrueFalseTotal
$params{"on_hangup"} and $mask &= '18446744073709551611' 2 11 13
353  
354 13 my $fakeevents = $self->{fakeevents};
355  
356 13 ▶ $self->{refaddr_for_fileno}->{$fd} == refaddr $handle or
Branch: 1 branch
BranchTrueFalseTotal
$self->{"refaddr_for_fileno"}{$fd} == builtin::refaddr($handle) or croak("Epoll cannot cope with fd $fd changing handle under it") 13 0 13
357 croak "Epoll cannot cope with fd $fd changing handle under it";
358  
359 13 ▶ if( $mask ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask) { } 1 12 13
360 1 ▶ if( exists $fakeevents->{$fd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fakeevents->{$fd}) { } 0 1 1
361 0 $fakeevents->{$fd} = $mask;
362 }
363 else {
364 1 my @bits;
365 1 ▶ push @bits, 'in' if $mask & WATCH_READ;
Branch: 1 branch
BranchTrueFalseTotal
if $mask & 1 1 0 1
366 1 ▶ push @bits, 'out' if $mask & WATCH_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if $mask & 2 0 1 1
367 1 ▶ push @bits, 'hup' if $mask & WATCH_HUP;
Branch: 1 branch
BranchTrueFalseTotal
if $mask & 4 0 1 1
368  
369 1 ▶ defined $epoll->modify( $handle, \@bits, $cb )
Branch: 1 branch
BranchTrueFalseTotal
defined $epoll->modify($handle, \@bits, $cb) or croak("Cannot EPOLL_CTL_MOD($fd,$mask) - $!") 1 0 1
370 or croak "Cannot EPOLL_CTL_MOD($fd,$mask) - $!";
371 }
372  
373 1 $self->{masks}->{$fd} = $mask;
374 }
375 else {
376 12 ▶ if( exists $fakeevents->{$fd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fakeevents->{$fd}) { } 1 11 12
377 1 delete $fakeevents->{$fd};
378 }
379 else {
380 11 ▶ defined $epoll->delete( $handle )
Branch: 1 branch
BranchTrueFalseTotal
defined $epoll->delete($handle) or croak("Cannot EPOLL_CTL_DEL($fd) - $!") 11 0 11
381 or croak "Cannot EPOLL_CTL_DEL($fd) - $!";
382 }
383  
384 12 delete $self->{masks}->{$fd};
385 12 delete $self->{callbacks}->{$fd};
386  
387 12 delete $self->{refaddr_for_fileno}->{$fd};
388 }
389 }
390  
391 sub watch_signal
392 9 ▶{
Subroutine watch_signal: called CC=3 SCAR=11.0
Pod documented
393 9 my $self = shift;
394 9 my ( $signal, $code ) = @_;
395  
396 9 ▶ exists $SIG{$signal} or croak "Unrecognised signal name $signal";
Branch: 1 branch
BranchTrueFalseTotal
exists $SIG{$signal} or croak("Unrecognised signal name $signal") 8 1 9
397  
398 # We cannot simply set $SIG{$signal} = $code here, because of perl bug
399 # http://rt.perl.org/rt3/Ticket/Display.html?id=82040
400 # Instead, we'll store a tiny piece of code that just sets a flag, and
401 # check the flags on return from the epoll_pwait call.
402  
403 8 $self->{signals}{$signal} = SignalWatch( $code, 0, $SIG{$signal} );
404 8 my $pending = \$self->{signals}{$signal}->pending;
405  
406 8 my $signum = $self->signame2num( $signal );
407 8 sigprocmask( SIG_BLOCK, POSIX::SigSet->new( $signum ) );
408  
409 # Note this is an unsafe signal handler, and as such it should do as little
410 # as possible.
411 8 ▶ my $sigaction = POSIX::SigAction->new( sub { $$pending = 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
412 8 ▶ sigaction( $signum, $sigaction ) or croak "Unable to sigaction - $!";
Branch: 1 branch
BranchTrueFalseTotal
sigaction($signum, $sigaction) or croak("Unable to sigaction - $!") 8 0 8
413 }
414  
415 sub unwatch_signal
416 8 ▶{
Subroutine unwatch_signal: called CC=4 SCAR=14.0
Pod documented
417 8 my $self = shift;
418 8 my ( $signal ) = @_;
419  
420 8 ▶ exists $SIG{$signal} or croak "Unrecognised signal name $signal";
Branch: 1 branch
BranchTrueFalseTotal
exists $SIG{$signal} or croak("Unrecognised signal name $signal") 8 0 8
421  
422 # When we saved the original value, we might have got an undef. But %SIG
423 # doesn't like having undef assigned back in, so we need to translate
424 8 ▶ $SIG{$signal} = ( $self->{signals}{$signal} && $self->{signals}{$signal}->orig ) || 'DEFAULT';
Condition80%4/5
$self->{'signals'}{$signal} && $self->{'signals'}{$signal}->orig || 'DEFAULT'
l!l
26
$self->{"signals"}{$signal} and $self->{"signals"}{$signal}->orig
!ll&&!rl&&r
062
MC/DC50%1/2
$self->{'signals'}{$signal} && $self->{'signals'}{$signal}->orig || 'DEFAULT'
$self->{"signals"}{$signal} $self->{"signals"}{$signal}->orig
Truth table66%2/3
A and B
A= $self->{"signals"}{$signal}B= $self->{"signals"}{$signal}->orig
ABresult
111
0X0
100
425  
426 8 delete $self->{signals}{$signal};
427
428 8 my $signum = $self->signame2num( $signal );
429  
430 8 sigprocmask( SIG_UNBLOCK, POSIX::SigSet->new( $signum ) );
431 }
432  
433 sub post_fork
434 6 ▶{
Subroutine post_fork: called CC=7 SCAR=19.9
Pod documented
435 6 my $self = shift;
436  
437 5 my $epoll = $self->{epoll} = Linux::Epoll->new;
438 6 $self->{pid} = $$;
439  
440 6 ▶ my $watches = $self->{iowatches} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $watches = $self->{"iowatches"} or return 6 0 6
441 6 my $fakeevents = $self->{fakeevents};
442  
443 6 foreach my $watch ( values %$watches ) {
444 1 my ( $handle ) = @$watch;
445 1 my $fd = $handle->fileno;
446  
447 1 ▶ next if $fakeevents->{$fd};
Branch: 1 branch
BranchTrueFalseTotal
if $fakeevents->{$fd} 0 1 1
448  
449 1 my $mask = $self->{masks}->{$fd};
450 1 my @bits;
451 1 ▶ push @bits, 'in' if $mask & WATCH_READ;
Branch: 1 branch
BranchTrueFalseTotal
if $mask & 1 1 0 1
452 1 ▶ push @bits, 'out' if $mask & WATCH_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if $mask & 2 0 1 1
453 1 ▶ push @bits, 'hup' if $mask & WATCH_HUP;
Branch: 1 branch
BranchTrueFalseTotal
if $mask & 4 0 1 1
454  
455 1 my $cb = $self->{callbacks}->{$fd};
456 1 $epoll->add( $handle, \@bits, $cb );
457 }
458 }
459  
460 =head1 SEE ALSO
461  
462 =over 4
463  
464 =item *
465  
466 L<Linux::Epoll> - O(1) multiplexing for Linux
467  
468 =item *
469  
470 L<IO::Async::Loop::Poll> - use IO::Async with poll(2)
471  
472 =back
473  
474 =head1 AUTHOR
475  
476 Paul Evans <leonerd@leonerd.org.uk>
477  
478 =cut
479  
480 17 0x55AA;
↑ Summary