Lyricdev
GitHub

class Semaphore

pub class Semaphore

A count of permits that tasks take and give back.

Semaphore.release, from any task on any thread, ends the wait of those in Semaphore.acquire, and nobody needs to hold a permit to give one back. A permit is no hold, so a panic poisons no semaphore.

Methods

pub static fn new(permits: int): Semaphore

Creates a semaphore of permits permits.

Panics

Panics if permits is negative.

pub fn acquire(): void throws Cancelled

Takes a permit, waiting while none is left.

Errors

Throws Cancelled, and takes no permit, when the task is cancelled before or during the wait.

Panics

Panics if no task is running.

pub fn tryAcquire(): bool

Takes a permit where one is left, without waiting, and returns whether it took one.

pub fn release(): void

Gives a permit back, and wakes the tasks that wait for one.

pub fn available(): int

Returns the permits left now; another thread may take or give one the next moment.