1 |
=head1 NAME |
2 |
|
3 |
Coro::Semaphore - non-binary semaphores |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Coro::Semaphore; |
8 |
|
9 |
$sig = new Coro::Semaphore [initial value]; |
10 |
|
11 |
$sig->down; # wait for signal |
12 |
|
13 |
# ... some other "thread" |
14 |
|
15 |
$sig->up; |
16 |
|
17 |
=head1 DESCRIPTION |
18 |
|
19 |
This module implements counting semaphores. You can initialize a mutex |
20 |
with any level of parallel users, that is, you can intialize a sempahore |
21 |
that can be C<down>ed more than once until it blocks. There is no owner |
22 |
associated with semaphores, so one coroutine can C<down> it while another |
23 |
can C<up> it. |
24 |
|
25 |
Counting semaphores are typically used to coordinate access to |
26 |
resources, with the semaphore count initialized to the number of free |
27 |
resources. Coroutines then increment the count when resources are added |
28 |
and decrement the count when resources are removed. |
29 |
|
30 |
=over 4 |
31 |
|
32 |
=cut |
33 |
|
34 |
package Coro::Semaphore; |
35 |
|
36 |
use Coro (); |
37 |
|
38 |
$VERSION = 0.45; |
39 |
|
40 |
=item new [inital count] |
41 |
|
42 |
Creates a new sempahore object with the given initial lock count. The |
43 |
default lock count is 1, which means it is unlocked by default. Zero (or |
44 |
negative values) are also allowed, in which case the semaphore is locked |
45 |
by default. |
46 |
|
47 |
=cut |
48 |
|
49 |
sub new { |
50 |
bless [defined $_[1] ? $_[1] : 1], $_[0]; |
51 |
} |
52 |
|
53 |
=item $sem->down |
54 |
|
55 |
Decrement the counter, therefore "locking" the semaphore. This method |
56 |
waits until the semaphore is available if the counter is zero. |
57 |
|
58 |
=cut |
59 |
|
60 |
sub down { |
61 |
while ($_[0][0] <= 0) { |
62 |
push @{$_[0][1]}, $Coro::current; |
63 |
Coro::schedule; |
64 |
} |
65 |
--$_[0][0]; |
66 |
} |
67 |
|
68 |
=item $sem->up |
69 |
|
70 |
Unlock the semaphore again. |
71 |
|
72 |
=cut |
73 |
|
74 |
sub up { |
75 |
if (++$_[0][0] > 0) { |
76 |
(shift @{$_[0][1]})->ready if @{$_[0][1]}; |
77 |
} |
78 |
} |
79 |
|
80 |
=item $sem->try |
81 |
|
82 |
Try to C<down> the semaphore. Returns true when this was possible, |
83 |
otherwise return false and leave the semaphore unchanged. |
84 |
|
85 |
=cut |
86 |
|
87 |
sub try { |
88 |
if ($_[0][0] > 0) { |
89 |
--$_[0][0]; |
90 |
return 1; |
91 |
} else { |
92 |
return 0; |
93 |
} |
94 |
} |
95 |
|
96 |
=item $sem->waiters |
97 |
|
98 |
In scalar context, returns the number of coroutines waiting for this |
99 |
semaphore. |
100 |
|
101 |
=cut |
102 |
|
103 |
sub waiters { |
104 |
@{$_[0][1]}; |
105 |
} |
106 |
|
107 |
=item $guard = $sem->guard |
108 |
|
109 |
This method calls C<down> and then creates a guard object. When the guard |
110 |
object is destroyed it automatically calls C<up>. |
111 |
|
112 |
=cut |
113 |
|
114 |
sub guard { |
115 |
&down; |
116 |
# double indirection because bless works on the referenced |
117 |
# object, not (only) on the reference itself. |
118 |
bless \\$_[0], Coro::Semaphore::Guard::; |
119 |
} |
120 |
|
121 |
sub Coro::Semaphore::Guard::DESTROY { |
122 |
&up(${${$_[0]}}); |
123 |
} |
124 |
|
125 |
1; |
126 |
|
127 |
=back |
128 |
|
129 |
=head1 AUTHOR |
130 |
|
131 |
Marc Lehmann <pcg@goof.com> |
132 |
http://www.goof.com/pcg/marc/ |
133 |
|
134 |
=cut |
135 |
|