Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add optional minimum-priority-level to size. Need to figure it out

for get, too (blocking here is harder, as will be testing).
  • Loading branch information...
commit 59bc08fac27b25448b70ed1ed0bcd1366b91f7d4 1 parent 6cc5ff4
@dmcbride authored
Showing with 19 additions and 3 deletions.
  1. +5 −1 Changes
  2. +13 −2 lib/Coro/PrioChannel.pm
  3. +1 −0  t/05_size.t
View
6 Changes
@@ -1,9 +1,13 @@
Revision history for Coro-PrioChannel
{{$NEXT}}
+ * Add optional minimum-priority-level to size. Need to figure it out
+ for get, too (blocking here is harder, as will be testing).
+
+0.002 2012-04-11
* Add github metadata
* Add some better documentation for put (hey, it's all relative)
* Add test for size method
0.001 2012-04-10
- * Initial revision.
+ * Initial revision based nearly completely on Coro::Channel.
View
15 lib/Coro/PrioChannel.pm
@@ -32,6 +32,7 @@ use List::Util qw(first sum);
sub SGET() { 0 }
sub SPUT() { 1 }
sub DATA() { 2 }
+sub MAX() { PRIO_MAX - PRIO_MIN + DATA + 1 }
=item new
@@ -67,13 +68,16 @@ sub put {
Return the next element from the queue at the highest priority, waiting if
necessary.
+TODO: allow an optional parameter to wait for a message of a minimum priority
+level (i.e., ignore messages of lower priority).
+
=cut
sub get {
Coro::Semaphore::down $_[0][SGET];
Coro::Semaphore::up $_[0][SPUT];
- my $a = first { $_ && scalar @$_ } reverse @{$_[0]}[DATA()..DATA() + PRIO_MAX()-PRIO_MIN() + 1];
+ my $a = first { $_ && scalar @$_ } reverse @{$_[0]}[DATA..MAX];
ref $a ? shift @$a : undef;
}
@@ -92,10 +96,17 @@ sub shutdown {
Same as Coro::Channel.
+An optional parameter allows you to specify the minimum priority level
+that you want to check the size against, i.e., to ignore messages of
+lower priority. This can be used for example if you're in the middle of
+an action and you want to check if there is a higher-priority message to
+deal with before resuming the current activity. This will not block.
+
=cut
sub size {
- sum map { $_ ? scalar @$_ : 0 } @{$_[0]}[DATA..DATA + PRIO_MAX()-PRIO_MIN() + 1];
+ my $min = @_ > 1 ? $_[1] - PRIO_MIN + DATA : DATA;
+ sum map { $_ ? scalar @$_ : 0 } @{$_[0]}[$min..MAX];
}
=back
View
1  t/05_size.t
@@ -12,5 +12,6 @@ $c->put(0);
$c->put(1, PRIO_MAX);
is($c->size(), 3, "Add up all the items across all queues");
+is($c->size(PRIO_NORMAL), 2, "Add up all the items of normal priority and higher");
done_testing();
Please sign in to comment.
Something went wrong with that request. Please try again.