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

filter: Statementstmt 90.9% 130 / 143 Branchbran 83.3% 55 / 66 Conditioncond 39.2% 11 / 28 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 100% 20 / 20 Podpod 83.3% 5 / 6 totaltotal 81.1% 224 / 276 CC 45 SCAR 29.2
CC
45
Cov
83%
loop_once
48.1
post_poll
10.0
unwatch_io
9.0
CRAP 18.5
« blib/lib/IO/Async/Loop.pm ↑ Summary blib/lib/IO/Async/Resolver.pm »
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-2024 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Loop::Poll 0.805;
7  
8 84 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 84 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 84 ▶use constant API_VERSION => '0.49';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 84 ▶use base qw( IO::Async::Loop );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 84 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 84 ▶use IO::Poll qw( POLLIN POLLOUT POLLPRI POLLHUP POLLERR );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 84 ▶use Errno qw( EINTR );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 84 ▶use Fcntl qw( S_ISREG );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # Only Linux, or FreeBSD 8.0 and above, are known always to be able to report
23 # EOF conditions on filehandles using POLLHUP
24 use constant _CAN_ON_HANGUP =>
25 ( $^O eq "linux" ) ||
26 84 ▶ ( $^O eq "freebsd" and do { no warnings 'numeric'; (POSIX::uname)[2] >= 8.0 } );
Subroutine BEGIN: called CC=3 SCAR=13.6
Subroutine BEGIN: called CC=3 SCAR=13.6
Condition16%1/6
$^O eq 'linux' || $^O eq 'freebsd' && do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} (POSIX::uname())[2] >= 8.0 }
l!l&&r!l&&!r
8400
$^O eq "freebsd" and do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} (POSIX::uname())[2] >= 8 }
!ll&&!rl&&r
000
MC/DC0%0/3
$^O eq 'linux' || $^O eq 'freebsd' && do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} (POSIX::uname())[2] >= 8.0 }
$^O eq 'linux' $^O eq "freebsd" do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} (POSIX::uname())[2] >= 8 }
Truth table25%1/4
A || B and C
A= $^O eq 'linux'B= $^O eq "freebsd"C= do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} (POSIX::uname())[2] >= 8 }
ABCresult
1XX1
0111
00X0
0100
27  
28 # poll() on most platforms claims that ISREG files are always read- and
29 # write-ready, but not on MSWin32. We need to fake this
30 84 ▶use constant FAKE_ISREG_READY => IO::Async::OS->HAVE_FAKE_ISREG_READY;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 # poll() on most platforms indicates POLLOUT when connect() fails, but not on
32 # MSWin32. Have to poll also for POLLPRI in that case
33 84 ▶use constant POLL_CONNECT_POLLPRI => IO::Async::OS->HAVE_POLL_CONNECT_POLLPRI;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 84 ▶use constant _CAN_WATCHDOG => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 84 ▶use constant WATCHDOG_ENABLE => IO::Async::Loop->WATCHDOG_ENABLE;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 =head1 NAME
39  
40 C<IO::Async::Loop::Poll> - use C<IO::Async> with C<poll(2)>
41  
42 =head1 SYNOPSIS
43  
44 =for highlighter language=perl
45  
46 Normally an instance of this class would not be directly constructed by a
47 program. It may however, be useful for runinng L<IO::Async> with an existing
48 program already using an C<IO::Poll> object.
49  
50 use IO::Poll;
51 use IO::Async::Loop::Poll;
52  
53 my $poll = IO::Poll->new;
54 my $loop = IO::Async::Loop::Poll->new( poll => $poll );
55  
56 $loop->add( ... );
57  
58 while(1) {
59 my $timeout = ...
60 my $ret = $poll->poll( $timeout );
61 $loop->post_poll;
62 }
63  
64 =head1 DESCRIPTION
65  
66 This subclass of L<IO::Async::Loop> uses the C<poll(2)> system call to perform
67 read-ready and write-ready tests.
68  
69 By default, this loop will use the underlying C<poll()> system call directly,
70 bypassing the usual L<IO::Poll> object wrapper around it because of a number
71 of bugs and design flaws in that class; namely
72  
73 =over 2
74  
75 =item *
76  
77 L<https://rt.cpan.org/Ticket/Display.html?id=93107> - IO::Poll relies on
78 stable stringification of IO handles
79  
80 =item *
81  
82 L<https://rt.cpan.org/Ticket/Display.html?id=25049> - IO::Poll->poll() with no
83 handles always returns immediately
84  
85 =back
86  
87 However, to integrate with existing code that uses an C<IO::Poll> object, a
88 C<post_poll> can be called immediately after the C<poll> method that
89 C<IO::Poll> object. The appropriate mask bits are maintained on the
90 C<IO::Poll> object when notifiers are added or removed from the loop, or when
91 they change their C<want_*> status. The C<post_poll> method inspects the
92 result bits and invokes the C<on_read_ready> or C<on_write_ready> methods on
93 the notifiers.
94  
95 =cut
96  
97 =head1 CONSTRUCTOR
98  
99 =cut
100  
101 =head2 new
102  
103 $loop = IO::Async::Loop::Poll->new( %args );
104  
105 This function returns a new instance of a C<IO::Async::Loop::Poll> object. It
106 takes the following named arguments:
107  
108 =over 8
109  
110 =item C<poll>
111  
112 The C<IO::Poll> object to use for notification. Optional; if a value is not
113 given, the underlying C<IO::Poll::_poll()> function is invoked directly,
114 outside of the object wrapping.
115  
116 =back
117  
118 =cut
119  
120 sub new
121 {
122 83 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
123 83 my ( %args ) = @_;
124  
125 83 my $poll = delete $args{poll};
126  
127 83 my $self = $class->__new( %args );
128  
129 83 $self->{poll} = $poll;
130 83 $self->{pollmask} = {};
131  
132 83 return $self;
133 }
134  
135 =head1 METHODS
136  
137 =cut
138  
139 =head2 post_poll
140  
141 $count = $loop->post_poll;
142  
143 This method checks the returned event list from a C<IO::Poll::poll> call,
144 and calls any of the notification methods or callbacks that are appropriate.
145 It returns the total number of callbacks that were invoked; that is, the
146 total number of C<on_read_ready> and C<on_write_ready> callbacks for
147 C<watch_io>, and C<watch_time> event callbacks.
148  
149 =cut
150  
151 sub post_poll
152 {
153 1252 ▶ my $self = shift;
Subroutine post_poll: called CC=10 SCAR=23.0
Pod documented
154  
155 1252 my $iowatches = $self->{iowatches};
156 1252 my $poll = $self->{poll};
157  
158 1252 my $count = 0;
159  
160 1252 alarm( IO::Async::Loop->WATCHDOG_INTERVAL ) if WATCHDOG_ENABLE;
161  
162 1252 foreach my $fd ( keys %$iowatches ) {
163 2668 ▶ my $watch = $iowatches->{$fd} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $watch = $iowatches->{$fd} or next 1 2667 2668
164  
165 my $events = $poll ? $poll->events( $watch->[0] )
166 2667 ▶ : $self->{pollevents}{$fd};
Branch: 1 branch
BranchTrueFalseTotal
$poll ? : 2 2665 2667
167 2667 if( FAKE_ISREG_READY and $self->{fake_isreg}{$fd} ) {
168 $events |= $self->{fake_isreg}{$fd} & ( POLLIN|POLLOUT );
169 }
170  
171 # We have to test separately because kernel doesn't report POLLIN when
172 # a pipe gets closed.
173 2667 ▶ if( $events & (POLLIN|POLLHUP|POLLERR) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($events & 25) 1266 1401 2667
174 1266 ▶ $count++, $watch->[1]->() if defined $watch->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $watch->[1] 1262 4 1266
175 }
176  
177 2667 ▶ if( $events & (POLLOUT|POLLPRI|POLLHUP|POLLERR) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($events & 30) 676 1991 2667
178 676 ▶ $count++, $watch->[2]->() if defined $watch->[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $watch->[2] 95 581 676
179 }
180  
181 2667 ▶ if( $events & (POLLHUP|POLLERR) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($events & 24) 583 2084 2667
182 583 ▶ $count++, $watch->[3]->() if defined $watch->[3];
Branch: 1 branch
BranchTrueFalseTotal
if defined $watch->[3] 2 581 583
183 }
184 }
185  
186 # Since we have no way to know if the timeout occurred, we'll have to
187 # attempt to fire any waiting timeout events anyway
188 1252 $count += $self->_manage_queues;
189  
190 1250 alarm( 0 ) if WATCHDOG_ENABLE;
191  
192 1250 return $count;
193 }
194  
195 sub is_running
196 {
197 1 ▶ my $self = shift;
Subroutine is_running: called CC=1 SCAR=0.0
Pod undocumented
198 1 return $self->{running};
199 }
200  
201 =head2 loop_once
202  
203 $count = $loop->loop_once( $timeout );
204  
205 This method calls the C<poll> method on the stored C<IO::Poll> object,
206 passing in the value of C<$timeout>, and then runs the C<post_poll> method
207 on itself. It returns the total number of callbacks invoked by the
208 C<post_poll> method, or C<undef> if the underlying C<poll> method returned
209 an error.
210  
211 =cut
212  
213 sub loop_once
214 {
215 1250 ▶ my $self = shift;
Subroutine loop_once: called CC=19 SCAR=38.7
Pod documented
216 1250 my ( $timeout ) = @_;
217  
218 1250 $self->_adjust_timeout( \$timeout );
219  
220 1250 $timeout = 0 if FAKE_ISREG_READY and keys %{ $self->{fake_isreg} };
221  
222 # Round up to nearest millisecond
223 1250 ▶ if( $timeout ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout) 1107 143 1250
224 1107 my $mils = $timeout * 1000;
225 1107 my $fraction = $mils - int $mils;
226 1107 ▶ $timeout += ( 1 - $fraction ) / 1000 if $fraction;
Branch: 1 branch
BranchTrueFalseTotal
if $fraction 61 1046 1107
227 }
228  
229 1250 ▶ if( my $poll = $self->{poll} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $poll = $self->{'poll'}) { } 0 1250 1250
230 0 my $pollret;
231  
232 0 $self->pre_wait;
233 # There is a bug in IO::Poll at least version 0.07, where poll with no
234 # registered masks returns immediately, rather than waiting for a timeout
235 # This has been reported:
236 # http://rt.cpan.org/Ticket/Display.html?id=25049
237 0 ▶ if( $poll->handles ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($poll->handles) { } 0 0 0
238 0 $pollret = $poll->poll( $timeout );
239  
240 0 ▶ if( ( $pollret == -1 and $! == EINTR ) or $pollret == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($pollret == -1 and $! == 4 or $pollret == 0 and defined $self->{"sigproxy"}) 0 0 0
Condition0%0/9
$pollret == -1 and $! == 4 or $pollret == 0 and defined $self->{"sigproxy"}
l!l&&r!l&&!r
000
$pollret == -1 and $! == 4
!ll&&!rl&&r
000
$pollret == 0 and defined $self->{"sigproxy"}
!ll&&!rl&&r
000
MC/DC0%0/4
$pollret == -1 and $! == 4 or $pollret == 0 and defined $self->{"sigproxy"}
$pollret == -1 $! == 4 $pollret == 0 defined $self->{"sigproxy"}
Truth table0%0/7
A and B or C and D
A= $pollret == -1B= $! == 4C= $pollret == 0D= defined $self->{"sigproxy"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
241 and defined $self->{sigproxy} ) {
242 # A signal occurred and we have a sigproxy. Allow one more poll
243 # call with zero timeout. If it finds something, keep that result.
244 # If it finds nothing, keep -1
245  
246 # Preserve $! whatever happens
247 0 local $!;
248  
249 0 my $secondattempt = $poll->poll( 0 );
250 0 ▶ $pollret = $secondattempt if $secondattempt > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $secondattempt > 0 0 0 0
251 }
252 }
253 else {
254 # Workaround - we'll use select to fake a millisecond-accurate sleep
255 0 $pollret = select( undef, undef, undef, $timeout );
256 }
257  
258 0 $self->post_wait;
259  
260 0 ▶ return undef unless defined $pollret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pollret 0 0 0
261 0 return $self->post_poll;
262 }
263 else {
264 1250 my @pollmasks = %{ $self->{pollmask} };
265  
266 1250 $self->pre_wait;
267  
268 # Perl 5.8.x's IO::Poll::_poll gets confused with no masks
269 1250 my $pollret;
270 1250 ▶ if( @pollmasks ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pollmasks) { } 1155 95 1250
271 1155 ▶ my $msec = defined $timeout ? $timeout * 1000 : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 1044 111 1155
272 1155 $pollret = IO::Poll::_poll( $msec, @pollmasks );
273 1155 ▶ if( $pollret == -1 and $! == EINTR or
Branch: 1 branch
BranchTrueFalseTotal
if ($pollret == -1 and $! == 4 or $pollret == 0 and $self->{"sigproxy"}) 48 1107 1155
Condition66%6/9
$pollret == -1 and $! == 4 or $pollret == 0 and $self->{"sigproxy"}
l!l&&r!l&&!r
4801107
$pollret == -1 and $! == 4
!ll&&!rl&&r
1107048
$pollret == 0 and $self->{"sigproxy"}
!ll&&!rl&&r
10031040
MC/DC25%1/4
$pollret == -1 and $! == 4 or $pollret == 0 and $self->{"sigproxy"}
$pollret == -1 $! == 4 $pollret == 0 $self->{"sigproxy"}
Truth table28%2/7
A and B or C and D
A= $pollret == -1B= $! == 4C= $pollret == 0D= $self->{"sigproxy"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
274 $pollret == 0 and $self->{sigproxy} ) {
275 48 local $!;
276  
277 48 @pollmasks = %{ $self->{pollmask} };
278 48 my $secondattempt = IO::Poll::_poll( $msec, @pollmasks );
279 48 ▶ $pollret = $secondattempt if $secondattempt > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $secondattempt > 0 48 0 48
280 }
281  
282 }
283 else {
284 # Workaround - we'll use select to fake a millisecond-accurate sleep
285 95 $pollret = select( undef, undef, undef, $timeout );
286 }
287  
288 1250 $self->post_wait;
289  
290 1250 ▶ return undef unless defined $pollret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pollret 0 1250 1250
291  
292 1250 $self->{pollevents} = { @pollmasks };
293 1250 return $self->post_poll;
294 }
295 }
296  
297 sub watch_io
298 {
299 771 ▶ my $self = shift;
Subroutine watch_io: called CC=8 SCAR=20.8
Pod documented
300 771 my %params = @_;
301  
302 771 $self->__watch_io( %params );
303  
304 771 my $poll = $self->{poll};
305  
306 771 my $handle = $params{handle};
307 771 my $fileno = $handle->fileno;
308  
309 my $curmask = $poll ? $poll->mask( $handle )
310 771 ▶ : $self->{pollmask}{$fileno};
Branch: 1 branch
BranchTrueFalseTotal
$poll ? : 4 767 771
311 771 ▶ $curmask ||= 0;
Condition100%2/2
$curmask ||= 0
l!l
15756
MC/DC100%1/1
$curmask ||= 0
$curmask
Truth table100%2/2
A
A= $curmask
Aresult
11
00
312  
313 771 my $mask = $curmask;
314 771 ▶ $params{on_read_ready} and $mask |= POLLIN;
Branch: 1 branch
BranchTrueFalseTotal
$params{"on_read_ready"} and $mask |= 1 678 93 771
315 771 ▶ $params{on_write_ready} and $mask |= POLLOUT | (POLL_CONNECT_POLLPRI ? POLLPRI : 0);
Branch: 1 branch
BranchTrueFalseTotal
$params{"on_write_ready"} and $mask |= 4 93 678 771
316 771 ▶ $params{on_hangup} and $mask |= POLLHUP;
Branch: 1 branch
BranchTrueFalseTotal
$params{"on_hangup"} and $mask |= 16 2 769 771
317  
318 771 if( FAKE_ISREG_READY and S_ISREG +(stat $handle)[2] ) {
319 $self->{fake_isreg}{$fileno} = $mask;
320 }
321  
322 771 ▶ return if $mask == $curmask;
Branch: 1 branch
BranchTrueFalseTotal
if $mask == $curmask 1 770 771
323  
324 770 ▶ if( $poll ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($poll) { } 4 766 770
325 4 $poll->mask( $handle, $mask );
326 }
327 else {
328 766 $self->{pollmask}{$fileno} = $mask;
329 }
330 }
331  
332 sub unwatch_io
333 {
334 698 ▶ my $self = shift;
Subroutine unwatch_io: called CC=9 SCAR=22.0
Pod documented
335 698 my %params = @_;
336  
337 698 $self->__unwatch_io( %params );
338  
339 698 my $poll = $self->{poll};
340  
341 698 my $handle = $params{handle};
342 698 my $fileno = $handle->fileno;
343  
344 my $curmask = $poll ? $poll->mask( $handle )
345 698 ▶ : $self->{pollmask}{$fileno};
Branch: 1 branch
BranchTrueFalseTotal
$poll ? : 3 695 698
346 698 ▶ $curmask ||= 0;
Condition100%2/2
$curmask ||= 0
l!l
66335
MC/DC100%1/1
$curmask ||= 0
$curmask
Truth table100%2/2
A
A= $curmask
Aresult
11
00
347  
348 698 my $mask = $curmask;
349 698 ▶ $params{on_read_ready} and $mask &= ~POLLIN;
Branch: 1 branch
BranchTrueFalseTotal
$params{"on_read_ready"} and $mask &= '18446744073709551614' 580 118 698
350 698 ▶ $params{on_write_ready} and $mask &= ~(POLLOUT | (POLL_CONNECT_POLLPRI ? POLLPRI : 0));
Branch: 1 branch
BranchTrueFalseTotal
$params{"on_write_ready"} and $mask &= '18446744073709551611' 120 578 698
351 698 ▶ $params{on_hangup} and $mask &= ~POLLHUP;
Branch: 1 branch
BranchTrueFalseTotal
$params{"on_hangup"} and $mask &= '18446744073709551599' 2 696 698
352  
353 698 if( FAKE_ISREG_READY and S_ISREG +(stat $handle)[2] ) {
354 if( $mask ) {
355 $self->{fake_isreg}{$handle->fileno} = $mask;
356 }
357 else {
358 delete $self->{fake_isreg}{$handle->fileno};
359 }
360 }
361  
362 698 ▶ return if $mask == $curmask;
Branch: 1 branch
BranchTrueFalseTotal
if $mask == $curmask 35 663 698
363  
364 663 ▶ if( $poll ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($poll) { } 3 660 663
365 3 $poll->mask( $handle, $mask );
366 }
367 else {
368 $mask ? ( $self->{pollmask}{$fileno} = $mask )
369 660 ▶ : ( delete $self->{pollmask}{$fileno} );
Branch: 1 branch
BranchTrueFalseTotal
$mask ? : 12 648 660
370 }
371 }
372  
373 =head1 AUTHOR
374  
375 Paul Evans <leonerd@leonerd.org.uk>
376  
377 =cut
378  
379 84 0x55AA;
« blib/lib/IO/Async/Loop.pm ↑ Summary blib/lib/IO/Async/Resolver.pm »