Skip to content

Std.Semaphore

effect Semaphore

A fixed number of permits. dbg shows the capability as <semaphore>.

  • fn available() -> Int

    The number of permits that no task holds. Does not suspend.

effect Permit

Permits held, opened with Permits or TryPermits.

  • fn count() -> Int

    How many permits are held.

handler Semaphore: Semaphore
init(permits: Int)

Makes a semaphore with permits permits. permits less than 1 is a defect.

error NoPermits

TryPermits could not take the permits without waiting.

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).

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.

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).

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.