package moonpool

  1. Overview
  2. Docs

Module Moonpool.RunnerSource

Abstract runner.

This provides an abstraction for running tasks in the background.

  • since 0.3
Sourcetype task = unit -> unit
Sourcetype t = private {
  1. run_async : task -> unit;
  2. shutdown : wait:bool -> unit -> unit;
  3. size : unit -> int;
  4. num_tasks : unit -> int;
}

A runner.

If a runner is no longer needed, shutdown can be used to signal all worker threads in it to stop (after they finish their work), and wait for them to stop.

The threads are distributed across a fixed domain pool (whose size is determined by Domain.recommended_domain_count on OCaml 5, and simple the single runtime on OCaml 4).

Sourceval size : t -> int

Number of threads/workers.

Sourceval num_tasks : t -> int

Current number of tasks. This is at best a snapshot, useful for metrics and debugging.

Sourceval shutdown : t -> unit

Shutdown the runner and wait for it to terminate. Idempotent.

Sourceval shutdown_without_waiting : t -> unit

Shutdown the pool, and do not wait for it to terminate. Idempotent.

Sourceexception Shutdown
Sourceval run_async : t -> task -> unit

run_async pool f schedules f for later execution on the runner in one of the threads. f() will run on one of the runner's worker threads/domains.

  • raises Shutdown

    if the runner was shut down before run_async was called.

Sourceval run_wait_block : t -> (unit -> 'a) -> 'a

run_wait_block pool f schedules f for later execution on the pool, like run_async. It then blocks the current thread until f() is done executing, and returns its result. If f() raises an exception, then run_wait_block pool f will raise it as well.

NOTE be careful with deadlocks (see notes in Fut.wait_block).

Sourcemodule For_runner_implementors : sig ... end