… | |
… | |
31 | |
31 | |
32 | =cut |
32 | =cut |
33 | |
33 | |
34 | package Coro::Semaphore; |
34 | package Coro::Semaphore; |
35 | |
35 | |
36 | no warnings qw(uninitialized); |
36 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
37 | |
37 | |
38 | use Coro (); |
38 | use Coro (); |
39 | |
39 | |
40 | $VERSION = 0.52; |
40 | $VERSION = 1.7; |
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 |
… | |
… | |
77 | my $timeout = Coro::Timer::timeout($_[1]); |
77 | my $timeout = Coro::Timer::timeout($_[1]); |
78 | |
78 | |
79 | while ($_[0][0] <= 0) { |
79 | while ($_[0][0] <= 0) { |
80 | push @{$_[0][1]}, $Coro::current; |
80 | push @{$_[0][1]}, $Coro::current; |
81 | Coro::schedule; |
81 | Coro::schedule; |
82 | $timeout and return; |
82 | if ($timeout) { |
|
|
83 | # ugly as hell. slow, too, btw! |
|
|
84 | for (0..$#{$_[0][1]}) { |
|
|
85 | if ($_[0][1][$_] == $Coro::current) { |
|
|
86 | splice @{$_[0][1]}, $_, 1; |
|
|
87 | return; |
|
|
88 | } |
|
|
89 | } |
|
|
90 | die; |
|
|
91 | } |
83 | } |
92 | } |
84 | |
93 | |
85 | --$_[0][0]; |
94 | --$_[0][0]; |
86 | return 1; |
95 | return 1; |
87 | } |
96 | } |
… | |
… | |
139 | |
148 | |
140 | sub guard { |
149 | sub guard { |
141 | &down; |
150 | &down; |
142 | # double indirection because bless works on the referenced |
151 | # double indirection because bless works on the referenced |
143 | # object, not (only) on the reference itself. |
152 | # object, not (only) on the reference itself. |
144 | bless \\$_[0], Coro::Semaphore::guard; |
153 | bless \\$_[0], Coro::Semaphore::guard::; |
145 | } |
154 | } |
146 | |
155 | |
147 | sub timed_guard { |
156 | sub timed_guard { |
148 | &timed_down |
157 | &timed_down |
149 | ? bless \\$_[0], Coro::Semaphore::guard |
158 | ? bless \\$_[0], Coro::Semaphore::guard:: |
150 | : (); |
159 | : (); |
151 | } |
160 | } |
152 | |
161 | |
153 | sub Coro::Semaphore::guard::DESTROY { |
162 | sub Coro::Semaphore::guard::DESTROY { |
154 | &up(${${$_[0]}}); |
163 | &up(${${$_[0]}}); |
155 | } |
164 | } |
156 | |
165 | |
157 | 1; |
166 | 1 |
158 | |
167 | |
159 | =back |
168 | =back |
160 | |
169 | |
161 | =head1 AUTHOR |
170 | =head1 AUTHOR |
162 | |
171 | |
163 | Marc Lehmann <pcg@goof.com> |
172 | Marc Lehmann <schmorp@schmorp.de> |
164 | http://www.goof.com/pcg/marc/ |
173 | http://home.schmorp.de/ |
165 | |
174 | |
166 | =cut |
175 | =cut |
167 | |
176 | |