Coro::Semaphore - counting semaphores
use Coro::Semaphore; $sig = new Coro::Semaphore [initial value]; $sig->down; # wait for signal # ... some other "thread" $sig->up;
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.
Similar to down
, but does not actually decrement the counter. Instead,
when this function returns, a following call to down
or try
is
guaranteed to succeed without blocking, until the next coroutine switch
(cede
etc.).
Note that using wait
is much less efficient than using down
, so try
to prefer down
whenever possible.
If you pass a callback argument to wait
, it will not wait, but
immediately return. The callback will be called as soon as the semaphore
becomes available (which might be instantly), and gets passed the
semaphore as first argument.
The callback might down
the semaphore exactly once, might wake up other
coroutines, but is NOT allowed to block (switch to other coroutines).
This is considered a rather experimental interface, and is subject to change.
down
the semaphore. Returns true when this was possible,
otherwise return false and leave the semaphore unchanged.
This method calls down
and then creates a guard object. When the guard
object is destroyed it automatically calls up
.
Marc Lehmann <schmorp@schmorp.de> http://home.schmorp.de/