| 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
Condition100%2/2
$prototype // "Future"
MC/DC100%1/1
$prototype // "Future"
$prototype
Truth table100%2/2
A
A= $prototype
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
@$shift_waiters and @$items
MC/DC100%2/2
@$shift_waiters and @$items
@$shift_waiters
@$items
Truth table100%3/3
A and B
A= @$shift_waitersB= @$items
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 38 | my $items = $self->{items}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 38 | my $max = $self->{max_items}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 38 | ▶ | if( defined $max ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 14 | ▶ | my $f = $self->{f_factory} ? $self->{f_factory}->() : $self->{f_prototype}->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 14 | ▶ | push @{ $self->{push_waiters} //= [] }, sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$self->{"push_waiters"} //= []
MC/DC100%1/1
$self->{"push_waiters"} //= []
$self->{"push_waiters"}
Truth table100%2/2
A
A= $self->{"push_waiters"}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'} || []
MC/DC100%1/1
$self->{'push_waiters'} || []
$self->{'push_waiters'}
Truth table100%2/2
A
A= $self->{'push_waiters'}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 28 | ▶ | shift @$push_waiters | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
@$push_waiters and !defined($max_items) || @$items < $max_items and $push_waiters->[0]->()
@$push_waiters and !defined($max_items) || @$items < $max_items
not defined $max_items or @$items < $max_items
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]->()
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 9 | ▶ | my $f = $self->{f_factory} ? $self->{f_factory}->() : $self->{f_prototype}->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not @$items and $self->{"finished"}
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"}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 5 | ▶ | my $f = $self->{f_factory} ? $self->{f_factory}->() : $self->{f_prototype}->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
not @$items and $self->{"finished"}
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"}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | =head1 AUTHOR | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | Paul Evans <leonerd@leonerd.org.uk> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 6 | 0x55AA; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||