mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-13 14:30:32 -05:00
feat: Pool.run_wait_block can return a value now
This commit is contained in:
parent
e4b159c695
commit
9f04d254af
2 changed files with 8 additions and 6 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 *)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue