Example/Handoff.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 193 CC 3 SCAR 9.7
CC
3
Cov
0%
left
6.0
_cancel
6.0
new
2.0
CRAP 2.6
1 ########################################################################
2 # housekeeping
3 ########################################################################
4 package LinkedList::Single::Example::Handoff v2.0.0;
5 use v5.40;
6  
7 use Carp qw( croak );
8  
9 use LinkedList::Single qw( v2 );
10  
11 use LinkedList::Single::Example::Lock;
12  
13 ########################################################################
14 # the queue is a list that begins with a permanent anchor node:
15 #
16 # anchor -> item 1 -> item 2 -> ... -> last item -> sentinel
17 #
18 # every item is a node [ $id, @data ].
19 #
20 # three things are kept beside the list, all of them cursors:
21 #
22 # anchor the first node, never removed. it gives the first item a
23 # predecessor, which is what the handles need.
24 # last the last node (the anchor when the queue is empty). the
25 # sentinel after it is where the next item is pushed, so
26 # enqueue never walks the list.
27 # pred id => cursor on the node *before* that item. a singly
28 # linked list can only remove a node from its predecessor,
29 # so this is what makes cancel O(1).
30 #
31 # the cursors are only ever used to step off of ('next' returns a new
32 # cursor), never advanced, so one cursor can safely be stored in two
33 # places.
34 #
35 # which operations may touch a node that a stored cursor stands on is
36 # the whole difficulty: 'drop' (remove the node after) and 'push'
37 # leave every other node where it is (so does the pointer append_to),
38 # but 'fragment' moves the *contents* of the node it cuts at into
39 # another node, so a cursor that stood on that node is left looking at
40 # the wrong data. take uses it, and requeue puts items back whose
41 # predecessors are new, so both finish by re-walking the few nodes whose
42 # predecessors may have changed (_reindex).
43 ########################################################################
44  
45 sub new( $class, %arg )
46 {
47 my $lock = $arg{ lock } // LinkedList::Single::Example::Lock->new;
48 my $list = LinkedList::Single->new;
49  
50 # an empty list: push fills the sentinel in place.
51  
52 $list->push( 0 );
53  
54 my $anchor = $list->cursor;
55  
56 bless
57 {
58 lock => $lock
59 , list => $list
60 , anchor => $anchor
61 , last => $anchor
62 , pred => {}
63 , serial => 0
64 , size => 0
65  
66 # statistics: nodes this module's code stepped over, and
67 # index entries thrown away, while the lock was held.
68  
69 , walked => 0
70 , purged => 0
71 }
72 => $class
73 }
74  
75 ########################################################################
76 # public interface: take the lock and do the work.
77 ########################################################################
78  
79 # queue one item; returns its handle (an id).
80  
81 sub enqueue( $self, @data )
82 {
83 $self->{ lock }->hold( \&_enqueue, $self, @data )
84 }
85  
86 # the whole queue as one batch, in O(1) list operations.
87 #
88 # the lock-held code hands back the old index along with the batch,
89 # the way the array hands back its old array, and it is let go here
90 # once the lock has been released. it has to be gone before the batch
91 # is processed: its cursors stand on the batch's nodes, and would keep
92 # each one (and its payload) alive after processing had finished with it.
93  
94 sub drain( $self )
95 {
96 my( $batch, $retired )
97 = $self->{ lock }->hold( \&_drain, $self )->@*;
98  
99 undef $retired;
100  
101 $batch
102 }
103  
104 # at most $k items from the front.
105  
106 sub take( $self, $k )
107 {
108 my( $batch, $retired )
109 = $self->{ lock }->hold( \&_take, $self, $k )->@*;
110  
111 # only set when the whole queue went (see _drain).
112  
113 undef $retired;
114  
115 $batch
116 }
117  
118 # remove one queued item by handle. true if it was still queued.
119  
120 sub cancel( $self, $id )
121 {
122 $self->{ lock }->hold( \&_cancel, $self, $id )
123 }
124  
125 # put what is left of a batch back at the front of the queue.
126 # returns the number of items put back.
127  
128 sub requeue( $self, $batch )
129 {
130 # a fully consumed batch has nothing to give back.
131  
132 $batch
133 or return 0;
134  
135 $self->{ lock }->hold( \&_requeue, $self, $batch )
136 }
137  
138 ########################################################################
139 # work on a batch: the lock is NOT held. the batch belongs to the
140 # caller, nothing else can see it.
141 #
142 # the handler gets ( $id, @data ) for each item. stopping after $limit
143 # items leaves the rest in the batch for requeue. each item is let go
144 # as the batch advances past it.
145 ########################################################################
146  
147 sub process( $self, $batch, $handler, $limit = undef )
148 {
149 $batch
150 or return 0;
151  
152 my $done = 0;
153  
154 # consume ends quietly on die "\n", and puts the item it was
155 # on back to the caller as the head of what is left. any other
156 # exception is carped and also stops the batch there.
157  
158 $batch->consume
159 (
160 sub( @item )
161 {
162 defined $limit
163 && $done >= $limit
164 and die "\n";
165  
166 $handler->( @item );
167  
168 ++$done;
169 }
170 );
171  
172 $done
173 }
174  
175 # items left in a batch.
176  
177 sub left( $self, $batch )
178 {
179 $batch
180 ? $batch->count
181 : 0
182 }
183  
184 ########################################################################
185 # the work done while the lock is held
186 ########################################################################
187  
188 sub _enqueue( $self, @data )
189 {
190 my $id = ++$self->{ serial };
191 my $last = $self->{ last };
192  
193 # the node after $last is the sentinel: push fills it and hangs
194 # a new sentinel off the end. the old last node is the new
195 # item's predecessor, and the filled node is the new last.
196  
197 $last->next->push( $id, @data );
198  
199 $self->{ pred }{ $id } = $last;
200 $self->{ last } = $last->next;
201  
202 ++$self->{ size };
203  
204 $id
205 }
206  
207 ########################################################################
208 # the entire queue goes in one fragment. nothing is walked.
209 #
210 # the fragment takes the old sentinel with it, and the node after the
211 # anchor is left as a fresh, empty one for the next enqueue to fill.
212 #
213 # the index is swapped out, not cleared: every handle in it points into
214 # the batch, so it leaves with the batch and the caller frees it after
215 # the lock is released. freeing it is proportional to the queue (a
216 # cursor object per item), and an array that keeps an index for cancel
217 # has exactly the same bill. 'purged' counts the entries retired.
218 ########################################################################
219  
220 sub _drain( $self )
221 {
222 my $anchor = $self->{ anchor };
223 my $batch = $anchor->next->fragment;
224 my $index = $self->{ pred };
225  
226 $self->{ purged } += $self->{ size };
227  
228 $self->{ pred } = {};
229 $self->{ last } = $anchor;
230 $self->{ size } = 0;
231  
232 [ $batch, $index ]
233 }
234  
235 ########################################################################
236 # the first $k items. walking to the end of them is the O(k) part;
237 # the cut itself is two fragments:
238 #
239 # anchor -> a1 .. ak -> r1 -> r2 ...
240 #
241 # $rest = r1 and everything after it (r1 becomes a sentinel)
242 # $batch = a1 .. ak, ending at that sentinel
243 #
244 # and the rest goes back after the anchor, into the empty node that
245 # a1 left behind. the contents of a1 and r1 have moved, so the
246 # predecessors of r1 and r2 are re-recorded.
247 ########################################################################
248  
249 sub _take( $self, $k )
250 {
251 $k > 0
252 or return [ LinkedList::Single::Fragment->new ];
253  
254 my $anchor = $self->{ anchor };
255 my $cut = $anchor->next;
256 my @id = ();
257  
258 while( $cut && @id < $k )
259 {
260 push @id, ( $cut->data )[ 0 ];
261  
262 $cut->advance;
263 }
264  
265 $self->{ walked } += @id;
266  
267 # ran off the end: that is the whole queue.
268  
269 $cut
270 or return _drain( $self );
271  
272 my $rest = $cut->fragment;
273 my $batch = $anchor->next->fragment;
274  
275 $rest->push_onto( $anchor->next );
276  
277 # the taken items are no longer cancellable.
278  
279 delete $self->{ pred }->@{ @id };
280  
281 $self->{ size } -= @id;
282  
283 _reindex( $self, 2 );
284  
285 [ $batch ]
286 }
287  
288 ########################################################################
289 # remove one item from the middle of the queue: O(1).
290 #
291 # $pred stands on the node before the item; drop unlinks the node
292 # after it by pointer, which moves nothing else. the item after the
293 # one removed now follows $pred, so its handle is re-pointed.
294 ########################################################################
295  
296 sub _cancel( $self, $id )
297 {
298 my $pred = delete $self->{ pred }{ $id }
299 // return 0;
300  
301 # next( 2 ) and not next->next: the intermediate cursor of the
302 # latter would be kept by the Cursor class (the stale whence slot, see the POD) with the node about
303 # to be dropped, and the payload would outlive the cancel. the
304 # target always exists, so two steps land on the sentinel at worst
305 # and never past it (the bounds fix for next( $n ) does not apply).
306  
307 my $after = $pred->next( 2 );
308  
309 if( $after )
310 {
311 $self->{ pred }{ ( $after->data )[ 0 ] } = $pred;
312 }
313 else
314 {
315 # the item was the last one: its predecessor is now.
316  
317 $self->{ last } = $pred;
318 }
319  
320 # void context: the data is not wanted, and goes with the node.
321  
322 $pred->drop;
323  
324 --$self->{ size };
325  
326 1
327 }
328  
329 ########################################################################
330 # the unprocessed rest of a batch goes back to the front. append_to
331 # puts it after the anchor and the old queue after it, without copying
332 # a node; it walks the batch once to find its end. every item put back
333 # gets a handle again, and so does the old front of the queue (and the
334 # one behind it), since its predecessor is now the end of the batch.
335 ########################################################################
336  
337 sub _requeue( $self, $batch )
338 {
339 my $count = $batch->count;
340  
341 $batch->append_to( $self->{ anchor } );
342  
343 $self->{ size } += $count;
344 $self->{ walked } += $count;
345  
346 _reindex( $self, $count + 2 );
347  
348 $count
349 }
350  
351 ########################################################################
352 # record the predecessor of each of the first $count items, walking
353 # from the anchor, and if that reaches the end of the queue, the last
354 # node too. nothing past the window is touched.
355 ########################################################################
356  
357 sub _reindex( $self, $count )
358 {
359 my $pred = $self->{ anchor };
360 my $item = $pred->next;
361  
362 for( 1 .. $count )
363 {
364 $item
365 or last;
366  
367 $self->{ pred }{ ( $item->data )[ 0 ] } = $pred;
368  
369 ++$self->{ walked };
370  
371 $pred = $item;
372 $item = $pred->next;
373 }
374  
375 # no item left to look at: $pred is the last node.
376  
377 $item
378 or $self->{ last } = $pred;
379  
380 return
381 }
382  
383 ########################################################################
384 # introspection
385 ########################################################################
386  
387 # arrayref of [ $id, @data ], oldest first.
388  
389 sub contents( $self )
390 {
391 my @item
392 = ( ( scalar $self->{ list }->extract ) // [] )->@*;
393  
394 # the anchor is not an item.
395  
396 shift @item;
397  
398 \@item
399 }
400  
401 sub size( $self )
402 {
403 $self->{ size }
404 }
405  
406 sub lock( $self )
407 {
408 $self->{ lock }
409 }
410  
411 sub walked( $self )
412 {
413 $self->{ walked }
414 }
415  
416 sub purged( $self )
417 {
418 $self->{ purged }
419 }
420  
421 1
422 __END__