Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

coro::semaphore(3pm) [debian man page]

Semaphore(3pm)						User Contributed Perl Documentation					    Semaphore(3pm)

NAME
Coro::Semaphore - counting semaphores SYNOPSIS
use Coro; $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 thread 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. Threads then increment the count when resources are added and decrement the count when resources are removed. You don't have to load "Coro::Semaphore" manually, it will be loaded automatically when you "use Coro" and call the "new" constructor. 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->count Returns the current semaphore count. $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. $sem->down Decrement the counter, therefore "locking" the semaphore. This method waits until the semaphore is available if the counter is zero. $sem->wait 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 thread switch ("cede" etc.). Note that using "wait" is much less efficient than using "down", so try to prefer "down" whenever possible. $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 "down" the semaphore exactly once, might wake up other threads, but is NOT allowed to block (switch to other threads). $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 threads 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 calls "up". AUTHOR
Marc Lehmann <schmorp@schmorp.de> http://home.schmorp.de/ perl v5.14.2 2012-04-13 Semaphore(3pm)

Check Out this Related Man Page

SemaphoreSet(3pm)					User Contributed Perl Documentation					 SemaphoreSet(3pm)

NAME
Coro::SemaphoreSet - efficient set of counting semaphores SYNOPSIS
use Coro; $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 Coro::Semaphore). 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. You don't have to load "Coro::SemaphoreSet" manually, it will be loaded automatically when you "use Coro" and call the "new" constructor. new [inital count] Creates a new semaphore set with the given initial lock count for each individual semaphore. See Coro::Semaphore. $semset->down ($id) Decrement the counter, therefore "locking" the named semaphore. This method waits until the semaphore is available if the counter is zero. $semset->up ($id) Unlock the semaphore again. If the semaphore reaches the default count for this set and has no waiters, the space allocated for it will be freed. $semset->try ($id) Try to "down" the semaphore. Returns true when this was possible, otherwise return false and leave the semaphore unchanged. $semset->count ($id) Return the current semaphore count for the specified semaphore. $semset->waiters ($id) Returns the number (in scalar context) or list (in list context) of waiters waiting on the specified semaphore. $semset->wait ($id) Same as Coro::Semaphore::wait on the specified semaphore. $guard = $semset->guard ($id) This method calls "down" and then creates a guard object. When the guard object is destroyed it automatically calls "up". $semaphore = $semset->sem ($id) This SemaphoreSet version is based on Coro::Semaphore's. This function creates (if necessary) the underlying Coro::Semaphore object and returns it. You may legally call any Coro::Semaphore method on it, but note that calling "$semset->up" can invalidate the returned semaphore. AUTHOR
Marc Lehmann <schmorp@schmorp.de> http://home.schmorp.de/ perl v5.14.2 2012-04-13 SemaphoreSet(3pm)
Man Page