Module Await.ScopeSource

A scope for structured concurrency.

Sourcetype !'a t : (value mod contended non_float portable unforkable unyielding) with 'a @@ contended portable

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.

Sourceval with_ : Await_kernel.Await.t @ local -> 'a @ portable -> f: (Await_kernel.Await.t @ local -> ('a t @ local -> 'b @ unique once) @ local once) @ local once unyielding -> 'b @ unique once @@ 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.

Sourcemodule Global : sig ... end

Allows creating non-local scopes for performing unstructured concurrency.

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

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

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

terminator scope returns the terminator of the scope.

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

terminate scope terminates the scope.

Sourcemodule Task_handle : sig ... end
Sourcemodule Token : sig ... end
Sourceval 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.

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