src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Wed Jul 25 17:05:49 2007 +0200 (2007-07-25)
changeset 23981 03b71bf91318
parent 23973 b6ce6de5b700
child 23991 d4417ba26706
permissions -rw-r--r--
added trace flag, official tracing operation;
added named CRITICAL';
schedule: tuned signature, actually observe dependencies on running tasks;
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@23961
    27
val critical_lock = Mutex.mutex ();
wenzelm@23961
    28
val critical_thread = ref (NONE: Thread.thread option);
wenzelm@23961
    29
wenzelm@23961
    30
in
wenzelm@23961
    31
wenzelm@23961
    32
fun self_critical () =
wenzelm@23961
    33
  (case ! critical_thread of
wenzelm@23961
    34
    NONE => false
wenzelm@23961
    35
  | SOME id => Thread.equal (id, Thread.self ()));
wenzelm@23961
    36
wenzelm@23981
    37
fun CRITICAL' name e =
wenzelm@23961
    38
  if self_critical () then e ()
wenzelm@23961
    39
  else
wenzelm@23961
    40
    let
wenzelm@23961
    41
      val _ =
wenzelm@23961
    42
        if Mutex.trylock critical_lock then ()
wenzelm@23961
    43
        else
wenzelm@23981
    44
         (tracing (fn () => "CRITICAL " ^ name ^ ": waiting for lock");
wenzelm@23981
    45
          Mutex.lock critical_lock;
wenzelm@23981
    46
          tracing (fn () => "CRITICAL " ^ name ^ ": obtained lock"));
wenzelm@23961
    47
      val _ = critical_thread := SOME (Thread.self ());
wenzelm@23961
    48
      val result = Exn.capture e ();
wenzelm@23961
    49
      val _ = critical_thread := NONE;
wenzelm@23961
    50
      val _ = Mutex.unlock critical_lock;
wenzelm@23961
    51
    in Exn.release result end;
wenzelm@23961
    52
wenzelm@23981
    53
fun CRITICAL e = CRITICAL' "" e;
wenzelm@23981
    54
wenzelm@23961
    55
end;
wenzelm@23961
    56
wenzelm@23973
    57
wenzelm@23973
    58
(* scheduling -- non-interruptible threads working on a queue of tasks *)
wenzelm@23973
    59
wenzelm@23973
    60
fun schedule n next_task tasks =
wenzelm@23973
    61
  let
wenzelm@23973
    62
    (*protected execution*)
wenzelm@23973
    63
    val lock = Mutex.mutex ();
wenzelm@23981
    64
    fun PROTECTED k e =
wenzelm@23973
    65
      let
wenzelm@23981
    66
        val _ =
wenzelm@23981
    67
          if Mutex.trylock lock then ()
wenzelm@23981
    68
          else
wenzelm@23981
    69
           (tracing (fn () => "PROTECTED " ^ Int.toString k ^ ": waiting for lock");
wenzelm@23981
    70
            Mutex.lock lock;
wenzelm@23981
    71
            tracing (fn () => "PROTECTED " ^ Int.toString k ^ ": obtained lock"));
wenzelm@23973
    72
        val res = Exn.capture e ();
wenzelm@23973
    73
        val _ = Mutex.unlock lock;
wenzelm@23973
    74
      in Exn.release res end;
wenzelm@23973
    75
wenzelm@23981
    76
    (*the queue of tasks*)
wenzelm@23973
    77
    val queue = ref tasks;
wenzelm@23981
    78
    fun dequeue k = PROTECTED k (fn () =>
wenzelm@23973
    79
      let
wenzelm@23981
    80
        val (next, tasks') = next_task (! queue);
wenzelm@23973
    81
        val _ = queue := tasks';
wenzelm@23981
    82
      in next end);
wenzelm@23973
    83
wenzelm@23973
    84
    (*worker threads*)
wenzelm@23973
    85
    val running = ref 0;
wenzelm@23973
    86
    val status = ref ([]: exn list);
wenzelm@23973
    87
    val finished = ConditionVar.conditionVar ();
wenzelm@23981
    88
    fun wait () = ConditionVar.waitUntil (finished, lock, Time.fromMilliseconds 500);
wenzelm@23981
    89
    fun continue cont k =
wenzelm@23981
    90
      (PROTECTED k (fn () => queue := cont (! queue)); ConditionVar.signal finished; work k ())
wenzelm@23981
    91
    and work k () =
wenzelm@23981
    92
      (case dequeue k of
wenzelm@23981
    93
        (Task.Task f, cont) =>
wenzelm@23981
    94
          (tracing (fn () => "TASK " ^ Int.toString k);
wenzelm@23981
    95
           case Exn.capture f () of
wenzelm@23981
    96
            Exn.Result () => continue cont k
wenzelm@23981
    97
          | Exn.Exn exn =>
wenzelm@23981
    98
              (PROTECTED k (fn () => status := exn :: ! status); continue cont k))
wenzelm@23981
    99
      | (Task.Running, _) =>
wenzelm@23981
   100
          (tracing (fn () => "WAITING " ^ Int.toString k);
wenzelm@23981
   101
           PROTECTED k wait; work k ())
wenzelm@23981
   102
      | (Task.Finished, _) =>
wenzelm@23981
   103
         (tracing (fn () => "TERMINATING " ^ Int.toString k);
wenzelm@23981
   104
          PROTECTED k (fn () => running := ! running - 1);
wenzelm@23973
   105
          ConditionVar.signal finished));
wenzelm@23973
   106
wenzelm@23973
   107
    (*main control: fork and wait*)
wenzelm@23973
   108
    fun fork 0 = ()
wenzelm@23973
   109
      | fork k =
wenzelm@23973
   110
         (running := ! running + 1;
wenzelm@23973
   111
          Thread.fork (work k, [Thread.InterruptState Thread.InterruptDefer]);
wenzelm@23973
   112
          fork (k - 1));
wenzelm@23981
   113
    val _ = PROTECTED 0 (fn () =>
wenzelm@23981
   114
     (fork (Int.max (n, 1)); while ! running <> 0 do (tracing (fn () => "MAIN WAIT"); wait ())));
wenzelm@23973
   115
wenzelm@23973
   116
  in ! status end;
wenzelm@23973
   117
wenzelm@23961
   118
end;
wenzelm@23961
   119
wenzelm@23981
   120
val CRITICAL' = Multithreading.CRITICAL';
wenzelm@23961
   121
val CRITICAL = Multithreading.CRITICAL;