@@ -80,11 +80,35 @@ let async pool f =
8080 Multi_channel. send pd.task_chan (Work (fun _ -> step (do_task f) p));
8181 p
8282
83+ let prepare_for_await chan () =
84+ let promise = Atomic. make (Pending [] ) in
85+ let release () =
86+ match Atomic. get promise with
87+ | (Returned _ | Raised _ ) -> ()
88+ | Pending _ ->
89+ match Atomic. exchange promise (Returned () ) with
90+ | Pending ks ->
91+ ks
92+ |> List. iter @@ fun (k , c ) ->
93+ Multi_channel. send_foreign c (Work (fun _ -> continue k () ))
94+ | _ -> ()
95+ and await () =
96+ match Atomic. get promise with
97+ | (Returned _ | Raised _ ) -> ()
98+ | Pending _ -> perform (Wait (promise, chan))
99+ in
100+ Domain_local_await. { release; await }
101+
83102let rec worker task_chan =
84103 match Multi_channel. recv task_chan with
85104 | Quit -> Multi_channel. clear_local_state task_chan
86105 | Work f -> f () ; worker task_chan
87106
107+ let worker task_chan =
108+ Domain_local_await. using
109+ ~prepare_for_await: (prepare_for_await task_chan)
110+ ~while_running: (fun () -> worker task_chan)
111+
88112let run (type a ) pool (f : unit -> a ) : a =
89113 let pd = get_pool_data pool in
90114 let p = Atomic. make (Pending [] ) in
@@ -105,6 +129,11 @@ let run (type a) pool (f : unit -> a) : a =
105129 in
106130 loop ()
107131
132+ let run pool f =
133+ Domain_local_await. using
134+ ~prepare_for_await: (prepare_for_await (get_pool_data pool).task_chan)
135+ ~while_running: (fun () -> run pool f)
136+
108137let named_pools = Hashtbl. create 8
109138let named_pools_mutex = Mutex. create ()
110139
0 commit comments