Slightly refactor lord's and vassal's loops
[dups.git] / dups.ml
diff --git a/dups.ml b/dups.ml
index 6628d99..e214d25 100644 (file)
--- a/dups.ml
+++ b/dups.ml
@@ -283,51 +283,48 @@ end = struct
   end
 
   let lord t ~njobs ~vassals ~ic ~ocs =
-    eprintf "[debug] [lord] started\n%!";
     let active_vassals = ref njobs in
     let results = Queue.create () in
-    let rec dispatch () =
-      match Ipc.recv ic with
-      | ((Exiting i) : ('input, 'output) msg_from_vassal) ->
+    let rec loop () =
+      match ((Ipc.recv ic) : ('input, 'output) msg_from_vassal) with
+      | Exiting i ->
           close_out ocs.(i);
           decr active_vassals;
-          if !active_vassals = 0 then
-            ()
-          else
-            dispatch ()
-      | ((Ready i) : ('input, 'output) msg_from_vassal) ->
+          if !active_vassals = 0 then () else loop ()
+      | Ready i ->
           Ipc.send ocs.(i) (Job (next t));
-          dispatch ()
-      | ((Result (i, result)) : ('input, 'output) msg_from_vassal) ->
+          loop ()
+      | Result (i, result) ->
           Queue.add result results;
           Ipc.send ocs.(i) (Job (next t));
-          dispatch ()
+          loop ()
     in
     let rec wait = function
-      | [] -> ()
+      | [] ->
+          ()
       | vassals ->
           let pid, _process_status = Unix.wait () in
           (* TODO: handle process_status *)
           wait (List.filter vassals ~f:(fun p -> p <> pid))
     in
-    dispatch ();
+    loop ();
     close_in ic;
     wait vassals;
     of_queue results
 
   let vassal i ~f ~vassal_pipe_r ~lord_pipe_w =
-    eprintf "[debug] [vassal %d] started\n%!" i;
     let ic = Unix.in_channel_of_descr vassal_pipe_r in
     let oc = Unix.out_channel_of_descr lord_pipe_w in
-    let rec work msg =
-      Ipc.send oc msg;
-      match Ipc.recv ic with
-      | (Job (Some x) : 'input msg_from_lord) ->
-          work (Result (i, (x, f x)))
-      | (Job None : 'input msg_from_lord) ->
+    let rec loop () =
+      match (Ipc.recv ic : 'input msg_from_lord) with
+      | Job (Some x) ->
+          Ipc.send oc (Result (i, (x, f x)));
+          loop ()
+      | Job None ->
           Ipc.send oc (Exiting i)
     in
-    work (Ready i);
+    Ipc.send oc (Ready i);
+    loop ();
     close_in ic;
     close_out oc;
     exit 0
This page took 0.022618 seconds and 4 git commands to generate.