Module Picos_std_sync.Latch

A dynamic single-use countdown latch.

Latches are typically used for determining when a finite set of parallel computations is done. If the size of the set is known a priori, then the latch can be initialized with the size as initial count and then each computation just decrements the latch.

If the size is unknown, i.e. it is determined dynamically, then a latch is initialized with a count of one, the a priori known computations are started and then the latch is decremented. When a computation is stsrted, the latch is incremented, and then decremented once the computation has finished.

type t

Represents a dynamic countdown latch.

val create : ?padded:bool -> int -> t

create initial creates a new countdown latch with the specified initial count.

  • raises Invalid_argument

    in case the specified initial count is negative.

val try_decr : t -> bool

try_decr latch attempts to decrement the count of the latch and returns true in case the count of the latch was greater than zero and false in case the count already was zero.

val decr : t -> unit

decr latch is equivalent to:

if not (try_decr latch) then
  invalid_arg "zero count"

ℹī¸ This operation is not cancelable.

  • raises Invalid_argument

    in case the count of the latch is zero.

val try_incr : t -> bool

try_incr latch attempts to increment the count of the latch and returns true on success and false on failure, which means that the latch has already reached zero.

val incr : t -> unit

incr latch is equivalent to:

if not (try_incr latch) then
  invalid_arg "zero count"
  • raises Invalid_argument

    in case the count of the latch is zero.

val await : t -> unit

await latch returns after the count of the latch has reached zero.

val await_evt : t -> unit Picos_std_event.Event.t

await_evt latch returns an event that can be committed to once the count of the latch has reached zero.