blib/lib/POE/Loop/IO_Poll.pm

filter: Statementstmt 87.8% 130 / 148 Branchbran 82.4% 61 / 74 Conditioncond 65.6% 21 / 32 MC/DCmcdc 33.3% 6 / 18 Subroutinesub 95.4% 21 / 22 Podpod 0.0% 0 / 14 totaltotal 77.6% 239 / 308 CC 51 SCAR 31.4
CC
51
Cov
83%
loop_do_timeslice
42.4
BEGIN
6.1
mode_to_poll
6.0
CRAP 23.2
« blib/lib/POE/NFA.pm ↑ Summary blib/lib/POE/Session.pm »
1 # IO::Poll event loop bridge for POE::Kernel. The theory is that this
2 # will be faster for large scale applications. This file is
3 # contributed by Matt Sergeant (baud).
4  
5 # Empty package to appease perl.
6 package POE::Loop::IO_Poll;
7  
8 55 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 54 $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places)
10  
11 # Include common signal handling.
12 55 ▶use POE::Loop::PerlSignals;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Everything plugs into POE::Kernel;
15 package POE::Kernel;
16  
17 =for poe_tests
18  
19 sub skip_tests {
20 return "IO::Poll is not 100% compatible with $^O" if (
21 $^O eq "MSWin32" and not $ENV{POE_DANTIC}
22 );
23 return "IO::Poll tests require the IO::Poll module" if (
24 do { eval "use IO::Poll"; $@ }
25 );
26 }
27  
28 =cut
29  
30 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # Be sure we're using a contemporary version of IO::Poll. There were
33 # issues with certain versions of IO::Poll prior to 0.05. The latest
34 # version is 0.01, however.
35 54 ▶use IO::Poll 0.01;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 # Hand off to POE::Loop::Select if we're running under ActivePerl.
38 54 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=18.1
39 54 ▶ if ($^O eq "MSWin32" and not $ENV{POE_DANTIC}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" and not $ENV{"POE_DANTIC"}) 0 54 54
Condition33%1/3
$^O eq "MSWin32" and not $ENV{"POE_DANTIC"}
!ll&&!rl&&r
5400
MC/DC0%0/2
$^O eq "MSWin32" and not $ENV{"POE_DANTIC"}
$^O eq "MSWin32" not $ENV{"POE_DANTIC"}
Truth table33%1/3
A and not(B)
A= $^O eq "MSWin32"B= not $ENV{"POE_DANTIC"}
ABresult
0X0
100
111
40 0 warn "IO::Poll is defective on $^O. Falling back to IO::Select.\n";
41 0 require POE::Loop::Select;
42 0 POE::Loop::Select->import();
43 0 die "not really dying";
44 }
45 }
46  
47 54 ▶use Errno qw(EINPROGRESS EWOULDBLOCK EINTR);
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 54 ▶use IO::Poll qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
50 POLLRDNORM POLLWRNORM POLLRDBAND POLLERR POLLHUP POLLNVAL
51 54 );
52  
53 # Many systems' IO::Poll don't define POLLRDNORM.
54 # Usually upgrading IO::Poll helps.
55 54 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=15.7
56 54 my $x = eval { POLLRDNORM };
57 54 ▶ if ($@ or not defined $x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $x) 0 54 54
Condition33%1/3
$@ or not defined $x
l!l&&r!l&&!r
0054
MC/DC0%0/2
$@ or not defined $x
$@ not defined $x
Truth table33%1/3
A or not(B)
A= $@B= not defined $x
ABresult
1X1
011
000
58 0 warn(
59 "Your IO::Poll doesn't define POLLRDNORM. Falling back to IO::Select.\n"
60 );
61 0 require POE::Loop::Select;
62 0 POE::Loop::Select->import();
63 0 die "not really dying";
64 }
65 }
66  
67 54 my %poll_fd_masks;
68  
69 # Allow $^T to change without affecting our internals.
70 54 my $start_time = monotime();
71  
72 #------------------------------------------------------------------------------
73 # Loop construction and destruction.
74  
75 98 ▶sub loop_initialize {
Subroutine loop_initialize: called CC=1 SCAR=0.0
Pod undocumented
76 98 my $self = shift;
77 98 %poll_fd_masks = ();
78 }
79  
80 74 ▶sub loop_finalize {
Subroutine loop_finalize: called CC=1 SCAR=0.0
Pod undocumented
81 74 my $self = shift;
82 74 $self->loop_ignore_all_signals();
83 }
84  
85 #------------------------------------------------------------------------------
86 # Signal handler maintenance functions.
87  
88 0 ▶sub loop_attach_uidestroy {
Subroutine loop_attach_uidestroy: not called CC=1 SCAR=0.0
Pod undocumented
89 # does nothing
90 }
91  
92 #------------------------------------------------------------------------------
93 # Maintain time watchers. For this loop, we simply save the next
94 # event time in a scalar. loop_do_timeslice() will use the saved
95 # value. A "paused" time watcher is just a timeout for some future
96 # time.
97  
98 54 my $_next_event_time = monotime();
99  
100 515 ▶sub loop_resume_time_watcher {
Subroutine loop_resume_time_watcher: called CC=1 SCAR=0.0
Pod undocumented
101 515 $_next_event_time = $_[1];
102 }
103  
104 538 ▶sub loop_reset_time_watcher {
Subroutine loop_reset_time_watcher: called CC=1 SCAR=0.0
Pod undocumented
105 538 $_next_event_time = $_[1];
106 }
107  
108 341 ▶sub loop_pause_time_watcher {
Subroutine loop_pause_time_watcher: called CC=1 SCAR=0.0
Pod undocumented
109 341 $_next_event_time = monotime() + 3600;
110 }
111  
112 # A static function; not some object method.
113  
114 1476 ▶sub mode_to_poll {
Subroutine mode_to_poll: called CC=4 SCAR=17.9
Pod undocumented
115 1476 ▶ return POLLRDNORM if $_[0] == MODE_RD;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] == 0 605 871 1476
116 871 ▶ return POLLWRNORM if $_[0] == MODE_WR;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] == 1 871 0 871
117 0 ▶ return POLLRDBAND if $_[0] == MODE_EX;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] == 2 0 0 0
118 0 croak "unknown I/O mode $_[0]";
119 }
120  
121 #------------------------------------------------------------------------------
122 # Maintain filehandle watchers.
123  
124 492 ▶sub loop_watch_filehandle {
Subroutine loop_watch_filehandle: called CC=2 SCAR=6.9
Pod undocumented
125 492 my ($self, $handle, $mode) = @_;
126 492 my $fileno = fileno($handle);
127  
128 492 my $type = mode_to_poll($mode);
129 492 ▶ my $current = $poll_fd_masks{$fileno} || 0;
Condition100%2/2
$poll_fd_masks{$fileno} || 0
l!l
67425
MC/DC100%1/1
$poll_fd_masks{$fileno} || 0
$poll_fd_masks{$fileno}
Truth table100%2/2
A
A= $poll_fd_masks{$fileno}
Aresult
11
00
130 492 my $new = $current | $type;
131  
132 492 if (TRACE_FILES) {
133 492 POE::Kernel::_warn(
134 sprintf(
135 "<fh> Watch $fileno: " .
136 "Current mask: 0x%02X - including 0x%02X = 0x%02X\n",
137 $current, $type, $new
138 )
139 );
140 }
141  
142 268 $poll_fd_masks{$fileno} = $new;
143 }
144  
145 378 ▶sub loop_ignore_filehandle {
Subroutine loop_ignore_filehandle: called CC=3 SCAR=11.0
Pod undocumented
146 378 my ($self, $handle, $mode) = @_;
147 378 my $fileno = fileno($handle);
148  
149 378 my $type = mode_to_poll($mode);
150 378 ▶ my $current = $poll_fd_masks{$fileno} || 0;
Condition100%2/2
$poll_fd_masks{$fileno} || 0
l!l
266112
MC/DC100%1/1
$poll_fd_masks{$fileno} || 0
$poll_fd_masks{$fileno}
Truth table100%2/2
A
A= $poll_fd_masks{$fileno}
Aresult
11
00
151 378 my $new = $current & ~$type;
152  
153 378 if (TRACE_FILES) {
154 378 POE::Kernel::_warn(
155 sprintf(
156 "<fh> Ignore $fileno: " .
157 ": Current mask: 0x%02X - removing 0x%02X = 0x%02X\n",
158 $current, $type, $new
159 )
160 );
161 }
162  
163 165 ▶ if ($new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new) { } 7 371 378
164 219 $poll_fd_masks{$fileno} = $new;
165 }
166 else {
167 158 delete $poll_fd_masks{$fileno};
168 }
169 }
170  
171 335 ▶sub loop_pause_filehandle {
Subroutine loop_pause_filehandle: called CC=3 SCAR=11.0
Pod undocumented
172 335 my ($self, $handle, $mode) = @_;
173 335 my $fileno = fileno($handle);
174  
175 335 my $type = mode_to_poll($mode);
176 335 ▶ my $current = $poll_fd_masks{$fileno} || 0;
Condition50%1/2
$poll_fd_masks{$fileno} || 0
l!l
3350
MC/DC0%0/1
$poll_fd_masks{$fileno} || 0
$poll_fd_masks{$fileno}
Truth table50%1/2
A
A= $poll_fd_masks{$fileno}
Aresult
11
00
177 335 my $new = $current & ~$type;
178  
179 335 if (TRACE_FILES) {
180 335 POE::Kernel::_warn(
181 sprintf(
182 "<fh> Pause $fileno: " .
183 ": Current mask: 0x%02X - removing 0x%02X = 0x%02X\n",
184 $current, $type, $new
185 )
186 );
187 }
188  
189 283 ▶ if ($new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new) { } 183 152 335
190 161 $poll_fd_masks{$fileno} = $new;
191 }
192 else {
193 100 delete $poll_fd_masks{$fileno};
194 }
195 }
196  
197 271 ▶sub loop_resume_filehandle {
Subroutine loop_resume_filehandle: called CC=2 SCAR=6.9
Pod undocumented
198 271 my ($self, $handle, $mode) = @_;
199 271 my $fileno = fileno($handle);
200  
201 271 my $type = mode_to_poll($mode);
202 271 ▶ my $current = $poll_fd_masks{$fileno} || 0;
Condition100%2/2
$poll_fd_masks{$fileno} || 0
l!l
17794
MC/DC100%1/1
$poll_fd_masks{$fileno} || 0
$poll_fd_masks{$fileno}
Truth table100%2/2
A
A= $poll_fd_masks{$fileno}
Aresult
11
00
203 271 my $new = $current | $type;
204  
205 271 if (TRACE_FILES) {
206 271 POE::Kernel::_warn(
207 sprintf(
208 "<fh> Resume $fileno: " .
209 "Current mask: 0x%02X - including 0x%02X = 0x%02X\n",
210 $current, $type, $new
211 )
212 );
213 }
214  
215 201 $poll_fd_masks{$fileno} = $new;
216 }
217  
218 #------------------------------------------------------------------------------
219 # The event loop itself.
220  
221 801 ▶sub loop_do_timeslice {
Subroutine loop_do_timeslice: called CC=37 SCAR=37.5
Pod undocumented
222 801 my $self = shift;
223  
224 # Check for a hung kernel.
225 801 $self->_test_if_kernel_is_idle();
226  
227 # Set the poll timeout based on current queue conditions. If there
228 # are FIFO events, then the poll timeout is zero and move on.
229 # Otherwise set the poll timeout until the next pending event, if
230 # there are any. If nothing is waiting, set the timeout for some
231 # constant number of seconds.
232  
233 801 my $timeout = $_next_event_time;
234  
235 801 my $now = monotime();
236 801 ▶ if (defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) { } 801 0 801
237 801 $timeout -= $now;
238 801 ▶ $timeout = 0 if $timeout < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < 0 416 385 801
239 }
240 else {
241 0 die "shouldn't happen" if ASSERT_DATA;
242 0 $timeout = 3600;
243 }
244  
245 801 if (TRACE_EVENTS) {
246 801 POE::Kernel::_warn(
247 '<ev> Kernel::run() iterating. ' .
248 sprintf(
249 "now(%.4f) timeout(%.4f) then(%.4f)\n",
250 $now-$start_time, $timeout, ($now-$start_time)+$timeout
251 )
252 );
253 }
254  
255 801 if (TRACE_FILES) {
256 801 foreach (sort { $a<=>$b} keys %poll_fd_masks) {
257 1975 my @types;
258 1722 ▶ push @types, "plain-file" if -f;
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ 279 1696 1975
259 1971 ▶ push @types, "directory" if -d;
Branch: 1 branch
BranchTrueFalseTotal
if -d $_ 0 1696 1696
260 1971 ▶ push @types, "symlink" if -l;
Branch: 1 branch
BranchTrueFalseTotal
if -l $_ 22 1953 1975
261 1831 ▶ push @types, "pipe" if -p;
Branch: 1 branch
BranchTrueFalseTotal
if -p $_ 94 1718 1812
262 1910 ▶ push @types, "socket" if -S;
Branch: 1 branch
BranchTrueFalseTotal
if -S $_ 191 1859 2050
263 1839 ▶ push @types, "block-special" if -b;
Branch: 1 branch
BranchTrueFalseTotal
if -b $_ 591 1816 2407
264 2287 ▶ push @types, "character-special" if -c;
Branch: 1 branch
BranchTrueFalseTotal
if -c $_ 366 1921 2287
265 1921 ▶ push @types, "tty" if -t;
Branch: 1 branch
BranchTrueFalseTotal
if -t STDIN 146 1775 1921
266 1921 my @modes;
267 1865 my $flags = $poll_fd_masks{$_};
268 1842 ▶ push @modes, 'r' if $flags & (POLLRDNORM | POLLHUP | POLLERR);
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 88 1678 235 1913
269 1898 ▶ push @modes, 'w' if $flags & (POLLWRNORM | POLLHUP | POLLERR);
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 280 190 1723 1913
270 1776 ▶ push @modes, 'x' if $flags & (POLLRDBAND | POLLHUP | POLLERR);
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 152 113 1718 1831
271 1753 POE::Kernel::_warn(
272 "<fh> file descriptor $_ = modes(@modes) types(@types)\n"
273 );
274 }
275 }
276  
277 # Avoid looking at filehandles if we don't need to.
278 # TODO The added code to make this sleep is non-optimal. There is a
279 # way to do this in fewer tests.
280  
281 724 ▶ if (scalar keys %poll_fd_masks) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar keys %poll_fd_masks) { } 541 101 642
elsif ($timeout) { } 0 120 120
282  
283 # There are filehandles to poll, so do so.
284  
285 # Check filehandles, or wait for a period of time to elapse.
286 537 my $hits = IO::Poll::_poll($timeout * 1000, my @results = %poll_fd_masks);
287  
288 537 if (ASSERT_FILES) {
289 657 ▶ if ($hits < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hits < 0) 6 516 522
290 126 ▶ POE::Kernel::_trap("<fh> poll returned $hits (error): $!")
Branch: 1 branch
BranchTrueFalseTotal
unless $! == 115 or $! == 11 or $! == 4 or $! == 0 0 6 6
Condition66%6/9
$! == 115 or $! == 11 or $! == 4 or $! == 0
l!l&&r!l&&!r
13023
$! == 115 or $! == 11 or $! == 4
l!l&&r!l&&!r
23130
$! == 115 or $! == 11
l!l&&r!l&&!r
23013
MC/DC0%0/4
$! == 115 or $! == 11 or $! == 4 or $! == 0
$! == 115 $! == 11 $! == 4 $! == 0
Truth table20%1/5
A or B or C or D
A= $! == 115B= $! == 11C= $! == 4D= $! == 0
ABCDresult
1XXX1
01XX1
001X1
00011
00000
291 unless ( ($! == EINPROGRESS) or
292 ($! == EWOULDBLOCK) or
293 ($! == EINTR) or
294 ($! == 0) # SIGNAL_PIPE strangeness
295 );
296 }
297 }
298  
299 642 if (TRACE_FILES) {
300 522 ▶ if ($hits > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hits > 0) { } 202 320 522
elsif ($hits == 0) { } 314 6 320
301 221 POE::Kernel::_warn "<fh> poll hits = $hits\n";
302 }
303 elsif ($hits == 0) {
304 314 POE::Kernel::_warn "<fh> poll timed out...\n";
305 }
306 }
307  
308 # If poll has seen filehandle activity, then gather up the
309 # active filehandles and synchronously dispatch events to the
310 # appropriate handlers.
311  
312 782 ▶ if ($hits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hits > 0) 202 320 522
313  
314 # This is where they're gathered.
315  
316 202 my (@rd_ready, @wr_ready, @ex_ready);
317 202 my %poll_fd_results = @results;
318 202 ▶ while (my ($fd, $got_mask) = each %poll_fd_results) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %poll_fd_results) 1284 202 1486
319 1284 ▶ next unless $got_mask;
Branch: 1 branch
BranchTrueFalseTotal
unless $got_mask 875 409 1284
320  
321 409 my $watch_mask = $poll_fd_masks{$fd};
322 409 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($watch_mask & 64 and $got_mask & 120) 220 189 409
Condition100%3/3
$watch_mask & 64 and $got_mask & 120
!ll&&!rl&&r
108137366
MC/DC100%2/2
$watch_mask & 64 and $got_mask & 120
$watch_mask & 64 $got_mask & 120
Truth table100%3/3
A and B
A= $watch_mask & 64B= $got_mask & 120
ABresult
0X0
100
111
323 $watch_mask & POLLRDNORM and
324 $got_mask & (POLLRDNORM | POLLHUP | POLLERR | POLLNVAL)
325 ) {
326 220 if (TRACE_FILES) {
327 220 POE::Kernel::_warn "<fh> enqueuing read for fileno $fd";
328 }
329  
330 220 push @rd_ready, $fd;
331 }
332  
333 409 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($watch_mask & 256 and $got_mask & 312) 190 219 409
Condition66%2/3
$watch_mask & 256 and $got_mask & 312
!ll&&!rl&&r
3640247
MC/DC50%1/2
$watch_mask & 256 and $got_mask & 312
$watch_mask & 256 $got_mask & 312
Truth table66%2/3
A and B
A= $watch_mask & 256B= $got_mask & 312
ABresult
0X0
100
111
334 $watch_mask & POLLWRNORM and
335 $got_mask & (POLLWRNORM | POLLHUP | POLLERR | POLLNVAL)
336 ) {
337 190 if (TRACE_FILES) {
338 190 POE::Kernel::_warn "<fh> enqueuing write for fileno $fd";
339 }
340  
341 190 push @wr_ready, $fd;
342 }
343  
344 409 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($watch_mask & 128 and $got_mask & 184) 0 409 409
Condition33%1/3
$watch_mask & 128 and $got_mask & 184
!ll&&!rl&&r
61100
MC/DC0%0/2
$watch_mask & 128 and $got_mask & 184
$watch_mask & 128 $got_mask & 184
Truth table33%1/3
A and B
A= $watch_mask & 128B= $got_mask & 184
ABresult
0X0
100
111
345 $watch_mask & POLLRDBAND and
346 $got_mask & (POLLRDBAND | POLLHUP | POLLERR | POLLNVAL)
347 ) {
348 0 if (TRACE_FILES) {
349 0 POE::Kernel::_warn "<fh> enqueuing expedite for fileno $fd";
350 }
351  
352 0 push @ex_ready, $fd;
353 }
354 }
355  
356 202 ▶ @rd_ready and $self->_data_handle_enqueue_ready(MODE_RD, @rd_ready);
Branch: 1 branch
BranchTrueFalseTotal
@rd_ready and $self->_data_handle_enqueue_ready(0, @rd_ready) 155 47 202
357 202 ▶ @wr_ready and $self->_data_handle_enqueue_ready(MODE_WR, @wr_ready);
Branch: 1 branch
BranchTrueFalseTotal
@wr_ready and $self->_data_handle_enqueue_ready(1, @wr_ready) 83 119 202
358 202 ▶ @ex_ready and $self->_data_handle_enqueue_ready(MODE_EX, @ex_ready);
Branch: 1 branch
BranchTrueFalseTotal
@ex_ready and $self->_data_handle_enqueue_ready(2, @ex_ready) 0 202 202
359 }
360 }
361 elsif ($timeout) {
362  
363 # No filehandles to poll on. Try to sleep instead. Use sleep()
364 # itself on MSWin32. Use a dummy four-argument select() everywhere
365 # else.
366  
367 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
368 0 sleep($timeout);
369 }
370 else {
371 0 CORE::select(undef, undef, undef, $timeout);
372 }
373 }
374  
375 # Dispatch whatever events are due.
376 522 $self->_data_ev_dispatch_due();
377 }
378  
379 ### Run for as long as there are sessions to service.
380  
381 74 ▶sub loop_run {
Subroutine loop_run: called CC=2 SCAR=6.9
Pod undocumented
382 74 my $self = shift;
383 74 ▶ while ($self->_data_ses_count()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_data_ses_count) 801 74 875
384 801 $self->loop_do_timeslice();
385 }
386 }
387  
388 74 ▶sub loop_halt {
Subroutine loop_halt: called CC=1 SCAR=0.0
Pod undocumented
389 # does nothing
390 }
391  
392 54 1;
393  
394 __END__
« blib/lib/POE/NFA.pm ↑ Summary blib/lib/POE/Session.pm »