blib/lib/CPS/Governor/Deferred.pm

filter: Statementstmt 100% 36 / 36 Branchbran 100% 4 / 4 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 9 / 9 Podpod 100% 6 / 6 totaltotal 100% 68 / 68 CC 7 SCAR 6.6
CC
7
Cov
100%
again
3.0
new
2.0
is_pending
2.0
CRAP 1.9
« blib/lib/CPS/Functional.pm ↑ Summary blib/lib/CPS.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2009 -- leonerd@leonerd.org.uk
5  
6 package CPS::Governor::Deferred;
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use base qw( CPS::Governor );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '0.18';
14  
15 =head1 NAME
16  
17 C<CPS::Governor::Deferred> - iterate at some later point
18  
19 =head1 SYNOPSIS
20  
21 use CPS qw( gkforeach );
22 use CPS::Governor::Deferred;
23  
24 my $gov = CPS::Governor::Deferred->new;
25  
26 gkforeach( $gov, [ 1 .. 10 ],
27 sub {
28 my ( $item, $knext ) = @_;
29  
30 print "A$item ";
31 goto &$knext;
32 },
33 sub {},
34 );
35  
36 gkforeach( $gov, [ 1 .. 10 ],
37 sub {
38 my ( $item, $knext ) = @_;
39  
40 print "B$item ";
41 goto &$knext;
42 },
43 sub {},
44 );
45  
46 $gov->flush;
47  
48 =head1 DESCRIPTION
49  
50 This L<CPS::Governor> allows the functions using it to delay their iteration
51 until some later point when the containing program invokes it. This allows two
52 main advantages:
53  
54 =over 4
55  
56 =item *
57  
58 CPU-intensive operations may be split apart and mixed with other IO operations
59  
60 =item *
61  
62 Multiple control functions may be executed in pseudo-parallel, interleaving
63 iterations of each giving a kind of concurrency
64  
65 =back
66  
67 These are achieved by having the governor store a list of code references that
68 need to be invoked, rather than invoking them immediately. These references
69 can then be invoked later, perhaps by using an idle watcher in an event
70 framework.
71  
72 Because each code reference hasn't yet been invoked by the time the C<again>
73 method is called, the original caller is free to store more pending references
74 with the governor. This allows multiple control functions to be interleaved,
75 as in the C<A> and C<B> example above.
76  
77 =cut
78  
79 =head1 CONSTRUCTOR
80  
81 =cut
82  
83 =head2 $gov = CPS::Governor::Deferred->new( %args )
84  
85 Returns a new instance of a C<CPS::Governor::Deferred> object. Requires no
86 parameters but may take any of the following to adjust its default behaviour:
87  
88 =over 8
89  
90 =item defer_after => INT
91  
92 If given some positive number, C<$n> then the first C<$n-1> invocations of the
93 C<again> method will in fact be executed immediately. Thereafter they will be
94 enqueued in the normal mechanism. This gives the effect that longrunning loops
95 will be executed in batches of C<$n>.
96  
97 If not supplied then every invocation of C<again> will use the queueing
98 mechanism.
99  
100 =back
101  
102 =cut
103  
104 sub new
105 {
106 2 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
107 2 my %args = @_;
108  
109 2 my $self = $class->SUPER::new( %args );
110  
111 2 ▶ $self->{defer_after} = $args{defer_after} || 0;
Condition100%2/2
$args{'defer_after'} || 0
l!l
11
MC/DC100%1/1
$args{'defer_after'} || 0
$args{'defer_after'}
Truth table100%2/2
A
A= $args{'defer_after'}
Aresult
11
00
112  
113 2 return $self;
114 }
115  
116 sub again
117 {
118 11 ▶ my $self = shift;
Subroutine again: called CC=3 SCAR=11.0
Pod documented
119  
120 11 ▶ if( $self->{defer_after} and ++$self->{count} < $self->{defer_after} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"defer_after"} and ++$self->{"count"} < $self->{"defer_after"}) 4 7 11
Condition100%3/3
$self->{"defer_after"} and ++$self->{"count"} < $self->{"defer_after"}
!ll&&!rl&&r
524
MC/DC100%2/2
$self->{"defer_after"} and ++$self->{"count"} < $self->{"defer_after"}
$self->{"defer_after"} ++$self->{"count"} < $self->{"defer_after"}
Truth table100%3/3
A and B
A= $self->{"defer_after"}B= ++$self->{"count"} < $self->{"defer_after"}
ABresult
0X0
100
111
121 4 my $code = shift;
122 # args still in @_
123  
124 4 goto &$code;
125 }
126  
127 7 $self->later( @_ );
128 }
129  
130 sub later
131 {
132 7 ▶ my $self = shift;
Subroutine later: called CC=1 SCAR=0.0
Pod documented
133  
134 7 push @{ $self->{queue} }, [ @_ ];
135 }
136  
137 =head1 METHODS
138  
139 =cut
140  
141 =head2 $pending = $gov->is_pending
142  
143 Returns true if at least one code reference has been stored that hasn't yet
144 been invoked.
145  
146 =cut
147  
148 sub is_pending
149 {
150 9 ▶ my $self = shift;
Subroutine is_pending: called CC=2 SCAR=6.9
Pod documented
151  
152 9 ▶ return $self->{queue} && @{ $self->{queue} } > 0;
Condition100%3/3
$self->{'queue'} && @{$self->{'queue'};} > 0
!ll&&!rl&&r
144
MC/DC100%2/2
$self->{'queue'} && @{$self->{'queue'};} > 0
$self->{'queue'} @{$self->{'queue'};} > 0
Truth table100%3/3
A && B
A= $self->{'queue'}B= @{$self->{'queue'};} > 0
ABresult
0X0
100
111
153 }
154  
155 =head2 $gov->prod
156  
157 Invokes all of the currently-stored code references, in the order they were
158 stored. If any new references are stored by these, they will not yet be
159 invoked, but will be available for the next time this method is called.
160  
161 =cut
162  
163 sub prod
164 {
165 6 ▶ my $self = shift;
Subroutine prod: called CC=2 SCAR=6.9
Pod documented
166  
167 6 $self->{count} = 0;
168  
169 6 my $queue = $self->{queue};
170 6 $self->{queue} = [];
171  
172 6 foreach my $item ( @$queue ) {
173 6 my ( $code, @args ) = @$item;
174 6 $code->( @args );
175 }
176 }
177  
178 =head2 $gov->flush
179  
180 Repeatedly calls C<prod> until no more code references are pending.
181  
182 =cut
183  
184 sub flush
185 {
186 1 ▶ my $self = shift;
Subroutine flush: called CC=2 SCAR=6.9
Pod documented
187  
188 1 ▶ $self->prod while $self->is_pending;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_pending and do { $self->prod } 2 1 3
189 }
190  
191 =head1 SUBCLASS METHODS
192  
193 The following methods are used internally to implement the functionality,
194 which may be useful to implementors of subclasses.
195  
196 =cut
197  
198 =head2 $gov->later( $code, @args )
199  
200 Used to enqueue the C<$code> ref to be invoked later with the given C<@args>,
201 once it is determined this should be deferred (rather than being invoked
202 immediately in the case of the first few invocations when C<defer_after> is
203 set).
204  
205 =cut
206  
207 =head1 AUTHOR
208  
209 Paul Evans <leonerd@leonerd.org.uk>
210  
211 =cut
212  
213 2 0x55AA;
« blib/lib/CPS/Functional.pm ↑ Summary blib/lib/CPS.pm »