… | |
… | |
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 |
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 coroutine can C<down> it while another |
22 | associated with semaphores, so one coroutine can C<down> it while another |
23 | can C<up> it. |
23 | can C<up> it. |
24 | |
24 | |
… | |
… | |
35 | |
35 | |
36 | use Coro (); |
36 | use Coro (); |
37 | |
37 | |
38 | $VERSION = 0.45; |
38 | $VERSION = 0.45; |
39 | |
39 | |
40 | =item new [inital count, default one] |
40 | =item new [inital count] |
41 | |
41 | |
42 | 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 |
43 | default lock count is 1, which means it is unlocked by default. Zero (or |
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 |
44 | negative values) are also allowed, in which case the semaphore is locked |
45 | by default. |
45 | by default. |
… | |
… | |
113 | object is destroyed it automatically calls C<up>. |
113 | object is destroyed it automatically calls C<up>. |
114 | |
114 | |
115 | =cut |
115 | =cut |
116 | |
116 | |
117 | sub guard { |
117 | sub guard { |
118 | $_[0]->down; |
118 | &down; |
119 | # double indirection because bless works on the referenced |
119 | # double indirection because bless works on the referenced |
120 | # object, not (only) on the reference itself. |
120 | # object, not (only) on the reference itself. |
121 | bless \\$_[0], Coro::Semaphore::Guard::; |
121 | bless \\$_[0], Coro::Semaphore::Guard::; |
122 | } |
122 | } |
123 | |
123 | |
124 | sub Coro::Semaphore::Guard::DESTROY { |
124 | sub Coro::Semaphore::Guard::DESTROY { |
125 | ${${$_[0]}}->up; |
125 | &up(${${$_[0]}}); |
126 | } |
126 | } |
127 | |
127 | |
128 | 1; |
128 | 1; |
129 | |
129 | |
130 | =back |
130 | =back |