Module Await_sync.Semaphore

A poisonable counting semaphore.

module Acquired_or_would_block : sig ... end

A poisonable counting semaphore.

type t

Represents a poisonable counting semaphore.

val sexp_of_t : t -> Sexplib0.Sexp.t
val sexp_of_t__stack : t @ local -> Sexplib0.Sexp.t @ local
val max_value : int @@ portable

Maximum counter value allowed by the semaphore implementation.

val create : ?padded:bool @ local -> (int -> t) @ local @@ portable

create n creates a new counting semaphore with the given count n.

The optional padded argument specifies whether to pad the data structure to avoid false sharing. See Atomic.make for a longer explanation.

val release : t @ local -> unit @@ portable

release t increments the count of the semaphore or does nothing in case the semaphore has been poisoned.

  • raises Sys_error

    in case the count would overflow.

val acquire : Await_kernel.Await.t @ local -> (t @ local -> unit) @ local @@ portable

acquire w t waits until the count of the semaphore is greater than 0 and then atomically decrements the count.

val acquire_or_cancel : Await_kernel.Await.t @ local -> (Await_kernel.Cancellation.t @ local -> (t @ local -> unit Await_kernel.Or_canceled.t) @ local) @ local @@ portable

acquire_or_cancel w c t is Completed (acquire w t) if c is not canceled, otherwise it is Canceled.

val try_acquire : t @ local -> Acquired_or_would_block.t @@ portable

try_acquire t attempts to atomically and in a wait-free way decrement the count of the semaphore, unless the count is already 0. Returns Acquired if the semaphore was successfully acquired, or Would_block if the count is already 0 and the operation would block

val get_value : t @ local -> int @@ portable

get_value t returns the current count of the semaphore or 0 in case the semaphore has been poisoned.

This should only be used for debugging or informational messages.

val poison : t @ local -> unit @@ portable

poison t marks the semaphore as poisoned.

val is_poisoned : t @ local -> bool @@ portable

is_poisoned t determines whether the semaphore has been poisoned.