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