29 Jul 2020 13:10:08 UTC
- Distribution: Coro
- Module version: 6.57
- Source (raw)
- Browse (raw)
- How to Contribute
- Issues (8)
- Testers (939 / 22 / 0)
- KwaliteeBus factor: 1
- % Coverage
- License: unknown
- Activity24 month
- Download (193.48KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest version
Coro::Semaphore - counting semaphores
use Coro; $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 initialize a sempahore that can be
downed more than once until it blocks. There is no owner associated with semaphores, so one thread can
downit while another can
upit (or vice versa),
upcan be called before
downand so on: the semaphore is really just an integer counter that optionally blocks when it is 0.
Counting semaphores are typically used to coordinate access to resources, with the semaphore count initialized to the number of free resources. Threads then increment the count when resources are added and decrement the count when resources are removed.
You don't have to load
Coro::Semaphoremanually, it will be loaded automatically when you
use Coroand call the
- new [initial 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.
Returns the current semaphore count. The semaphore can be down'ed without blocking when the count is strictly higher than
- $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.
Decrement the counter, therefore "locking" the semaphore. This method waits until the semaphore is available if the counter is zero or less.
down, but does not actually decrement the counter. Instead, when this function returns, a following call to
tryis guaranteed to succeed without blocking, until the next thread switch (
Note that using
waitis much less efficient than using
down, so try to prefer
- $sem->wait ($callback)
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
downthe semaphore exactly once, might wake up other threads, but is NOT allowed to block (switch to other threads).
Unlock the semaphore again.
downthe semaphore. Returns true when this was possible, otherwise return false and leave the semaphore unchanged.
In scalar context, returns the number of threads waiting for this semaphore. Might accidentally cause WW3 if called in other contexts, so don't use these.
- $guard = $sem->guard
This method calls
downand then creates a guard object. When the guard object is destroyed it automatically calls
Marc A. Lehmann <firstname.lastname@example.org> http://software.schmorp.de/pkg/Coro.html