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