Skip to content

Commit

Permalink
feat: better implementation for wait_pids without timers
Browse files Browse the repository at this point in the history
  • Loading branch information
leostera committed Mar 21, 2024
1 parent d1e28fd commit a9c585e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions bench/spawn_many.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ module Test_app = struct

List.iter (fun pid -> send pid Loop_stop) pids;

Logger.info (fun f ->
let t1 = Ptime_clock.now () in
let delta = Ptime.diff t1 t0 in
let delta = Ptime.Span.to_float_s delta in
f "sent %d messages in %.3fs" (List.length pids) delta);

wait_pids pids;

Logger.info (fun f ->
Expand Down
25 changes: 23 additions & 2 deletions riot/runtime/import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,34 @@ let is_process_alive pid =
| None -> false

let wait_pids pids =
(* First we make sure we are monitoring all the pids we are awaiting *)
List.iter monitor pids;

(* Immediately after monitoring, we want to make sure we remove
from the list all the pids that are already terminated, since we won't
receive monitoring messages for those. *)
let pool = _get_pool () in
let pids =
List.filter
(fun pid ->
match Proc_table.get pool.processes pid with
| Some proc -> Process.is_alive proc
| None -> false)
pids
in

(* Now we can create our selector function to select the monitoring
messages for the pids we care about. *)
let selector msg =
let open Process.Messages in
match msg with
| Process.Messages.Monitor (Process_down pid) when List.mem pid pids ->
`select Process.Messages.(Process_down pid)
| Monitor (Process_down pid) when List.mem pid pids ->
`select (Process_down pid)
| _ -> `skip
in

(* And we can enter the receive loop, filtering out the pids as they come.
When the list of pids becomes empty, we exit the recursion. *)
let rec do_wait pids =
match receive ~selector () with
| Process_down pid ->
Expand Down

0 comments on commit a9c585e

Please sign in to comment.