From 9f04d254afa96a9877b23aab309184da0cd68331 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 24 Jun 2023 15:00:50 -0400 Subject: [PATCH] feat: Pool.run_wait_block can return a value now --- src/pool.ml | 8 ++++---- src/pool.mli | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/pool.ml b/src/pool.ml index 386f2209..b9f511b1 100644 --- a/src/pool.ml +++ b/src/pool.ml @@ -63,17 +63,17 @@ let run_async (self : t) (task : task) : unit = let run = run_async -let run_wait_block self task : unit = +let run_wait_block self (f : unit -> 'a) : 'a = let q = Bb_queue.create () in run_async self (fun () -> try - task (); - Bb_queue.push q (Ok ()) + let x = f () in + Bb_queue.push q (Ok x) with exn -> let bt = Printexc.get_raw_backtrace () in Bb_queue.push q (Error (exn, bt))); match Bb_queue.pop q with - | Ok () -> () + | Ok x -> x | Error (exn, bt) -> Printexc.raise_with_backtrace exn bt let[@inline] size self = Array.length self.threads diff --git a/src/pool.mli b/src/pool.mli index 473c88e7..4df914e8 100644 --- a/src/pool.mli +++ b/src/pool.mli @@ -82,10 +82,12 @@ val run : t -> (unit -> unit) -> unit [@@deprecated "use run_async"] (** deprecated alias to {!run_async} *) -val run_wait_block : t -> (unit -> unit) -> unit +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. + 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. {b NOTE} be careful with deadlocks (see notes in {!Fut.wait_block}). @since 0.3 *)