Module Await.MvarSource

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

Sourcetype !'a t : value mod contended non_float portable

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.

Sourceval create : ?padded:bool @ local -> unit -> 'a t @@ 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.

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

create_full v returns a new mvar filled with v.

Sourceval put : Await_kernel.Await.t @ local -> 'a t @ local -> 'a @ unique once portable contended -> unit @@ 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.

Sourceval 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 @@ portable

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

Sourcemodule Ok_or_already_full : sig ... end
Sourceval try_put : 'a t @ local -> 'a @ unique once portable contended -> Ok_or_already_full.t @@ portable

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

Sourceval put_exn : 'a t @ local -> 'a @ unique once portable contended -> unit @@ portable

put_exn t a sets the value of t to a.

Sourceval take : Await_kernel.Await.t @ local -> 'a t @ local -> 'a @ unique once portable contended @@ 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.

Sourceval 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 @@ portable

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

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

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

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

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

Sourceval wait_until_empty : Await_kernel.Await.t @ local -> _ t @ local -> unit @@ portable

wait_until_empty w t waits until the mvar t becomes empty.

  • raises Terminated

    if w is terminated before t becomes empty.

Sourceval wait_until_empty_or_cancel : Await_kernel.Await.t @ local -> Await_kernel.Cancellation.t @ local -> _ t @ local -> unit Await_kernel.Or_canceled.t @@ 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.