Coro::SemaphoreSet - hash of semaphores.


NAME

Coro::SemaphoreSet - hash of semaphores.


SYNOPSIS

 use Coro::SemaphoreSet;
 $sig = new Coro::SemaphoreSet [initial value];
 $sig->down("semaphoreid"); # wait for signal
 # ... some other "thread"
 $sig->up("semaphoreid");


DESCRIPTION

This module implements sets of counting semaphores (see the Coro::Semaphore manpage). It is nothing more than a hash with normal semaphores as members, but is more efficiently managed.

This is useful if you want to allow parallel tasks to run in parallel but not on the same problem. Just use a SemaphoreSet and lock on the problem identifier.

new [inital count]
Creates a new sempahore set with the given initial lock count for each individual semaphore. See the Coro::Semaphore manpage.

$sem->down($id)
Decrement the counter, therefore ``locking'' the named semaphore. This method waits until the semaphore is available if the counter is zero.

$status = $sem->timed_down($id, $timeout)
Like down, but returns false if semaphore couldn't be acquired within $timeout seconds, otherwise true.

$sem->up($id)
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($id)
In scalar context, returns the number of coroutines waiting for this semaphore.

$guard = $sem->guard($id)
This method calls down and then creates a guard object. When the guard object is destroyed it automatically calls up.

$guard = $sem->timed_guard($id, $timeout)
Like guard, but returns undef if semaphore couldn't be acquired within $timeout seconds, otherwise the guard object.


AUTHOR

 Marc Lehmann <pcg@goof.com>
 http://www.goof.com/pcg/marc/

 Coro::SemaphoreSet - hash of semaphores.