… | |
… | |
31 | |
31 | |
32 | =cut |
32 | =cut |
33 | |
33 | |
34 | package Coro::Semaphore; |
34 | package Coro::Semaphore; |
35 | |
35 | |
36 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
36 | no warnings; |
37 | |
37 | |
38 | use Coro (); |
38 | use Coro (); |
39 | |
39 | |
40 | $VERSION = 1.1; |
40 | $VERSION = 4.745; |
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 |
… | |
… | |
50 | |
50 | |
51 | sub new { |
51 | sub new { |
52 | bless [defined $_[1] ? $_[1] : 1], $_[0]; |
52 | bless [defined $_[1] ? $_[1] : 1], $_[0]; |
53 | } |
53 | } |
54 | |
54 | |
|
|
55 | =item $sem->count |
|
|
56 | |
|
|
57 | Returns the current semaphore count. |
|
|
58 | |
|
|
59 | =cut |
|
|
60 | |
|
|
61 | sub count { |
|
|
62 | $_[0][0] |
|
|
63 | } |
|
|
64 | |
|
|
65 | =item $sem->adjust ($diff) |
|
|
66 | |
|
|
67 | Atomically adds the amount given to the current semaphore count. If the |
|
|
68 | count becomes positive, wakes up any waiters. Does not block if the count |
|
|
69 | becomes negative, however. |
|
|
70 | |
|
|
71 | =cut |
|
|
72 | |
|
|
73 | sub adjust { |
|
|
74 | # basically a weird copy of up |
|
|
75 | if (($_[0][0] += $_[1]) > 0) { |
|
|
76 | (shift @{$_[0][1]})->ready if @{$_[0][1]}; |
|
|
77 | } |
|
|
78 | } |
|
|
79 | |
55 | =item $sem->down |
80 | =item $sem->down |
56 | |
81 | |
57 | Decrement the counter, therefore "locking" the semaphore. This method |
82 | Decrement the counter, therefore "locking" the semaphore. This method |
58 | waits until the semaphore is available if the counter is zero. |
83 | waits until the semaphore is available if the counter is zero. |
59 | |
84 | |
60 | =item $status = $sem->timed_down($timeout) |
85 | =item $status = $sem->timed_down ($timeout) |
61 | |
86 | |
62 | Like C<down>, but returns false if semaphore couldn't be acquired within |
87 | Like C<down>, but returns false if semaphore couldn't be acquired within |
63 | $timeout seconds, otherwise true. |
88 | $timeout seconds, otherwise true. |
64 | |
89 | |
65 | =cut |
90 | =cut |
66 | |
91 | |
67 | sub down { |
92 | sub down { |
68 | while ($_[0][0] <= 0) { |
93 | while ($_[0][0] <= 0) { |
69 | push @{$_[0][1]}, $Coro::current; |
94 | push @{$_[0][1]}, $Coro::current; |
70 | Coro::schedule; |
95 | &Coro::schedule; |
71 | } |
96 | } |
72 | --$_[0][0]; |
97 | --$_[0][0]; |
73 | } |
98 | } |
74 | |
99 | |
75 | sub timed_down { |
100 | sub timed_down { |
76 | require Coro::Timer; |
101 | require Coro::Timer; |
77 | my $timeout = Coro::Timer::timeout($_[1]); |
102 | my $timeout = Coro::Timer::timeout ($_[1]); |
78 | |
103 | |
79 | while ($_[0][0] <= 0) { |
104 | while ($_[0][0] <= 0) { |
80 | push @{$_[0][1]}, $Coro::current; |
105 | push @{$_[0][1]}, $Coro::current; |
81 | Coro::schedule; |
106 | &Coro::schedule; |
82 | if ($timeout) { |
107 | if ($timeout) { |
83 | # ugly as hell. slow, too, btw! |
108 | # ugly as hell. slow, too, btw! |
84 | for (0..$#{$_[0][1]}) { |
109 | for (0..$#{$_[0][1]}) { |
85 | if ($_[0][1][$_] == $Coro::current) { |
110 | if ($_[0][1][$_] == $Coro::current) { |
86 | splice @{$_[0][1]}, $_, 1; |
111 | splice @{$_[0][1]}, $_, 1; |
… | |
… | |
137 | =item $guard = $sem->guard |
162 | =item $guard = $sem->guard |
138 | |
163 | |
139 | This method calls C<down> and then creates a guard object. When the guard |
164 | This method calls C<down> and then creates a guard object. When the guard |
140 | object is destroyed it automatically calls C<up>. |
165 | object is destroyed it automatically calls C<up>. |
141 | |
166 | |
142 | =item $guard = $sem->timed_guard($timeout) |
167 | =item $guard = $sem->timed_guard ($timeout) |
143 | |
168 | |
144 | Like C<guard>, but returns undef if semaphore couldn't be acquired within |
169 | Like C<guard>, but returns undef if semaphore couldn't be acquired within |
145 | $timeout seconds, otherwise the guard object. |
170 | $timeout seconds, otherwise the guard object. |
146 | |
171 | |
147 | =cut |
172 | =cut |
… | |
… | |
161 | |
186 | |
162 | sub Coro::Semaphore::guard::DESTROY { |
187 | sub Coro::Semaphore::guard::DESTROY { |
163 | &up(${${$_[0]}}); |
188 | &up(${${$_[0]}}); |
164 | } |
189 | } |
165 | |
190 | |
166 | 1; |
|
|
167 | |
|
|
168 | =back |
191 | =back |
169 | |
192 | |
170 | =head1 AUTHOR |
193 | =head1 AUTHOR |
171 | |
194 | |
172 | Marc Lehmann <pcg@goof.com> |
195 | Marc Lehmann <schmorp@schmorp.de> |
173 | http://home.schmorp.de/ |
196 | http://home.schmorp.de/ |
174 | |
197 | |
175 | =cut |
198 | =cut |
176 | |
199 | |
|
|
200 | 1 |
|
|
201 | |