… | |
… | |
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 | |
… | |
… | |
33 | |
33 | |
34 | package Coro::Semaphore; |
34 | package Coro::Semaphore; |
35 | |
35 | |
36 | use Coro (); |
36 | use Coro (); |
37 | |
37 | |
38 | $VERSION = 0.12; |
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. |
… | |
… | |
94 | } else { |
94 | } else { |
95 | return 0; |
95 | return 0; |
96 | } |
96 | } |
97 | } |
97 | } |
98 | |
98 | |
|
|
99 | =item $sem->waiters |
|
|
100 | |
|
|
101 | In scalar context, returns the number of coroutines waiting for this |
|
|
102 | semaphore. |
|
|
103 | |
|
|
104 | =cut |
|
|
105 | |
|
|
106 | sub waiters { |
|
|
107 | @{$_[0][1]}; |
|
|
108 | } |
|
|
109 | |
|
|
110 | =item $guard = $sem->guard |
|
|
111 | |
|
|
112 | This method calls C<down> and then creates a guard object. When the guard |
|
|
113 | object is destroyed it automatically calls C<up>. |
|
|
114 | |
|
|
115 | =cut |
|
|
116 | |
|
|
117 | sub guard { |
|
|
118 | &down; |
|
|
119 | # double indirection because bless works on the referenced |
|
|
120 | # object, not (only) on the reference itself. |
|
|
121 | bless \\$_[0], Coro::Semaphore::Guard::; |
|
|
122 | } |
|
|
123 | |
|
|
124 | sub Coro::Semaphore::Guard::DESTROY { |
|
|
125 | &up(${${$_[0]}}); |
|
|
126 | } |
|
|
127 | |
99 | 1; |
128 | 1; |
100 | |
129 | |
101 | =back |
130 | =back |
102 | |
131 | |
103 | =head1 AUTHOR |
132 | =head1 AUTHOR |