SemaphoreSet(3pm)

πŸ’– SemaphoreSet(3pm) πŸ’–

okay bestie, pull up a chair, because today we're talking about Coro::SemaphoreSet β€” a perl manual page and honestly? it's giving organized chaos in the best way. πŸŽ€


✨ NAME ✨

Coro::SemaphoreSet β€” an efficient set of counting semaphores. yes, a set. a little squad of semaphores. a semaphore friend group, if you will. πŸ’…


✨ SYNOPSIS ✨

use Coro;

$sig = new Coro::SemaphoreSet [initial value];

$sig->down ("semaphoreid"); # wait for signal

# ... some other "thread"

$sig->up ("semaphoreid");

see how clean that is?? no cap. πŸ¦‹


✨ DESCRIPTION ✨

So this module implements sets of counting semaphores (peep Coro::Semaphore if you wanna meet the originals). Real talk: it's literally nothing more than a hash with normal semaphores as members β€” but it's more efficiently managed. that's the whole glow-up. ✨

it's lowkey perfect when you want parallel tasks to run in parallel, but not on the same problem. just use a SemaphoreSet and lock on the problem identifier. main character energy: everyone gets a lane. 🫢

🌸 bestie note: you do not have to load Coro::SemaphoreSet manually β€” it gets loaded automatically when you use Coro and call the new constructor. the module simply arrives. iconic.

🌟 Methods 🌟

new [initial count]

creates a new semaphore set, with the given initial lock count for each individual semaphore. go read Coro::Semaphore for the deets. πŸ’œ

$semset->down ($id)

decrements the counter, therefore "locking" the named semaphore. and if the counter is at zero? this method just waits, patiently, until the semaphore is available. it's not rushing you. it's not going to rush you. 😌

$semset->up ($id)

unlocks the semaphore again. and here's the cute part: if the semaphore reaches the default count for this set and has no waiters, the space allocated for it gets freed. it cleans up after itself. we love a self-aware queen. πŸ’…

$semset->try ($id)

tries to "down" the semaphore. returns true when that was possible β€” otherwise returns false and leaves the semaphore unchanged. no side effects, no drama, no mess. πŸ™ˆ

$semset->count ($id)

returns the current semaphore count for the specified semaphore. simple. gorgeous. no notes. ✨

$semset->waiters ($id)

returns the number of waiters waiting on the specified semaphore β€” in scalar context β€” or the list of them, in list context. yes, it's context-aware. it's a switch-hitter. πŸ˜­πŸ’–

$semset->wait ($id)

same as Coro::Semaphore::wait on the specified semaphore. literally just that. fr fr. 🌈

$guard = $semset->guard ($id)

this method calls "down" and then creates a guard object. and when that guard object gets destroyed? it automatically calls "up". it's the friend who locks up behind you without being asked. πŸ‘‘

$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 heads up:

πŸ’€ warning bestie: calling $semset->up can invalidate the returned semaphore. so like. be mindful. don't be out here holding a stale reference and acting surprised. 😭

✨ AUTHOR/SUPPORT/CONTACT ✨

Marc A. Lehmann <schmorp@schmorp.de>
http://software.schmorp.de/pkg/Coro.html

and that's the whole page, bestie!! 🌸 this was perl v5.40.1, dated 2025-03-21 β€” shoutout to the version and the date, you two ate. πŸ’… mwah, go forth and semaphore responsibly. πŸŽ€πŸ’–βœ¨

πŸ“¦ where this page comes from