Lambda/Poll.pm

filter: Statementstmt 87.2% 89 / 102 Branchbran 61.1% 33 / 54 Conditioncond 40.0% 8 / 20 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 93.7% 15 / 16 Podpod 37.5% 3 / 8 totaltotal 71.5% 151 / 211 CC 36 SCAR 30.7
CC
36
Cov
74%
reset_timer
50.7
poll_cancel
12.0
yield
7.0
CRAP 21.5
« Lambda/Backtrace.pm ↑ Summary Lambda/Thread.pm »
1 # $Id: Poll.pm,v 1.7 2009/11/30 13:25:06 dk Exp $
2 package IO::Lambda::Poll;
3 3 use vars qw(
4 @ISA @EXPORT_OK %EXPORT_TAGS
5 $DEBUG @RECORDS @TIMER $TIMER_ACTIVE $MASTER
6 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 3 ▶$DEBUG = $IO::Lambda::DEBUG{poll} || 0;
Condition50%1/2
$IO::Lambda::DEBUG{'poll'} || 0
l!l
03
MC/DC0%0/1
$IO::Lambda::DEBUG{'poll'} || 0
$IO::Lambda::DEBUG{'poll'}
Truth table50%1/2
A
A= $IO::Lambda::DEBUG{'poll'}
Aresult
11
00
9 3 @ISA = qw(Exporter);
10 3 @EXPORT_OK = qw(poll_event poll_cancel poller);
11 3 %EXPORT_TAGS = ( all => \@EXPORT_OK);
12
13 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use IO::Lambda qw(:all :dev set_frame get_frame);
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 3 $MASTER = bless {}, __PACKAGE__;
19
20 # register yield handler
21 3 IO::Lambda::add_loop($MASTER);
22 END {
23 3 ▶ @RECORDS = ();
Subroutine END: called CC=1 SCAR=0.0
24 3 IO::Lambda::remove_loop($MASTER);
25 };
26
27 # There'll also be a single timer as we need timeouts
28 3 $TIMER[WATCH_OBJ] = bless {}, "IO::Lambda::Poll::Timer";
29 sub IO::Lambda::Poll::Timer::io_handler
30 {
31 2 ▶ warn "poll.timer < expired\n" if $DEBUG;
Subroutine io_handler: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Poll::DEBUG 0 2 2
32 2 $TIMER_ACTIVE = 0;
33 }
34
35 18 ▶sub empty { 0 == @RECORDS }
Subroutine empty: called CC=1 SCAR=0.0
Pod undocumented
36
37 sub remove
38 {
39 3 ▶ my $lambda = $_[1];
Subroutine remove: called CC=3 SCAR=11.0
Pod undocumented
40 3 my $n = @RECORDS;
41 3 @RECORDS = grep { $_-> {this} ne $lambda } @RECORDS;
42 3 ▶ return if $n == @RECORDS;
Branch: 1 branch
BranchTrueFalseTotal
if $n == @IO::Lambda::Poll::RECORDS 2 1 3
43 1 ▶ warn "poll.remove $lambda\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Poll::DEBUG 0 1 1
44 1 reset_timer();
45 }
46
47 sub yield
48 {
49 16 ▶ warn "poll.yield\n" if $DEBUG > 1;
Subroutine yield: called CC=7 SCAR=19.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Poll::DEBUG > 1 0 16 16
50 16 my $time = time;
51
52 16 my @new;
53 16 my @frame = get_frame;
54 16 for my $rec ( @RECORDS) {
55 my ( $ok, @result) = $rec-> {poller}-> (
56 defined($rec->{deadline}) && $rec->{deadline} <= $time,
57 16 ▶ @{ $rec-> {param}}
Condition100%3/3
defined $rec->{'deadline'} && $rec->{'deadline'} <= $time
!ll&&!rl&&r
4102
MC/DC100%2/2
defined $rec->{'deadline'} && $rec->{'deadline'} <= $time
defined $rec->{'deadline'} $rec->{'deadline'} <= $time
Truth table100%3/3
A && B
A= defined $rec->{'deadline'}B= $rec->{'deadline'} <= $time
ABresult
0X0
100
111
58 );
59 16 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 10 6 16
60 10 push @new, $rec;
61 10 next;
62 }
63 6 ▶ warn "poll.resolve($rec)\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Poll::DEBUG 0 6 6
64 6 my $this = $rec-> {this};
65 6 $this-> set_frame($rec-> {method}, $rec->{callback}, @{ $rec->{context} });
66 6 $this-> callout( $rec-> {callback}, @result);
67 6 $this-> resolve( $rec-> {bind});
68 }
69 16 set_frame(@frame);
70 16 ▶ return if @RECORDS == @new;
Branch: 1 branch
BranchTrueFalseTotal
if @IO::Lambda::Poll::RECORDS == @new 10 6 16
71
72 6 @RECORDS = @new;
73 6 reset_timer();
74 }
75
76 sub reset_timer
77 {
78 14 ▶ my ( $expires, $frequency);
Subroutine reset_timer: called CC=19 SCAR=39.3
Pod undocumented
79 14 for my $rec (@RECORDS) {
80 7 my ($f,$d) = @{$rec}{qw(frequency deadline)};
81 7 ▶ $frequency = $f if not defined($frequency) or (defined($f) and $frequency > $f);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $frequency or defined $f and $frequency > $f 7 0 7
Condition16%1/6
not defined $frequency or defined $f and $frequency > $f
l!l&&r!l&&!r
700
defined $f and $frequency > $f
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $frequency or defined $f and $frequency > $f
not defined $frequency defined $f $frequency > $f
Truth table25%1/4
not(A) or B and C
A= not defined $frequencyB= defined $fC= $frequency > $f
ABCresult
1XX1
0111
00X0
0100
82 7 ▶ $expires = $d if not defined($expires) or (defined($d) and $expires > $d);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $expires or defined $d and $expires > $d 7 0 7
Condition16%1/6
not defined $expires or defined $d and $expires > $d
l!l&&r!l&&!r
700
defined $d and $expires > $d
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $expires or defined $d and $expires > $d
not defined $expires defined $d $expires > $d
Truth table25%1/4
not(A) or B and C
A= not defined $expiresB= defined $dC= $expires > $d
ABCresult
1XX1
0111
00X0
0100
83 }
84
85 14 ▶ if ( defined $frequency) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $frequency) 4 10 14
86 4 $frequency += time;
87 4 ▶ if ( defined $expires) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $expires) { } 4 0 4
elsif (@IO::Lambda::Poll::RECORDS) { } 0 0 0
88 4 ▶ $expires = $frequency if $expires > $frequency;
Branch: 1 branch
BranchTrueFalseTotal
if $expires > $frequency 2 2 4
89 } elsif ( @RECORDS) {
90 0 $expires = $frequency;
91 }
92 }
93
94 14 ▶ if ( defined $expires) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $expires) { } 4 10 14
elsif ($IO::Lambda::Poll::TIMER_ACTIVE) { } 2 8 10
95 4 ▶ if ( $TIMER_ACTIVE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Lambda::Poll::TIMER_ACTIVE) { } 0 4 4
96 0 ▶ if ( abs( $expires - $TIMER[WATCH_DEADLINE]) > 0.001) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $expires - $IO::Lambda::Poll::TIMER[2] > "0.001") 0 0 0
97 # restart the active timer
98 0 ▶ warn "poll.timer > restart $expires/$TIMER[WATCH_DEADLINE]\n"
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Poll::DEBUG 0 0 0
99 if $DEBUG;
100 0 $IO::Lambda::LOOP-> remove_event( \@TIMER);
101 0 $TIMER[WATCH_DEADLINE] = $expires;
102 0 $IO::Lambda::LOOP-> after( \@TIMER);
103 }
104 # else, same timeout, on already active timer - do nothing
105 } else {
106 # resubmit
107 4 ▶ warn "poll.timer > submit $expires\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Poll::DEBUG 0 4 4
108 4 $TIMER[WATCH_DEADLINE] = $expires;
109 4 $IO::Lambda::LOOP-> after( \@TIMER);
110 4 $TIMER_ACTIVE = 1;
111 }
112 } elsif ( $TIMER_ACTIVE) {
113 2 ▶ warn "poll.timer > stop\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Poll::DEBUG 0 2 2
114 # stop timer
115 2 $IO::Lambda::LOOP-> remove_event( \@TIMER);
116 2 $TIMER_ACTIVE = 0;
117 }
118 }
119
120 sub poll_event
121 {
122 7 ▶ my ( $cb, $method, $poller, $deadline, $frequency, @param ) = @_;
Subroutine poll_event: called CC=4 SCAR=14.0
Pod documented
123
124 7 ▶ $deadline += time if defined($deadline) and $deadline < 1_000_000_000;
Branch: 1 branch
BranchTrueFalseTotal
if defined $deadline and $deadline < 1000000000 4 3 7
Condition66%2/3
defined $deadline and $deadline < 1000000000
!ll&&!rl&&r
304
MC/DC50%1/2
defined $deadline and $deadline < 1000000000
defined $deadline $deadline < 1000000000
Truth table66%2/3
A and B
A= defined $deadlineB= $deadline < 1000000000
ABresult
0X0
100
111
125
126 7 push @RECORDS, {
127 this => this,
128 bind => this-> bind,
129 method => $method,
130 callback => $cb,
131 context => [ context ],
132 poller => $poller,
133 deadline => $deadline,
134 param => \@param,
135 frequency => $frequency,
136 };
137
138 7 reset_timer;
139 7 ▶ warn "poll.new($RECORDS[-1]) on ", this, "\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Poll::DEBUG 0 7 7
140
141 7 return $RECORDS[-1];
142 }
143
144 # don't call this, use lambda-> cancel_event( $record->{bind} )
145 sub poll_cancel
146 {
147 0 ▶ my $rec = shift;
Subroutine poll_cancel: not called CC=3 SCAR=24.8
Pod documented
148 0 my $n = @RECORDS;
149 0 @RECORDS = grep { $rec != $_ } @RECORDS;
150 0 ▶ return if $n == @RECORDS;
Branch: 1 branch
BranchTrueFalseTotal
if $n == @IO::Lambda::Poll::RECORDS 0 0 0
151 0 ▶ warn "poll.cancel($rec)\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Poll::DEBUG 0 0 0
152 0 reset_timer;
153 }
154
155 sub poll_handler
156 {
157 9 ▶ my ( $expired, $cb, @opt) = @_;
Subroutine poll_handler: called CC=3 SCAR=11.0
Pod undocumented
158 9 my @res = $cb->(@opt);
159 9 ▶ return 1,@res if $res[0];
Branch: 1 branch
BranchTrueFalseTotal
if $res[0] 3 6 9
160 6 ▶ return 1,0 if $expired;
Branch: 1 branch
BranchTrueFalseTotal
if $expired 1 5 6
161 5 return 0;
162 }
163
164 sub poller(&)
165 {
166 2 ▶ my $cb = _subname poller => shift;
Subroutine poller: called CC=1 SCAR=0.0
Pod documented
167
168 lambda {
169 5 ▶ my %opt = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
170 poll_event(
171 undef, undef, \&poll_handler,
172 exists($opt{timeout}) ? $opt{timeout} : $opt{deadline},
173 $opt{frequency},
174 5 ▶ $cb, %opt
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{'timeout'} ? : 2 3 5
175 );
176 }
177 2 }
178
179 3 1;
180
181 __DATA__
« Lambda/Backtrace.pm ↑ Summary Lambda/Thread.pm »