File Coverage

blib/lib/Coro/Countdown.pm
Criterion Covered Total %
statement 22 22 100.0
branch 5 6 83.3
condition 1 2 50.0
subroutine 8 8 100.0
pod 5 5 100.0
total 41 43 95.3


line stmt bran cond sub pod time code
1              
2             package Coro::Countdown;
3             # ABSTRACT: a counter that signals when it reaches 0
4             $Coro::Countdown::VERSION = '0.02';
5 1     1   23414 use strict;
  1         13  
  1         46  
6 1     1   9 use warnings;
  1         3  
  1         51  
7 1     1   8 use Coro;
  1         2  
  1         423  
8              
9 1   50 1 1 93 sub new { bless [$_[1] || 0, new Coro::Signal], $_[0] }
10 5     5 1 5416 sub count { $_[0][0] }
11 3     3 1 507 sub up { ++$_[0][0] }
12              
13             sub join {
14 2     2 1 4653   my $self = shift;
15 2 100       6   return if $self->count == 0;
16 1         11   $self->[1]->wait;
17             }
18              
19             sub down {
20 3     3 1 10   my $self = shift;
21              
22 3 100       8   if (--$self->[0] <= 0) {
23 1 50       7     if ($self->[1]->awaited) {
24 1         5       $self->[1]->broadcast;
25 1         14       $self->[1] = new Coro::Signal;
26                 }
27              
28 1         2     $self->[0] = 0;
29               }
30              
31 3         10   $self->[0];
32             }
33              
34             1;
35              
36             __END__
37            
38             =pod
39            
40             =encoding UTF-8
41            
42             =head1 NAME
43            
44             Coro::Countdown - a counter that signals when it reaches 0
45            
46             =head1 VERSION
47            
48             version 0.02
49            
50             =head1 SYNOPSIS
51            
52             use Coro;
53             use Coro::Countdown;
54            
55             my $counter = Coro::Countdown->new;
56             $counter->up;
57            
58             async { $counter->down };
59            
60             # Block until $counter->down is called
61             $counter->join;
62            
63             =head1 DESCRIPTION
64            
65             Oftentimes it is necessary to wait until all users of a resource have completed
66             before a program may continue. Examples of this include a pool of pending
67             network requests, etc. A countdown signal will broadcast to any waiters once
68             all "checked out" resources have been "returned".
69            
70             =head1 METHODS
71            
72             =head2 new
73            
74             Optionally takes an initial value, defaulting to 0.
75            
76             =head2 join
77            
78             Cedes until the count decrements to 0. If the counter is already at 0, returns
79             immediately.
80            
81             =head2 count
82            
83             Returns the current counter value.
84            
85             =head2 up
86            
87             Increments the counter value ("checks out" a resource).
88            
89             =head2 down
90            
91             Decrements the counter value ("returns" the resource). If the counter reaches
92             0, all watchers are signaled and the counter resets. It is then ready to be
93             reused.
94            
95             =head1 AUTHOR
96            
97             Jeff Ober <sysread@fastmail.fm>
98            
99             =head1 COPYRIGHT AND LICENSE
100            
101             This software is copyright (c) 2017 by Jeff Ober.
102            
103             This is free software; you can redistribute it and/or modify it under
104             the same terms as the Perl 5 programming language system itself.
105            
106             =cut
107