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; |
8 | |
8 | |
9 | $sig = new Coro::Semaphore [initial value]; |
9 | $sig = new Coro::Semaphore [initial value]; |
10 | |
10 | |
11 | $sig->down; # wait for signal |
11 | $sig->down; # wait for signal |
12 | |
12 | |
… | |
… | |
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 | |
|
|
30 | 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. |
29 | |
32 | |
30 | =over 4 |
33 | =over 4 |
31 | |
34 | |
32 | =cut |
35 | =cut |
33 | |
36 | |
34 | package Coro::Semaphore; |
37 | package Coro::Semaphore; |
35 | |
38 | |
36 | no warnings; |
39 | use common::sense; |
37 | |
40 | |
38 | use Coro (); |
41 | use Coro (); |
39 | |
42 | |
40 | $VERSION = 5.0; |
43 | our $VERSION = 6.02; |
41 | |
44 | |
42 | =item new [inital count] |
45 | =item new [inital count] |
43 | |
46 | |
44 | Creates a new sempahore object with the given initial lock count. The |
47 | 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 |
48 | default lock count is 1, which means it is unlocked by default. Zero (or |
… | |
… | |
63 | |
66 | |
64 | =item $sem->wait |
67 | =item $sem->wait |
65 | |
68 | |
66 | Similar to C<down>, but does not actually decrement the counter. Instead, |
69 | 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 |
70 | 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 |
71 | guaranteed to succeed without blocking, until the next thread switch |
69 | (C<cede> etc.). |
72 | (C<cede> etc.). |
70 | |
73 | |
71 | Note that using C<wait> is much less efficient than using C<down>, so try |
74 | Note that using C<wait> is much less efficient than using C<down>, so try |
72 | to prefer C<down> whenever possible. |
75 | to prefer C<down> whenever possible. |
73 | |
76 | |
… | |
… | |
77 | immediately return. The callback will be called as soon as the semaphore |
80 | immediately return. The callback will be called as soon as the semaphore |
78 | becomes available (which might be instantly), and gets passed the |
81 | becomes available (which might be instantly), and gets passed the |
79 | semaphore as first argument. |
82 | semaphore as first argument. |
80 | |
83 | |
81 | The callback might C<down> the semaphore exactly once, might wake up other |
84 | 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). |
85 | 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 | |
86 | |
87 | =cut |
87 | =cut |
88 | |
88 | |
89 | #=item $status = $sem->timed_down ($timeout) |
89 | #=item $status = $sem->timed_down ($timeout) |
90 | # |
90 | # |
… | |
… | |
123 | Try to C<down> the semaphore. Returns true when this was possible, |
123 | Try to C<down> the semaphore. Returns true when this was possible, |
124 | otherwise return false and leave the semaphore unchanged. |
124 | otherwise return false and leave the semaphore unchanged. |
125 | |
125 | |
126 | =item $sem->waiters |
126 | =item $sem->waiters |
127 | |
127 | |
128 | In scalar context, returns the number of coroutines waiting for this |
128 | In scalar context, returns the number of threads waiting for this |
129 | semaphore. |
129 | semaphore. |
130 | |
130 | |
131 | =item $guard = $sem->guard |
131 | =item $guard = $sem->guard |
132 | |
132 | |
133 | This method calls C<down> and then creates a guard object. When the guard |
133 | This method calls C<down> and then creates a guard object. When the guard |
… | |
… | |
135 | |
135 | |
136 | =cut |
136 | =cut |
137 | |
137 | |
138 | sub guard { |
138 | sub guard { |
139 | &down; |
139 | &down; |
140 | # double indirection because bless works on the referenced |
|
|
141 | # object, not (only) on the reference itself. |
|
|
142 | bless \\$_[0], Coro::Semaphore::guard::; |
140 | bless [$_[0]], Coro::Semaphore::guard:: |
143 | } |
141 | } |
144 | |
142 | |
145 | #=item $guard = $sem->timed_guard ($timeout) |
143 | #=item $guard = $sem->timed_guard ($timeout) |
146 | # |
144 | # |
147 | #Like C<guard>, but returns undef if semaphore couldn't be acquired within |
145 | #Like C<guard>, but returns undef if semaphore couldn't be acquired within |
… | |
… | |
152 | # ? bless \\$_[0], Coro::Semaphore::guard:: |
150 | # ? bless \\$_[0], Coro::Semaphore::guard:: |
153 | # : (); |
151 | # : (); |
154 | #} |
152 | #} |
155 | |
153 | |
156 | sub Coro::Semaphore::guard::DESTROY { |
154 | sub Coro::Semaphore::guard::DESTROY { |
157 | &up(${${$_[0]}}); |
155 | &up($_[0][0]); |
158 | } |
156 | } |
159 | |
157 | |
160 | =back |
158 | =back |
161 | |
159 | |
162 | =head1 AUTHOR |
160 | =head1 AUTHOR |