… | |
… | |
31 | |
31 | |
32 | =cut |
32 | =cut |
33 | |
33 | |
34 | package Coro::Semaphore; |
34 | package Coro::Semaphore; |
35 | |
35 | |
36 | no warnings qw(uninitialized); |
36 | no warnings; |
37 | |
37 | |
38 | use Coro (); |
38 | use Coro (); |
39 | |
39 | |
40 | $VERSION = 0.5; |
40 | $VERSION = 4.9; |
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. |
|
|
84 | |
|
|
85 | =item $status = $sem->timed_down ($timeout) |
|
|
86 | |
|
|
87 | Like C<down>, but returns false if semaphore couldn't be acquired within |
|
|
88 | $timeout seconds, otherwise true. |
59 | |
89 | |
60 | =cut |
90 | =cut |
61 | |
91 | |
62 | sub down { |
92 | sub down { |
63 | while ($_[0][0] <= 0) { |
93 | while ($_[0][0] <= 0) { |
64 | push @{$_[0][1]}, $Coro::current; |
94 | push @{$_[0][1]}, $Coro::current; |
65 | Coro::schedule; |
95 | &Coro::schedule; |
66 | } |
96 | } |
67 | --$_[0][0]; |
97 | --$_[0][0]; |
|
|
98 | } |
|
|
99 | |
|
|
100 | sub timed_down { |
|
|
101 | require Coro::Timer; |
|
|
102 | my $timeout = Coro::Timer::timeout ($_[1]); |
|
|
103 | |
|
|
104 | while ($_[0][0] <= 0) { |
|
|
105 | push @{$_[0][1]}, $Coro::current; |
|
|
106 | &Coro::schedule; |
|
|
107 | if ($timeout) { |
|
|
108 | # ugly as hell. slow, too, btw! |
|
|
109 | for (0..$#{$_[0][1]}) { |
|
|
110 | if ($_[0][1][$_] == $Coro::current) { |
|
|
111 | splice @{$_[0][1]}, $_, 1; |
|
|
112 | return; |
|
|
113 | } |
|
|
114 | } |
|
|
115 | die; |
|
|
116 | } |
|
|
117 | } |
|
|
118 | |
|
|
119 | --$_[0][0]; |
|
|
120 | return 1; |
68 | } |
121 | } |
69 | |
122 | |
70 | =item $sem->up |
123 | =item $sem->up |
71 | |
124 | |
72 | Unlock the semaphore again. |
125 | Unlock the semaphore again. |
… | |
… | |
109 | =item $guard = $sem->guard |
162 | =item $guard = $sem->guard |
110 | |
163 | |
111 | 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 |
112 | object is destroyed it automatically calls C<up>. |
165 | object is destroyed it automatically calls C<up>. |
113 | |
166 | |
|
|
167 | =item $guard = $sem->timed_guard ($timeout) |
|
|
168 | |
|
|
169 | Like C<guard>, but returns undef if semaphore couldn't be acquired within |
|
|
170 | $timeout seconds, otherwise the guard object. |
|
|
171 | |
114 | =cut |
172 | =cut |
115 | |
173 | |
116 | sub guard { |
174 | sub guard { |
117 | &down; |
175 | &down; |
118 | # double indirection because bless works on the referenced |
176 | # double indirection because bless works on the referenced |
119 | # object, not (only) on the reference itself. |
177 | # object, not (only) on the reference itself. |
120 | bless \\$_[0], Coro::Semaphore::Guard::; |
178 | bless \\$_[0], Coro::Semaphore::guard::; |
121 | } |
179 | } |
122 | |
180 | |
|
|
181 | sub timed_guard { |
|
|
182 | &timed_down |
|
|
183 | ? bless \\$_[0], Coro::Semaphore::guard:: |
|
|
184 | : (); |
|
|
185 | } |
|
|
186 | |
123 | sub Coro::Semaphore::Guard::DESTROY { |
187 | sub Coro::Semaphore::guard::DESTROY { |
124 | &up(${${$_[0]}}); |
188 | &up(${${$_[0]}}); |
125 | } |
189 | } |
126 | |
190 | |
127 | 1; |
|
|
128 | |
|
|
129 | =back |
191 | =back |
130 | |
192 | |
131 | =head1 AUTHOR |
193 | =head1 AUTHOR |
132 | |
194 | |
133 | Marc Lehmann <pcg@goof.com> |
195 | Marc Lehmann <schmorp@schmorp.de> |
134 | http://www.goof.com/pcg/marc/ |
196 | http://home.schmorp.de/ |
135 | |
197 | |
136 | =cut |
198 | =cut |
137 | |
199 | |
|
|
200 | 1 |
|
|
201 | |