blib/lib/Thread/Queue/Priority.pm

filter: Statementstmt 60.6% 97 / 160 Branchbran 48.4% 31 / 64 Conditioncond 50.0% 21 / 42 MC/DCmcdc 17.3% 4 / 23 Subroutinesub 81.2% 13 / 16 Podpod 100% 8 / 8 totaltotal 55.5% 174 / 313 CC 54 SCAR 31.4
CC
54
Cov
56%
dequeue_timed
90.0
dequeue
26.3
_validate_timeout
20.0
CRAP 23.1
↑ Summary
1 package Thread::Queue::Priority;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '1.03';
7 4 $VERSION = eval $VERSION;
8  
9 4 ▶use threads::shared 1.21;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Carp errors from threads::shared calls should complain about caller
13 4 our @CARP_NOT = ("threads::shared");
14  
15 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
16 3 my $class = shift;
17 3 my %queue :shared = ();
18 3 my %self :shared = (
19 '_queue' => \%queue,
20 '_count' => 0,
21 '_ended' => 0,
22 );
23 3 return bless(\%self, $class);
24 }
25  
26 # add items to the tail of a queue
27 22 ▶sub enqueue {
Subroutine enqueue: called CC=5 SCAR=16.4
Pod documented
28 22 my ($self, $item, $priority) = @_;
29 22 lock(%{$self});
30  
31 # if the queue has "ended" then we can't enqueue anything
32 22 ▶ if ($self->{'_ended'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ended"}) 0 22 22
33 0 require Carp;
34 0 Carp::croak("'enqueue' method called on queue that has been 'end'ed");
35 }
36  
37 22 my $queue = $self->{'_queue'};
38 22 ▶ $priority = defined($priority) ? $self->_validate_priority($priority) : 50;
Branch: 1 branch
BranchTrueFalseTotal
defined $priority ? : 4 18 22
39  
40 # if the priority group hasn't been created then create it
41 22 my @group :shared = ();
42 22 ▶ $queue->{$priority} = \@group unless exists($queue->{$priority});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $queue->{$priority} 6 16 22
43  
44 # increase our global count
45 22 ++$self->{'_count'};
46  
47 # add the new item to the priority list and signal that we're done
48 22 ▶ push(@{$self->{'_queue'}->{$priority}}, shared_clone($item)) and cond_signal(%{$self});
Branch: 1 branch
BranchTrueFalseTotal
push @{$$self{"_queue"}{$priority};}, shared_clone($item) and &cond_signal(\%{$self;}) 22 0 22
49 }
50  
51 # return a count of the number of items on a queue
52 6 ▶sub pending {
Subroutine pending: called CC=3 SCAR=11.8
Pod documented
53 6 my $self = shift;
54 6 lock(%{$self});
55  
56 # return undef if the queue has ended and is empty
57 6 ▶ return if $self->{'_ended'} && !$self->{'_count'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_ended"} and not $self->{"_count"} 0 6 6
Condition33%1/3
$self->{"_ended"} and not $self->{"_count"}
!ll&&!rl&&r
600
MC/DC0%0/2
$self->{"_ended"} and not $self->{"_count"}
$self->{"_ended"} not $self->{"_count"}
Truth table33%1/3
A and not(B)
A= $self->{"_ended"}B= not $self->{"_count"}
ABresult
0X0
100
111
58 6 return $self->{'_count'};
59 }
60  
61 # indicate that no more data will enter the queue
62 0 ▶sub end {
Subroutine end: not called CC=1 SCAR=6.9
Pod documented
63 0 my $self = shift;
64 0 lock(%{$self});
65  
66 # no more data is coming
67 0 $self->{'_ended'} = 1;
68  
69 # try to release at least one blocked thread
70 0 cond_signal(%{$self});
71 }
72  
73 # return 1 or more items from the head of a queue, blocking if needed
74 12 ▶sub dequeue {
Subroutine dequeue: called CC=13 SCAR=32.7
Pod documented
75 12 my $self = shift;
76 12 lock(%{$self});
77  
78 12 my $queue = $self->{'_queue'};
79 12 ▶ my $count = scalar(@_) ? $self->_validate_count(shift(@_)) : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 5 7 12
80  
81 # wait for requisite number of items
82 0 ▶ cond_wait(%{$self}) while (($self->{'_count'} < $count) && ! $self->{'_ended'});
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_count"} < $count and not $self->{"_ended"} and do { &cond_wait(\%{$self;}) } 0 7 7
Condition33%1/3
$self->{"_count"} < $count and not $self->{"_ended"}
!ll&&!rl&&r
700
MC/DC0%0/2
$self->{"_count"} < $count and not $self->{"_ended"}
$self->{"_count"} < $count not $self->{"_ended"}
Truth table33%1/3
A and not(B)
A= $self->{"_count"} < $countB= not $self->{"_ended"}
ABresult
0X0
100
111
83 7 ▶ cond_signal(%{$self}) if (($self->{'_count'} > $count) || $self->{'_ended'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_count"} > $count or $self->{"_ended"} 6 1 7
Condition66%2/3
$self->{"_count"} > $count or $self->{"_ended"}
l!l&&r!l&&!r
601
MC/DC50%1/2
$self->{"_count"} > $count or $self->{"_ended"}
$self->{"_count"} > $count $self->{"_ended"}
Truth table66%2/3
A or B
A= $self->{"_count"} > $countB= $self->{"_ended"}
ABresult
1X1
011
000
84  
85 # if no longer blocking, try getting whatever is left on the queue
86 7 ▶ return $self->dequeue_nb($count) if ($self->{'_ended'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_ended"} 0 7 7
87  
88 # return single item
89 7 ▶ if ($count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1) 7 0 7
90 7 for my $priority (sort keys %{$queue}) {
91 7 ▶ if (scalar(@{$queue->{$priority}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$queue->{$priority};}) 7 0 7
92 7 --$self->{'_count'};
93 7 return shift(@{$queue->{$priority}});
94 }
95 }
96 0 return;
97 }
98  
99 # return multiple items
100 0 my @items = ();
101 0 for (1 .. $count) {
102 0 for my $priority (sort keys %{$queue}) {
103 0 ▶ if (scalar(@{$queue->{$priority}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$queue->{$priority};}) 0 0 0
104 0 --$self->{'_count'};
105 0 push(@items, shift(@{$queue->{$priority}}));
106 }
107 }
108 }
109 0 return @items;
110 }
111  
112 # return items from the head of a queue with no blocking
113 6 ▶sub dequeue_nb {
Subroutine dequeue_nb: called CC=8 SCAR=28.7
Pod documented
114 6 my $self = shift;
115 6 lock(%{$self});
116  
117 6 my $queue = $self->{'_queue'};
118 6 ▶ my $count = scalar(@_) ? $self->_validate_count(shift(@_)) : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 5 1 6
119  
120 # return single item
121 1 ▶ if ($count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1) 1 0 1
122 1 for my $priority (sort keys %{$queue}) {
123 1 ▶ if (scalar(@{$queue->{$priority}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$queue->{$priority};}) 0 1 1
124 0 --$self->{'_count'};
125 0 return shift(@{$queue->{$priority}});
126 }
127 }
128 1 return;
129 }
130  
131 # return multiple items
132 0 my @items = ();
133 0 for (1 .. $count) {
134 0 for my $priority (sort keys %{$queue}) {
135 0 ▶ if (scalar(@{$queue->{$priority}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$queue->{$priority};}) 0 0 0
136 0 --$self->{'_count'};
137 0 push(@items, shift(@{$queue->{$priority}}));
138 }
139 }
140 }
141  
142 0 return @items;
143 }
144  
145 # return items from the head of a queue, blocking if needed up to a timeout
146 0 ▶sub dequeue_timed {
Subroutine dequeue_timed: not called CC=9 SCAR=45.0
Pod documented
147 0 my $self = shift;
148 0 lock(%{$self});
149  
150 0 my $queue = $self->{'_queue'};
151 0 ▶ my $timeout = scalar(@_) ? $self->_validate_timeout(shift(@_)) : -1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 0 0 0
152 0 ▶ my $count = scalar(@_) ? $self->_validate_count(shift(@_)) : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 0 0 0
153  
154 # timeout may be relative or absolute
155 # convert to an absolute time for use with cond_timedwait()
156 # so if the timeout is less than a year then we assume it's relative
157 0 ▶ $timeout += time() if ($timeout < 322000000); # more than one year
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < 322000000 0 0 0
158  
159 # wait for requisite number of items, or until timeout
160 0 ▶ while ($self->{'_count'} < $count && !$self->{'_ended'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_count"} < $count and not $self->{"_ended"}) 0 0 0
Condition0%0/3
$self->{"_count"} < $count and not $self->{"_ended"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_count"} < $count and not $self->{"_ended"}
$self->{"_count"} < $count not $self->{"_ended"}
Truth table0%0/3
A and not(B)
A= $self->{"_count"} < $countB= not $self->{"_ended"}
ABresult
0X0
100
111
161 0 ▶ last unless cond_timedwait(%{$self}, $timeout);
Branch: 1 branch
BranchTrueFalseTotal
unless &cond_timedwait(\%{$self;}, $timeout) 0 0 0
162 }
163 0 ▶ cond_signal(%{$self}) if (($self->{'_count'} > $count) || $self->{'_ended'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_count"} > $count or $self->{"_ended"} 0 0 0
Condition0%0/3
$self->{"_count"} > $count or $self->{"_ended"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_count"} > $count or $self->{"_ended"}
$self->{"_count"} > $count $self->{"_ended"}
Truth table0%0/3
A or B
A= $self->{"_count"} > $countB= $self->{"_ended"}
ABresult
1X1
011
000
164  
165 # get whatever we need off the queue if available
166 0 return $self->dequeue_nb($count);
167 }
168  
169 # return an item without removing it from a queue
170 18 ▶sub peek {
Subroutine peek: called CC=4 SCAR=13.9
Pod documented
171 18 my $self = shift;
172 18 lock(%{$self});
173  
174 18 my $queue = $self->{'_queue'};
175 18 ▶ my $index = scalar(@_) ? $self->_validate_index(shift(@_)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 16 2 18
176  
177 15 for my $priority (sort keys %{$queue}) {
178 28 my $size = scalar(@{$queue->{$priority}});
179 28 ▶ if ($index < $size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $size) { } 15 13 28
180 15 return $queue->{$priority}->[$index];
181 } else {
182 13 $index = ($index - $size);
183 }
184 }
185  
186 0 return;
187 }
188  
189 ### internal functions ###
190  
191 # check value of the requested index
192 16 ▶sub _validate_index {
Subroutine _validate_index: called CC=5 SCAR=16.1
193 16 my ($self, $index) = @_;
194  
195 16 ▶ if (!defined($index) || !looks_like_number($index) || (int($index) != $index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $index && &looks_like_number($index) or int $index != $index) 3 13 16
Condition100%6/6
not defined $index && &looks_like_number($index) or int $index != $index
l!l&&r!l&&!r
2113
defined $index && &looks_like_number($index)
!ll&&!rl&&r
1114
MC/DC100%3/3
not defined $index && &looks_like_number($index) or int $index != $index
defined $index &looks_like_number($index) int $index != $index
Truth table100%4/4
not(A && B) or C
A= defined $indexB= &looks_like_number($index)C= int $index != $index
ABCresult
0XX1
10X1
1111
1100
196 3 require Carp;
197 3 my ($method) = (caller(1))[3];
198 3 my $class_name = ref($self);
199 3 $method =~ s/${class_name}:://;
200 3 ▶ $index = 'undef' unless defined($index);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 1 2 3
201 3 Carp::croak("Invalid 'index' argument (${index}) to '${method}' method");
202 }
203  
204 13 return $index;
205 }
206  
207 # check value of the requested count
208 10 ▶sub _validate_count {
Subroutine _validate_count: called CC=6 SCAR=18.1
209 10 my ($self, $count) = @_;
210  
211 10 ▶ if (!defined($count) || !looks_like_number($count) || (int($count) != $count) || ($count < 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $count && &looks_like_number($count) or int $count != $count or $count < 1) 10 0 10
Condition88%8/9
not defined $count && &looks_like_number($count) or int $count != $count or $count < 1
l!l&&r!l&&!r
640
not defined $count && &looks_like_number($count) or int $count != $count
l!l&&r!l&&!r
424
defined $count && &looks_like_number($count)
!ll&&!rl&&r
226
MC/DC0%0/4
not defined $count && &looks_like_number($count) or int $count != $count or $count < 1
defined $count &looks_like_number($count) int $count != $count $count < 1
Truth table80%4/5
not(A && B) or C or D
A= defined $countB= &looks_like_number($count)C= int $count != $countD= $count < 1
ABCDresult
0XXX1
10XX1
111X1
11011
11000
212 10 require Carp;
213 10 my ($method) = (caller(1))[3];
214 10 my $class_name = ref($self);
215 10 $method =~ s/${class_name}:://;
216 10 ▶ $count = 'undef' unless defined($count);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 2 8 10
217 10 Carp::croak("Invalid 'count' argument (${count}) to '${method}' method");
218 }
219  
220 0 return $count;
221 }
222  
223 # check value of the requested timeout
224 0 ▶sub _validate_timeout {
Subroutine _validate_timeout: not called CC=4 SCAR=30.0
225 0 my ($self, $timeout) = @_;
226  
227 0 ▶ if (!defined($timeout) || !looks_like_number($timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $timeout and &looks_like_number($timeout)) 0 0 0
Condition0%0/3
defined $timeout and &looks_like_number($timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $timeout and &looks_like_number($timeout)
defined $timeout &looks_like_number($timeout)
Truth table0%0/3
A and B
A= defined $timeoutB= &looks_like_number($timeout)
ABresult
0X0
100
111
228 0 require Carp;
229 0 my ($method) = (caller(1))[3];
230 0 my $class_name = ref($self);
231 0 $method =~ s/${class_name}:://;
232 0 ▶ $timeout = 'undef' unless defined($timeout);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
233 0 Carp::croak("Invalid 'timeout' argument (${timeout}) to '${method}' method");
234 }
235  
236 0 return $timeout;
237 }
238  
239 # check value of the requested timeout
240 4 ▶sub _validate_priority {
Subroutine _validate_priority: called CC=6 SCAR=28.6
241 4 my ($self, $priority) = @_;
242  
243 4 ▶ if (!defined($priority) || !looks_like_number($priority) || (int($priority) != $priority) || ($priority < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $priority && &looks_like_number($priority) or int $priority != $priority or $priority < 0) 0 4 4
Condition33%3/9
not defined $priority && &looks_like_number($priority) or int $priority != $priority or $priority < 0
l!l&&r!l&&!r
004
not defined $priority && &looks_like_number($priority) or int $priority != $priority
l!l&&r!l&&!r
004
defined $priority && &looks_like_number($priority)
!ll&&!rl&&r
004
MC/DC0%0/4
not defined $priority && &looks_like_number($priority) or int $priority != $priority or $priority < 0
defined $priority &looks_like_number($priority) int $priority != $priority $priority < 0
Truth table20%1/5
not(A && B) or C or D
A= defined $priorityB= &looks_like_number($priority)C= int $priority != $priorityD= $priority < 0
ABCDresult
0XXX1
10XX1
111X1
11011
11000
244 0 require Carp;
245 0 my ($method) = (caller(1))[3];
246 0 my $class_name = ref($self);
247 0 $method =~ s/${class_name}:://;
248 0 ▶ $priority = 'undef' unless defined($priority);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $priority 0 0 0
249 0 Carp::croak("Invalid 'priority' argument (${priority}) to '${method}' method");
250 }
251  
252 4 return $priority;
253 }
254  
255 4 1;
256  
257 =head1 NAME
258  
259 Thread::Queue::Priority - Thread-safe queues with priorities
260  
261 =head1 VERSION
262  
263 This document describes Thread::Queue::Priority version 1.03
264  
265 =head1 SYNOPSIS
266  
267 use strict;
268 use warnings;
269  
270 use threads;
271 use Thread::Queue::Priority;
272  
273 # create a new empty queue
274 my $q = Thread::Queue::Priority->new();
275  
276 # add a new element with default priority 50
277 $q->enqueue("foo");
278  
279 # add a new element with priority 1
280 $q->enqueue("foo", 1);
281  
282 # dequeue the highest priority on the queue
283 my $value = $q->dequeue();
284  
285 =head1 DESCRIPTION
286  
287 This is a variation on L<Thread::Queue> that will dequeue items based on their
288 priority. This module is NOT a drop-in replacement for L<Thread::Queue> as it
289 does not implement all of its methods as they don't all make sense. However,
290 for the methods implemented and described below, consider the functionality to
291 be the same as that of L<Thread::Queue>.
292  
293 =head1 QUEUE CREATION
294  
295 =over
296  
297 =item ->new()
298  
299 Creates a new empty queue. A list cannot be created with items already on it.
300  
301 =item ->enqueue(ITEM, PRIORITY)
302  
303 Adds an item onto the queue with the givern priority. Only one item may be
304 added at a time. If no priority is given, it is given a default value of 50.
305 There are no constraints on the priority number with the exception that it must
306 be greater than zero and it must be a number. The smaller the number, the
307 greater the priority.
308  
309 =item ->dequeue()
310  
311 =item ->dequeue(COUNT)
312  
313 Removes and returns the requested number of items (default is 1) in priority
314 order where smaller numbers indicate greater priority. If the queue contains
315 fewer than the requested number of items, then the thread will be blocked until
316 the requisite number of items are available (i.e., until other threads
317 <enqueue> more items).
318  
319 =item ->dequeue_nb()
320  
321 =item ->dequeue_nb(COUNT)
322  
323 This functions the same as C<dequeue> but it will not block if the queue is
324 empty or the queue does not have COUNT items. Instead it will return whatever
325 is on the queue up to COUNT, or C<undef> if the queue is empty. Again, items
326 will come off the queue in priority order where smaller numbers have a higher
327 priority.
328  
329 =item ->dequeue_timed(TIMEOUT)
330  
331 =item ->dequeue_timed(TIMEOUT, COUNT)
332  
333 This functions the same as C<dequeue> but will only block for the length of the
334 given timeout. If the timeout is reached, it returns whatever items there are
335 on the queue, or C<undef> if the queue is empty. Again, items will come off the
336 queue in priority order where smaller numbers have a higher priority.
337  
338 The timeout may be a number of seconds relative to the current time (e.g., 5
339 seconds from when the call is made), or may be an absolute timeout in I<epoch>
340 seconds the same as would be used with
341 L<cond_timedwait()|threads::shared/"cond_timedwait VARIABLE, ABS_TIMEOUT">.
342 Fractional seconds (e.g., 2.5 seconds) are also supported (to the extent of
343 the underlying implementation).
344  
345 If C<TIMEOUT> is missing, C<undef>, or less than or equal to 0, then this call
346 behaves the same as C<dequeue_nb>.
347  
348 =item ->pending()
349  
350 Returns the number of items still in the queue. Returns C<undef> if the queue
351 has been ended (see below), and there are no more items in the queue.
352  
353 =item ->end()
354  
355 Declares that no more items will be added to the queue.
356  
357 All threads blocking on C<dequeue()> calls will be unblocked with any
358 remaining items in the queue and/or C<undef> being returned. Any subsequent
359 calls to C<dequeue()> will behave like C<dequeue_nb()>.
360  
361 Once ended, no more items may be placed in the queue.
362  
363 =item ->peek(INDEX)
364  
365 Returns n item from the queue without dequeuing anything. Defaults to the
366 the head of queue (at index position 0) if no index is specified. Negative
367 index values are supported as with L<arrays|perldata/"Subscripts"> (i.e., -1
368 is the end of the queue, -2 is next to last, and so on).
369  
370 If no items exists at the specified index (i.e., the queue is empty, or the
371 index is beyond the number of items on the queue), then C<undef> is returned.
372  
373 Remember, the returned item is not removed from the queue, so manipulating a
374 C<peek>ed at reference affects the item on the queue.
375  
376 =back
377  
378 =head1 SEE ALSO
379  
380 L<Thread::Queue>, L<threads>, L<threads::shared>
381  
382 =head1 MAINTAINER
383  
384 Paul Lockaby S<E<lt>plockaby AT cpan DOT orgE<gt>>
385  
386 =head1 CREDIT
387  
388 Significant portions of this module are directly from L<Thread::Queue> which is
389 maintained by Jerry D. Hedden, <jdhedden AT cpan DOT org>.
390  
391 =head1 LICENSE
392  
393 This program is free software; you can redistribute it and/or modify it under
394 the same terms as Perl itself.
395  
396 =cut
↑ Summary