--- Coro/Coro/Semaphore.pm 2005/12/26 23:20:59 1.51 +++ Coro/Coro/Semaphore.pm 2007/07/25 14:30:48 1.57 @@ -37,7 +37,7 @@ use Coro (); -$VERSION = 1.7; +$VERSION = 1.9; =item new [inital count] @@ -52,12 +52,37 @@ bless [defined $_[1] ? $_[1] : 1], $_[0]; } +=item $sem->count + +Returns the current semaphore count. + +=cut + +sub count { + $_[0][0] +} + +=item $sem->adjust ($diff) + +Atomically adds the amount given to the current semaphore count. If the +count becomes positive, wakes up any waiters. Does not block if the count +becomes negative, however. + +=cut + +sub adjust { + # basically a weird copy of up + if (($_[0][0] += $_[1]) > 0) { + (shift @{$_[0][1]})->ready if @{$_[0][1]}; + } +} + =item $sem->down Decrement the counter, therefore "locking" the semaphore. This method waits until the semaphore is available if the counter is zero. -=item $status = $sem->timed_down($timeout) +=item $status = $sem->timed_down ($timeout) Like C, but returns false if semaphore couldn't be acquired within $timeout seconds, otherwise true. @@ -67,7 +92,7 @@ sub down { while ($_[0][0] <= 0) { push @{$_[0][1]}, $Coro::current; - Coro::schedule; + &Coro::schedule; } --$_[0][0]; } @@ -78,7 +103,7 @@ while ($_[0][0] <= 0) { push @{$_[0][1]}, $Coro::current; - Coro::schedule; + &Coro::schedule; if ($timeout) { # ugly as hell. slow, too, btw! for (0..$#{$_[0][1]}) { @@ -139,7 +164,7 @@ This method calls C and then creates a guard object. When the guard object is destroyed it automatically calls C. -=item $guard = $sem->timed_guard($timeout) +=item $guard = $sem->timed_guard ($timeout) Like C, but returns undef if semaphore couldn't be acquired within $timeout seconds, otherwise the guard object.