From 059ee8a1d533cd0ebc26cc8b8f1e2ec373718443 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 15 Jun 2023 10:35:41 -0400 Subject: [PATCH] add `Pool.num_tasks` --- src/pool.ml | 7 ++++++- src/pool.mli | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/pool.ml b/src/pool.ml index 8cb1912e..87257f7d 100644 --- a/src/pool.ml +++ b/src/pool.ml @@ -51,7 +51,12 @@ let run (self : t) (f : task) : unit = | Exit -> () | Bb_queue.Closed -> raise Shutdown -let size self = Array.length self.threads +let[@inline] size self = Array.length self.threads + +let num_tasks (self : t) : int = + let n = ref 0 in + Array.iter (fun q -> n := !n + Bb_queue.size q) self.qs; + !n exception Got_task of task diff --git a/src/pool.mli b/src/pool.mli index d2e8a72c..3fc6c983 100644 --- a/src/pool.mli +++ b/src/pool.mli @@ -52,6 +52,11 @@ val create : val size : t -> int (** Number of threads *) +val num_tasks : t -> int +(** Current number of tasks. This is at best a snapshot, useful for metrics + and debugging. + @since 0.2 *) + val shutdown : t -> unit (** Shutdown the pool and wait for it to terminate. Idempotent. *)