Module Await_sync.Scope

A scope for structured concurrency.

type !'a t

t represents a scope for structured concurrency.

Scopes are usually created via with_ with a local context value which is provided to each task, may have new tasks added to them (from any implementation of concurrency), and wait until all tasks exit. This way it is safe to destroy resources used within the scope after the scope exits.

Any uncaught exception within the scope will terminate the scope and all the tasks spawned into it and close the scope. This way error handling becomes simpler as one doesn't have to otherwise explicitly arrange for termination of siblings and children in case of unhandled errors.

val with_ : Await_kernel.Await.t @ local -> ('a @ portable -> (f:('a t @ local -> 'b) @ local once -> 'b) @ local) @ local @@ portable

with_ await context ~f calls f scope with a new scope for concurrency and does not return until all the tasks added to the scope have exited. An uncaught exception from f or any task added to the scope will terminate all of the tasks and the exception will then be raised out of with_ after all of the tasks have exited.

module Global : sig ... end

Allows creating non-local scopes for performing unstructured concurrency.

val context : 'a t @ local -> 'a @ local portable contended @@ portable

context scope returns the context value that the scope was created with.

val terminator : 'a t @ local -> Await_kernel.Terminator.t @ local @@ portable

terminator scope returns the terminator of the scope.

val terminate : 'a t @ local -> Base.unit @@ portable

terminate scope terminates the scope.

module Task_handle : sig ... end
module Token : sig ... end
val add : 'a t @ local -> 'a Token.t @ unique @@ portable

add scope gives a linear token representing a task being added to the scope. The token must be used or dropped.

val failure : 'a t @ local -> (Base.exn * Base.Backtrace.t) Base.or_null @@ portable

failure returns This (exn, bt) containing the first uncaught exception raised within the scope or Null in case there have been none. This is usually only called after the scope is known to have finished.