blib/lib/Future/Queue.pm

filter: Statementstmt 100% 78 / 78 Branchbran 88.2% 30 / 34 Conditioncond 83.3% 20 / 24 MC/DCmcdc 69.2% 9 / 13 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 92.8% 155 / 167 CC 27 SCAR 13.6
CC
27
Cov
94%
_manage_push_waiters
6.0
push
6.0
shift_atmost
4.1
CRAP 3.9
↑ Summary
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, 2019-2024 -- leonerd@leonerd.org.uk
5  
6 package Future::Queue 0.52;
7  
8 6 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 C<Future::Queue> - a FIFO queue of values that uses L<Future>s
16  
17 =head1 SYNOPSIS
18  
19 use Future::Queue;
20 use Future::AsyncAwait;
21  
22 my $queue = Future::Queue->new;
23  
24 async sub process_queue
25 {
26 while(1) {
27 my $thing = await $queue->shift;
28 ...
29 }
30 }
31  
32 my $f = process_queue();
33 $queue->push( "a thing" );
34  
35 =head1 DESCRIPTION
36  
37 Objects in this class provide a simple FIFO queue the stores arbitrary perl
38 values. Values may be added into the queue using the L</push> method, and
39 retrieved from it using the L</shift> method.
40  
41 Values may be stored within the queue object for C<shift> to retrieve later,
42 or if the queue is empty then the future that C<shift> returns will be
43 completed once an item becomes available.
44  
45 =cut
46  
47 =head1 CONSTRUCTOR
48  
49 =cut
50  
51 =head2 new
52  
53 $queue = Future::Queue->new( %params );
54  
55 Returns a new C<Future::Queue> instance.
56  
57 Takes the following named arguments:
58  
59 =over 4
60  
61 =item max_items => INT
62  
63 I<Since version 0.50.>
64  
65 Optional. If defined, there can be at most the given number of items in the
66 queue. Attempts to call L</push> beyond that will yield a future that remains
67 pending, until a subsequent L</shift> operation makes enough space.
68  
69 =item prototype => STRING or OBJECT or CODE
70  
71 I<Since verison 0.51.>
72  
73 Optional. If defined, gives either a class name, an object instance to clone
74 or a code reference to invoke when a new pending C<Future> instance is needed
75 by the C<shift> or C<push> methods when they cannot complete immediately.
76  
77 $f = $prototype->(); # if CODE reference
78 $f = $prototype->new; # otherwise
79  
80 If not provided, a default of C<Future> will be used.
81  
82 =back
83  
84 =cut
85  
86 sub new
87 15 ▶{
Subroutine new: called CC=3 SCAR=11.0
Pod documented
88 15 my $class = shift;
89 15 my %params = @_;
90  
91 15 my $prototype = $params{prototype};
92  
93 return bless {
94 items => [],
95 max_items => $params{max_items},
96 15 ▶ shift_waiters => [],
Branch: 1 branch
BranchTrueFalseTotal
ref $prototype eq 'CODE' ? : 1 14 15
Condition100%2/2
$prototype // "Future"
l!l
212
MC/DC100%1/1
$prototype // "Future"
$prototype
Truth table100%2/2
A
A= $prototype
Aresult
11
00
97 ( ref $prototype eq "CODE" ) ?
98 ( f_factory => $prototype ) :
99 ( f_prototype => $prototype // "Future" ),
100 }, $class;
101 }
102  
103 =head2 push
104  
105 $queue->push( @items );
106  
107 await $queue->push( @items );
108  
109 Adds more items into the queue. If the queue was previously empty and there is
110 at least one C<shift> future waiting, then the next one will be completed by
111 this method.
112  
113 I<Since version 0.50> this can take multiple items; earlier versions can only
114 take one value at once.
115  
116 This method always returns a L<Future> instance. If C<max_items> is defined
117 then it is possible that this future will be in a still-pending state;
118 indicating that there was not yet space in the queue to add the items. It will
119 become completed once enough L</shift> calls have been made to make space for
120 them.
121  
122 If C<max_items> is not defined then these instances will always be immediately
123 complete; it is safe to drop or ignore it, or call the method in void context.
124  
125 If the queue has been finished then more items cannot be pushed and an
126 exception will be thrown.
127  
128 =cut
129  
130 sub _manage_shift_waiters
131 53 ▶{
Subroutine _manage_shift_waiters: called CC=3 SCAR=11.0
132 53 my $self = shift;
133  
134 53 my $items = $self->{items};
135 53 my $shift_waiters = $self->{shift_waiters};
136  
137 53 ▶ ( shift @$shift_waiters )->()
Branch: 1 branch
BranchTrueFalseTotal
@$shift_waiters and @$items and do { CORE::shift(@$shift_waiters)->() } 9 53 62
Condition100%3/3
@$shift_waiters and @$items
!ll&&!rl&&r
5129
MC/DC100%2/2
@$shift_waiters and @$items
@$shift_waiters @$items
Truth table100%3/3
A and B
A= @$shift_waitersB= @$items
ABresult
0X0
100
111
138 while @$shift_waiters and @$items;
139 }
140  
141 39 ▶sub push :method
Subroutine push: called CC=6 SCAR=17.9
Pod documented
142 {
143 39 my $self = shift;
144 39 my @more = @_;
145  
146 $self->{finished} and
147 39 ▶ croak "Cannot ->push more items to a Future::Queue that has been finished";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"finished"} and croak("Cannot ->push more items to a Future::Queue that has been finished") 1 38 39
148  
149 38 my $items = $self->{items};
150 38 my $max = $self->{max_items};
151  
152 38 ▶ if( defined $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max) { } 24 14 38
153 24 my $count = $max - @$items;
154 24 push @$items, splice @more, 0, $count;
155 }
156 else {
157 14 push @$items, @more;
158 14 @more = ();
159 }
160  
161 38 $self->_manage_shift_waiters;
162 38 ▶ return Future->done if !@more;
Branch: 1 branch
BranchTrueFalseTotal
unless @more 24 14 38
163  
164 14 ▶ my $f = $self->{f_factory} ? $self->{f_factory}->() : $self->{f_prototype}->new;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'f_factory'} ? : 0 14 14
165 14 ▶ push @{ $self->{push_waiters} //= [] }, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$self->{"push_waiters"} //= []
l!l
113
MC/DC100%1/1
$self->{"push_waiters"} //= []
$self->{"push_waiters"}
Truth table100%2/2
A
A= $self->{"push_waiters"}
Aresult
11
00
166 15 my $count = $max - @$items;
167 15 push @$items, splice @more, 0, $count;
168 15 $self->_manage_shift_waiters;
169  
170 15 ▶ return 0 if @more;
Branch: 1 branch
BranchTrueFalseTotal
if @more 1 14 15
171  
172 14 $f->done;
173 14 return 1;
174 14 };
175 14 return $f;
176 }
177  
178 =head2 shift
179  
180 $item = await $queue->shift;
181  
182 Returns a C<Future> that will yield the next item from the queue. If there is
183 already an item then this will be taken and the returned future will be
184 immediate. If not, then the returned future will be pending, and the next
185 C<push> method will complete it.
186  
187 If the queue has been finished then the future will yield an empty list, or
188 C<undef> in scalar context.
189  
190 If C<undef> is a valid item in your queue, make sure to test this condition
191 carefully. For example:
192  
193 while( ( my $item ) = await $queue->shift ) {
194 ...
195 }
196  
197 Here, the C<await> expression and the assignment are in list context, so the
198 loop will continue to iterate while I<any> value is assigned, even if that
199 value is C<undef>. The loop will only stop once no items are returned,
200 indicating the end of the queue.
201  
202 =cut
203  
204 sub _manage_push_waiters
205 28 ▶{
Subroutine _manage_push_waiters: called CC=6 SCAR=17.9
206 28 my $self = shift;
207  
208 28 my $items = $self->{items};
209 28 my $max_items = $self->{max_items};
210 28 ▶ my $push_waiters = $self->{push_waiters} || [];
Condition100%2/2
$self->{'push_waiters'} || []
l!l
1216
MC/DC100%1/1
$self->{'push_waiters'} || []
$self->{'push_waiters'}
Truth table100%2/2
A
A= $self->{'push_waiters'}
Aresult
11
00
211  
212 28 ▶ shift @$push_waiters
Branch: 1 branch
BranchTrueFalseTotal
@$push_waiters and !defined($max_items) || @$items < $max_items and $push_waiters->[0]->() and do { CORE::shift @$push_waiters } 14 28 42
Condition88%8/9
@$push_waiters and !defined($max_items) || @$items < $max_items and $push_waiters->[0]->()
!ll&&!rl&&r
27114
@$push_waiters and !defined($max_items) || @$items < $max_items
!ll&&!rl&&r
22515
not defined $max_items or @$items < $max_items
l!l&&r!l&&!r
0155
MC/DC75%3/4
@$push_waiters and !defined($max_items) || @$items < $max_items and $push_waiters->[0]->()
@$push_waiters not defined $max_items @$items < $max_items $push_waiters->[0]->()
Truth table66%4/6
A and not(B) or C and D
A= @$push_waitersB= not defined $max_itemsC= @$items < $max_itemsD= $push_waiters->[0]->()
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
213 while @$push_waiters and
214 ( !defined $max_items or @$items < $max_items )
215 and $push_waiters->[0]->();
216 }
217  
218 18 ▶sub shift :method
Subroutine shift: called CC=4 SCAR=13.9
Pod documented
219 {
220 18 my $self = shift;
221  
222 18 my $items = $self->{items};
223  
224 18 ▶ if( @$items ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$items) 8 10 18
225 8 my @more = shift @$items;
226 8 $self->_manage_push_waiters;
227 8 return Future->done( @more );
228 }
229  
230 10 ▶ return Future->done if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 1 9 10
231  
232 9 ▶ my $f = $self->{f_factory} ? $self->{f_factory}->() : $self->{f_prototype}->new;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'f_factory'} ? : 1 8 9
233 9 ▶ push @{ $self->{shift_waiters} }, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
234 6 ▶ return $f->done if !@$items and $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if not @$items and $self->{"finished"} 1 5 6
Condition66%2/3
not @$items and $self->{"finished"}
!ll&&!rl&&r
501
MC/DC50%1/2
not @$items and $self->{"finished"}
not @$items $self->{"finished"}
Truth table66%2/3
not(A) and B
A= not @$itemsB= $self->{"finished"}
ABresult
0X0
100
111
235 5 $f->done( shift @$items );
236 5 $self->_manage_push_waiters;
237 9 };
238 9 return $f;
239 }
240  
241 =head2 shift_atmost
242  
243 @items = await $queue->shift_atmost( $count );
244  
245 I<Since version 0.50.>
246  
247 A bulk version of L</shift> that can return multiple items at once.
248  
249 Returns a C<Future> that will yield the next few items from the queue. If
250 there is already at least one item in the queue then up to C<$count> items
251 will be taken, and the returned future will be immediate. If not, then the
252 returned future will be pending and the next C<push> method will complete it.
253  
254 =cut
255  
256 sub shift_atmost
257 16 ▶{
Subroutine shift_atmost: called CC=4 SCAR=14.1
Pod documented
258 16 my $self = shift;
259 16 my ( $count ) = @_;
260  
261 16 my $items = $self->{items};
262  
263 16 ▶ if( @$items ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$items) 11 5 16
264 11 my @more = splice @$items, 0, $count;
265 11 $self->_manage_push_waiters;
266 11 return Future->done( @more );
267 }
268  
269 5 ▶ return Future->done if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 0 5 5
270  
271 5 ▶ my $f = $self->{f_factory} ? $self->{f_factory}->() : $self->{f_prototype}->new;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'f_factory'} ? : 0 5 5
272 5 ▶ push @{ $self->{shift_waiters} }, sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
273 4 ▶ return $f->done if !@$items and $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if not @$items and $self->{"finished"} 0 4 4
Condition33%1/3
not @$items and $self->{"finished"}
!ll&&!rl&&r
400
MC/DC0%0/2
not @$items and $self->{"finished"}
not @$items $self->{"finished"}
Truth table33%1/3
not(A) and B
A= not @$itemsB= $self->{"finished"}
ABresult
0X0
100
111
274 4 $f->done( splice @$items, 0, $count );
275 4 $self->_manage_push_waiters;
276 5 };
277 5 return $f;
278 }
279  
280 =head2 finish
281  
282 $queue->finish;
283  
284 I<Since version 0.50.>
285  
286 Marks that the queue is now finished. Once the current list of items has been
287 exhausted, any further attempts to C<shift> more will yield empty.
288  
289 =cut
290  
291 sub finish
292 2 ▶{
Subroutine finish: called CC=2 SCAR=6.9
Pod documented
293 2 my $self = shift;
294 2 $self->{finished}++;
295  
296 2 ▶ ( shift @{ $self->{shift_waiters} } )->() while @{ $self->{shift_waiters} };
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{"shift_waiters"};} and do { CORE::shift(@{$$self{"shift_waiters"};})->() } 1 2 3
297 }
298  
299 =head1 AUTHOR
300  
301 Paul Evans <leonerd@leonerd.org.uk>
302  
303 =cut
304  
305 6 0x55AA;
↑ Summary