Moonpool.Ws_poolWork-stealing thread pool.
A pool of threads with a worker-stealing scheduler. The pool contains a fixed number of threads that wait for work items to come, process these, and loop.
This is good for CPU-intensive tasks that feature a lot of small tasks. Note that tasks will not always be processed in the order they are scheduled, so this is not great for workloads where the latency of individual tasks matter (for that see Fifo_pool).
This implements Runner.t since 0.3.
If a pool is no longer needed, shutdown can be used to signal all 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 simply the single runtime on OCaml 4).
include module type of RunnerA 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).
val size : t -> intNumber of threads/workers.
val num_tasks : t -> intCurrent number of tasks. This is at best a snapshot, useful for metrics and debugging.
val shutdown : t -> unitShutdown the runner and wait for it to terminate. Idempotent.
val shutdown_without_waiting : t -> unitShutdown the pool, and do not wait for it to terminate. Idempotent.
val run_async : ?ls:Task_local_storage.t -> t -> task -> unitrun_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.
val run_wait_block : ?ls:Task_local_storage.t -> t -> (unit -> 'a) -> 'arun_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 about the required discipline to avoid deadlocks).
val dummy : tRunner that fails when scheduling tasks on it. Calling run_async on it will raise Failure.
module For_runner_implementors : sig ... endThis module is specifically intended for users who implement their own runners. Regular users of Moonpool should not need to look at it.
val get_current_runner : unit -> t optionAccess the current runner. This returns Some r if the call happens on a thread that belongs in a runner.
val get_current_storage : unit -> Task_local_storage.t optionget_current_storage runner gets the local storage for the currently running task.
type ('a, 'b) create_args =
?on_init_thread:(dom_id:int -> t_id:int -> unit -> unit) ->
?on_exit_thread:(dom_id:int -> t_id:int -> unit -> unit) ->
?on_exn:(exn -> Stdlib.Printexc.raw_backtrace -> unit) ->
?around_task:((t -> 'b) * (t -> 'b -> unit)) ->
?num_threads:int ->
?name:string ->
'aval create : (unit -> t, _) create_argscreate () makes a new thread pool.
val with_ : (unit -> (t -> 'a) -> 'a, _) create_args