Semaphore(3pm)
π Semaphore(3pm) β the counting semaphores page, but make it cute π
okay bestie, welcome to the Semaphore manual page from the User Contributed Perl Documentation collection. she's a Perl module page, she's iconic, and she's about to teach you semaphores with zero gatekeeping. pull up a chair πͺβ¨
πΈ NAME πΈ
Coro::Semaphore β counting semaphores, periodt π
β¨ SYNOPSIS β¨
use Coro;
$sig = new Coro::Semaphore [initial value];
$sig->down; # wait for signal
# ... some other "thread"
$sig->up;
π¦ DESCRIPTION π¦
okay so. this module implements counting semaphores. you can initialize a mutex with any level of parallel users β like, you can set up a semaphore that can be "down"ed more than once until it finally blocks. that's the whole vibe.
and listen: there is no owner associated with semaphores. none. so one thread can "down" it while another thread "ups" it (or the other way around, whatever, it's not that deep), "up" can be called before "down", and honestly the semaphore is really just an integer counter that optionally blocks when it is 0. that's her. that's the whole personality. π’
counting semaphores are typically used to coordinate access to resources, with the semaphore count initialized to the number of free resources. then threads increment the count when resources are added and decrement the count when resources are removed. chef's kiss, very demure, very mindful π
you don't have to load Coro::Semaphore manually, bestie β it will be loaded automatically when you use Coro and call the new constructor. the module does the work, you just live your life π
π the methods (aka the main characters) π
new [initial count]β creates a new semaphore object with the given initial lock count. the default lock count is1, which means it is unlocked by default. zero (or negative values) are also allowed, in which case the semaphore is locked by default. yes, you can start locked. she's born that way π$sem->countβ returns the current semaphore count. the semaphore can be down'ed without blocking when the count is strictly higher than0. so, like, count > 0 = go off, count <= 0 = wait your turn β¨$sem->adjust ($diff)β atomically adds the amount given to the current semaphore count. if the count becomes positive, it wakes up any waiters. does not block if the count becomes negative, however. so don't expect it to politely pause for you π$sem->downβ decrement the counter, therefore "locking" the semaphore. this method waits until the semaphore is available if the counter is zero or less. it's giving patience, it's giving queue π$sem->waitβ similar todown, but does not actually decrement the counter. instead, when this function returns, a following call todownortryis guaranteed to succeed without blocking, until the next thread switch (cedeetc.). lowkey handy, highkey inefficient β οΈ
note that usingwaitis much less efficient than usingdown, so try to preferdownwhenever possible. fr fr, listen to the manpage.
$sem->wait ($callback)β if you pass a callback argument towait, 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). do not do it. the vibes will be ruined. π
$sem->upβ unlock the semaphore again. the reverse girlie. π«Ά$sem->tryβ try to "down" the semaphore. returns true when this was possible, otherwise returns false and leaves the semaphore unchanged. no blocking, just a yes or a no, very honest of her π$sem->waitersβ 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. not me warning you about the apocalypse in a perl manpage ππ$guard = $sem->guardβ this method callsdownand then creates a guard object. when the guard object is destroyed it automatically callsup. self-cleaning, iconic, main character energy π
π AUTHOR/SUPPORT/CONTACT π
Marc A. Lehmann <schmorp@schmorp.de> <http://software.schmorp.de/pkg/Coro.html>
and that's the page, bestie! π this lil' gem was built by the perl v5.40.1 toolchain and dated 2025-03-21, and the manual page itself is filed under Semaphore(3pm). now go forth and semaphore responsibly. mwah πππ¦
π¦ where this page comes from
- debian trixie: libcoro-perl 6.570-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libcoro-perl 6.570-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms