Module Parallel_scheduler

type t

t represents a scheduler that runs each task on one member of an internal work-stealing pool of worker domains.

val create : ?max_domains:Base.int -> Base.unit -> t

create ~max_domains () creates a scheduler that spawns worker threads on no more than max_domains domains.

val stop : t -> Base.unit

stop t waits for pending tasks to complete and joins all worker domains. Attempting to schedule new tasks after calling stop will raise.

During stop, idle workers will not attempt to steal asynchronous tasks.

val is_stopped : t -> Base.bool

is_stopped t returns true if t has been stopped.

val parallel : t -> f:(Parallel_kernel.t @ local -> 'a) @ once shareable -> 'a

parallel t ~f creates an implementation of parallelism backed by t, applies f, and waits for it to complete.

val concurrent : t -> terminator:Await.Terminator.t @ local -> (f:(Parallel_kernel.t Concurrent.t @ local portable -> 'a) @ once shareable -> 'a) @ local

concurrent t ~terminator ~f creates an implementation of concurrency and parallelism backed by t, applies f, and waits for it to complete. Blocking operations in f may be terminated via terminator.

There is currently no mechanism to limit the creation rate of concurrent tasks. If concurrent work is generated faster than it can be executed, the scheduler's queues will grow unboundedly, leading to resource exhaustion.

Running a concurrent task requires allocating a fiber, which consumes a significant chunk of memory and address space (similar to a thread). There is a lighter-weight implementation of fibers based on stack checks, but it is not currently available.

module Expert : sig ... end