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.53 by root, Wed Feb 1 23:59:41 2006 UTC vs.
Revision 1.76 by root, Sat Nov 8 16:46:32 2008 UTC

31 31
32=cut 32=cut
33 33
34package Coro::Semaphore; 34package Coro::Semaphore;
35 35
36BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } 36no warnings;
37 37
38use Coro (); 38use Coro ();
39 39
40$VERSION = 1.9; 40$VERSION = 4.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
50 50
51sub new { 51sub new {
52 bless [defined $_[1] ? $_[1] : 1], $_[0]; 52 bless [defined $_[1] ? $_[1] : 1], $_[0];
53} 53}
54 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
55=item $sem->down 80=item $sem->down
56 81
57Decrement the counter, therefore "locking" the semaphore. This method 82Decrement the counter, therefore "locking" the semaphore. This method
58waits until the semaphore is available if the counter is zero. 83waits until the semaphore is available if the counter is zero.
59 84
60=item $status = $sem->timed_down($timeout) 85=item $status = $sem->timed_down ($timeout)
61 86
62Like C<down>, but returns false if semaphore couldn't be acquired within 87Like C<down>, but returns false if semaphore couldn't be acquired within
63$timeout seconds, otherwise true. 88$timeout seconds, otherwise true.
64 89
65=cut 90=cut
66 91
67sub down { 92sub down {
68 while ($_[0][0] <= 0) { 93 while ($_[0][0] <= 0) {
69 push @{$_[0][1]}, $Coro::current; 94 push @{$_[0][1]}, $Coro::current;
70 Coro::schedule; 95 &Coro::schedule;
71 } 96 }
72 --$_[0][0]; 97 --$_[0][0];
73} 98}
74 99
75sub timed_down { 100sub timed_down {
76 require Coro::Timer; 101 require Coro::Timer;
77 my $timeout = Coro::Timer::timeout($_[1]); 102 my $timeout = Coro::Timer::timeout ($_[1]);
78 103
79 while ($_[0][0] <= 0) { 104 while ($_[0][0] <= 0) {
80 push @{$_[0][1]}, $Coro::current; 105 push @{$_[0][1]}, $Coro::current;
81 Coro::schedule; 106 &Coro::schedule;
82 if ($timeout) { 107 if ($timeout) {
83 # ugly as hell. slow, too, btw! 108 # ugly as hell. slow, too, btw!
84 for (0..$#{$_[0][1]}) { 109 for (0..$#{$_[0][1]}) {
85 if ($_[0][1][$_] == $Coro::current) { 110 if ($_[0][1][$_] == $Coro::current) {
86 splice @{$_[0][1]}, $_, 1; 111 splice @{$_[0][1]}, $_, 1;
137=item $guard = $sem->guard 162=item $guard = $sem->guard
138 163
139This 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
140object is destroyed it automatically calls C<up>. 165object is destroyed it automatically calls C<up>.
141 166
142=item $guard = $sem->timed_guard($timeout) 167=item $guard = $sem->timed_guard ($timeout)
143 168
144Like C<guard>, but returns undef if semaphore couldn't be acquired within 169Like C<guard>, but returns undef if semaphore couldn't be acquired within
145$timeout seconds, otherwise the guard object. 170$timeout seconds, otherwise the guard object.
146 171
147=cut 172=cut

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines