Lambda/Loop/Select.pm

filter: Statementstmt 91.3% 138 / 151 Branchbran 69.7% 60 / 86 Conditioncond 75.0% 24 / 32 MC/DCmcdc 57.8% 11 / 19 Subroutinesub 100% 13 / 13 Podpod 87.5% 7 / 8 totaltotal 81.8% 253 / 309 CC 60 SCAR 37.3
CC
60
Cov
83%
yield
68.5
rebuild_vectors
7.2
remove
5.0
CRAP 41.7
« Lambda/HTTP/Server.pm ↑ Summary Lambda/Throttle.pm »
1 # $Id: Select.pm,v 1.18 2010/01/01 14:52:17 dk Exp $
2
3 package IO::Lambda::Loop::Select;
4 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶use Errno qw(EINTR EAGAIN);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use IO::Lambda qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 28 IO::Lambda::Loop::default('Select');
11
12 28 ▶our $DEBUG = $IO::Lambda::DEBUG{select} || 0;
Condition50%1/2
$IO::Lambda::DEBUG{'select'} || 0
l!l
028
MC/DC0%0/1
$IO::Lambda::DEBUG{'select'} || 0
$IO::Lambda::DEBUG{'select'}
Truth table50%1/2
A
A= $IO::Lambda::DEBUG{'select'}
Aresult
11
00
13
14 # IO::Select::select doesn't distinguish between select returning 0 and -1, don't have
15 # time to fix that. I'll just use a plain select instead, it'll be faster also.
16
17 sub new
18 27 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod documented
19 27 my $self = bless {} , shift;
20 27 $self-> {$_} = '' for qw(read write exc);
21 27 $self-> {items} = {};
22 27 $self-> {timers} = [];
23 27 return $self;
24 }
25
26 sub empty
27 856555 ▶{
Subroutine empty: called CC=2 SCAR=6.9
Pod documented
28 856555 my $self = shift;
29 return (
30 856555 @{$self->{timers}} +
31 856555 ▶ keys(%{$self-> {items}})
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'timers'};} + keys(%{$$self{'items'};}) ? : 346 856209 856555
32 ) ? 0 : 1;
33 }
34
35 sub yield
36 173 ▶{
Subroutine yield: called CC=42 SCAR=42.3
Pod documented
37 173 my ( $self, $nonblocking ) = @_;
38
39 173 ▶ return if $self-> empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->empty 0 173 173
40
41 173 my $t;
42 173 ▶ $t = 0 if $nonblocking;
Branch: 1 branch
BranchTrueFalseTotal
if $nonblocking 0 173 173
43
44 173 my ($min,$max) = ( 0, -1);
45 173 my $ct = time;
46
47 # timers
48 173 for ( @{$self-> {timers}}) {
49 133 ▶ $t = $_->[WATCH_DEADLINE]
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->[2] and !defined($t) || $t > $_->[2] 107 26 133
Condition83%5/6
defined $_->[2] and !defined($t) || $t > $_->[2]
!ll&&!rl&&r
026107
not defined $t or $t > $_->[2]
l!l&&r!l&&!r
911626
MC/DC66%2/3
defined $_->[2] and !defined($t) || $t > $_->[2]
defined $_->[2] not defined $t $t > $_->[2]
Truth table75%3/4
A and not(B) or C
A= defined $_->[2]B= not defined $tC= $t > $_->[2]
ABCresult
0XX0
1000
11X1
1011
50 if defined $_->[WATCH_DEADLINE] and
51 (!defined($t) or $t > $_-> [WATCH_DEADLINE]);
52 }
53
54 # handles
55 173 my ( $R, $W, $E) = @{$self}{qw(read write exc)};
56
57 173 ▶ while ( my ( $fileno, $bucket) = each %{ $self-> {items}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"items"};}) 155 173 328
58 155 for ( @$bucket) {
59 155 ▶ $t = $_->[WATCH_DEADLINE]
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->[2] and !defined($t) || $t > $_->[2] 24 131 155
Condition100%6/6
defined $_->[2] and !defined($t) || $t > $_->[2]
!ll&&!rl&&r
126524
not defined $t or $t > $_->[2]
l!l&&r!l&&!r
1865
MC/DC100%3/3
defined $_->[2] and !defined($t) || $t > $_->[2]
defined $_->[2] not defined $t $t > $_->[2]
Truth table100%4/4
A and not(B) or C
A= defined $_->[2]B= not defined $tC= $t > $_->[2]
ABCresult
0XX0
1000
11X1
1011
60 if defined $_->[WATCH_DEADLINE] and
61 (!defined($t) or $t > $_-> [WATCH_DEADLINE]);
62 }
63 155 ▶ warn "select: fileno $fileno\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Loop::Select::DEBUG 0 155 155
64 155 ▶ $max = $fileno if $max < $fileno;
Branch: 1 branch
BranchTrueFalseTotal
if $max < $fileno 122 33 155
65 155 ▶ $min = $fileno if !defined($min) or $min > $fileno;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $min or $min > $fileno 0 155 155
Condition33%1/3
not defined $min or $min > $fileno
l!l&&r!l&&!r
00155
MC/DC0%0/2
not defined $min or $min > $fileno
not defined $min $min > $fileno
Truth table33%1/3
not(A) or B
A= not defined $minB= $min > $fileno
ABresult
1X1
011
000
66 }
67 173 ▶ if ( defined $t) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $t) { } 109 64 173
elsif ($IO::Lambda::Loop::Select::DEBUG) { } 0 64 64
68 109 $t -= $ct;
69 109 ▶ $t = 0 if $t < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $t < 0 41 68 109
70 109 ▶ warn "select: timeout=$t\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Loop::Select::DEBUG 0 109 109
71 } elsif ( $DEBUG) {
72 0 warn "select: no timeout\n";
73 }
74
75 # do select
76 173 my $n = select( $R, $W, $E, $t);
77 173 ▶ warn "select: $n handles ready\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Loop::Select::DEBUG 0 173 173
78 173 ▶ if ( $n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 2 171 173
79 2 ▶ if ( $! == EINTR or $! == EAGAIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 4 or $! == 11) { } 2 0 2
Condition33%1/3
$! == 4 or $! == 11
l!l&&r!l&&!r
200
MC/DC0%0/2
$! == 4 or $! == 11
$! == 4 $! == 11
Truth table33%1/3
A or B
A= $! == 4B= $! == 11
ABresult
1X1
011
000
80 # ignore
81 2 ▶ warn "select: $!\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Loop::Select::DEBUG 0 2 2
82 } else {
83 # find out the rogue handles
84 0 ▶ if ( $DEBUG > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Lambda::Loop::Select::DEBUG > 1) 0 0 0
85 0 my $h = $R | $W | $E;
86 0 ▶ for ( my $i = 0; $i < length($h); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $h) 0 0 0
87 0 my $v = '';
88 0 ▶ for ( my $j = 0; $j < 8; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 8) 0 0 0
89 0 my $fd = $i * 8 + $j;
90 0 ▶ next unless vec($h,$fd,1);
Branch: 1 branch
BranchTrueFalseTotal
unless vec $h, $fd, 1 0 0 0
91 0 vec($v,$fd,1) = 1;
92 0 ▶ next if select($v,$v,$v,0) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if select($v, $v, $v, 0) >= 0 0 0 0
93 0 warn "select: bad handle #$fd\n";
94 }
95 }
96 }
97 0 die "select() error:$!:$^E";
98 }
99 }
100
101 # expired timers
102 173 my ( @kill, @expired);
103
104 173 $t = $self-> {timers};
105 @$t = grep {
106 173 ▶ ($$_[WATCH_DEADLINE] <= $ct) ? do {
Branch: 1 branch
BranchTrueFalseTotal
$_->[2] <= $ct ? : 47 86 133
107 47 push @expired, $_;
108 47 0;
109 } : 1;
110 } @$t;
111
112 # handles
113 173 ▶ if ( $n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) { } 89 84 173
114 # process selected handles
115 89 ▶ for ( my $i = $min; $i <= $max && $n > 0; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $max and $n > 0) 1095 89 1184
Condition100%3/3
$i <= $max and $n > 0
!ll&&!rl&&r
8271095
MC/DC100%2/2
$i <= $max and $n > 0
$i <= $max $n > 0
Truth table100%3/3
A and B
A= $i <= $maxB= $n > 0
ABresult
0X0
100
111
116 1095 my $what =
117 vec( $R, $i, 1) * IO_READ +
118 vec( $W, $i, 1) * IO_WRITE +
119 vec( $E, $i, 1) * IO_EXCEPTION
120 ;
121 1095 ▶ next unless $what;
Branch: 1 branch
BranchTrueFalseTotal
unless $what 975 120 1095
122
123 120 my $bucket = $self-> {items}-> {$i};
124 @$bucket = grep {
125 120 ▶ ($$_[WATCH_IO_FLAGS] & $what) ? do {
Branch: 1 branch
BranchTrueFalseTotal
$_->[5] & $what ? : 120 0 120
126 120 $$_[WATCH_IO_FLAGS] &= $what;
127 120 push @expired, $_;
128 120 0;
129 } : 1;
130 } @$bucket;
131 120 ▶ delete $self-> {items}->{$i} unless @$bucket;
Branch: 1 branch
BranchTrueFalseTotal
unless @$bucket 120 0 120
132 120 $n--;
133 }
134 } else {
135 # else process timeouts
136 84 my @kill;
137 84 ▶ while ( my ( $fileno, $bucket) = each %{ $self-> {items}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"items"};}) 9 84 93
138 @$bucket = grep {
139 9 (
140 defined($_->[WATCH_DEADLINE]) &&
141 $_->[WATCH_DEADLINE] <= $ct
142 9 ▶ ) ? do {
Branch: 1 branch
BranchTrueFalseTotal
defined $_->[2] && $_->[2] <= $ct ? : 1 8 9
Condition100%3/3
defined $_->[2] and $_->[2] <= $ct
!ll&&!rl&&r
711
MC/DC100%2/2
defined $_->[2] and $_->[2] <= $ct
defined $_->[2] $_->[2] <= $ct
Truth table100%3/3
A and B
A= defined $_->[2]B= $_->[2] <= $ct
ABresult
0X0
100
111
143 1 $$_[WATCH_IO_FLAGS] = 0;
144 1 push @expired, $_;
145 1 0;
146 } : 1;
147 } @$bucket;
148 9 ▶ push @kill, $fileno unless @$bucket;
Branch: 1 branch
BranchTrueFalseTotal
unless @$bucket 1 8 9
149 }
150 84 delete @{$self->{items}}{@kill};
151 }
152 173 $self-> rebuild_vectors;
153
154 # call them
155 173 $$_[WATCH_OBJ]-> io_handler( $_) for @expired;
156 }
157
158 sub watch
159 127 ▶{
Subroutine watch: called CC=5 SCAR=16.1
Pod documented
160 127 my ( $self, $rec) = @_;
161 127 my $fileno = fileno $rec->[WATCH_IO_HANDLE];
162 127 ▶ die "Invalid filehandle" unless defined $fileno;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fileno 0 127 127
163 127 my $flags = $rec->[WATCH_IO_FLAGS];
164
165 127 ▶ vec($self-> {read}, $fileno, 1) = 1 if $flags & IO_READ;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 4 115 12 127
166 127 ▶ vec($self-> {write}, $fileno, 1) = 1 if $flags & IO_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 2 12 115 127
167 127 ▶ vec($self-> {exc}, $fileno, 1) = 1 if $flags & IO_EXCEPTION;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 1 1 126 127
168
169 127 push @{$self-> {items}-> {$fileno}}, $rec;
170 }
171
172 sub after
173 58 ▶{
Subroutine after: called CC=1 SCAR=0.0
Pod documented
174 58 my ( $self, $rec) = @_;
175 58 push @{$self-> {timers}}, $rec;
176 }
177
178 sub remove
179 44 ▶{
Subroutine remove: called CC=5 SCAR=16.1
Pod documented
180 44 my ($self, $obj) = @_;
181
182 44 @{$self-> {timers}} = grep {
183 7 ▶ defined($_->[WATCH_OBJ]) and $_->[WATCH_OBJ] != $obj
Condition66%2/3
defined $_->[0] && $_->[0] != $obj
!ll&&!rl&&r
052
MC/DC50%1/2
defined $_->[0] && $_->[0] != $obj
defined $_->[0] $_->[0] != $obj
Truth table66%2/3
A && B
A= defined $_->[0]B= $_->[0] != $obj
ABresult
0X0
100
111
184 44 } @{$self-> {timers}};
185
186 44 my @kill;
187 44 ▶ while ( my ( $fileno, $bucket) = each %{$self->{items}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"items"};}) 25 44 69
188 25 ▶ @$bucket = grep { defined($_->[WATCH_OBJ]) and $_->[WATCH_OBJ] != $obj } @$bucket;
Condition66%2/3
defined $_->[0] && $_->[0] != $obj
!ll&&!rl&&r
0520
MC/DC50%1/2
defined $_->[0] && $_->[0] != $obj
defined $_->[0] $_->[0] != $obj
Truth table66%2/3
A && B
A= defined $_->[0]B= $_->[0] != $obj
ABresult
0X0
100
111
189 25 ▶ next if @$bucket;
Branch: 1 branch
BranchTrueFalseTotal
if @$bucket 20 5 25
190 5 push @kill, $fileno;
191 }
192 44 delete @{$self->{items}}{@kill};
193
194 44 $self-> rebuild_vectors;
195 }
196
197 sub remove_event
198 10 ▶{
Subroutine remove_event: called CC=3 SCAR=11.0
Pod documented
199 10 my ($self, $rec) = @_;
200
201 10 @{$self-> {timers}} = grep { $_ != $rec } @{$self-> {timers}};
202
203 10 my @kill;
204 10 ▶ while ( my ( $fileno, $bucket) = each %{$self->{items}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"items"};}) 2 10 12
205 2 @$bucket = grep { $_ != $rec } @$bucket;
206 2 ▶ next if @$bucket;
Branch: 1 branch
BranchTrueFalseTotal
if @$bucket 2 0 2
207 0 push @kill, $fileno;
208 }
209 10 delete @{$self->{items}}{@kill};
210
211 10 $self-> rebuild_vectors;
212
213 }
214
215 sub rebuild_vectors
216 227 ▶{
Subroutine rebuild_vectors: called CC=7 SCAR=19.7
Pod undocumented
217 227 my $self = $_[0];
218 227 $self-> {$_} = '' for qw(read write exc);
219 227 my $r = \ $self-> {read};
220 227 my $w = \ $self-> {write};
221 227 my $e = \ $self-> {exc};
222 227 ▶ while ( my ( $fileno, $bucket) = each %{$self->{items}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"items"};}) 56 227 283
223 56 for my $flags ( map { $_-> [WATCH_IO_FLAGS] } @$bucket) {
224 56 ▶ vec($$r, $fileno, 1) = 1 if $flags & IO_READ;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 4 56 0 56
225 56 ▶ vec($$w, $fileno, 1) = 1 if $flags & IO_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 2 0 56 56
226 56 ▶ vec($$e, $fileno, 1) = 1 if $flags & IO_EXCEPTION;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 1 0 56 56
227 }
228 }
229 }
230
231 28 1;
232
233 __DATA__
« Lambda/HTTP/Server.pm ↑ Summary Lambda/Throttle.pm »