src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Sun Jul 29 22:41:59 2007 +0200 (2007-07-29)
changeset 24066 fb455cb475df
parent 24063 736c03ae92f5
child 24069 8a15a04e36f6
permissions -rw-r--r--
more informative tracing;
tuned;
wenzelm@23961
     1
(*  Title:      Pure/ML-Systems/multithreading_polyml.ML
wenzelm@23961
     2
    ID:         $Id$
wenzelm@23961
     3
    Author:     Makarius
wenzelm@23961
     4
wenzelm@23961
     5
Multithreading in Poly/ML (version 5.1).
wenzelm@23961
     6
*)
wenzelm@23961
     7
wenzelm@23961
     8
open Thread;
wenzelm@23961
     9
wenzelm@23961
    10
structure Multithreading: MULTITHREADING =
wenzelm@23961
    11
struct
wenzelm@23961
    12
wenzelm@23981
    13
val trace = ref false;
wenzelm@23981
    14
fun tracing msg =
wenzelm@23981
    15
  if ! trace
wenzelm@23981
    16
  then (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
wenzelm@23981
    17
  else ();
wenzelm@23961
    18
wenzelm@23981
    19
val available = true;
wenzelm@23973
    20
val max_threads = ref 1;
wenzelm@23973
    21
wenzelm@23973
    22
wenzelm@23961
    23
(* critical section -- may be nested within the same thread *)
wenzelm@23961
    24
wenzelm@23961
    25
local
wenzelm@23961
    26
wenzelm@24060
    27
fun add_name "" = ""
wenzelm@24060
    28
  | add_name name = " " ^ name;
wenzelm@24060
    29
wenzelm@24060
    30
fun add_name' "" = ""
wenzelm@24060
    31
  | add_name' name = " [" ^ name ^ "]";
wenzelm@23961
    32
wenzelm@24063
    33
val critical_lock = Mutex.mutex ();
wenzelm@24063
    34
val critical_thread = ref (NONE: Thread.thread option);
wenzelm@24063
    35
val critical_name = ref "";
wenzelm@24063
    36
wenzelm@23961
    37
in
wenzelm@23961
    38
wenzelm@23961
    39
fun self_critical () =
wenzelm@23961
    40
  (case ! critical_thread of
wenzelm@23961
    41
    NONE => false
wenzelm@23961
    42
  | SOME id => Thread.equal (id, Thread.self ()));
wenzelm@23961
    43
wenzelm@23991
    44
fun NAMED_CRITICAL name e =
wenzelm@23961
    45
  if self_critical () then e ()
wenzelm@23961
    46
  else
wenzelm@23961
    47
    let
wenzelm@23961
    48
      val _ =
wenzelm@23961
    49
        if Mutex.trylock critical_lock then ()
wenzelm@23961
    50
        else
wenzelm@24060
    51
          let
wenzelm@24060
    52
            val timer = Timer.startRealTimer ();
wenzelm@24060
    53
            val _ = tracing (fn () =>
wenzelm@24060
    54
              "CRITICAL" ^ add_name name ^ add_name' (! critical_name) ^ ": waiting for lock");
wenzelm@24060
    55
            val _ = Mutex.lock critical_lock;
wenzelm@24060
    56
            val _ = tracing (fn () =>
wenzelm@24060
    57
              "CRITICAL" ^ add_name name ^ add_name' (! critical_name) ^ ": obtained lock after " ^
wenzelm@24060
    58
              Time.toString (Timer.checkRealTimer timer));
wenzelm@24060
    59
          in () end;
wenzelm@23961
    60
      val _ = critical_thread := SOME (Thread.self ());
wenzelm@24060
    61
      val _ = critical_name := name;
wenzelm@23961
    62
      val result = Exn.capture e ();
wenzelm@24060
    63
      val _ = critical_name := "";
wenzelm@23961
    64
      val _ = critical_thread := NONE;
wenzelm@23961
    65
      val _ = Mutex.unlock critical_lock;
wenzelm@23961
    66
    in Exn.release result end;
wenzelm@23961
    67
wenzelm@23991
    68
fun CRITICAL e = NAMED_CRITICAL "" e;
wenzelm@23981
    69
wenzelm@23961
    70
end;
wenzelm@23961
    71
wenzelm@23973
    72
wenzelm@23973
    73
(* scheduling -- non-interruptible threads working on a queue of tasks *)
wenzelm@23973
    74
wenzelm@24063
    75
fun inc i = (i := ! i + 1; ! i);
wenzelm@24063
    76
fun dec i = (i := ! i - 1; ! i);
wenzelm@24063
    77
wenzelm@23973
    78
fun schedule n next_task tasks =
wenzelm@23973
    79
  let
wenzelm@23973
    80
    (*protected execution*)
wenzelm@23973
    81
    val lock = Mutex.mutex ();
wenzelm@24063
    82
    fun PROTECTED name e =
wenzelm@23973
    83
      let
wenzelm@23981
    84
        val _ =
wenzelm@23981
    85
          if Mutex.trylock lock then ()
wenzelm@23981
    86
          else
wenzelm@24063
    87
           (tracing (fn () => "PROTECTED " ^ name ^ ": waiting for lock");
wenzelm@23981
    88
            Mutex.lock lock;
wenzelm@24063
    89
            tracing (fn () => "PROTECTED " ^ name ^ ": obtained lock"));
wenzelm@23973
    90
        val res = Exn.capture e ();
wenzelm@23973
    91
        val _ = Mutex.unlock lock;
wenzelm@23973
    92
      in Exn.release res end;
wenzelm@23973
    93
wenzelm@23981
    94
    (*the queue of tasks*)
wenzelm@23973
    95
    val queue = ref tasks;
wenzelm@24063
    96
    fun dequeue () = PROTECTED "dequeue" (fn () =>
wenzelm@23973
    97
      let
wenzelm@23981
    98
        val (next, tasks') = next_task (! queue);
wenzelm@23973
    99
        val _ = queue := tasks';
wenzelm@23981
   100
      in next end);
wenzelm@23973
   101
wenzelm@23973
   102
    (*worker threads*)
wenzelm@23973
   103
    val running = ref 0;
wenzelm@24063
   104
    val active = ref 0;
wenzelm@24066
   105
    fun trace_active () = tracing (fn () => "SCHEDULE: " ^ Int.toString (! active) ^ " active");
wenzelm@23973
   106
    val status = ref ([]: exn list);
wenzelm@24060
   107
    val wakeup = ConditionVar.conditionVar ();
wenzelm@24060
   108
    fun wait () = ConditionVar.wait (wakeup, lock);
wenzelm@24066
   109
    fun work () =
wenzelm@24063
   110
      (case dequeue () of
wenzelm@23981
   111
        (Task.Task f, cont) =>
wenzelm@24063
   112
          (case Exn.capture f () of
wenzelm@24066
   113
            Exn.Result () => continue cont
wenzelm@23981
   114
          | Exn.Exn exn =>
wenzelm@24066
   115
              (PROTECTED "status" (fn () => status := exn :: ! status); continue cont))
wenzelm@23981
   116
      | (Task.Running, _) =>
wenzelm@24066
   117
          (PROTECTED "wait" (fn () =>
wenzelm@24066
   118
            (dec active; trace_active ();
wenzelm@24066
   119
             wait ();
wenzelm@24066
   120
             inc active; trace_active ()));
wenzelm@24066
   121
           work ())
wenzelm@23981
   122
      | (Task.Finished, _) =>
wenzelm@24063
   123
         (PROTECTED "running" (fn () => (dec active; dec running));
wenzelm@24066
   124
          ConditionVar.broadcast wakeup))
wenzelm@24066
   125
    and continue cont =
wenzelm@24066
   126
      (PROTECTED "cont" (fn () => queue := cont (! queue));
wenzelm@24066
   127
       ConditionVar.broadcast wakeup; work ());
wenzelm@23973
   128
wenzelm@23973
   129
    (*main control: fork and wait*)
wenzelm@23973
   130
    fun fork 0 = ()
wenzelm@23973
   131
      | fork k =
wenzelm@24063
   132
         (inc running; inc active;
wenzelm@24066
   133
          Thread.fork (work, [Thread.InterruptState Thread.InterruptDefer]);
wenzelm@23973
   134
          fork (k - 1));
wenzelm@24063
   135
    val _ = PROTECTED "main" (fn () =>
wenzelm@24063
   136
     (fork (Int.max (n, 1));
wenzelm@24066
   137
      while ! running <> 0 do (trace_active (); wait ())));
wenzelm@23973
   138
wenzelm@23973
   139
  in ! status end;
wenzelm@23973
   140
wenzelm@23961
   141
end;
wenzelm@23961
   142
wenzelm@23991
   143
val NAMED_CRITICAL = Multithreading.NAMED_CRITICAL;
wenzelm@23961
   144
val CRITICAL = Multithreading.CRITICAL;