… | |
… | |
17 | =head1 DESCRIPTION |
17 | =head1 DESCRIPTION |
18 | |
18 | |
19 | This module implements counting semaphores. You can initialize a mutex |
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 |
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 |
21 | that can be C<down>ed more than once until it blocks. There is no owner |
22 | associated with semaphores, so one thread can C<down> it while another |
22 | associated with semaphores, so one thread can C<down> it while another can |
23 | can C<up> it. |
23 | C<up> it (or vice versa), C<up> can be called before C<down> and so on: |
|
|
24 | the semaphore is really just an integer counter that optionally blocks |
|
|
25 | when it is 0. |
24 | |
26 | |
25 | Counting semaphores are typically used to coordinate access to |
27 | Counting semaphores are typically used to coordinate access to |
26 | resources, with the semaphore count initialized to the number of free |
28 | resources, with the semaphore count initialized to the number of free |
27 | resources. Threads then increment the count when resources are added |
29 | resources. Threads then increment the count when resources are added |
28 | and decrement the count when resources are removed. |
30 | and decrement the count when resources are removed. |
29 | |
31 | |
30 | You don't have to load C<Coro::Semaphore> manually, it will be loaded |
32 | You don't have to load C<Coro::Semaphore> manually, it will be loaded |
31 | automatically when you C<use Coro> and call the C<new> constructor. |
33 | automatically when you C<use Coro> and call the C<new> constructor. |
32 | |
34 | |
33 | =over 4 |
35 | =over 4 |
34 | |
36 | |
35 | =cut |
37 | =cut |
36 | |
38 | |
… | |
… | |
38 | |
40 | |
39 | use common::sense; |
41 | use common::sense; |
40 | |
42 | |
41 | use Coro (); |
43 | use Coro (); |
42 | |
44 | |
43 | our $VERSION = 5.37; |
45 | our $VERSION = 6.49; |
44 | |
46 | |
45 | =item new [inital count] |
47 | =item new [inital count] |
46 | |
48 | |
47 | Creates a new sempahore object with the given initial lock count. The |
49 | Creates a new sempahore object with the given initial lock count. The |
48 | default lock count is 1, which means it is unlocked by default. Zero (or |
50 | default lock count is 1, which means it is unlocked by default. Zero (or |
49 | negative values) are also allowed, in which case the semaphore is locked |
51 | negative values) are also allowed, in which case the semaphore is locked |
50 | by default. |
52 | by default. |
51 | |
53 | |
52 | =item $sem->count |
54 | =item $sem->count |
53 | |
55 | |
54 | Returns the current semaphore count. |
56 | Returns the current semaphore count. The semaphore can be down'ed without |
|
|
57 | blocking when the count is strictly higher than C<0>. |
55 | |
58 | |
56 | =item $sem->adjust ($diff) |
59 | =item $sem->adjust ($diff) |
57 | |
60 | |
58 | Atomically adds the amount given to the current semaphore count. If the |
61 | Atomically adds the amount given to the current semaphore count. If the |
59 | count becomes positive, wakes up any waiters. Does not block if the count |
62 | count becomes positive, wakes up any waiters. Does not block if the count |
60 | becomes negative, however. |
63 | becomes negative, however. |
61 | |
64 | |
62 | =item $sem->down |
65 | =item $sem->down |
63 | |
66 | |
64 | Decrement the counter, therefore "locking" the semaphore. This method |
67 | Decrement the counter, therefore "locking" the semaphore. This method |
65 | waits until the semaphore is available if the counter is zero. |
68 | waits until the semaphore is available if the counter is zero or less. |
66 | |
69 | |
67 | =item $sem->wait |
70 | =item $sem->wait |
68 | |
71 | |
69 | Similar to C<down>, but does not actually decrement the counter. Instead, |
72 | Similar to C<down>, but does not actually decrement the counter. Instead, |
70 | when this function returns, a following call to C<down> or C<try> is |
73 | when this function returns, a following call to C<down> or C<try> is |
… | |
… | |
124 | otherwise return false and leave the semaphore unchanged. |
127 | otherwise return false and leave the semaphore unchanged. |
125 | |
128 | |
126 | =item $sem->waiters |
129 | =item $sem->waiters |
127 | |
130 | |
128 | In scalar context, returns the number of threads waiting for this |
131 | In scalar context, returns the number of threads waiting for this |
129 | semaphore. |
132 | semaphore. Might accidentally cause WW3 if called in other contexts, so |
|
|
133 | don't use these. |
130 | |
134 | |
131 | =item $guard = $sem->guard |
135 | =item $guard = $sem->guard |
132 | |
136 | |
133 | This method calls C<down> and then creates a guard object. When the guard |
137 | This method calls C<down> and then creates a guard object. When the guard |
134 | object is destroyed it automatically calls C<up>. |
138 | object is destroyed it automatically calls C<up>. |
… | |
… | |
155 | &up($_[0][0]); |
159 | &up($_[0][0]); |
156 | } |
160 | } |
157 | |
161 | |
158 | =back |
162 | =back |
159 | |
163 | |
160 | =head1 AUTHOR |
164 | =head1 AUTHOR/SUPPORT/CONTACT |
161 | |
165 | |
162 | Marc Lehmann <schmorp@schmorp.de> |
166 | Marc A. Lehmann <schmorp@schmorp.de> |
163 | http://home.schmorp.de/ |
167 | http://software.schmorp.de/pkg/Coro.html |
164 | |
168 | |
165 | =cut |
169 | =cut |
166 | |
170 | |
167 | 1 |
171 | 1 |
168 | |
172 | |