Legend:
Library
Module
Module type
Parameter
Class
Class type
Library
Module
Module type
Parameter
Class
Class type
Thread pool.
A pool of threads. The pool contains a fixed number of threads that wait for work items to come, process these, and loop.
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 Runner
type t = private {
run_async : task -> unit;
shutdown : wait:bool -> unit -> unit;
size : unit -> int;
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).
val size : t -> int
Number of threads/workers.
val num_tasks : t -> int
Current number of tasks. This is at best a snapshot, useful for metrics and debugging.
val shutdown : t -> unit
Shutdown the runner and wait for it to terminate. Idempotent.
val shutdown_without_waiting : t -> unit
Shutdown the pool, and do not wait for it to terminate. Idempotent.
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.
val 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
).
module For_runner_implementors : sig ... end
A thread wrapper f
takes the current thread, the current pool, and the worker function loop : unit -> unit
which is the worker's main loop, and returns a new loop function. By default it just returns the same loop function but it can be used to install tracing, effect handlers, etc.
val add_global_thread_loop_wrapper : thread_loop_wrapper -> unit
add_global_thread_loop_wrapper f
installs f
to be installed in every new pool worker thread, for all existing pools, and all new pools created with create
. These wrappers accumulate: they all apply, but their order is not specified.
type 'a create_args =
?on_init_thread:(dom_id:int -> t_id:int -> unit -> unit) ->
?on_exit_thread:(dom_id:int -> t_id:int -> unit -> unit) ->
?thread_wrappers:thread_loop_wrapper list ->
?on_exn:(exn -> Printexc.raw_backtrace -> unit) ->
?around_task:((t -> 'a) * (t -> 'a -> unit)) ->
?min:int ->
?per_domain:int ->
'a
val create : (unit -> t) create_args
create ()
makes a new thread pool.
val with_ : (unit -> (t -> 'a) -> 'a) create_args