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

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