line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Coro::Countdown; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
$Coro::Countdown::VERSION = '0.01'; |
5
|
1
|
|
|
1
|
|
15946
|
use strict; |
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
30
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
44
|
|
7
|
1
|
|
|
1
|
|
8
|
use Coro; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
268
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
50
|
1
|
1
|
176
|
sub new { bless [$_[1] || 0, new Coro::Signal], $_[0] } |
10
|
5
|
|
|
5
|
1
|
7315
|
sub count { $_[0][0] } |
11
|
3
|
|
|
3
|
1
|
420
|
sub up { ++$_[0][0] } |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub join { |
14
|
2
|
|
|
2
|
1
|
4974
|
my $self = shift; |
15
|
2
|
100
|
|
|
|
5
|
return if $self->count == 0; |
16
|
1
|
|
|
|
|
10
|
$self->[1]->wait; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub down { |
20
|
3
|
|
|
3
|
1
|
11
|
my $self = shift; |
21
|
|
|
|
|
|
|
|
22
|
3
|
100
|
|
|
|
8
|
if (--$self->[0] <= 0) { |
23
|
1
|
50
|
|
|
|
5
|
if ($self->[1]->awaited) { |
24
|
1
|
|
|
|
|
5
|
$self->[1]->broadcast; |
25
|
1
|
|
|
|
|
14
|
$self->[1] = new Coro::Signal; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
1
|
|
|
|
|
3
|
$self->[0] = 0; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
3
|
|
|
|
|
11
|
$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.01 |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 SYNOPSIS |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
use Coro; |
53
|
|
|
|
|
|
|
use Coro::Countdown; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $counter = Coro::Countdown->new; |
56
|
|
|
|
|
|
|
$counter->inc; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
async { $counter->dec }; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Block until $counter->dec 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
|
|
|
|
|
|
|
|