Example/Handoff/Marked.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 149 CC 2 SCAR 8.6
CC
2
Cov
0%
_take
6.0
new
2.0
enqueue
2.0
CRAP 2.4
1 ########################################################################
2 # housekeeping
3 ########################################################################
4 package LinkedList::Single::Example::Handoff::Marked v2.0.0;
5 use v5.40;
6  
7 use Carp qw( croak );
8  
9 use LinkedList::Single::Example::Lock;
10  
11 ########################################################################
12 # the array version that an experienced perl programmer writes when
13 # cancel has to be fast: the same array of [ $id, @data ], plus a hash
14 # id => item so that a handle finds its item without a search.
15 #
16 # an array cannot remove an item from the middle cheaply, so cancel
17 # does not: it empties the item in place, leaving a husk [ undef ]
18 # that stays in the array until a take or a process passes over it.
19 # the payload is let go at cancel; the husk is not.
20 #
21 # so cancel is O(1) here as well, and what the list is left with is
22 # the cost of the husks: memory kept until they are dequeued, and take
23 # having to step over them.
24 ########################################################################
25  
26 sub new( $class, %arg )
27 {
28 my $lock = $arg{ lock } // LinkedList::Single::Example::Lock->new;
29  
30 bless
31 {
32 lock => $lock
33 , queue => []
34 , item => {}
35 , live => 0
36 , serial => 0
37 , touched => 0
38 , purged => 0
39 }
40 => $class
41 }
42  
43 ########################################################################
44 # public interface: take the lock and do the work.
45 ########################################################################
46  
47 sub enqueue( $self, @data )
48 {
49 $self->{ lock }->hold( \&_enqueue, $self, @data )
50 }
51  
52 # as in LinkedList::Single::Example::Handoff, the old index leaves with the batch and is
53 # freed once the lock has been released.
54  
55 sub drain( $self )
56 {
57 my( $batch, $retired )
58 = $self->{ lock }->hold( \&_drain, $self )->@*;
59  
60 undef $retired;
61  
62 $batch
63 }
64  
65 sub take( $self, $k )
66 {
67 $self->{ lock }->hold( \&_take, $self, $k )
68 }
69  
70 sub cancel( $self, $id )
71 {
72 $self->{ lock }->hold( \&_cancel, $self, $id )
73 }
74  
75 sub requeue( $self, $batch )
76 {
77 left( $self, $batch )
78 or return 0;
79  
80 $self->{ lock }->hold( \&_requeue, $self, $batch )
81 }
82  
83 ########################################################################
84 # work on a batch: the lock is not held. a batch from drain still has
85 # the husks of cancelled items in it, which are skipped.
86 ########################################################################
87  
88 sub process( $self, $batch, $handler, $limit = undef )
89 {
90 my $done = 0;
91  
92 while
93 (
94 $batch->@*
95 && ( ! defined $limit || $done < $limit )
96 )
97 {
98 my $item = shift $batch->@*;
99  
100 defined $item->[ 0 ]
101 or next;
102  
103 $handler->( $item->@* );
104  
105 ++$done;
106 }
107  
108 $done
109 }
110  
111 sub left( $self, $batch )
112 {
113 scalar grep { defined $_->[ 0 ] } $batch->@*
114 }
115  
116 ########################################################################
117 # the work done while the lock is held
118 ########################################################################
119  
120 sub _enqueue( $self, @data )
121 {
122 my $id = ++$self->{ serial };
123 my $item = [ $id, @data ];
124  
125 push $self->{ queue }->@*, $item;
126  
127 $self->{ item }{ $id } = $item;
128  
129 ++$self->{ live };
130  
131 $id
132 }
133  
134 # swap the arrays and the index. the batch is the old array as it
135 # stands, husks and all; the old index goes back to be freed outside
136 # the lock.
137  
138 sub _drain( $self )
139 {
140 my $batch = $self->{ queue };
141 my $index = $self->{ item };
142  
143 $self->{ purged } += $self->{ live };
144  
145 $self->{ queue } = [];
146 $self->{ item } = {};
147 $self->{ live } = 0;
148  
149 [ $batch, $index ]
150 }
151  
152 # step over the front of the queue, husks included, until $k live
153 # items have been found; splice off what was passed and keep the
154 # live ones.
155  
156 sub _take( $self, $k )
157 {
158 my $queue = $self->{ queue };
159 my $end = 0;
160 my $found = 0;
161  
162 while( $end < $queue->@* && $found < $k )
163 {
164 ++$found
165 if defined $queue->[ $end ][ 0 ];
166  
167 ++$end;
168 }
169  
170 $self->{ touched } += $end;
171  
172 my $batch = [ grep { defined $_->[ 0 ] } splice $queue->@*, 0, $end ];
173  
174 delete $self->{ item }->@{ map { $_->[ 0 ] } $batch->@* };
175  
176 $self->{ live } -= $batch->@*;
177  
178 $batch
179 }
180  
181 sub _cancel( $self, $id )
182 {
183 my $item = delete $self->{ item }{ $id }
184 // return 0;
185  
186 # empty the item in place; the array keeps the husk.
187  
188 $item->@* = ( undef );
189  
190 --$self->{ live };
191  
192 1
193 }
194  
195 sub _requeue( $self, $batch )
196 {
197 my @live = grep { defined $_->[ 0 ] } $batch->@*;
198  
199 unshift $self->{ queue }->@*, @live;
200  
201 $self->{ item }->@{ map { $_->[ 0 ] } @live } = @live;
202  
203 $self->{ live } += @live;
204 $self->{ touched } += @live;
205  
206 $batch->@* = ();
207  
208 scalar @live
209 }
210  
211 ########################################################################
212 # introspection
213 ########################################################################
214  
215 sub contents( $self )
216 {
217 [ map { [ $_->@* ] } grep { defined $_->[ 0 ] } $self->{ queue }->@* ]
218 }
219  
220 sub size( $self )
221 {
222 $self->{ live }
223 }
224  
225 sub lock( $self )
226 {
227 $self->{ lock }
228 }
229  
230 sub walked( $self )
231 {
232 $self->{ touched }
233 }
234  
235 sub purged( $self )
236 {
237 $self->{ purged }
238 }
239  
240 1
241 __END__