Module Await_sync.Mvar

A linear mutable cell that can be either empty or full (i.e., hold a single unique value at a time).

type !'a t

An 'a Mvar.t is a mutable cell that is either empty or contains a single value of type 'a. One can put new values, and wait to read the value with take.

Mvars are entirely linear - every put value is taken exactly once.

val create : ?padded:bool @ local -> (unit -> 'a t) @ local @@ portable

create () returns a new empty mvar.

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

val create_full : ?padded:bool @ local -> ('a @ unique once portable contended -> 'a t) @ local @@ portable

create_full v returns a new mvar filled with v.

val put : Await_kernel.Await.t @ local -> ('a t @ local -> ('a @ unique once portable contended -> unit) @ local) @ local @@ portable

put w t a waits using w until the mvar t is empty, and then sets the value to a. If there are multiple concurrent puts, there is no fairness guarantee (ie, puts may happen out of order or may be starved).

  • raises Terminated

    if w is terminated before t becomes empty.

val put_or_cancel : Await_kernel.Await.t @ local -> (Await_kernel.Cancellation.t @ local -> ('a t @ local -> ('a @ unique once portable contended -> unit Await_kernel.Or_canceled.t) @ local) @ local) @ local @@ portable

put_or_cancel w c t a is Completed (put w t a) if c is not canceled, otherwise it is Canceled.

module Ok_or_already_full : sig ... end
val try_put : 'a t @ local -> ('a @ unique once portable contended -> Ok_or_already_full.t) @ local @@ portable

try_put t a sets the value of t to a and returns Ok if it is empty, otherwise it returns Already_full.

val put_exn : 'a t @ local -> ('a @ unique once portable contended -> unit) @ local @@ portable

put_exn t a sets the value of t to a.

val take : Await_kernel.Await.t @ local -> ('a t @ local -> 'a @ unique once portable contended) @ local @@ portable

take w t waits using w until the mvar t is full, then clears it and returns its value. If there are multiple concurrent calls to take then only one will be fulfilled and the others will continue waiting on future values. There is no ordering guarantee for which take call will be filled first.

  • raises Terminated

    if w is terminated before t becomes full.

val take_or_cancel : Await_kernel.Await.t @ local -> (Await_kernel.Cancellation.t @ local -> ('a t @ local -> 'a Await_kernel.Or_canceled.t @ unique once portable contended) @ local) @ local @@ portable

take_or_cancel w c t a is Completed (take w t a) if c is not canceled, otherwise it is Canceled.

val try_take : 'a t @ local -> 'a or_null @ unique once portable contended @@ portable

try_take t empties the mvar t and returns This v if it is currently full, or returns Null otherwise.

val take_exn : 'a t @ local -> 'a @ unique once portable contended @@ portable

take_exn empties the mvar t and returns its value v if it is currently full.

  • raises Empty

    in case t was empty.

val is_full : _ t @ local -> bool @@ portable

is_full t is true if the mvar t is currently full.

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

wait_until_empty w t waits until the mvar t becomes empty.

  • raises Terminated

    if w is terminated before t becomes empty.

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

wait_until_empty_or_cancel w c t is Completed (wait_until_empty w t) if c is not canceled, otherwise it is Canceled.