--- Coro/Coro/Semaphore.pm 2003/11/05 20:02:46 1.36 +++ Coro/Coro/Semaphore.pm 2008/05/29 03:31:52 1.62 @@ -33,11 +33,11 @@ package Coro::Semaphore; -BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } +no warnings; use Coro (); -$VERSION = 0.8; +$VERSION = 4.73; =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,18 +92,18 @@ sub down { while ($_[0][0] <= 0) { push @{$_[0][1]}, $Coro::current; - Coro::schedule; + &Coro::schedule; } --$_[0][0]; } sub timed_down { require Coro::Timer; - my $timeout = Coro::Timer::timeout($_[1]); + my $timeout = Coro::Timer::timeout ($_[1]); 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. @@ -163,14 +188,14 @@ &up(${${$_[0]}}); } -1; - =back =head1 AUTHOR - Marc Lehmann - http://www.goof.com/pcg/marc/ + Marc Lehmann + http://home.schmorp.de/ =cut +1 +