more informative tracing;
authorwenzelm
Sun, 29 Jul 2007 22:41:59 +0200
changeset 24066 fb455cb475df
parent 24065 21483400c2ca
child 24067 69b51bc5ce06
more informative tracing; tuned;
src/Pure/ML-Systems/multithreading_polyml.ML
--- a/src/Pure/ML-Systems/multithreading_polyml.ML	Sun Jul 29 22:41:58 2007 +0200
+++ b/src/Pure/ML-Systems/multithreading_polyml.ML	Sun Jul 29 22:41:59 2007 +0200
@@ -102,36 +102,39 @@
     (*worker threads*)
     val running = ref 0;
     val active = ref 0;
+    fun trace_active () = tracing (fn () => "SCHEDULE: " ^ Int.toString (! active) ^ " active");
     val status = ref ([]: exn list);
     val wakeup = ConditionVar.conditionVar ();
     fun wait () = ConditionVar.wait (wakeup, lock);
-    fun continue cont k =
-      (PROTECTED "cont" (fn () => queue := cont (! queue));
-       ConditionVar.broadcast wakeup; work k ())
-    and work k () =
+    fun work () =
       (case dequeue () of
         (Task.Task f, cont) =>
           (case Exn.capture f () of
-            Exn.Result () => continue cont k
+            Exn.Result () => continue cont
           | Exn.Exn exn =>
-              (PROTECTED "status" (fn () => status := exn :: ! status); continue cont k))
+              (PROTECTED "status" (fn () => status := exn :: ! status); continue cont))
       | (Task.Running, _) =>
-          (PROTECTED "wait" (fn () => (dec active; wait (); inc active)); work k ())
+          (PROTECTED "wait" (fn () =>
+            (dec active; trace_active ();
+             wait ();
+             inc active; trace_active ()));
+           work ())
       | (Task.Finished, _) =>
          (PROTECTED "running" (fn () => (dec active; dec running));
-          ConditionVar.broadcast wakeup));
+          ConditionVar.broadcast wakeup))
+    and continue cont =
+      (PROTECTED "cont" (fn () => queue := cont (! queue));
+       ConditionVar.broadcast wakeup; work ());
 
     (*main control: fork and wait*)
     fun fork 0 = ()
       | fork k =
          (inc running; inc active;
-          Thread.fork (work k, [Thread.InterruptState Thread.InterruptDefer]);
+          Thread.fork (work, [Thread.InterruptState Thread.InterruptDefer]);
           fork (k - 1));
     val _ = PROTECTED "main" (fn () =>
      (fork (Int.max (n, 1));
-      while ! running <> 0 do
-        (tracing (fn () => "MAIN: " ^ Int.toString (! active) ^ " active");
-         wait ())));
+      while ! running <> 0 do (trace_active (); wait ())));
 
   in ! status end;