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