Lambda/Loop/Select.pm

filter: Statementstmt 91.3% 138 / 151 Branchbran 68.8% 62 / 90 Conditioncond 76.9% 20 / 26 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 100% 13 / 13 Podpod 87.5% 7 / 8 totaltotal 81.5% 247 / 303 CC 60 SCAR 37.3
CC
60
Cov
82%
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 {
19 27 ▶ my $self = bless {} , shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
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 {
28 867555 ▶ my $self = shift;
Subroutine empty: called CC=2 SCAR=6.9
Pod documented
29 return (
30 867555 @{$self->{timers}} +
31 867555 ▶ keys(%{$self-> {items}})
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'timers'};} + keys(%{$$self{'items'};}) ? : 356 867199 867555
32 ) ? 0 : 1;
33 }
34
35 sub yield
36 {
37 178 ▶ my ( $self, $nonblocking ) = @_;
Subroutine yield: called CC=42 SCAR=42.3
Pod documented
38
39 178 ▶ return if $self-> empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->empty 0 178 178
40
41 178 my $t;
42 178 ▶ $t = 0 if $nonblocking;
Branch: 1 branch
BranchTrueFalseTotal
if $nonblocking 0 178 178
43
44 178 my ($min,$max) = ( 0, -1);
45 178 my $ct = time;
46
47 # timers
48 178 for ( @{$self-> {timers}}) {
49 132 ▶ $t = $_->[WATCH_DEADLINE]
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->[2] and !defined($t) || $t > $_->[2] 106 26 132
Condition83%5/6
defined $_->[2] and !defined($t) || $t > $_->[2]
!ll&&!rl&&r
010032
not defined $t or $t > $_->[2]
l!l&&r!l&&!r
901626
MC/DC33%1/3
defined $_->[2] and !defined($t) || $t > $_->[2]
defined $_->[2] not defined $t $t > $_->[2]
Truth table50%2/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 178 my ( $R, $W, $E) = @{$self}{qw(read write exc)};
56
57 178 ▶ while ( my ( $fileno, $bucket) = each %{ $self-> {items}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"items"};}) 160 178 338
58 160 for ( @$bucket) {
59 160 ▶ $t = $_->[WATCH_DEADLINE]
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->[2] and !defined($t) || $t > $_->[2] 22 138 160
Condition100%6/6
defined $_->[2] and !defined($t) || $t > $_->[2]
!ll&&!rl&&r
1311712
not defined $t or $t > $_->[2]
l!l&&r!l&&!r
1847
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
60 if defined $_->[WATCH_DEADLINE] and
61 (!defined($t) or $t > $_-> [WATCH_DEADLINE]);
62 }
63 160 ▶ warn "select: fileno $fileno\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Loop::Select::DEBUG 0 160 160
64 160 ▶ $max = $fileno if $max < $fileno;
Branch: 1 branch
BranchTrueFalseTotal
if $max < $fileno 122 38 160
65 160 ▶ $min = $fileno if !defined($min) or $min > $fileno;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $min or $min > $fileno 0 160 160
Condition33%1/3
not defined $min or $min > $fileno
l!l&&r!l&&!r
00160
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 178 ▶ if ( defined $t) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $t) { } 108 70 178
elsif ($IO::Lambda::Loop::Select::DEBUG) { } 0 70 70
68 108 $t -= $ct;
69 108 ▶ $t = 0 if $t < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $t < 0 41 67 108
70 108 ▶ warn "select: timeout=$t\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Loop::Select::DEBUG 0 108 108
71 } elsif ( $DEBUG) {
72 0 warn "select: no timeout\n";
73 }
74
75 # do select
76 178 my $n = select( $R, $W, $E, $t);
77 178 ▶ warn "select: $n handles ready\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Loop::Select::DEBUG 0 178 178
78 178 ▶ if ( $n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 1 177 178
79 1 ▶ if ( $! == EINTR or $! == EAGAIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 4 or $! == 11) { } 1 0 1
Condition33%1/3
$! == 4 or $! == 11
l!l&&r!l&&!r
100
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 1 ▶ warn "select: $!\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Loop::Select::DEBUG 0 1 1
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 178 my ( @kill, @expired);
103
104 178 $t = $self-> {timers};
105 @$t = grep {
106 178 ▶ ($$_[WATCH_DEADLINE] <= $ct) ? do {
Branch: 1 branch
BranchTrueFalseTotal
$_->[2] <= $ct ? : 47 85 132
107 47 push @expired, $_;
108 47 0;
109 } : 1;
110 } @$t;
111
112 # handles
113 178 ▶ if ( $n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) { } 95 83 178
114 # process selected handles
115 95 ▶ for ( my $i = $min; $i <= $max && $n > 0; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $max and $n > 0) 1179 95 1274
Condition100%3/3
$i <= $max and $n > 0
!ll&&!rl&&r
83121179
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 1179 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 1179 ▶ next unless $what;
Branch: 1 branch
BranchTrueFalseTotal
unless $what 1059 120 1179
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 83 my @kill;
137 83 ▶ while ( my ( $fileno, $bucket) = each %{ $self-> {items}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"items"};}) 8 83 91
138 @$bucket = grep {
139 8 (
140 defined($_->[WATCH_DEADLINE]) &&
141 $_->[WATCH_DEADLINE] <= $ct
142 8 ▶ ) ? do {
Branch: 1 branch
BranchTrueFalseTotal
defined $_->[2] && $_->[2] <= $ct ? : 1 7 8
Condition100%3/3
defined $_->[2] and $_->[2] <= $ct
!ll&&!rl&&r
611
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 8 ▶ push @kill, $fileno unless @$bucket;
Branch: 1 branch
BranchTrueFalseTotal
unless @$bucket 1 7 8
149 }
150 83 delete @{$self->{items}}{@kill};
151 }
152 178 $self-> rebuild_vectors;
153
154 # call them
155 178 $$_[WATCH_OBJ]-> io_handler( $_) for @expired;
156 }
157
158 sub watch
159 {
160 127 ▶ my ( $self, $rec) = @_;
Subroutine watch: called CC=5 SCAR=16.1
Pod documented
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 {
174 58 ▶ my ( $self, $rec) = @_;
Subroutine after: called CC=1 SCAR=0.0
Pod documented
175 58 push @{$self-> {timers}}, $rec;
176 }
177
178 sub remove
179 {
180 46 ▶ my ($self, $obj) = @_;
Subroutine remove: called CC=5 SCAR=16.1
Pod documented
181
182 46 @{$self-> {timers}} = grep {
183 7 ▶ defined($_->[WATCH_OBJ]) and $_->[WATCH_OBJ] != $obj
Branch: 1 branch
BranchTrueFalseTotal
defined $_->[0] and $_->[0] != $obj 7 0 7
184 46 } @{$self-> {timers}};
185
186 46 my @kill;
187 46 ▶ while ( my ( $fileno, $bucket) = each %{$self->{items}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"items"};}) 27 46 73
188 27 ▶ @$bucket = grep { defined($_->[WATCH_OBJ]) and $_->[WATCH_OBJ] != $obj } @$bucket;
Branch: 1 branch
BranchTrueFalseTotal
defined $_->[0] and $_->[0] != $obj 27 0 27
189 27 ▶ next if @$bucket;
Branch: 1 branch
BranchTrueFalseTotal
if @$bucket 22 5 27
190 5 push @kill, $fileno;
191 }
192 46 delete @{$self->{items}}{@kill};
193
194 46 $self-> rebuild_vectors;
195 }
196
197 sub remove_event
198 {
199 10 ▶ my ($self, $rec) = @_;
Subroutine remove_event: called CC=3 SCAR=11.0
Pod documented
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 {
217 234 ▶ my $self = $_[0];
Subroutine rebuild_vectors: called CC=7 SCAR=19.7
Pod undocumented
218 234 $self-> {$_} = '' for qw(read write exc);
219 234 my $r = \ $self-> {read};
220 234 my $w = \ $self-> {write};
221 234 my $e = \ $self-> {exc};
222 234 ▶ while ( my ( $fileno, $bucket) = each %{$self->{items}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"items"};}) 63 234 297
223 63 for my $flags ( map { $_-> [WATCH_IO_FLAGS] } @$bucket) {
224 63 ▶ vec($$r, $fileno, 1) = 1 if $flags & IO_READ;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 4 63 0 63
225 63 ▶ vec($$w, $fileno, 1) = 1 if $flags & IO_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 2 0 63 63
226 63 ▶ vec($$e, $fileno, 1) = 1 if $flags & IO_EXCEPTION;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 1 0 63 63
227 }
228 }
229 }
230
231 28 1;
232
233 __DATA__
« Lambda/HTTP/Server.pm ↑ Summary Lambda/Throttle.pm »