Skip to content

Commit

Permalink
feat: rewrite wait_pids (Close #71)
Browse files Browse the repository at this point in the history
  • Loading branch information
leostera committed Mar 20, 2024
1 parent e2d5af1 commit d1e28fd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
19 changes: 15 additions & 4 deletions riot/runtime/import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,21 @@ let is_process_alive pid =
| Some proc -> Process.is_alive proc
| None -> false

let rec wait_pids pids =
match pids with
| [] -> ()
| pid :: tail -> wait_pids (if is_process_alive pid then pids else tail)
let wait_pids pids =
List.iter monitor pids;
let selector msg =
match msg with
| Process.Messages.Monitor (Process_down pid) when List.mem pid pids ->
`select Process.Messages.(Process_down pid)
| _ -> `skip
in
let rec do_wait pids =
match receive ~selector () with
| Process_down pid ->
let pids = List.filter (fun pid' -> not (Pid.equal pid' pid)) pids in
if List.length pids = 0 then () else do_wait pids
in
do_wait pids

module Timer = struct
type timeout = Util.Timeout.t
Expand Down
2 changes: 2 additions & 0 deletions test/link_processes_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ let main () =
loop ())
in

List.iter monitor [ pid1; pid2 ];

sleep 0.5;
(* once we send this exit signal to pid1, and it dies, it should take pid2 down with it *)
exit pid1 Normal;
Expand Down
8 changes: 3 additions & 5 deletions test/readme_example.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ let () =
Riot.run @@ fun () ->
let pid =
spawn (fun () ->
let selector msg =
match msg with
| Hello_world -> `select `hello_world
| _ -> `skip
in
let selector msg =
match msg with Hello_world -> `select `hello_world | _ -> `skip
in
match receive ~selector () with
| `hello_world ->
Logger.info (fun f -> f "hello world from %a!" Pid.pp (self ()));
Expand Down

0 comments on commit d1e28fd

Please sign in to comment.