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

filter: Statementstmt 100% 95 / 95 Branchbran 91.6% 22 / 24 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 17 / 17 Podpod 85.7% 6 / 7 totaltotal 98.0% 148 / 151 CC 18 SCAR 14.9
CC
18
Cov
98%
post_select
10.0
unwatch_io
4.0
pre_select
3.0
CRAP 4.4
« blib/lib/IO/Async/Channel.pm ↑ 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-2024 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Loop::Select 0.805;
7  
8 15 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 ▶use constant API_VERSION => '0.49';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 15 ▶use base qw( IO::Async::Loop );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 ▶use IO::Async::OS;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # select() on most platforms claims that ISREG files are always read- and
20 # write-ready, but not on MSWin32. We need to fake this
21 15 ▶use constant FAKE_ISREG_READY => IO::Async::OS->HAVE_FAKE_ISREG_READY;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 # select() on most platforms indicates write-ready when connect() fails, but
23 # not on MSWin32. Have to pull from evec in that case
24 15 ▶use constant SELECT_CONNECT_EVEC => IO::Async::OS->HAVE_SELECT_CONNECT_EVEC;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 15 ▶use constant _CAN_WATCHDOG => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 15 ▶use constant WATCHDOG_ENABLE => IO::Async::Loop->WATCHDOG_ENABLE;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head1 NAME
30  
31 C<IO::Async::Loop::Select> - use L<IO::Async> with C<select(2)>
32  
33 =head1 SYNOPSIS
34  
35 =for highlighter language=perl
36  
37 Normally an instance of this class would not be directly constructed by a
38 program. It may however, be useful for runinng L<IO::Async> with an existing
39 program already using a C<select> call.
40  
41 use IO::Async::Loop::Select;
42  
43 my $loop = IO::Async::Loop::Select->new;
44  
45 $loop->add( ... );
46  
47 while(1) {
48 my ( $rvec, $wvec, $evec ) = ('') x 3;
49 my $timeout;
50  
51 $loop->pre_select( \$rvec, \$wvec, \$evec, \$timeout );
52 ...
53 my $ret = select( $rvec, $wvec, $evec, $timeout );
54 ...
55 $loop->post_select( $rvec, $evec, $wvec );
56 }
57  
58 =head1 DESCRIPTION
59  
60 This subclass of L<IO::Async::Loop> uses the C<select(2)> syscall to perform
61 read-ready and write-ready tests.
62  
63 To integrate with an existing C<select>-based event loop, a pair of methods
64 C<pre_select> and C<post_select> can be called immediately before and
65 after a C<select> call. The relevant bits in the read-ready, write-ready and
66 exceptional-state bitvectors are set by the C<pre_select> method, and tested
67 by the C<post_select> method to pick which event callbacks to invoke.
68  
69 =cut
70  
71 =head1 CONSTRUCTOR
72  
73 =cut
74  
75 =head2 new
76  
77 $loop = IO::Async::Loop::Select->new;
78  
79 This function returns a new instance of a C<IO::Async::Loop::Select> object.
80 It takes no special arguments.
81  
82 =cut
83  
84 sub new
85 {
86 14 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
87  
88 14 my $self = $class->__new( @_ );
89  
90 14 $self->{rvec} = '';
91 14 $self->{wvec} = '';
92 14 $self->{evec} = '';
93  
94 14 $self->{avec} = ''; # Bitvector of handles always to claim are ready
95  
96 14 return $self;
97 }
98  
99 =head1 METHODS
100  
101 =cut
102  
103 =head2 pre_select
104  
105 $loop->pre_select( \$readvec, \$writevec, \$exceptvec, \$timeout );
106  
107 This method prepares the bitvectors for a C<select> call, setting the bits
108 that the Loop is interested in. It will also adjust the C<$timeout> value if
109 appropriate, reducing it if the next event timeout the Loop requires is sooner
110 than the current value.
111  
112 =over 8
113  
114 =item \$readvec
115  
116 =item \$writevec
117  
118 =item \$exceptvec
119  
120 Scalar references to the reading, writing and exception bitvectors
121  
122 =item \$timeout
123  
124 Scalar reference to the timeout value
125  
126 =back
127  
128 =cut
129  
130 sub pre_select
131 {
132 94 ▶ my $self = shift;
Subroutine pre_select: called CC=3 SCAR=11.0
Pod documented
133 94 my ( $readref, $writeref, $exceptref, $timeref ) = @_;
134  
135 # BITWISE operations
136 94 $$readref |= $self->{rvec};
137 94 $$writeref |= $self->{wvec};
138 94 $$exceptref |= $self->{evec};
139  
140 94 $self->_adjust_timeout( $timeref );
141  
142 94 $$timeref = 0 if FAKE_ISREG_READY and length $self->{avec};
143  
144 # Round up to nearest millisecond
145 94 ▶ if( $$timeref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$timeref) 82 12 94
146 82 my $mils = $$timeref * 1000;
147 82 my $fraction = $mils - int $mils;
148 82 ▶ $$timeref += ( 1 - $fraction ) / 1000 if $fraction;
Branch: 1 branch
BranchTrueFalseTotal
if $fraction 11 71 82
149 }
150  
151 94 return;
152 }
153  
154 =head2 post_select
155  
156 $loop->post_select( $readvec, $writevec, $exceptvec );
157  
158 This method checks the returned bitvectors from a C<select> call, and calls
159 any of the callbacks that are appropriate.
160  
161 =over 8
162  
163 =item $readvec
164  
165 =item $writevec
166  
167 =item $exceptvec
168  
169 Scalars containing the read-ready, write-ready and exception bitvectors
170  
171 =back
172  
173 =cut
174  
175 sub post_select
176 {
177 92 ▶ my $self = shift;
Subroutine post_select: called CC=10 SCAR=23.1
Pod documented
178 92 my ( $readvec, $writevec, $exceptvec ) = @_;
179  
180 92 my $iowatches = $self->{iowatches};
181  
182 92 my $count = 0;
183  
184 92 alarm( IO::Async::Loop->WATCHDOG_INTERVAL ) if WATCHDOG_ENABLE;
185  
186 92 foreach my $fd ( keys %$iowatches ) {
187 68 ▶ my $watch = $iowatches->{$fd} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $watch = $iowatches->{$fd} or next 1 67 68
188  
189 67 my $fileno = $watch->[0]->fileno;
190  
191 67 ▶ if( vec( $readvec, $fileno, 1 ) or
Branch: 1 branch
BranchTrueFalseTotal
if (vec $readvec, $fileno, 1 or 0) 35 32 67
Condition100%2/2
vec $readvec, $fileno, 1 or 0
l!l
3532
MC/DC100%1/1
vec $readvec, $fileno, 1 or 0
vec $readvec, $fileno, 1
Truth table100%2/2
A
A= vec $readvec, $fileno, 1
Aresult
11
00
192 FAKE_ISREG_READY and vec( $self->{avec}, $fileno, 1 ) and vec( $self->{rvec}, $fileno, 1 ) ) {
193 35 ▶ $count++, $watch->[1]->() if defined $watch->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $watch->[1] 35 0 35
194 }
195  
196 67 ▶ if( vec( $writevec, $fileno, 1 ) or
Branch: 1 branch
BranchTrueFalseTotal
if (vec $writevec, $fileno, 1 or 0 or 0) 0 58 58
Condition100%4/4
vec $writevec, $fileno, 1 or 0 or 0
l!l
958
vec $writevec, $fileno, 1 or 0
l!l
958
MC/DC100%1/1
vec $writevec, $fileno, 1 or 0 or 0
vec $writevec, $fileno, 1
Truth table100%2/2
A
A= vec $writevec, $fileno, 1
Aresult
11
00
197 SELECT_CONNECT_EVEC and vec( $exceptvec, $fileno, 1 ) or
198 FAKE_ISREG_READY and vec( $self->{avec}, $fileno, 1 ) and vec( $self->{wvec}, $fileno, 1 ) ) {
199 9 ▶ $count++, $watch->[2]->() if defined $watch->[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $watch->[2] 8 1 9
200 }
201 }
202  
203 # Since we have no way to know if the timeout occurred, we'll have to
204 # attempt to fire any waiting timeout events anyway
205  
206 92 $self->_manage_queues;
207  
208 92 alarm( 0 ) if WATCHDOG_ENABLE;
209 }
210  
211 sub is_running
212 {
213 1 ▶ my $self = shift;
Subroutine is_running: called CC=1 SCAR=0.0
Pod undocumented
214 1 return $self->{running};
215 }
216  
217 =head2 loop_once
218  
219 $count = $loop->loop_once( $timeout );
220  
221 This method calls the C<pre_select> method to prepare the bitvectors for a
222 C<select> syscall, performs it, then calls C<post_select> to process the
223 result. It returns the total number of callbacks invoked by the
224 C<post_select> method, or C<undef> if the underlying C<select(2)> syscall
225 returned an error.
226  
227 =cut
228  
229 sub loop_once
230 {
231 88 ▶ my $self = shift;
Subroutine loop_once: called CC=2 SCAR=6.9
Pod documented
232 88 my ( $timeout ) = @_;
233  
234 88 my ( $rvec, $wvec, $evec ) = ('') x 3;
235  
236 88 $self->pre_select( \$rvec, \$wvec, \$evec, \$timeout );
237  
238 88 $self->pre_wait;
239 88 my $ret = select( $rvec, $wvec, $evec, $timeout );
240 88 $self->post_wait;
241  
242 88 ▶ if( $ret < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret < 0) 17 71 88
243 # r/w/e vec can't be trusted
244 17 $rvec = $wvec = $evec = '';
245 }
246  
247 {
248 88 local $!;
249 88 $self->post_select( $rvec, $wvec, $evec );
250 }
251  
252 88 return $ret;
253 }
254  
255 sub watch_io
256 {
257 23 ▶ my $self = shift;
Subroutine watch_io: called CC=3 SCAR=11.0
Pod documented
258 23 my %params = @_;
259  
260 23 $self->__watch_io( %params );
261  
262 23 my $fileno = $params{handle}->fileno;
263  
264 23 ▶ vec( $self->{rvec}, $fileno, 1 ) = 1 if $params{on_read_ready};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"on_read_ready"} 17 6 23
265 23 ▶ vec( $self->{wvec}, $fileno, 1 ) = 1 if $params{on_write_ready};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"on_write_ready"} 8 15 23
266  
267 # MSWin32 does not indicate writeready for connect() errors, HUPs, etc
268 # but it does indicate exceptional
269 23 vec( $self->{evec}, $fileno, 1 ) = 1 if SELECT_CONNECT_EVEC and $params{on_write_ready};
270  
271 23 vec( $self->{avec}, $fileno, 1 ) = 1 if FAKE_ISREG_READY and stat( $params{handle} ) and -f _;
272 }
273  
274 sub unwatch_io
275 {
276 14 ▶ my $self = shift;
Subroutine unwatch_io: called CC=4 SCAR=13.9
Pod documented
277 14 my %params = @_;
278  
279 14 $self->__unwatch_io( %params );
280  
281 14 my $fileno = $params{handle}->fileno;
282  
283 14 ▶ vec( $self->{rvec}, $fileno, 1 ) = 0 if $params{on_read_ready};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"on_read_ready"} 10 4 14
284 14 ▶ vec( $self->{wvec}, $fileno, 1 ) = 0 if $params{on_write_ready};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"on_write_ready"} 8 6 14
285  
286 14 vec( $self->{evec}, $fileno, 1 ) = 0 if SELECT_CONNECT_EVEC and $params{on_write_ready};
287  
288 14 vec( $self->{avec}, $fileno, 1 ) = 0 if FAKE_ISREG_READY and stat( $params{handle} ) and -f _;
289  
290 # vec will grow a bit vector as needed, but never shrink it. We'll trim
291 # trailing null bytes
292 14 $_ =~s/\0+\z// for $self->{rvec}, $self->{wvec}, $self->{evec}, $self->{avec};
293 }
294  
295 =head1 SEE ALSO
296  
297 =over 4
298  
299 =item *
300  
301 L<IO::Select> - OO interface to select system call
302  
303 =back
304  
305 =head1 AUTHOR
306  
307 Paul Evans <leonerd@leonerd.org.uk>
308  
309 =cut
310  
311 15 0x55AA;
« blib/lib/IO/Async/Channel.pm ↑ Summary Build »