A counting semaphore.
The API is based on OCaml's Semaphore.Counting
.
The difference is that when waiting for the semaphore this will switch to the next runnable fiber, whereas the stdlib one will block the whole domain.
Semaphores are thread-safe and so can be shared between domains and used to synchronise between them.
The type of counting semaphores.
make n
returns a new counting semaphore, with initial value n
. The initial value n
must be nonnegative.
release t
increments the value of semaphore t
. If other fibers are waiting on t
, the one that has been waiting the longest is resumed.
acquire t
blocks the calling fiber until the value of semaphore t
is not zero, then atomically decrements the value of t
and returns.
get_value t
returns the current value of semaphore t
.