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.367'; # 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 BEGIN {
39 54 ▶ if ($^O eq "MSWin32" and not $ENV{POE_DANTIC}) {
Subroutine BEGIN: called CC=3 SCAR=18.1
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(
50 POLLRDNORM POLLWRNORM POLLRDBAND POLLERR POLLHUP POLLNVAL
51 54 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # Many systems' IO::Poll don't define POLLRDNORM.
54 # Usually upgrading IO::Poll helps.
55 BEGIN {
56 54 ▶ my $x = eval { POLLRDNORM };
Subroutine BEGIN: called CC=3 SCAR=15.7
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 sub loop_initialize {
76 98 ▶ my $self = shift;
Subroutine loop_initialize: called CC=1 SCAR=0.0
Pod undocumented
77 98 %poll_fd_masks = ();
78 }
79  
80 sub loop_finalize {
81 74 ▶ my $self = shift;
Subroutine loop_finalize: called CC=1 SCAR=0.0
Pod undocumented
82 74 $self->loop_ignore_all_signals();
83 }
84  
85 #------------------------------------------------------------------------------
86 # Signal handler maintenance functions.
87  
88 ▶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 sub loop_resume_time_watcher {
101 514 ▶ $_next_event_time = $_[1];
Subroutine loop_resume_time_watcher: called CC=1 SCAR=0.0
Pod undocumented
102 }
103  
104 sub loop_reset_time_watcher {
105 578 ▶ $_next_event_time = $_[1];
Subroutine loop_reset_time_watcher: called CC=1 SCAR=0.0
Pod undocumented
106 }
107  
108 sub loop_pause_time_watcher {
109 348 ▶ $_next_event_time = monotime() + 3600;
Subroutine loop_pause_time_watcher: called CC=1 SCAR=0.0
Pod undocumented
110 }
111  
112 # A static function; not some object method.
113  
114 sub mode_to_poll {
115 1476 ▶ return POLLRDNORM if $_[0] == MODE_RD;
Subroutine mode_to_poll: called CC=4 SCAR=17.9
Pod undocumented
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 sub loop_watch_filehandle {
125 492 ▶ my ($self, $handle, $mode) = @_;
Subroutine loop_watch_filehandle: called CC=2 SCAR=6.9
Pod undocumented
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 sub loop_ignore_filehandle {
146 378 ▶ my ($self, $handle, $mode) = @_;
Subroutine loop_ignore_filehandle: called CC=3 SCAR=11.0
Pod undocumented
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
267111
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) { } 8 370 378
164 220 $poll_fd_masks{$fileno} = $new;
165 }
166 else {
167 157 delete $poll_fd_masks{$fileno};
168 }
169 }
170  
171 sub loop_pause_filehandle {
172 335 ▶ my ($self, $handle, $mode) = @_;
Subroutine loop_pause_filehandle: called CC=3 SCAR=11.0
Pod undocumented
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 sub loop_resume_filehandle {
198 271 ▶ my ($self, $handle, $mode) = @_;
Subroutine loop_resume_filehandle: called CC=2 SCAR=6.9
Pod undocumented
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 sub loop_do_timeslice {
222 848 ▶ my $self = shift;
Subroutine loop_do_timeslice: called CC=37 SCAR=37.5
Pod undocumented
223  
224 # Check for a hung kernel.
225 848 $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 848 my $timeout = $_next_event_time;
234  
235 848 my $now = monotime();
236 848 ▶ if (defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) { } 848 0 848
237 848 $timeout -= $now;
238 848 ▶ $timeout = 0 if $timeout < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < 0 417 431 848
239 }
240 else {
241 0 die "shouldn't happen" if ASSERT_DATA;
242 0 $timeout = 3600;
243 }
244  
245 848 if (TRACE_EVENTS) {
246 848 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 848 if (TRACE_FILES) {
256 848 foreach (sort { $a<=>$b} keys %poll_fd_masks) {
257 2571 my @types;
258 2311 ▶ push @types, "plain-file" if -f;
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ 279 2292 2571
259 2567 ▶ push @types, "directory" if -d;
Branch: 1 branch
BranchTrueFalseTotal
if -d $_ 0 2292 2292
260 2567 ▶ push @types, "symlink" if -l;
Branch: 1 branch
BranchTrueFalseTotal
if -l $_ 15 2556 2571
261 2427 ▶ push @types, "pipe" if -p;
Branch: 1 branch
BranchTrueFalseTotal
if -p $_ 91 2307 2398
262 2503 ▶ push @types, "socket" if -S;
Branch: 1 branch
BranchTrueFalseTotal
if -S $_ 188 2455 2643
263 2435 ▶ push @types, "block-special" if -b;
Branch: 1 branch
BranchTrueFalseTotal
if -b $_ 591 2412 3003
264 2883 ▶ push @types, "character-special" if -c;
Branch: 1 branch
BranchTrueFalseTotal
if -c $_ 366 2517 2883
265 2517 ▶ push @types, "tty" if -t;
Branch: 1 branch
BranchTrueFalseTotal
if -t STDIN 146 2371 2517
266 2517 my @modes;
267 2461 my $flags = $poll_fd_masks{$_};
268 2438 ▶ push @modes, 'r' if $flags & (POLLRDNORM | POLLHUP | POLLERR);
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 88 2274 235 2509
269 2494 ▶ push @modes, 'w' if $flags & (POLLWRNORM | POLLHUP | POLLERR);
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 280 190 2319 2509
270 2372 ▶ push @modes, 'x' if $flags & (POLLRDBAND | POLLHUP | POLLERR);
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 152 113 2314 2427
271 2349 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 771 ▶ if (scalar keys %poll_fd_masks) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar keys %poll_fd_masks) { } 588 101 689
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 584 my $hits = IO::Poll::_poll($timeout * 1000, my @results = %poll_fd_masks);
287  
288 584 if (ASSERT_FILES) {
289 704 ▶ if ($hits < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hits < 0) 6 563 569
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
6023
$! == 115 or $! == 11 or $! == 4
l!l&&r!l&&!r
2360
$! == 115 or $! == 11
l!l&&r!l&&!r
2306
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 689 if (TRACE_FILES) {
300 569 ▶ if ($hits > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hits > 0) { } 255 314 569
elsif ($hits == 0) { } 308 6 314
301 274 POE::Kernel::_warn "<fh> poll hits = $hits\n";
302 }
303 elsif ($hits == 0) {
304 308 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 829 ▶ if ($hits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hits > 0) 255 314 569
313  
314 # This is where they're gathered.
315  
316 255 my (@rd_ready, @wr_ready, @ex_ready);
317 255 my %poll_fd_results = @results;
318 255 ▶ while (my ($fd, $got_mask) = each %poll_fd_results) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %poll_fd_results) 1907 255 2162
319 1907 ▶ next unless $got_mask;
Branch: 1 branch
BranchTrueFalseTotal
unless $got_mask 1443 464 1907
320  
321 464 my $watch_mask = $poll_fd_masks{$fd};
322 464 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($watch_mask & 64 and $got_mask & 120) 275 189 464
Condition100%3/3
$watch_mask & 64 and $got_mask & 120
!ll&&!rl&&r
108137421
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 275 if (TRACE_FILES) {
327 275 POE::Kernel::_warn "<fh> enqueuing read for fileno $fd";
328 }
329  
330 275 push @rd_ready, $fd;
331 }
332  
333 464 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($watch_mask & 256 and $got_mask & 312) 190 274 464
Condition66%2/3
$watch_mask & 256 and $got_mask & 312
!ll&&!rl&&r
4190247
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 464 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($watch_mask & 128 and $got_mask & 184) 0 464 464
Condition33%1/3
$watch_mask & 128 and $got_mask & 184
!ll&&!rl&&r
66600
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 255 ▶ @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) 208 47 255
357 255 ▶ @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) 81 174 255
358 255 ▶ @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 255 255
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 569 $self->_data_ev_dispatch_due();
377 }
378  
379 ### Run for as long as there are sessions to service.
380  
381 sub loop_run {
382 74 ▶ my $self = shift;
Subroutine loop_run: called CC=2 SCAR=6.9
Pod undocumented
383 74 ▶ while ($self->_data_ses_count()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_data_ses_count) 848 74 922
384 848 $self->loop_do_timeslice();
385 }
386 }
387  
388 ▶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 »