mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-05 19:00:33 -05:00
refactor moonpool_lwt
This commit is contained in:
parent
677ae5c36a
commit
2dbbad4ef2
1 changed files with 10 additions and 8 deletions
|
|
@ -175,6 +175,13 @@ module Main_state = struct
|
||||||
Scheduler_state.add_action_from_another_thread_ (get_st ()) f
|
Scheduler_state.add_action_from_another_thread_ (get_st ()) f
|
||||||
end
|
end
|
||||||
|
|
||||||
|
let await_lwt_from_another_thread fut =
|
||||||
|
let tr = M.Trigger.create () in
|
||||||
|
Main_state.add_action_from_another_thread (fun () ->
|
||||||
|
register_trigger_on_lwt_termination fut tr);
|
||||||
|
M.Trigger.await_exn tr;
|
||||||
|
await_lwt_terminated fut
|
||||||
|
|
||||||
let await_lwt (fut : _ Lwt.t) =
|
let await_lwt (fut : _ Lwt.t) =
|
||||||
if Scheduler_state.on_lwt_thread_ (Main_state.get_st ()) then (
|
if Scheduler_state.on_lwt_thread_ (Main_state.get_st ()) then (
|
||||||
(* can directly access the future *)
|
(* can directly access the future *)
|
||||||
|
|
@ -186,13 +193,8 @@ let await_lwt (fut : _ Lwt.t) =
|
||||||
register_trigger_on_lwt_termination fut tr;
|
register_trigger_on_lwt_termination fut tr;
|
||||||
M.Trigger.await_exn tr;
|
M.Trigger.await_exn tr;
|
||||||
await_lwt_terminated fut
|
await_lwt_terminated fut
|
||||||
) else (
|
) else
|
||||||
let tr = M.Trigger.create () in
|
await_lwt_from_another_thread fut
|
||||||
Main_state.add_action_from_another_thread (fun () ->
|
|
||||||
register_trigger_on_lwt_termination fut tr);
|
|
||||||
M.Trigger.await_exn tr;
|
|
||||||
await_lwt_terminated fut
|
|
||||||
)
|
|
||||||
|
|
||||||
let lwt_of_fut (fut : 'a M.Fut.t) : 'a Lwt.t =
|
let lwt_of_fut (fut : 'a M.Fut.t) : 'a Lwt.t =
|
||||||
if not (Main_state.on_lwt_thread ()) then
|
if not (Main_state.on_lwt_thread ()) then
|
||||||
|
|
@ -303,7 +305,7 @@ let run_in_lwt_and_await (f : unit -> 'a) : 'a =
|
||||||
(* run immediately *)
|
(* run immediately *)
|
||||||
f ()
|
f ()
|
||||||
else
|
else
|
||||||
await_lwt @@ spawn_lwt f
|
await_lwt_from_another_thread @@ spawn_lwt f
|
||||||
|
|
||||||
let lwt_main (f : _ -> 'a) : 'a =
|
let lwt_main (f : _ -> 'a) : 'a =
|
||||||
let st = setup () in
|
let st = setup () in
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue