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 qw(uninitialized); |
41 | use common::sense; |
37 | |
42 | |
38 | use Coro (); |
43 | use Coro (); |
39 | |
44 | |
40 | $VERSION = 0.5; |
45 | our $VERSION = 6.46; |
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 |
46 | negative values) are also allowed, in which case the semaphore is locked |
51 | negative values) are also allowed, in which case the semaphore is locked |
47 | by default. |
52 | by default. |
48 | |
53 | |
49 | =cut |
54 | =item $sem->count |
50 | |
55 | |
51 | sub new { |
56 | Returns the current semaphore count. The semaphore can be down'ed without |
52 | bless [defined $_[1] ? $_[1] : 1], $_[0]; |
57 | blocking when the count is strictly higher than C<0>. |
53 | } |
58 | |
|
|
59 | =item $sem->adjust ($diff) |
|
|
60 | |
|
|
61 | Atomically adds the amount given to the current semaphore count. If the |
|
|
62 | count becomes positive, wakes up any waiters. Does not block if the count |
|
|
63 | becomes negative, however. |
54 | |
64 | |
55 | =item $sem->down |
65 | =item $sem->down |
56 | |
66 | |
57 | Decrement the counter, therefore "locking" the semaphore. This method |
67 | Decrement the counter, therefore "locking" the semaphore. This method |
58 | waits until the semaphore is available if the counter is zero. |
68 | waits until the semaphore is available if the counter is zero or less. |
|
|
69 | |
|
|
70 | =item $sem->wait |
|
|
71 | |
|
|
72 | Similar to C<down>, but does not actually decrement the counter. Instead, |
|
|
73 | when this function returns, a following call to C<down> or C<try> is |
|
|
74 | guaranteed to succeed without blocking, until the next thread switch |
|
|
75 | (C<cede> etc.). |
|
|
76 | |
|
|
77 | Note that using C<wait> is much less efficient than using C<down>, so try |
|
|
78 | to prefer C<down> whenever possible. |
|
|
79 | |
|
|
80 | =item $sem->wait ($callback) |
|
|
81 | |
|
|
82 | If you pass a callback argument to C<wait>, it will not wait, but |
|
|
83 | immediately return. The callback will be called as soon as the semaphore |
|
|
84 | becomes available (which might be instantly), and gets passed the |
|
|
85 | semaphore as first argument. |
|
|
86 | |
|
|
87 | The callback might C<down> the semaphore exactly once, might wake up other |
|
|
88 | threads, but is I<NOT> allowed to block (switch to other threads). |
59 | |
89 | |
60 | =cut |
90 | =cut |
61 | |
91 | |
62 | sub down { |
92 | #=item $status = $sem->timed_down ($timeout) |
|
|
93 | # |
|
|
94 | #Like C<down>, but returns false if semaphore couldn't be acquired within |
|
|
95 | #$timeout seconds, otherwise true. |
|
|
96 | |
|
|
97 | #sub timed_down { |
|
|
98 | # require Coro::Timer; |
|
|
99 | # my $timeout = Coro::Timer::timeout ($_[1]); |
|
|
100 | # |
63 | while ($_[0][0] <= 0) { |
101 | # while ($_[0][0] <= 0) { |
64 | push @{$_[0][1]}, $Coro::current; |
102 | # push @{$_[0][1]}, $Coro::current; |
65 | Coro::schedule; |
103 | # &Coro::schedule; |
|
|
104 | # if ($timeout) { |
|
|
105 | # # ugly as hell. slow, too, btw! |
|
|
106 | # for (0..$#{$_[0][1]}) { |
|
|
107 | # if ($_[0][1][$_] == $Coro::current) { |
|
|
108 | # splice @{$_[0][1]}, $_, 1; |
|
|
109 | # return; |
|
|
110 | # } |
|
|
111 | # } |
|
|
112 | # die; |
|
|
113 | # } |
66 | } |
114 | # } |
|
|
115 | # |
67 | --$_[0][0]; |
116 | # --$_[0][0]; |
68 | } |
117 | # return 1; |
|
|
118 | #} |
69 | |
119 | |
70 | =item $sem->up |
120 | =item $sem->up |
71 | |
121 | |
72 | Unlock the semaphore again. |
122 | Unlock the semaphore again. |
73 | |
|
|
74 | =cut |
|
|
75 | |
|
|
76 | sub up { |
|
|
77 | if (++$_[0][0] > 0) { |
|
|
78 | (shift @{$_[0][1]})->ready if @{$_[0][1]}; |
|
|
79 | } |
|
|
80 | } |
|
|
81 | |
123 | |
82 | =item $sem->try |
124 | =item $sem->try |
83 | |
125 | |
84 | Try to C<down> the semaphore. Returns true when this was possible, |
126 | Try to C<down> the semaphore. Returns true when this was possible, |
85 | otherwise return false and leave the semaphore unchanged. |
127 | otherwise return false and leave the semaphore unchanged. |
86 | |
128 | |
87 | =cut |
|
|
88 | |
|
|
89 | sub try { |
|
|
90 | if ($_[0][0] > 0) { |
|
|
91 | --$_[0][0]; |
|
|
92 | return 1; |
|
|
93 | } else { |
|
|
94 | return 0; |
|
|
95 | } |
|
|
96 | } |
|
|
97 | |
|
|
98 | =item $sem->waiters |
129 | =item $sem->waiters |
99 | |
130 | |
100 | In scalar context, returns the number of coroutines waiting for this |
131 | In scalar context, returns the number of threads waiting for this |
101 | semaphore. |
132 | semaphore. Might accidentally cause WW3 if called in other contexts, so |
102 | |
133 | don't use these. |
103 | =cut |
|
|
104 | |
|
|
105 | sub waiters { |
|
|
106 | @{$_[0][1]}; |
|
|
107 | } |
|
|
108 | |
134 | |
109 | =item $guard = $sem->guard |
135 | =item $guard = $sem->guard |
110 | |
136 | |
111 | This method calls C<down> and then creates a guard object. When the guard |
137 | This method calls C<down> and then creates a guard object. When the guard |
112 | object is destroyed it automatically calls C<up>. |
138 | object is destroyed it automatically calls C<up>. |
113 | |
139 | |
114 | =cut |
140 | =cut |
115 | |
141 | |
116 | sub guard { |
142 | sub guard { |
117 | &down; |
143 | &down; |
118 | # double indirection because bless works on the referenced |
|
|
119 | # object, not (only) on the reference itself. |
|
|
120 | bless \\$_[0], Coro::Semaphore::Guard::; |
144 | bless [$_[0]], Coro::Semaphore::guard:: |
121 | } |
145 | } |
122 | |
146 | |
|
|
147 | #=item $guard = $sem->timed_guard ($timeout) |
|
|
148 | # |
|
|
149 | #Like C<guard>, but returns undef if semaphore couldn't be acquired within |
|
|
150 | #$timeout seconds, otherwise the guard object. |
|
|
151 | |
|
|
152 | #sub timed_guard { |
|
|
153 | # &timed_down |
|
|
154 | # ? bless \\$_[0], Coro::Semaphore::guard:: |
|
|
155 | # : (); |
|
|
156 | #} |
|
|
157 | |
123 | sub Coro::Semaphore::Guard::DESTROY { |
158 | sub Coro::Semaphore::guard::DESTROY { |
124 | &up(${${$_[0]}}); |
159 | &up($_[0][0]); |
125 | } |
160 | } |
126 | |
|
|
127 | 1; |
|
|
128 | |
161 | |
129 | =back |
162 | =back |
130 | |
163 | |
131 | =head1 AUTHOR |
164 | =head1 AUTHOR/SUPPORT/CONTACT |
132 | |
165 | |
133 | Marc Lehmann <pcg@goof.com> |
166 | Marc A. Lehmann <schmorp@schmorp.de> |
134 | http://www.goof.com/pcg/marc/ |
167 | http://software.schmorp.de/pkg/Coro.html |
135 | |
168 | |
136 | =cut |
169 | =cut |
137 | |
170 | |
|
|
171 | 1 |
|
|
172 | |