feat: Pool.run_wait_block can return a value now

This commit is contained in:
Simon Cruanes 2023-06-24 15:00:50 -04:00
parent e4b159c695
commit 9f04d254af
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 8 additions and 6 deletions

View file

@ -63,17 +63,17 @@ let run_async (self : t) (task : task) : unit =
let run = run_async 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 let q = Bb_queue.create () in
run_async self (fun () -> run_async self (fun () ->
try try
task (); let x = f () in
Bb_queue.push q (Ok ()) Bb_queue.push q (Ok x)
with exn -> with exn ->
let bt = Printexc.get_raw_backtrace () in let bt = Printexc.get_raw_backtrace () in
Bb_queue.push q (Error (exn, bt))); Bb_queue.push q (Error (exn, bt)));
match Bb_queue.pop q with match Bb_queue.pop q with
| Ok () -> () | Ok x -> x
| Error (exn, bt) -> Printexc.raise_with_backtrace exn bt | Error (exn, bt) -> Printexc.raise_with_backtrace exn bt
let[@inline] size self = Array.length self.threads let[@inline] size self = Array.length self.threads

View file

@ -82,10 +82,12 @@ val run : t -> (unit -> unit) -> unit
[@@deprecated "use run_async"] [@@deprecated "use run_async"]
(** deprecated alias to {!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 (** [run_wait_block pool f] schedules [f] for later execution
on the pool, like {!run_async}. 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}). {b NOTE} be careful with deadlocks (see notes in {!Fut.wait_block}).
@since 0.3 *) @since 0.3 *)