src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Mon Sep 24 13:52:50 2007 +0200 (2007-09-24 ago)
changeset 24688 a5754ca5c510
parent 24672 f311717d1f03
child 25704 df9c8074ff09
permissions -rw-r--r--
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm@23961
     1
(*  Title:      Pure/ML-Systems/multithreading_polyml.ML
wenzelm@23961
     2
    ID:         $Id$
wenzelm@23961
     3
    Author:     Makarius
wenzelm@23961
     4
wenzelm@24214
     5
Multithreading in Poly/ML 5.1 or later (cf. polyml/basis/Thread.sml).
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@24668
    13
  val ignore_interrupt: ('a -> 'b) -> 'a -> 'b
wenzelm@24668
    14
  val raise_interrupt: ('a -> 'b) -> 'a -> 'b
wenzelm@24688
    15
  structure TimeLimit: TIME_LIMIT
wenzelm@24208
    16
end;
wenzelm@24208
    17
wenzelm@23961
    18
structure Multithreading: MULTITHREADING =
wenzelm@23961
    19
struct
wenzelm@23961
    20
wenzelm@24072
    21
(* options *)
wenzelm@24069
    22
wenzelm@24119
    23
val trace = ref 0;
wenzelm@24119
    24
fun tracing level msg =
wenzelm@24119
    25
  if level <= ! trace
wenzelm@23981
    26
  then (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
wenzelm@23981
    27
  else ();
wenzelm@23961
    28
wenzelm@23981
    29
val available = true;
wenzelm@23973
    30
val max_threads = ref 1;
wenzelm@23973
    31
wenzelm@23973
    32
wenzelm@24069
    33
(* misc utils *)
wenzelm@24069
    34
wenzelm@24208
    35
fun cons x xs = x :: xs;
wenzelm@24069
    36
wenzelm@24208
    37
fun change r f = r := f (! r);
wenzelm@24069
    38
wenzelm@24069
    39
fun inc i = (i := ! i + 1; ! i);
wenzelm@24069
    40
fun dec i = (i := ! i - 1; ! i);
wenzelm@24069
    41
wenzelm@24208
    42
fun show "" = "" | show name = " " ^ name;
wenzelm@24208
    43
fun show' "" = "" | show' name = " [" ^ name ^ "]";
wenzelm@24208
    44
wenzelm@24208
    45
wenzelm@24208
    46
(* thread attributes *)
wenzelm@24208
    47
wenzelm@24208
    48
fun with_attributes new_atts f x =
wenzelm@24208
    49
  let
wenzelm@24208
    50
    val orig_atts = Thread.getAttributes ();
wenzelm@24208
    51
    fun restore () = Thread.setAttributes orig_atts;
wenzelm@24208
    52
  in
wenzelm@24208
    53
    Exn.release
wenzelm@24214
    54
    (*RACE for fully asynchronous interrupts!*)
wenzelm@24214
    55
    (let
wenzelm@24208
    56
        val _ = Thread.setAttributes new_atts;
wenzelm@24214
    57
        val result = Exn.capture (f orig_atts) 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@24668
    63
wenzelm@24668
    64
(* interrupt handling *)
wenzelm@24668
    65
wenzelm@24297
    66
fun uninterruptible f x = with_attributes
wenzelm@24297
    67
  [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer] f x;
wenzelm@24208
    68
wenzelm@24297
    69
fun interruptible f x = with_attributes
wenzelm@24297
    70
  [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce] f x;
wenzelm@24208
    71
wenzelm@24668
    72
fun ignore_interrupt f = uninterruptible (fn _ => f);
wenzelm@24668
    73
fun raise_interrupt f = interruptible (fn _ => f);
wenzelm@24668
    74
wenzelm@24688
    75
wenzelm@24688
    76
(* execution with time limit *)
wenzelm@24688
    77
wenzelm@24688
    78
structure TimeLimit =
wenzelm@24688
    79
struct
wenzelm@24688
    80
wenzelm@24688
    81
exception TimeOut;
wenzelm@24688
    82
wenzelm@24688
    83
fun timeLimit time f x =
wenzelm@24668
    84
  uninterruptible (fn atts => fn () =>
wenzelm@24668
    85
    let
wenzelm@24668
    86
      val worker = Thread.self ();
wenzelm@24688
    87
      val timeout = ref false;
wenzelm@24668
    88
      val watchdog = Thread.fork (interruptible (fn _ => fn () =>
wenzelm@24688
    89
        (OS.Process.sleep time; timeout := true; Thread.interrupt worker)), []);
wenzelm@24688
    90
wenzelm@24688
    91
      (*RACE! timeout signal vs. external Interrupt*)
wenzelm@24668
    92
      val result = Exn.capture (with_attributes atts (fn _ => f)) x;
wenzelm@24688
    93
      val was_timeout = (case result of Exn.Exn Interrupt => ! timeout | _ => false);
wenzelm@24688
    94
wenzelm@24668
    95
      val _ = Thread.interrupt watchdog handle Thread _ => ();
wenzelm@24688
    96
    in if was_timeout then raise TimeOut else Exn.release result end) ();
wenzelm@24688
    97
wenzelm@24688
    98
end;
wenzelm@24668
    99
wenzelm@24069
   100
wenzelm@23961
   101
(* critical section -- may be nested within the same thread *)
wenzelm@23961
   102
wenzelm@23961
   103
local
wenzelm@23961
   104
wenzelm@24063
   105
val critical_lock = Mutex.mutex ();
wenzelm@24063
   106
val critical_thread = ref (NONE: Thread.thread option);
wenzelm@24063
   107
val critical_name = ref "";
wenzelm@24063
   108
wenzelm@23961
   109
in
wenzelm@23961
   110
wenzelm@23961
   111
fun self_critical () =
wenzelm@23961
   112
  (case ! critical_thread of
wenzelm@23961
   113
    NONE => false
wenzelm@23961
   114
  | SOME id => Thread.equal (id, Thread.self ()));
wenzelm@23961
   115
wenzelm@23991
   116
fun NAMED_CRITICAL name e =
wenzelm@23961
   117
  if self_critical () then e ()
wenzelm@23961
   118
  else
wenzelm@24214
   119
    uninterruptible (fn atts => fn () =>
wenzelm@24208
   120
      let
wenzelm@24208
   121
        val name' = ! critical_name;
wenzelm@24208
   122
        val _ =
wenzelm@24208
   123
          if Mutex.trylock critical_lock then ()
wenzelm@24208
   124
          else
wenzelm@24208
   125
            let
wenzelm@24208
   126
              val timer = Timer.startRealTimer ();
wenzelm@24208
   127
              val _ = tracing 4 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
wenzelm@24208
   128
              val _ = Mutex.lock critical_lock;
wenzelm@24208
   129
              val time = Timer.checkRealTimer timer;
wenzelm@24208
   130
              val _ = tracing (if Time.> (time, Time.fromMilliseconds 10) then 3 else 4) (fn () =>
wenzelm@24208
   131
                "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
wenzelm@24208
   132
            in () end;
wenzelm@24208
   133
        val _ = critical_thread := SOME (Thread.self ());
wenzelm@24208
   134
        val _ = critical_name := name;
wenzelm@24214
   135
        val result = Exn.capture (with_attributes atts (fn _ => e)) ();
wenzelm@24208
   136
        val _ = critical_name := "";
wenzelm@24208
   137
        val _ = critical_thread := NONE;
wenzelm@24208
   138
        val _ = Mutex.unlock critical_lock;
wenzelm@24208
   139
      in Exn.release result end) ();
wenzelm@23961
   140
wenzelm@23991
   141
fun CRITICAL e = NAMED_CRITICAL "" e;
wenzelm@23981
   142
wenzelm@23961
   143
end;
wenzelm@23961
   144
wenzelm@23973
   145
wenzelm@24208
   146
(* scheduling -- multiple threads working on a queue of tasks *)
wenzelm@24208
   147
wenzelm@24208
   148
datatype 'a task =
wenzelm@24208
   149
  Task of {body: unit -> unit, cont: 'a -> 'a, fail: 'a -> 'a} | Wait | Terminate;
wenzelm@23973
   150
wenzelm@24214
   151
fun schedule n next_task = uninterruptible (fn _ => fn tasks =>
wenzelm@23973
   152
  let
wenzelm@23973
   153
    (*protected execution*)
wenzelm@23973
   154
    val lock = Mutex.mutex ();
wenzelm@24672
   155
    val protected_name = ref "";
wenzelm@24063
   156
    fun PROTECTED name e =
wenzelm@23973
   157
      let
wenzelm@24144
   158
        val name' = ! protected_name;
wenzelm@23981
   159
        val _ =
wenzelm@23981
   160
          if Mutex.trylock lock then ()
wenzelm@23981
   161
          else
wenzelm@24144
   162
            let
wenzelm@24144
   163
              val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": waiting");
wenzelm@24144
   164
              val _ = Mutex.lock lock;
wenzelm@24144
   165
              val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": passed");
wenzelm@24144
   166
            in () end;
wenzelm@24069
   167
        val _ = protected_name := name;
wenzelm@23973
   168
        val res = Exn.capture e ();
wenzelm@24069
   169
        val _ = protected_name := "";
wenzelm@23973
   170
        val _ = Mutex.unlock lock;
wenzelm@23973
   171
      in Exn.release res end;
wenzelm@23973
   172
wenzelm@24072
   173
    (*wakeup condition*)
wenzelm@24072
   174
    val wakeup = ConditionVar.conditionVar ();
wenzelm@24072
   175
    fun wakeup_all () = ConditionVar.broadcast wakeup;
wenzelm@24072
   176
    fun wait () = ConditionVar.wait (wakeup, lock);
wenzelm@24291
   177
    fun wait_timeout () = ConditionVar.waitUntil (wakeup, lock, Time.now () + Time.fromSeconds 1);
wenzelm@24072
   178
wenzelm@24214
   179
    (*queue of tasks*)
wenzelm@23973
   180
    val queue = ref tasks;
wenzelm@24072
   181
    val active = ref 0;
wenzelm@24119
   182
    fun trace_active () = tracing 1 (fn () => "SCHEDULE: " ^ Int.toString (! active) ^ " active");
wenzelm@24072
   183
    fun dequeue () =
wenzelm@23973
   184
      let
wenzelm@23981
   185
        val (next, tasks') = next_task (! queue);
wenzelm@23973
   186
        val _ = queue := tasks';
wenzelm@24072
   187
      in
wenzelm@24208
   188
        (case next of Wait =>
wenzelm@24208
   189
          (dec active; trace_active ();
wenzelm@24208
   190
            wait ();
wenzelm@24208
   191
            inc active; trace_active ();
wenzelm@24208
   192
            dequeue ())
wenzelm@24208
   193
        | _ => next)
wenzelm@24072
   194
      end;
wenzelm@23973
   195
wenzelm@24208
   196
    (*pool of running threads*)
wenzelm@23973
   197
    val status = ref ([]: exn list);
wenzelm@24208
   198
    val running = ref ([]: Thread.thread list);
wenzelm@24208
   199
    fun start f =
wenzelm@24208
   200
      (inc active;
wenzelm@24208
   201
       change running (cons (Thread.fork (f, [Thread.InterruptState Thread.InterruptDefer]))));
wenzelm@24208
   202
    fun stop () =
wenzelm@24208
   203
      (dec active;
wenzelm@24208
   204
       change running (List.filter (fn t => not (Thread.equal (t, Thread.self ())))));
wenzelm@24208
   205
wenzelm@24208
   206
   (*worker thread*)
wenzelm@24208
   207
    fun worker () =
wenzelm@24072
   208
      (case PROTECTED "dequeue" dequeue of
wenzelm@24208
   209
        Task {body, cont, fail} =>
wenzelm@24214
   210
          (case Exn.capture (interruptible (fn _ => body)) () of
wenzelm@24208
   211
            Exn.Result () =>
wenzelm@24208
   212
              (PROTECTED "cont" (fn () => (change queue cont; wakeup_all ())); worker ())
wenzelm@23981
   213
          | Exn.Exn exn =>
wenzelm@24208
   214
              PROTECTED "fail" (fn () =>
wenzelm@24208
   215
                (change status (cons exn); change queue fail; stop (); wakeup_all ())))
wenzelm@24208
   216
      | Terminate => PROTECTED "terminate" (fn () => (stop (); wakeup_all ())));
wenzelm@23973
   217
wenzelm@23973
   218
    (*main control: fork and wait*)
wenzelm@23973
   219
    fun fork 0 = ()
wenzelm@24208
   220
      | fork k = (start worker; fork (k - 1));
wenzelm@24063
   221
    val _ = PROTECTED "main" (fn () =>
wenzelm@24063
   222
     (fork (Int.max (n, 1));
wenzelm@24208
   223
      while not (List.null (! running)) do
wenzelm@24208
   224
      (trace_active ();
wenzelm@24208
   225
       if not (List.null (! status)) then (List.app Thread.interrupt (! running)) else ();
wenzelm@24291
   226
       wait_timeout ())));
wenzelm@23973
   227
wenzelm@24208
   228
  in ! status end);
wenzelm@23973
   229
wenzelm@23961
   230
end;
wenzelm@23961
   231
wenzelm@23991
   232
val NAMED_CRITICAL = Multithreading.NAMED_CRITICAL;
wenzelm@23961
   233
val CRITICAL = Multithreading.CRITICAL;
wenzelm@24688
   234
wenzelm@24668
   235
val ignore_interrupt = Multithreading.ignore_interrupt;
wenzelm@24668
   236
val raise_interrupt = Multithreading.raise_interrupt;
wenzelm@24208
   237
wenzelm@24688
   238
structure TimeLimit = Multithreading.TimeLimit;
wenzelm@24688
   239