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

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