src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Thu Aug 09 23:53:50 2007 +0200 (2007-08-09 ago)
changeset 24208 f4cafbaa05e4
parent 24144 ec51a0f7eefe
child 24214 0482ecc4ef11
permissions -rw-r--r--
schedule: more precise task model;
improved error handling: first failure causes interrupt of all threads;
misc cleanup;
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@24208
    10
signature MULTITHREADING =
wenzelm@24208
    11
sig
wenzelm@24208
    12
  include MULTITHREADING
wenzelm@24208
    13
  val uninterruptible: ('a -> 'b) -> 'a -> 'b
wenzelm@24208
    14
  val interruptible: ('a -> 'b) -> 'a -> 'b
wenzelm@24208
    15
end;
wenzelm@24208
    16
wenzelm@23961
    17
structure Multithreading: MULTITHREADING =
wenzelm@23961
    18
struct
wenzelm@23961
    19
wenzelm@24072
    20
(* options *)
wenzelm@24069
    21
wenzelm@24119
    22
val trace = ref 0;
wenzelm@24119
    23
fun tracing level msg =
wenzelm@24119
    24
  if level <= ! trace
wenzelm@23981
    25
  then (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
wenzelm@23981
    26
  else ();
wenzelm@23961
    27
wenzelm@23981
    28
val available = true;
wenzelm@23973
    29
val max_threads = ref 1;
wenzelm@23973
    30
wenzelm@23973
    31
wenzelm@24069
    32
(* misc utils *)
wenzelm@24069
    33
wenzelm@24208
    34
fun cons x xs = x :: xs;
wenzelm@24069
    35
wenzelm@24208
    36
fun change r f = r := f (! r);
wenzelm@24069
    37
wenzelm@24069
    38
fun inc i = (i := ! i + 1; ! i);
wenzelm@24069
    39
fun dec i = (i := ! i - 1; ! i);
wenzelm@24069
    40
wenzelm@24208
    41
fun show "" = "" | show name = " " ^ name;
wenzelm@24208
    42
fun show' "" = "" | show' name = " [" ^ name ^ "]";
wenzelm@24208
    43
wenzelm@24208
    44
wenzelm@24208
    45
(* thread attributes *)
wenzelm@24208
    46
wenzelm@24208
    47
local
wenzelm@24208
    48
wenzelm@24208
    49
fun with_attributes new_atts f x =
wenzelm@24208
    50
  let
wenzelm@24208
    51
    val orig_atts = Thread.getAttributes ();
wenzelm@24208
    52
    fun restore () = Thread.setAttributes orig_atts;
wenzelm@24208
    53
  in
wenzelm@24208
    54
    Exn.release
wenzelm@24208
    55
     (let
wenzelm@24208
    56
        val _ = Thread.setAttributes new_atts;
wenzelm@24208
    57
        val result = Exn.capture f x;
wenzelm@24208
    58
        val _ = restore ();
wenzelm@24208
    59
      in result end
wenzelm@24208
    60
      handle Interrupt => (restore (); Exn.Exn Interrupt))
wenzelm@24208
    61
  end;
wenzelm@24208
    62
wenzelm@24208
    63
fun with_interrupt_state state = with_attributes [Thread.InterruptState state];
wenzelm@24208
    64
wenzelm@24208
    65
in
wenzelm@24208
    66
wenzelm@24208
    67
fun uninterruptible f x = with_interrupt_state Thread.InterruptDefer f x;
wenzelm@24208
    68
fun interruptible f x = with_interrupt_state Thread.InterruptAsynchOnce f x;
wenzelm@24208
    69
wenzelm@24208
    70
end;
wenzelm@24208
    71
wenzelm@24069
    72
wenzelm@23961
    73
(* critical section -- may be nested within the same thread *)
wenzelm@23961
    74
wenzelm@23961
    75
local
wenzelm@23961
    76
wenzelm@24063
    77
val critical_lock = Mutex.mutex ();
wenzelm@24063
    78
val critical_thread = ref (NONE: Thread.thread option);
wenzelm@24063
    79
val critical_name = ref "";
wenzelm@24063
    80
wenzelm@23961
    81
in
wenzelm@23961
    82
wenzelm@23961
    83
fun self_critical () =
wenzelm@23961
    84
  (case ! critical_thread of
wenzelm@23961
    85
    NONE => false
wenzelm@23961
    86
  | SOME id => Thread.equal (id, Thread.self ()));
wenzelm@23961
    87
wenzelm@23991
    88
fun NAMED_CRITICAL name e =
wenzelm@23961
    89
  if self_critical () then e ()
wenzelm@23961
    90
  else
wenzelm@24208
    91
    uninterruptible (fn () =>
wenzelm@24208
    92
      let
wenzelm@24208
    93
        val name' = ! critical_name;
wenzelm@24208
    94
        val _ =
wenzelm@24208
    95
          if Mutex.trylock critical_lock then ()
wenzelm@24208
    96
          else
wenzelm@24208
    97
            let
wenzelm@24208
    98
              val timer = Timer.startRealTimer ();
wenzelm@24208
    99
              val _ = tracing 4 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
wenzelm@24208
   100
              val _ = Mutex.lock critical_lock;
wenzelm@24208
   101
              val time = Timer.checkRealTimer timer;
wenzelm@24208
   102
              val _ = tracing (if Time.> (time, Time.fromMilliseconds 10) then 3 else 4) (fn () =>
wenzelm@24208
   103
                "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
wenzelm@24208
   104
            in () end;
wenzelm@24208
   105
        val _ = critical_thread := SOME (Thread.self ());
wenzelm@24208
   106
        val _ = critical_name := name;
wenzelm@24208
   107
        val result = Exn.capture e ();
wenzelm@24208
   108
        val _ = critical_name := "";
wenzelm@24208
   109
        val _ = critical_thread := NONE;
wenzelm@24208
   110
        val _ = Mutex.unlock critical_lock;
wenzelm@24208
   111
      in Exn.release result end) ();
wenzelm@23961
   112
wenzelm@23991
   113
fun CRITICAL e = NAMED_CRITICAL "" e;
wenzelm@23981
   114
wenzelm@23961
   115
end;
wenzelm@23961
   116
wenzelm@23973
   117
wenzelm@24208
   118
(* scheduling -- multiple threads working on a queue of tasks *)
wenzelm@24208
   119
wenzelm@24208
   120
datatype 'a task =
wenzelm@24208
   121
  Task of {body: unit -> unit, cont: 'a -> 'a, fail: 'a -> 'a} | Wait | Terminate;
wenzelm@23973
   122
wenzelm@24069
   123
local
wenzelm@24069
   124
wenzelm@24069
   125
val protected_name = ref "";
wenzelm@24069
   126
wenzelm@24069
   127
in
wenzelm@24063
   128
wenzelm@24208
   129
fun schedule n next_task = uninterruptible (fn tasks =>
wenzelm@23973
   130
  let
wenzelm@23973
   131
    (*protected execution*)
wenzelm@23973
   132
    val lock = Mutex.mutex ();
wenzelm@24063
   133
    fun PROTECTED name e =
wenzelm@23973
   134
      let
wenzelm@24144
   135
        val name' = ! protected_name;
wenzelm@23981
   136
        val _ =
wenzelm@23981
   137
          if Mutex.trylock lock then ()
wenzelm@23981
   138
          else
wenzelm@24144
   139
            let
wenzelm@24144
   140
              val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": waiting");
wenzelm@24144
   141
              val _ = Mutex.lock lock;
wenzelm@24144
   142
              val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": passed");
wenzelm@24144
   143
            in () end;
wenzelm@24069
   144
        val _ = protected_name := name;
wenzelm@23973
   145
        val res = Exn.capture e ();
wenzelm@24069
   146
        val _ = protected_name := "";
wenzelm@23973
   147
        val _ = Mutex.unlock lock;
wenzelm@23973
   148
      in Exn.release res end;
wenzelm@23973
   149
wenzelm@24072
   150
    (*wakeup condition*)
wenzelm@24072
   151
    val wakeup = ConditionVar.conditionVar ();
wenzelm@24072
   152
    fun wakeup_all () = ConditionVar.broadcast wakeup;
wenzelm@24072
   153
    fun wait () = ConditionVar.wait (wakeup, lock);
wenzelm@24072
   154
wenzelm@23981
   155
    (*the queue of tasks*)
wenzelm@23973
   156
    val queue = ref tasks;
wenzelm@24072
   157
    val active = ref 0;
wenzelm@24119
   158
    fun trace_active () = tracing 1 (fn () => "SCHEDULE: " ^ Int.toString (! active) ^ " active");
wenzelm@24072
   159
    fun dequeue () =
wenzelm@23973
   160
      let
wenzelm@23981
   161
        val (next, tasks') = next_task (! queue);
wenzelm@23973
   162
        val _ = queue := tasks';
wenzelm@24072
   163
      in
wenzelm@24208
   164
        (case next of Wait =>
wenzelm@24208
   165
          (dec active; trace_active ();
wenzelm@24208
   166
            wait ();
wenzelm@24208
   167
            inc active; trace_active ();
wenzelm@24208
   168
            dequeue ())
wenzelm@24208
   169
        | _ => next)
wenzelm@24072
   170
      end;
wenzelm@23973
   171
wenzelm@24208
   172
    (*pool of running threads*)
wenzelm@23973
   173
    val status = ref ([]: exn list);
wenzelm@24208
   174
    val running = ref ([]: Thread.thread list);
wenzelm@24208
   175
    fun start f =
wenzelm@24208
   176
      (inc active;
wenzelm@24208
   177
       change running (cons (Thread.fork (f, [Thread.InterruptState Thread.InterruptDefer]))));
wenzelm@24208
   178
    fun stop () =
wenzelm@24208
   179
      (dec active;
wenzelm@24208
   180
       change running (List.filter (fn t => not (Thread.equal (t, Thread.self ())))));
wenzelm@24208
   181
wenzelm@24208
   182
   (*worker thread*)
wenzelm@24208
   183
    fun worker () =
wenzelm@24072
   184
      (case PROTECTED "dequeue" dequeue of
wenzelm@24208
   185
        Task {body, cont, fail} =>
wenzelm@24208
   186
          (case Exn.capture (interruptible body) () of
wenzelm@24208
   187
            Exn.Result () =>
wenzelm@24208
   188
              (PROTECTED "cont" (fn () => (change queue cont; wakeup_all ())); worker ())
wenzelm@23981
   189
          | Exn.Exn exn =>
wenzelm@24208
   190
              PROTECTED "fail" (fn () =>
wenzelm@24208
   191
                (change status (cons exn); change queue fail; stop (); wakeup_all ())))
wenzelm@24208
   192
      | Terminate => PROTECTED "terminate" (fn () => (stop (); wakeup_all ())));
wenzelm@23973
   193
wenzelm@23973
   194
    (*main control: fork and wait*)
wenzelm@23973
   195
    fun fork 0 = ()
wenzelm@24208
   196
      | fork k = (start worker; fork (k - 1));
wenzelm@24063
   197
    val _ = PROTECTED "main" (fn () =>
wenzelm@24063
   198
     (fork (Int.max (n, 1));
wenzelm@24208
   199
      while not (List.null (! running)) do
wenzelm@24208
   200
      (trace_active ();
wenzelm@24208
   201
       if not (List.null (! status)) then (List.app Thread.interrupt (! running)) else ();
wenzelm@24208
   202
       wait ())));
wenzelm@23973
   203
wenzelm@24208
   204
  in ! status end);
wenzelm@23973
   205
wenzelm@23961
   206
end;
wenzelm@23961
   207
wenzelm@24069
   208
end;
wenzelm@24069
   209
wenzelm@23991
   210
val NAMED_CRITICAL = Multithreading.NAMED_CRITICAL;
wenzelm@23961
   211
val CRITICAL = Multithreading.CRITICAL;
wenzelm@24208
   212