Module Await_kernel.Await

exception Terminated

Terminated is an exception indicating that an operation has been terminated.

type t

t is the type of implementations of awaiting. Operations that need to await something take a t that provides an implementation of awaiting for them to use.

Any awaiting operation can be terminated by the awaiting implementation, which results in a Terminated exception being raised.

val with_ : 'c 'r. terminator:Terminator.t @ local -> (yield:('c @ local -> Base.unit) Base.or_null @ local -> (await:('c @ local -> (Trigger.t -> Base.unit) @ local) @ local -> ('c @ local -> (f:(t @ local -> 'r @ local unique once) @ local once -> 'r @ local unique once) @ local) @ local) @ local) @ local @@ portable

with_ terminator ~yield ~await c ~f calls f with an awaiter that has terminator as its terminator, yield c as its implementation of yield, and await c as its implementation of await.

val create_global : terminator:Terminator.t -> yield:('c @ portable contended -> Base.unit) Base.or_null @ portable -> await: ('c @ portable contended -> (Trigger.t -> Base.unit) @ portable) @ portable -> 'c @ portable contended -> t @@ portable

create_global ~terminator ~yield ~await c creates a global awaiter with yield c as its implementation of yield and await c as its implementation of await. Both yield and await must be portable as the returned Await.t is not local, allowing it to escape to other capsules.

val terminator : t @ local -> Terminator.t @ local @@ portable

terminator t is the terminator associated with t. Awaiting operations should attempt to cancel themselves if they have been terminated, raising Terminated if they succeed in doing so.

val await : t @ local -> (on_terminate:Trigger.Source.t -> (await_on:Trigger.t -> Base.unit) @ local) @ local @@ portable

await t ~on_terminate ~await_on will use t to attach on_terminate to be signalled on termination and to wait until await_on has been signaled.

Note that the on_terminate trigger may not be used by await. The await_on trigger will be used before await returns.

val await_until_terminated : t @ local -> (Trigger.t -> Base.unit) @ local @@ portable

await_until_terminated t trigger is equivalent to await t ~on_terminate:(Trigger.source trigger) ~await_on:trigger.

val await_until_terminated_or_canceled : t @ local -> (Cancellation.t @ local -> (Trigger.t -> Base.unit) @ local) @ local @@ portable

await_until_terminated_or_canceled t c trigger is like await_until_terminated t trigger except it will also return in case the cancellation token has been cancelled .

val await_with_terminate : t @ local -> (Trigger.t -> (terminate:('r @ unique once portable contended -> Base.unit) @ once portable -> ('r @ unique once portable contended -> Base.unit) @ local once) @ local) @ local @@ portable

await_with_terminate t trigger ~terminate r will attach a trigger to call terminate r to the terminator of t and wait until trigger has been signalled.

val await_with_terminate_or_cancel : t @ local -> (Cancellation.t @ local -> (Trigger.t -> (terminate_or_cancel: ('r @ unique once portable contended -> Base.unit) @ once portable -> ('r @ unique once portable contended -> Base.unit) @ local once) @ local) @ local) @ local @@ portable

await_with_terminate_or_cancel t c trigger ~terminate_or_cancel r will attach a trigger to call terminate_or_cancel r to both the terminator of t and to the given cancellation token c and then wait until trigger has been signalled.

val is_terminated : t @ local -> Base.bool @@ portable

is_terminated t is Terminator.is_terminated (terminator t).

val with_terminator : t @ local -> (Terminator.t @ local -> t @ local) @ local @@ portable

with_terminator t new_terminator is an awaiter u like t where terminator u is new_terminator.

The main use case of with_terminator is to protect a blocking operation from being terminated by replacing the terminator with Terminator.never:

  blocking_operation (with_terminator t Terminator.never)
val await_never_terminated : t @ local -> (Trigger.t -> Base.unit) @ local @@ portable

await_never_terminated t trigger is await_until_terminated (with_terminator t Terminator.never) trigger.

val yield : t @ local -> Base.unit @@ portable

yield t yields to the scheduler using the implementation of yielding associated with t.

  • raises [Terminated]

    if the terminator associated with t has been terminated.

module For_testing : sig ... end