Module Concurrent.SchedulerSource

Sourcetype 'ctx concurrent := 'ctx t
Sourcetype ('resource, 'scope_ctx, 'concurrent_ctx) spawn_fn = 'scope_ctx Concurrent__.Types.Scope.t @ local -> ('scope_ctx Concurrent__.Types.Scope.Task_handle.t @ local unique -> 'concurrent_ctx @ local -> 'concurrent_ctx Concurrent__.Types.concurrent @ local portable -> 'resource @ unique once portable contended -> Base.unit) Concurrent__.Types.task @ once portable -> 'resource @ unique once portable contended -> 'resource Concurrent__.Types.spawn_result @ unique once portable contended
Sourcetype 'ctx t : value mod aliased contended non_float unyielding

Scheduler.t is the type representing a handle to a concurrent scheduler. A handle to the scheduler allows spawning unstructured concurrent tasks.

Sourcetype packed =
  1. | T : 'ctx t -> packed
Sourceval create : 'concurrent_ctx. spawn: (('resource : value_or_null) 'scope_ctx. ('resource, 'scope_ctx, 'concurrent_ctx) spawn_fn) -> 'concurrent_ctx t

create ~spawn creates a new scheduler with the given spawn function.

Sourceval with_context : 'ctx1 t @ portable -> ('ctx1 @ local -> 'ctx1 concurrent @ local portable -> f:('ctx2 @ local -> Base.unit) @ local once -> Base.unit) @ portable -> 'ctx2 t @ portable

with_context t ctx is a scheduler enriched with the new context-building function ctx, which is called on all spawned concurrent tasks with both the new Concurrent.t and the old context, in addition to a callback for the new context.

Sourceval spawn : 'concurrent_ctx t @ local -> 'scope_ctx Scope.t @ local -> ('scope_ctx Concurrent__.Types.Scope.t @ local -> ('concurrent_ctx @ local -> ('concurrent_ctx t @ local portable -> Base.unit @ portable contended) @ local once) @ local once) Task.t @ once portable -> Base.unit

spawn scheduler scope task spawns a new concurrent task into the given scope to execute the given task using the scheduler scheduler.

Sourceval spawn_with : 'concurrent_ctx t @ local -> 'scope_ctx Scope.t @ local -> ('scope_ctx Scope.t @ local -> ('concurrent_ctx @ local -> ('concurrent_ctx concurrent @ local portable -> ('resource @ unique once portable contended -> Base.unit) @ local once) @ local once) @ local once) Task.t @ once portable -> 'resource @ unique once portable contended -> 'resource spawn_result @ unique once portable contended

spawn_with scheduler task resource spawns a new concurrent task to execute the given task, using the scheduler scheduler providing it with some unique resource

The resource is guaranteed to either be passed to f, or returned as part of the Failed spawn_result to be handled by the caller.

Sourceval spawn_daemon : 'concurrent_ctx t @ local -> 'scope_ctx Scope.t @ local -> ('scope_ctx Concurrent__.Types.Scope.t @ local -> (Await.Cancellation.t @ local -> ('concurrent_ctx @ local -> ('concurrent_ctx t @ local portable -> Base.unit Await.Or_canceled.t @ portable contended) @ local once) @ local once) @ local once) Task.t @ once portable -> Base.unit

spawn_daemon scheduler scope task spawns a concurrent daemon task executing f into the given scope. Daemon tasks are given a cancellation token which is canceled once the outer scope and all non-daemon tasks spawned into it finish.

If the scope is terminated due to an exception, daemon tasks are terminated rather than canceled.

Sourceval spawn_daemon' : 'concurrent_ctx t @ local -> 'scope_ctx Scope.t @ local -> ('scope_ctx Scope.t @ local -> (Await.Cancellation.t @ local -> ('concurrent_ctx @ local -> ('concurrent_ctx concurrent @ local portable -> Base.unit) @ local once) @ local once) @ local once) Task.t @ once portable -> Base.unit

spawn_daemon' is like spawn_daemon, except the task function it expects returns unit instead of unit Or_canceled.t.