ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Semaphore.pm
(Generate patch)

Comparing Coro/Coro/Semaphore.pm (file contents):
Revision 1.25 by root, Tue Nov 27 03:11:48 2001 UTC vs.
Revision 1.50 by root, Mon Dec 26 18:48:11 2005 UTC

31 31
32=cut 32=cut
33 33
34package Coro::Semaphore; 34package Coro::Semaphore;
35 35
36no warnings qw(uninitialized); 36BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") }
37 37
38use Coro (); 38use 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
44Creates a new sempahore object with the given initial lock count. The 44Creates a new sempahore object with the given initial lock count. The
45default lock count is 1, which means it is unlocked by default. Zero (or 45default 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
140sub guard { 149sub 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
147sub timed_guard { 156sub 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
153sub Coro::Semaphore::guard::DESTROY { 162sub Coro::Semaphore::guard::DESTROY {
154 &up(${${$_[0]}}); 163 &up(${${$_[0]}});
155} 164}
156 165
1571; 1661
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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines