Module Thread_pool

module Thread_pool: Thread_pool

module Priority: module type of Linux_ext.Priority  with type t = Linux_ext.Priority.t
type t 
include Invariant.S
val create : max_num_threads:int -> t Core.Std.Or_error.t
create ~max_num_threads returns a new thread pool. It is an error if max_num_threads < 1.
val finished_with : t -> unit Core.Std.Or_error.t
finished_with t destroys all the threads in t, and makes t no longer usable.

It is an error to call finished_with if the thread pool has unfinished work or unfinished helper threads. It is an error to call any other operation on t after calling finished_with t.

val max_num_threads : t -> int
max_num_threads t returns the maximum number of threads that t is allowed to create.
val num_threads : t -> int
num_threads t returns the number of threads that the pool t has created.
val default_priority : t -> Priority.t
default_priority t returns the priority that will be used for work performed by t, unless that work is added with an overriding priority.
module Work_group: sig .. end
val create_work_group : ?min_assignable_threads:int ->
?max_assigned_threads:int ->
t -> Work_group.t Core.Std.Or_error.t
create_work_group t ~min_assignable_threads ~max_assigned_threads creates a new work group.

The thread pool does not internally refer to the Work_group.t it returns. So, it is OK for client code to use a finalizer to detect it becoming unused.

It is an error if any of the following are true:


val add_work_for_group : ?priority:Priority.t ->
?name:string ->
t ->
Work_group.t -> (unit -> unit) -> unit Core.Std.Or_error.t
add_work_for_group ?priority ?name t work_group f enqueues f to be done by some thread in the pool, subject to the thread-usage limits of work_group.

Exceptions raised by f are silently ignored.

It is an error to call add_work_for_group t work_group after having called finished_with_work_group t work_group.

While the work is run, the name of the thread running the work will be set (via Linux_ext.pr_set_name) to name and the priority of the thread will be set to priority.

val finished_with_work_group : t -> Work_group.t -> unit Core.Std.Or_error.t
finished_with_work_group t work_group informs thread pool t that the work_group will no longer be used.

It is an error to call finished_with_work_group work_group if work_group has unfinished work or has helper_threads for which finished_with_helper_thread hasn't yet been called.

module Helper_thread: sig .. end
val create_helper_thread : ?priority:Priority.t ->
?name:string ->
t ->
Work_group.t -> Helper_thread.t Core.Std.Or_error.t
create_helper_thread ?priority ?name t work_group creates a new helper thread that is part of work_group, i.e. until finished_with_helper_thread is called, the helper thread counts as one of the threads assigned to the work_group.

The thread pool does not internally refer to the Helper_thread.t it returns. So, it is OK for client code to use a finalizer to detect it becoming unused.

It is an error if no threads are available.

When the helper thread runs work, it will be at the helper thread's name and priority, except for work that is added with an overriding priority or name.

val add_work_for_helper_thread : ?priority:Priority.t ->
?name:string ->
t ->
Helper_thread.t -> (unit -> unit) -> unit Core.Std.Or_error.t
add_work_for_helper_thread ?priority ?name t helper_thread f enqueues f on helper_thread's work queue.

Exceptions raised by f are silently ignored.

It is an error to call add_work_for_helper_thread t after finished_with_helper_thread t.

When the helper thread runs f, it will be at the helper thread's name and priority, unless overriden by name or priority.

val finished_with_helper_thread : t -> Helper_thread.t -> unit Core.Std.Or_error.t
finished_with_helper_thread t helper_thread returns the helper thread to the general thread pool.

It is an error to call finished_with_helper_thread if the helper thread has unfinished work.

val sexp_of_t : t -> Sexplib.Sexp.t