… | |
… | |
14 | |
14 | |
15 | $sig->up; |
15 | $sig->up; |
16 | |
16 | |
17 | =head1 DESCRIPTION |
17 | =head1 DESCRIPTION |
18 | |
18 | |
|
|
19 | This module implements counted 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 | |
19 | =over 4 |
30 | =over 4 |
20 | |
31 | |
21 | =cut |
32 | =cut |
22 | |
33 | |
23 | package Coro::Semaphore; |
34 | package Coro::Semaphore; |
24 | |
35 | |
25 | use Coro (); |
36 | use Coro (); |
26 | |
37 | |
27 | $VERSION = 0.12; |
38 | $VERSION = 0.12; |
28 | |
39 | |
29 | =item new [inital count, default zero] |
40 | =item new [inital count, default one] |
30 | |
41 | |
31 | Creates a new sempahore object with the given initial lock count. The |
42 | Creates a new sempahore object with the given initial lock count. The |
32 | default lock count is 1, which means it is unlocked by default. |
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. |
33 | |
46 | |
34 | =cut |
47 | =cut |
35 | |
48 | |
36 | sub new { |
49 | sub new { |
37 | bless [defined $_[1] ? $_[1] : 1], $_[0]; |
50 | bless [defined $_[1] ? $_[1] : 1], $_[0]; |