1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Coro::Semaphore - non-binary semaphores |
3 | Coro::Semaphore - counting semaphores |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use Coro::Semaphore; |
7 | use Coro::Semaphore; |
8 | |
8 | |
… | |
… | |
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 coroutine can C<down> it while another |
22 | associated with semaphores, so one thread can C<down> it while another |
23 | can C<up> it. |
23 | can C<up> it. |
24 | |
24 | |
25 | Counting semaphores are typically used to coordinate access to |
25 | Counting semaphores are typically used to coordinate access to |
26 | resources, with the semaphore count initialized to the number of free |
26 | resources, with the semaphore count initialized to the number of free |
27 | resources. Coroutines then increment the count when resources are added |
27 | resources. Threads then increment the count when resources are added |
28 | and decrement the count when resources are removed. |
28 | and decrement the count when resources are removed. |
29 | |
29 | |
30 | =over 4 |
30 | =over 4 |
31 | |
31 | |
32 | =cut |
32 | =cut |
… | |
… | |
35 | |
35 | |
36 | no warnings; |
36 | no warnings; |
37 | |
37 | |
38 | use Coro (); |
38 | use Coro (); |
39 | |
39 | |
40 | $VERSION = 5.0; |
40 | $VERSION = 5.162; |
41 | |
41 | |
42 | =item new [inital count] |
42 | =item new [inital count] |
43 | |
43 | |
44 | Creates a new sempahore object with the given initial lock count. The |
44 | Creates a new sempahore object with the given initial lock count. The |
45 | default lock count is 1, which means it is unlocked by default. Zero (or |
45 | default lock count is 1, which means it is unlocked by default. Zero (or |
… | |
… | |
63 | |
63 | |
64 | =item $sem->wait |
64 | =item $sem->wait |
65 | |
65 | |
66 | Similar to C<down>, but does not actually decrement the counter. Instead, |
66 | Similar to C<down>, but does not actually decrement the counter. Instead, |
67 | when this function returns, a following call to C<down> or C<try> is |
67 | when this function returns, a following call to C<down> or C<try> is |
68 | guaranteed to succeed without blocking, until the next coroutine switch |
68 | guaranteed to succeed without blocking, until the next thread switch |
69 | (C<cede> etc.). |
69 | (C<cede> etc.). |
70 | |
70 | |
71 | Note that using C<wait> is much less efficient than using C<down>, so try |
71 | Note that using C<wait> is much less efficient than using C<down>, so try |
72 | to prefer C<down> whenever possible. |
72 | to prefer C<down> whenever possible. |
73 | |
73 | |
… | |
… | |
77 | immediately return. The callback will be called as soon as the semaphore |
77 | immediately return. The callback will be called as soon as the semaphore |
78 | becomes available (which might be instantly), and gets passed the |
78 | becomes available (which might be instantly), and gets passed the |
79 | semaphore as first argument. |
79 | semaphore as first argument. |
80 | |
80 | |
81 | The callback might C<down> the semaphore exactly once, might wake up other |
81 | The callback might C<down> the semaphore exactly once, might wake up other |
82 | coroutines, but is I<NOT> allowed to block (switch to other coroutines). |
82 | threads, but is I<NOT> allowed to block (switch to other threads). |
83 | |
|
|
84 | This is considered a rather experimental interface, and is subject to |
|
|
85 | change. |
|
|
86 | |
83 | |
87 | =cut |
84 | =cut |
88 | |
85 | |
89 | #=item $status = $sem->timed_down ($timeout) |
86 | #=item $status = $sem->timed_down ($timeout) |
90 | # |
87 | # |
… | |
… | |
123 | Try to C<down> the semaphore. Returns true when this was possible, |
120 | Try to C<down> the semaphore. Returns true when this was possible, |
124 | otherwise return false and leave the semaphore unchanged. |
121 | otherwise return false and leave the semaphore unchanged. |
125 | |
122 | |
126 | =item $sem->waiters |
123 | =item $sem->waiters |
127 | |
124 | |
128 | In scalar context, returns the number of coroutines waiting for this |
125 | In scalar context, returns the number of threads waiting for this |
129 | semaphore. |
126 | semaphore. |
130 | |
127 | |
131 | =item $guard = $sem->guard |
128 | =item $guard = $sem->guard |
132 | |
129 | |
133 | This method calls C<down> and then creates a guard object. When the guard |
130 | This method calls C<down> and then creates a guard object. When the guard |
… | |
… | |
135 | |
132 | |
136 | =cut |
133 | =cut |
137 | |
134 | |
138 | sub guard { |
135 | sub guard { |
139 | &down; |
136 | &down; |
140 | # double indirection because bless works on the referenced |
|
|
141 | # object, not (only) on the reference itself. |
|
|
142 | bless \\$_[0], Coro::Semaphore::guard::; |
137 | bless [$_[0]], Coro::Semaphore::guard:: |
143 | } |
138 | } |
144 | |
139 | |
145 | #=item $guard = $sem->timed_guard ($timeout) |
140 | #=item $guard = $sem->timed_guard ($timeout) |
146 | # |
141 | # |
147 | #Like C<guard>, but returns undef if semaphore couldn't be acquired within |
142 | #Like C<guard>, but returns undef if semaphore couldn't be acquired within |
… | |
… | |
152 | # ? bless \\$_[0], Coro::Semaphore::guard:: |
147 | # ? bless \\$_[0], Coro::Semaphore::guard:: |
153 | # : (); |
148 | # : (); |
154 | #} |
149 | #} |
155 | |
150 | |
156 | sub Coro::Semaphore::guard::DESTROY { |
151 | sub Coro::Semaphore::guard::DESTROY { |
157 | &up(${${$_[0]}}); |
152 | &up($_[0][0]); |
158 | } |
153 | } |
159 | |
154 | |
160 | =back |
155 | =back |
161 | |
156 | |
162 | =head1 AUTHOR |
157 | =head1 AUTHOR |