modify signature for Moonpool_lwt.run_in_lwt_and_await

This commit is contained in:
Simon Cruanes 2025-09-09 11:49:15 -04:00
parent 4f685313de
commit 4e19719c4f
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 6 additions and 13 deletions

View file

@ -229,18 +229,6 @@ let fut_of_lwt (lwt_fut : _ Lwt.t) : _ M.Fut.t =
fut fut
) )
let run_in_lwt_and_await (f : unit -> 'a Lwt.t) : 'a =
if Main_state.on_lwt_thread () then (
let fut = f () in
await_lwt fut
) else (
let fut, prom = Fut.make () in
Main_state.add_action_from_another_thread (fun () ->
let lwt_fut = f () in
transfer_lwt_to_fut lwt_fut prom);
Fut.await fut
)
module Setup_lwt_hooks (ARG : sig module Setup_lwt_hooks (ARG : sig
val st : Scheduler_state.st val st : Scheduler_state.st
end) = end) =
@ -307,6 +295,7 @@ let spawn_lwt f : _ Lwt.t =
lwt_fut lwt_fut
let spawn_lwt_ignore f = ignore (spawn_lwt f : unit Lwt.t) let spawn_lwt_ignore f = ignore (spawn_lwt f : unit Lwt.t)
let run_in_lwt_and_await (f : unit -> 'a) : 'a = await_lwt @@ spawn_lwt f
let on_lwt_thread = Main_state.on_lwt_thread let on_lwt_thread = Main_state.on_lwt_thread
let lwt_main (f : _ -> 'a) : 'a = let lwt_main (f : _ -> 'a) : 'a =

View file

@ -35,7 +35,11 @@ val await_lwt : 'a Lwt.t -> 'a
runner. This must be run from within a Moonpool runner so that the await-ing runner. This must be run from within a Moonpool runner so that the await-ing
effect is handled. *) effect is handled. *)
val run_in_lwt_and_await : (unit -> 'a Lwt.t) -> 'a val run_in_lwt_and_await : (unit -> 'a) -> 'a
(** [run_in_lwt_and_await f] runs [f()] in the lwt thread, just like
[spawn_lwt f], and then calls {!await_lwt} on the result. This means [f()]
can use Lwt functions and libraries, use {!await_lwt} on them freely, etc,
*)
(** {2 Wrappers around Lwt_main} *) (** {2 Wrappers around Lwt_main} *)