blib/lib/POE/Loop/Select.pm

filter: Statementstmt 96.5% 83 / 86 Branchbran 91.6% 33 / 36 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 13 totaltotal 86.5% 141 / 163 CC 30 SCAR 25.1
CC
30
Cov
95%
loop_do_timeslice
23.2
loop_finalize
3.1
loop_pause_filehandle
3.0
CRAP 12.3
« blib/lib/POE/Filter/Line.pm ↑ Summary blib/lib/POE/Resource/Signals.pm »
1 # Select loop bridge for POE::Kernel.
2  
3 # Empty package to appease perl.
4 package POE::Loop::Select;
5  
6 146 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Include common signal handling.
9 146 ▶use POE::Loop::PerlSignals;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 145 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 766 $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places)
13  
14 =for poe_tests
15  
16 sub skip_tests { return }
17  
18 =cut
19  
20 # Everything plugs into POE::Kernel.
21 package POE::Kernel;
22  
23 145 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 145 ▶use Errno qw(EINPROGRESS EWOULDBLOCK EINTR);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # select() vectors. They're stored in an array so that the MODE_*
27 # offsets can refer to them. This saves some code at the expense of
28 # clock cycles.
29 #
30 # [ $select_read_bit_vector, (MODE_RD)
31 # $select_write_bit_vector, (MODE_WR)
32 # $select_expedite_bit_vector (MODE_EX)
33 # ];
34 290 my @loop_vectors = ("", "", "");
35  
36 # A record of the file descriptors we are actively watching.
37 344 my %loop_filenos;
38  
39 # Allow $^T to change without affecting our internals.
40 470 my $start_time = monotime();
41  
42 #------------------------------------------------------------------------------
43 # Loop construction and destruction.
44  
45 195 ▶sub loop_initialize {
Subroutine loop_initialize: called CC=1 SCAR=0.0
Pod undocumented
46 195 my $self = shift;
47  
48 # Initialize the vectors as vectors.
49 195 @loop_vectors = ( '', '', '' );
50 195 vec($loop_vectors[MODE_RD], 0, 1) = 0;
51 195 vec($loop_vectors[MODE_WR], 0, 1) = 0;
52 195 vec($loop_vectors[MODE_EX], 0, 1) = 0;
53 }
54  
55 129 ▶sub loop_finalize {
Subroutine loop_finalize: called CC=3 SCAR=11.2
Pod undocumented
56 129 my $self = shift;
57  
58 # This is "clever" in that it relies on each symbol on the left to
59 # be stringified by the => operator.
60 129 my %kernel_modes = (
61 MODE_RD => MODE_RD,
62 MODE_WR => MODE_WR,
63 MODE_EX => MODE_EX,
64 );
65  
66 129 ▶ while (my ($mode_name, $mode_offset) = each(%kernel_modes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %kernel_modes) 387 129 516
67 387 my $bits = unpack('b*', $loop_vectors[$mode_offset]);
68 387 ▶ if (index($bits, '1') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($bits, 1) != -1) 0 387 387
69 0 POE::Kernel::_warn "<rc> LOOP VECTOR LEAK: $mode_name = $bits\a\n";
70 }
71 }
72  
73 129 $self->loop_ignore_all_signals();
74 }
75  
76 #------------------------------------------------------------------------------
77 # Signal handler maintenance functions.
78  
79 0 ▶sub loop_attach_uidestroy {
Subroutine loop_attach_uidestroy: not called CC=1 SCAR=0.0
Pod undocumented
80 # does nothing
81 }
82  
83 #------------------------------------------------------------------------------
84 # Maintain time watchers. For this loop, we simply save the next
85 # event time in a scalar. loop_do_timeslice() will use the saved
86 # value. A "paused" time watcher is just a timeout for some future
87 # time.
88  
89 145 my $_next_event_time = monotime();
90  
91 841 ▶sub loop_resume_time_watcher {
Subroutine loop_resume_time_watcher: called CC=1 SCAR=0.0
Pod undocumented
92 841 $_next_event_time = $_[1];
93 }
94  
95 1690 ▶sub loop_reset_time_watcher {
Subroutine loop_reset_time_watcher: called CC=1 SCAR=0.0
Pod undocumented
96 1690 $_next_event_time = $_[1];
97 }
98  
99 693 ▶sub loop_pause_time_watcher {
Subroutine loop_pause_time_watcher: called CC=1 SCAR=0.0
Pod undocumented
100 693 $_next_event_time = monotime() + 3600;
101 }
102  
103 #------------------------------------------------------------------------------
104 # Maintain filehandle watchers.
105  
106 933 ▶sub loop_watch_filehandle {
Subroutine loop_watch_filehandle: called CC=1 SCAR=0.0
Pod undocumented
107 933 my ($self, $handle, $mode) = @_;
108 933 my $fileno = fileno($handle);
109  
110 933 vec($loop_vectors[$mode], $fileno, 1) = 1;
111 933 $loop_filenos{$fileno} |= (1<<$mode);
112 }
113  
114 776 ▶sub loop_ignore_filehandle {
Subroutine loop_ignore_filehandle: called CC=3 SCAR=11.0
Pod undocumented
115 776 my ($self, $handle, $mode) = @_;
116 776 my $fileno = fileno($handle);
117  
118 776 vec($loop_vectors[$mode], $fileno, 1) = 0;
119 delete $loop_filenos{$fileno} unless (
120 776 ▶ $loop_filenos{$fileno} and $loop_filenos{$fileno} &= ~(1<<$mode)
Branch: 1 branch
BranchTrueFalseTotal
unless $loop_filenos{$fileno} and $loop_filenos{$fileno} &= ~(1 << $mode) 766 10 776
Condition100%3/3
$loop_filenos{$fileno} and $loop_filenos{$fileno} &= ~(1 << $mode)
!ll&&!rl&&r
22454210
MC/DC100%2/2
$loop_filenos{$fileno} and $loop_filenos{$fileno} &= ~(1 << $mode)
$loop_filenos{$fileno} $loop_filenos{$fileno} &= ~(1 << $mode)
Truth table100%3/3
A and B
A= $loop_filenos{$fileno}B= $loop_filenos{$fileno} &= ~(1 << $mode)
ABresult
0X0
100
111
121 );
122 }
123  
124 608 ▶sub loop_pause_filehandle {
Subroutine loop_pause_filehandle: called CC=3 SCAR=11.0
Pod undocumented
125 608 my ($self, $handle, $mode) = @_;
126 608 my $fileno = fileno($handle);
127  
128 608 vec($loop_vectors[$mode], $fileno, 1) = 0;
129 delete $loop_filenos{$fileno} unless (
130 608 ▶ $loop_filenos{$fileno} and $loop_filenos{$fileno} &= ~(1<<$mode)
Branch: 1 branch
BranchTrueFalseTotal
unless $loop_filenos{$fileno} and $loop_filenos{$fileno} &= ~(1 << $mode) 325 283 608
Condition66%2/3
$loop_filenos{$fileno} and $loop_filenos{$fileno} &= ~(1 << $mode)
!ll&&!rl&&r
0325283
MC/DC50%1/2
$loop_filenos{$fileno} and $loop_filenos{$fileno} &= ~(1 << $mode)
$loop_filenos{$fileno} $loop_filenos{$fileno} &= ~(1 << $mode)
Truth table66%2/3
A and B
A= $loop_filenos{$fileno}B= $loop_filenos{$fileno} &= ~(1 << $mode)
ABresult
0X0
100
111
131 );
132 }
133  
134 449 ▶sub loop_resume_filehandle {
Subroutine loop_resume_filehandle: called CC=1 SCAR=0.0
Pod undocumented
135 449 my ($self, $handle, $mode) = @_;
136 449 my $fileno = fileno($handle);
137  
138 449 vec($loop_vectors[$mode], $fileno, 1) = 1;
139 449 $loop_filenos{$fileno} |= (1<<$mode);
140 }
141  
142 #------------------------------------------------------------------------------
143 # The event loop itself.
144  
145 1795 ▶sub loop_do_timeslice {
Subroutine loop_do_timeslice: called CC=23 SCAR=31.4
Pod undocumented
146 1795 my $self = shift;
147  
148 # Check for a hung kernel.
149 1795 $self->_test_if_kernel_is_idle();
150  
151 # Set the select timeout based on current queue conditions. If
152 # there are FIFO events, then the timeout is zero to poll select and
153 # move on. Otherwise set the select timeout until the next pending
154 # event, if there are any. If nothing is waiting, set the timeout
155 # for some constant number of seconds.
156  
157 1795 my $timeout = $_next_event_time;
158  
159 1795 my $now = monotime();
160 1795 ▶ if (defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) { } 1795 0 1795
161 1795 $timeout -= $now;
162 1795 ▶ $timeout = 0 if $timeout < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < 0 794 1001 1795
163  
164 # Very large timeouts can trigger EINVAL on Mac OSX.
165 1795 ▶ $timeout = 3600 if $timeout > 3600;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout > 3600 0 1795 1795
166 }
167 else {
168 0 die "shouldn't happen" if ASSERT_DATA;
169 0 $timeout = 3600;
170 }
171  
172 # Tracing is relatively expensive, but it's not for live systems.
173 # We can get away with it being after the timeout calculation.
174 1795 if (TRACE_EVENTS) {
175 POE::Kernel::_warn(
176 '<ev> Kernel::run() iterating. ' .
177 sprintf(
178 "now(%.4f) timeout(%.4f) then(%.4f)\n",
179 $now - $start_time, $timeout, ($now - $start_time) + $timeout
180 )
181 );
182 }
183  
184 1795 if (TRACE_FILES) {
185 POE::Kernel::_warn(
186 "<fh> ,----- SELECT BITS IN -----\n",
187 "<fh> | READ : ", unpack('b*', $loop_vectors[MODE_RD]), "\n",
188 "<fh> | WRITE : ", unpack('b*', $loop_vectors[MODE_WR]), "\n",
189 "<fh> | EXPEDITE: ", unpack('b*', $loop_vectors[MODE_EX]), "\n",
190 "<fh> `--------------------------\n"
191 );
192 }
193  
194 # Avoid looking at filehandles if we don't need to.
195 # TODO The added code to make this sleep is non-optimal. There is a
196 # way to do this in fewer tests.
197  
198 1795 ▶ if (scalar keys %loop_filenos) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar keys %loop_filenos) { } 1790 5 1795
elsif ($timeout) { } 1 4 5
199 # There are filehandles to poll, so do so.
200  
201 # Check filehandles, or wait for a period of time to elapse.
202 1790 my $hits = CORE::select(
203 my $rout = $loop_vectors[MODE_RD],
204 my $wout = $loop_vectors[MODE_WR],
205 my $eout = $loop_vectors[MODE_EX],
206 $timeout,
207 );
208  
209 1790 if (ASSERT_FILES) {
210 if (
211 $hits < 0 and
212 $! != EINPROGRESS and
213 $! != EWOULDBLOCK and
214 $! != EINTR and
215 $! != 0 # this is caused by SIGNAL_PIPE
216 ) {
217 POE::Kernel::_trap("<fh> select error: $! (hits=$hits)");
218 }
219 }
220  
221 1790 if (TRACE_FILES) {
222 if ($hits > 0) {
223 POE::Kernel::_warn "<fh> select hits = $hits\n";
224 }
225 elsif ($hits == 0) {
226 POE::Kernel::_warn "<fh> select timed out...\n";
227 }
228 POE::Kernel::_warn(
229 "<fh> ,----- SELECT BITS OUT -----\n",
230 "<fh> | READ : ", unpack('b*', $rout), "\n",
231 "<fh> | WRITE : ", unpack('b*', $wout), "\n",
232 "<fh> | EXPEDITE: ", unpack('b*', $eout), "\n",
233 "<fh> `---------------------------\n"
234 );
235 }
236  
237 # If select has seen filehandle activity, then gather up the
238 # active filehandles and synchronously dispatch events to the
239 # appropriate handlers.
240  
241 1499 ▶ if ($hits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hits > 0) 294 1496 1790
242  
243 # This is where they're gathered. It's a variant on a neat
244 # hack Silmaril came up with.
245  
246 1257 my (@rd_selects, @wr_selects, @ex_selects);
247 588 foreach (keys %loop_filenos) {
248 1753 ▶ push(@rd_selects, $_) if vec($rout, $_, 1);
Branch: 1 branch
BranchTrueFalseTotal
if vec $rout, $_, 1 765 1142 1907
249 1756 ▶ push(@wr_selects, $_) if vec($wout, $_, 1);
Branch: 1 branch
BranchTrueFalseTotal
if vec $wout, $_, 1 614 1327 1941
250 1922 ▶ push(@ex_selects, $_) if vec($eout, $_, 1);
Branch: 1 branch
BranchTrueFalseTotal
if vec $eout, $_, 1 382 1881 2263
251 }
252  
253 1284 if (TRACE_FILES) {
254 if (@rd_selects) {
255 POE::Kernel::_warn(
256 "<fh> found pending rd selects: ",
257 join( ', ', sort { $a <=> $b } @rd_selects ),
258 "\n"
259 );
260 }
261 if (@wr_selects) {
262 POE::Kernel::_warn(
263 "<sl> found pending wr selects: ",
264 join( ', ', sort { $a <=> $b } @wr_selects ),
265 "\n"
266 );
267 }
268 if (@ex_selects) {
269 POE::Kernel::_warn(
270 "<sl> found pending ex selects: ",
271 join( ', ', sort { $a <=> $b } @ex_selects ),
272 "\n"
273 );
274 }
275 }
276  
277 1613 if (ASSERT_FILES) {
278 unless (@rd_selects or @wr_selects or @ex_selects) {
279 POE::Kernel::_trap(
280 "<fh> found no selects, with $hits hits from select???\n"
281 );
282 }
283 }
284  
285 # Enqueue the gathered selects, and flag them as temporarily
286 # paused. They'll resume after dispatch.
287  
288 1103 ▶ @rd_selects and $self->_data_handle_enqueue_ready(MODE_RD, @rd_selects);
Branch: 1 branch
BranchTrueFalseTotal
@rd_selects and $self->_data_handle_enqueue_ready(0, @rd_selects) 495 1999 2494
289 759 ▶ @wr_selects and $self->_data_handle_enqueue_ready(MODE_WR, @wr_selects);
Branch: 1 branch
BranchTrueFalseTotal
@wr_selects and $self->_data_handle_enqueue_ready(1, @wr_selects) 352 1632 1984
290 759 ▶ @ex_selects and $self->_data_handle_enqueue_ready(MODE_EX, @ex_selects);
Branch: 1 branch
BranchTrueFalseTotal
@ex_selects and $self->_data_handle_enqueue_ready(2, @ex_selects) 133 1851 1984
291 }
292 }
293 elsif ($timeout) {
294 # No filehandles to select on. Four-argument select() fails on
295 # MSWin32 with all undef bitmasks. Use sleep() there instead.
296  
297 # Not unconditionally the Time::HiRes microsleep because
298 # Time::HiRes may not be installed. This is only an issue until
299 # we can require versions of Perl that include Time::HiRes.
300 1550 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 236 229 465
301 1690 sleep($timeout);
302 }
303 else {
304 1690 CORE::select(undef, undef, undef, $timeout);
305 }
306 }
307  
308 # Dispatch whatever events are due.
309 1292 $self->_data_ev_dispatch_due();
310 }
311  
312 140 ▶sub loop_run {
Subroutine loop_run: called CC=2 SCAR=6.9
Pod undocumented
313 466 my $self = shift;
314  
315 # Run for as long as there are sessions to service.
316 357 ▶ while ($self->_data_ses_count()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_data_ses_count) 1940 449 2389
317 1899 $self->loop_do_timeslice();
318 }
319 }
320  
321 130 ▶sub loop_halt {
Subroutine loop_halt: called CC=1 SCAR=0.0
Pod undocumented
322 # does nothing
323 }
324  
325 145 1;
326  
327 __END__
« blib/lib/POE/Filter/Line.pm ↑ Summary blib/lib/POE/Resource/Signals.pm »