NAME
Coro::Semaphore - non-binary semaphores
SYNOPSIS
use Coro::Semaphore;
$sig = new Coro::Semaphore [initial value];
$sig->down; # wait for signal
# ... some other "thread"
$sig->up;
DESCRIPTION
This module implements counting semaphores. You can initialize a mutex with any level of parallel users, that is, you can intialize a sempahore that can be down
ed more than once until it blocks. There is no owner associated with semaphores, so one coroutine can down
it while another can up
it.
Counting semaphores are typically used to coordinate access to resources, with the semaphore count initialized to the number of free resources. Coroutines then increment the count when resources are added and decrement the count when resources are removed.
- new [inital count]
-
Creates a new sempahore object with the given initial lock count. The default lock count is 1, which means it is unlocked by default. Zero (or negative values) are also allowed, in which case the semaphore is locked by default.
- $sem->down
-
Decrement the counter, therefore "locking" the semaphore. This method waits until the semaphore is available if the counter is zero.
- $status = $sem->timed_down($timeout)
-
Like
down
, but returns false if semaphore couldn't be acquired within $timeout seconds, otherwise true. - $sem->up
-
Unlock the semaphore again.
- $sem->try
-
Try to
down
the semaphore. Returns true when this was possible, otherwise return false and leave the semaphore unchanged. - $sem->waiters
-
In scalar context, returns the number of coroutines waiting for this semaphore.
- $guard = $sem->guard
-
This method calls
down
and then creates a guard object. When the guard object is destroyed it automatically callsup
. - $guard = $sem->timed_guard($timeout)
-
Like
guard
, but returns undef if semaphore couldn't be acquired within $timeout seconds, otherwise the guard object.
AUTHOR
Marc Lehmann <schmorp@schmorp.de>
http://home.schmorp.de/