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.36 by pcg, Wed Nov 5 20:02:46 2003 UTC vs.
Revision 1.55 by root, Wed Mar 7 13:11:10 2007 UTC

35 35
36BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } 36BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") }
37 37
38use Coro (); 38use Coro ();
39 39
40$VERSION = 0.8; 40$VERSION = 1.9;
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
48 48
49=cut 49=cut
50 50
51sub new { 51sub new {
52 bless [defined $_[1] ? $_[1] : 1], $_[0]; 52 bless [defined $_[1] ? $_[1] : 1], $_[0];
53}
54
55=item $sem->count
56
57Returns the current semaphore count.
58
59=cut
60
61sub count {
62 $_[0][0]
53} 63}
54 64
55=item $sem->down 65=item $sem->down
56 66
57Decrement the counter, therefore "locking" the semaphore. This method 67Decrement the counter, therefore "locking" the semaphore. This method
65=cut 75=cut
66 76
67sub down { 77sub down {
68 while ($_[0][0] <= 0) { 78 while ($_[0][0] <= 0) {
69 push @{$_[0][1]}, $Coro::current; 79 push @{$_[0][1]}, $Coro::current;
70 Coro::schedule; 80 &Coro::schedule;
71 } 81 }
72 --$_[0][0]; 82 --$_[0][0];
73} 83}
74 84
75sub timed_down { 85sub timed_down {
76 require Coro::Timer; 86 require Coro::Timer;
77 my $timeout = Coro::Timer::timeout($_[1]); 87 my $timeout = Coro::Timer::timeout($_[1]);
78 88
79 while ($_[0][0] <= 0) { 89 while ($_[0][0] <= 0) {
80 push @{$_[0][1]}, $Coro::current; 90 push @{$_[0][1]}, $Coro::current;
81 Coro::schedule; 91 &Coro::schedule;
82 if ($timeout) { 92 if ($timeout) {
83 # ugly as hell. slow, too, btw! 93 # ugly as hell. slow, too, btw!
84 for (0..$#{$_[0][1]}) { 94 for (0..$#{$_[0][1]}) {
85 if ($_[0][1][$_] == $Coro::current) { 95 if ($_[0][1][$_] == $Coro::current) {
86 splice @{$_[0][1]}, $_, 1; 96 splice @{$_[0][1]}, $_, 1;
161 171
162sub Coro::Semaphore::guard::DESTROY { 172sub Coro::Semaphore::guard::DESTROY {
163 &up(${${$_[0]}}); 173 &up(${${$_[0]}});
164} 174}
165 175
1661;
167
168=back 176=back
169 177
170=head1 AUTHOR 178=head1 AUTHOR
171 179
172 Marc Lehmann <pcg@goof.com> 180 Marc Lehmann <schmorp@schmorp.de>
173 http://www.goof.com/pcg/marc/ 181 http://home.schmorp.de/
174 182
175=cut 183=cut
176 184
1851
186

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines