Std.Semaphore
Semaphore
Section titled “Semaphore”effect SemaphoreA fixed number of permits. dbg shows the capability as <semaphore>.
-
fn available() -> IntThe number of permits that no task holds. Does not suspend.
Permit
Section titled “Permit”effect PermitPermits held, opened with Permits or TryPermits.
-
fn count() -> IntHow many permits are held.
Semaphore
Section titled “Semaphore”handler Semaphore: Semaphoreinit(permits: Int)Makes a semaphore with permits permits. permits less than 1 is a defect.
NoPermits
Section titled “NoPermits”error NoPermitsTryPermits could not take the permits without waiting.
with_permit
Section titled “with_permit”fn with_permit[A, E, R](s: use Semaphore, f: fn() -> A fails E uses { R }) -> A fails E uses { Async, R, s }Waits for a permit, runs f, and gives the permit back however f ends. Same as
with_permits(s, 1, f).
with_permits
Section titled “with_permits”fn with_permits[A, E, R](s: use Semaphore, n: Int, f: fn() -> A fails E uses { R }) -> A fails E uses { Async, R, s }Opens Permits of n, runs f in it, and gives the permits back when f ends: with a
value, an error, a defect or an interruption.
try_with_permit
Section titled “try_with_permit”fn try_with_permit[A, E, R](s: use Semaphore, f: fn() -> A fails E uses { R }) -> Option[A] fails E uses { R, s }Runs f with a permit only if one can be taken without waiting. Same as
try_with_permits(s, 1, f).
try_with_permits
Section titled “try_with_permits”fn try_with_permits[A, E, R](s: use Semaphore, n: Int, f: fn() -> A fails E uses { R }) -> Option[A] fails E uses { R, s }Opens TryPermits of n and runs f in it, returning Some of its value; returns None
without running f if the permits cannot be taken. If f itself fails with NoPermits, this
returns None too.