| 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
MC/DC0%0/1
$IO::Lambda::DEBUG{'poll'} || 0
$IO::Lambda::DEBUG{'poll'}
Truth table50%1/2
A
A= $IO::Lambda::DEBUG{'poll'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | warn "poll.remove $lambda\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 16 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 10 | push @new, $rec; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 10 | next; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 6 | ▶ | warn "poll.resolve($rec)\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition16%1/6
not defined $frequency or defined $f and $frequency > $f
defined $f and $frequency > $f
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
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 7 | ▶ | $expires = $d if not defined($expires) or (defined($d) and $expires > $d); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
not defined $expires or defined $d and $expires > $d
defined $d and $expires > $d
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
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 14 | ▶ | if ( defined $frequency) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | $frequency += time; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | ▶ | if ( defined $expires) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 4 | ▶ | $expires = $frequency if $expires > $frequency; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | } elsif ( @RECORDS) { | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $expires = $frequency; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 14 | ▶ | if ( defined $expires) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | ▶ | if ( $TIMER_ACTIVE) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ( abs( $expires - $TIMER[WATCH_DEADLINE]) > 0.001) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | # restart the active timer | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | warn "poll.timer > restart $expires/$TIMER[WATCH_DEADLINE]\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $deadline and $deadline < 1000000000
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | warn "poll.cancel($rec)\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 6 | ▶ | return 1,0 if $expired; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 2 | } | |||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | __DATA__ | ||||||||||||||||||||||||||||||||||||||||||