Example/Handoff/Array.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 136 CC 2 SCAR 8.6
CC
2
Cov
0%
_drain
6.0
new
2.0
enqueue
2.0
CRAP 2.4
1 ########################################################################
2 # housekeeping
3 ########################################################################
4 package LinkedList::Single::Example::Handoff::Array v2.0.0;
5 use v5.40;
6  
7 use Carp qw( croak );
8  
9 use LinkedList::Single::Example::Lock;
10  
11 ########################################################################
12 # the queue is an array of [ $id, @data ]. a handle is the id; an
13 # array has no way to go from an id to a position except by looking,
14 # so cancel searches.
15 #
16 # drain has two forms, chosen by new( drain => ... ):
17 #
18 # swap ( $batch, $queue ) = ( $queue, [] )
19 # O(1) under the lock, and as cheap as the list's. this is
20 # what anyone who knows perl would write.
21 # copy copy the items out and clear the queue: O(n) under the
22 # lock. what a careless version does, and the version that
23 # many languages without reference swaps are stuck with.
24 #
25 # 'touched' counts the elements this module's code examines or copies
26 # one by one. what splice, unshift and the swap do inside perl is not
27 # counted: a memmove of the queue is cheap per element, but it is not
28 # free, and the timings are where that shows.
29 ########################################################################
30  
31 sub new( $class, %arg )
32 {
33 my $how = $arg{ drain } // 'swap';
34 my $lock = $arg{ lock } // LinkedList::Single::Example::Lock->new;
35  
36 $how =~ m{ \A (?: swap | copy ) \z }x
37 or croak "Botched new: drain must be 'swap' or 'copy', not '$how'";
38  
39 bless
40 {
41 lock => $lock
42 , queue => []
43 , drain => $how
44 , serial => 0
45 , touched => 0
46 }
47 => $class
48 }
49  
50 ########################################################################
51 # public interface: take the lock and do the work.
52 ########################################################################
53  
54 sub enqueue( $self, @data )
55 {
56 $self->{ lock }->hold( \&_enqueue, $self, @data )
57 }
58  
59 sub drain( $self )
60 {
61 $self->{ lock }->hold( \&_drain, $self )
62 }
63  
64 sub take( $self, $k )
65 {
66 $self->{ lock }->hold( \&_take, $self, $k )
67 }
68  
69 sub cancel( $self, $id )
70 {
71 $self->{ lock }->hold( \&_cancel, $self, $id )
72 }
73  
74 sub requeue( $self, $batch )
75 {
76 $batch->@*
77 or return 0;
78  
79 $self->{ lock }->hold( \&_requeue, $self, $batch )
80 }
81  
82 ########################################################################
83 # work on a batch: the lock is not held.
84 ########################################################################
85  
86 sub process( $self, $batch, $handler, $limit = undef )
87 {
88 my $done = 0;
89  
90 while
91 (
92 $batch->@*
93 && ( ! defined $limit || $done < $limit )
94 )
95 {
96 my $item = shift $batch->@*;
97  
98 $handler->( $item->@* );
99  
100 ++$done;
101 }
102  
103 $done
104 }
105  
106 sub left( $self, $batch )
107 {
108 scalar $batch->@*
109 }
110  
111 ########################################################################
112 # the work done while the lock is held
113 ########################################################################
114  
115 sub _enqueue( $self, @data )
116 {
117 my $id = ++$self->{ serial };
118  
119 push $self->{ queue }->@*, [ $id, @data ];
120  
121 $id
122 }
123  
124 sub _drain( $self )
125 {
126 my $queue = $self->{ queue };
127  
128 if( $self->{ drain } eq 'swap' )
129 {
130 $self->{ queue } = [];
131  
132 return $queue;
133 }
134  
135 # copy: every element is handled, and the emptied array keeps
136 # the capacity it grew to.
137  
138 my $batch = [ $queue->@* ];
139  
140 $self->{ touched } += $queue->@*;
141  
142 $queue->@* = ();
143  
144 $batch
145 }
146  
147 sub _take( $self, $k )
148 {
149 my $queue = $self->{ queue };
150 my $batch = [ splice $queue->@*, 0, $k ];
151  
152 $self->{ touched } += $batch->@*;
153  
154 $batch
155 }
156  
157 sub _cancel( $self, $id )
158 {
159 my $queue = $self->{ queue };
160 my $at = 0;
161  
162 ++$at
163 while $at < $queue->@*
164 && $queue->[ $at ][ 0 ] != $id;
165  
166 $self->{ touched } += $at + 1;
167  
168 $at < $queue->@*
169 or return 0;
170  
171 splice $queue->@*, $at, 1;
172  
173 1
174 }
175  
176 sub _requeue( $self, $batch )
177 {
178 my $count = $batch->@*;
179  
180 unshift $self->{ queue }->@*, $batch->@*;
181  
182 $self->{ touched } += $count;
183  
184 $batch->@* = ();
185  
186 $count
187 }
188  
189 ########################################################################
190 # introspection
191 ########################################################################
192  
193 sub contents( $self )
194 {
195 [ map { [ $_->@* ] } $self->{ queue }->@* ]
196 }
197  
198 sub size( $self )
199 {
200 scalar $self->{ queue }->@*
201 }
202  
203 sub lock( $self )
204 {
205 $self->{ lock }
206 }
207  
208 sub walked( $self )
209 {
210 $self->{ touched }
211 }
212  
213 sub purged( $self )
214 {
215 0
216 }
217  
218 1
219 __END__