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.19 by root, Sun Sep 16 01:34:36 2001 UTC vs.
Revision 1.69 by root, Tue Sep 23 00:02:20 2008 UTC

31 31
32=cut 32=cut
33 33
34package Coro::Semaphore; 34package Coro::Semaphore;
35 35
36no warnings;
37
36use Coro (); 38use Coro ();
37 39
38$VERSION = 0.5; 40$VERSION = 4.747;
39 41
40=item new [inital count] 42=item new [inital count]
41 43
42Creates a new sempahore object with the given initial lock count. The 44Creates a new sempahore object with the given initial lock count. The
43default 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 50
49sub new { 51sub new {
50 bless [defined $_[1] ? $_[1] : 1], $_[0]; 52 bless [defined $_[1] ? $_[1] : 1], $_[0];
51} 53}
52 54
55=item $sem->count
56
57Returns the current semaphore count.
58
59=cut
60
61sub count {
62 $_[0][0]
63}
64
65=item $sem->adjust ($diff)
66
67Atomically adds the amount given to the current semaphore count. If the
68count becomes positive, wakes up any waiters. Does not block if the count
69becomes negative, however.
70
71=cut
72
73sub 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
53=item $sem->down 80=item $sem->down
54 81
55Decrement the counter, therefore "locking" the semaphore. This method 82Decrement the counter, therefore "locking" the semaphore. This method
56waits until the semaphore is available if the counter is zero. 83waits until the semaphore is available if the counter is zero.
84
85=item $status = $sem->timed_down ($timeout)
86
87Like C<down>, but returns false if semaphore couldn't be acquired within
88$timeout seconds, otherwise true.
57 89
58=cut 90=cut
59 91
60sub down { 92sub down {
61 while ($_[0][0] <= 0) { 93 while ($_[0][0] <= 0) {
62 push @{$_[0][1]}, $Coro::current; 94 push @{$_[0][1]}, $Coro::current;
63 Coro::schedule; 95 &Coro::schedule;
64 } 96 }
65 --$_[0][0]; 97 --$_[0][0];
98}
99
100sub 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;
66} 121}
67 122
68=item $sem->up 123=item $sem->up
69 124
70Unlock the semaphore again. 125Unlock the semaphore again.
107=item $guard = $sem->guard 162=item $guard = $sem->guard
108 163
109This method calls C<down> and then creates a guard object. When the guard 164This method calls C<down> and then creates a guard object. When the guard
110object is destroyed it automatically calls C<up>. 165object is destroyed it automatically calls C<up>.
111 166
167=item $guard = $sem->timed_guard ($timeout)
168
169Like C<guard>, but returns undef if semaphore couldn't be acquired within
170$timeout seconds, otherwise the guard object.
171
112=cut 172=cut
113 173
114sub guard { 174sub guard {
115 &down; 175 &down;
116 # double indirection because bless works on the referenced 176 # double indirection because bless works on the referenced
117 # object, not (only) on the reference itself. 177 # object, not (only) on the reference itself.
118 bless \\$_[0], Coro::Semaphore::Guard::; 178 bless \\$_[0], Coro::Semaphore::guard::;
119} 179}
120 180
181sub timed_guard {
182 &timed_down
183 ? bless \\$_[0], Coro::Semaphore::guard::
184 : ();
185}
186
121sub Coro::Semaphore::Guard::DESTROY { 187sub Coro::Semaphore::guard::DESTROY {
122 &up(${${$_[0]}}); 188 &up(${${$_[0]}});
123} 189}
124 190
1251;
126
127=back 191=back
128 192
129=head1 AUTHOR 193=head1 AUTHOR
130 194
131 Marc Lehmann <pcg@goof.com> 195 Marc Lehmann <schmorp@schmorp.de>
132 http://www.goof.com/pcg/marc/ 196 http://home.schmorp.de/
133 197
134=cut 198=cut
135 199
2001
201

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines