src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Fri Feb 15 23:22:02 2008 +0100 (2008-02-15 ago)
changeset 26074 44c5419cd9f1
parent 25775 90525e67ede7
child 26083 abb3f8dd66dc
permissions -rw-r--r--
support for managed external processes;
wenzelm@23961
     1
(*  Title:      Pure/ML-Systems/multithreading_polyml.ML
wenzelm@23961
     2
    ID:         $Id$
wenzelm@23961
     3
    Author:     Makarius
wenzelm@23961
     4
wenzelm@25704
     5
Multithreading in Poly/ML 5.1 (cf. polyml/basis/Thread.sml).
wenzelm@23961
     6
*)
wenzelm@23961
     7
wenzelm@23961
     8
open Thread;
wenzelm@23961
     9
wenzelm@25704
    10
signature MULTITHREADING_POLYML =
wenzelm@25704
    11
sig
wenzelm@25704
    12
  val ignore_interrupt: ('a -> 'b) -> 'a -> 'b
wenzelm@25704
    13
  val raise_interrupt: ('a -> 'b) -> 'a -> 'b
wenzelm@25704
    14
  structure TimeLimit: TIME_LIMIT
wenzelm@25704
    15
end;
wenzelm@25704
    16
wenzelm@25704
    17
signature BASIC_MULTITHREADING =
wenzelm@25704
    18
sig
wenzelm@25704
    19
  include BASIC_MULTITHREADING
wenzelm@25704
    20
  include MULTITHREADING_POLYML
wenzelm@25704
    21
end;
wenzelm@25704
    22
wenzelm@24208
    23
signature MULTITHREADING =
wenzelm@24208
    24
sig
wenzelm@24208
    25
  include MULTITHREADING
wenzelm@25704
    26
  include MULTITHREADING_POLYML
wenzelm@24208
    27
end;
wenzelm@24208
    28
wenzelm@23961
    29
structure Multithreading: MULTITHREADING =
wenzelm@23961
    30
struct
wenzelm@23961
    31
wenzelm@24072
    32
(* options *)
wenzelm@24069
    33
wenzelm@24119
    34
val trace = ref 0;
wenzelm@24119
    35
fun tracing level msg =
wenzelm@24119
    36
  if level <= ! trace
wenzelm@23981
    37
  then (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
wenzelm@23981
    38
  else ();
wenzelm@23961
    39
wenzelm@23981
    40
val available = true;
wenzelm@25775
    41
wenzelm@23973
    42
val max_threads = ref 1;
wenzelm@23973
    43
wenzelm@25775
    44
fun max_threads_value () =
wenzelm@25775
    45
  let val m = ! max_threads
wenzelm@25775
    46
  in if m <= 0 then Thread.numProcessors () else m end;
wenzelm@25775
    47
wenzelm@23973
    48
wenzelm@24069
    49
(* misc utils *)
wenzelm@24069
    50
wenzelm@24208
    51
fun cons x xs = x :: xs;
wenzelm@24069
    52
wenzelm@24208
    53
fun change r f = r := f (! r);
wenzelm@24069
    54
wenzelm@24069
    55
fun inc i = (i := ! i + 1; ! i);
wenzelm@24069
    56
fun dec i = (i := ! i - 1; ! i);
wenzelm@24069
    57
wenzelm@24208
    58
fun show "" = "" | show name = " " ^ name;
wenzelm@24208
    59
fun show' "" = "" | show' name = " [" ^ name ^ "]";
wenzelm@24208
    60
wenzelm@24208
    61
wenzelm@24208
    62
(* thread attributes *)
wenzelm@24208
    63
wenzelm@24208
    64
fun with_attributes new_atts f x =
wenzelm@24208
    65
  let
wenzelm@24208
    66
    val orig_atts = Thread.getAttributes ();
wenzelm@24208
    67
    fun restore () = Thread.setAttributes orig_atts;
wenzelm@24208
    68
  in
wenzelm@24208
    69
    Exn.release
wenzelm@24214
    70
    (*RACE for fully asynchronous interrupts!*)
wenzelm@24214
    71
    (let
wenzelm@24208
    72
        val _ = Thread.setAttributes new_atts;
wenzelm@24214
    73
        val result = Exn.capture (f orig_atts) x;
wenzelm@24208
    74
        val _ = restore ();
wenzelm@24208
    75
      in result end
wenzelm@24208
    76
      handle Interrupt => (restore (); Exn.Exn Interrupt))
wenzelm@24208
    77
  end;
wenzelm@24208
    78
wenzelm@24668
    79
wenzelm@24668
    80
(* interrupt handling *)
wenzelm@24668
    81
wenzelm@24297
    82
fun uninterruptible f x = with_attributes
wenzelm@24297
    83
  [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer] f x;
wenzelm@24208
    84
wenzelm@24297
    85
fun interruptible f x = with_attributes
wenzelm@24297
    86
  [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce] f x;
wenzelm@24208
    87
wenzelm@24668
    88
fun ignore_interrupt f = uninterruptible (fn _ => f);
wenzelm@24668
    89
fun raise_interrupt f = interruptible (fn _ => f);
wenzelm@24668
    90
wenzelm@24688
    91
wenzelm@24688
    92
(* execution with time limit *)
wenzelm@24688
    93
wenzelm@24688
    94
structure TimeLimit =
wenzelm@24688
    95
struct
wenzelm@24688
    96
wenzelm@24688
    97
exception TimeOut;
wenzelm@24688
    98
wenzelm@24688
    99
fun timeLimit time f x =
wenzelm@24668
   100
  uninterruptible (fn atts => fn () =>
wenzelm@24668
   101
    let
wenzelm@24668
   102
      val worker = Thread.self ();
wenzelm@24688
   103
      val timeout = ref false;
wenzelm@24668
   104
      val watchdog = Thread.fork (interruptible (fn _ => fn () =>
wenzelm@24688
   105
        (OS.Process.sleep time; timeout := true; Thread.interrupt worker)), []);
wenzelm@24688
   106
wenzelm@24688
   107
      (*RACE! timeout signal vs. external Interrupt*)
wenzelm@24668
   108
      val result = Exn.capture (with_attributes atts (fn _ => f)) x;
wenzelm@24688
   109
      val was_timeout = (case result of Exn.Exn Interrupt => ! timeout | _ => false);
wenzelm@24688
   110
wenzelm@24668
   111
      val _ = Thread.interrupt watchdog handle Thread _ => ();
wenzelm@24688
   112
    in if was_timeout then raise TimeOut else Exn.release result end) ();
wenzelm@24688
   113
wenzelm@24688
   114
end;
wenzelm@24668
   115
wenzelm@24069
   116
wenzelm@26074
   117
(* managed external processes -- with propagation of interrupts *)
wenzelm@26074
   118
wenzelm@26074
   119
fun managed_process cmdline = uninterruptible (fn atts => fn () =>
wenzelm@26074
   120
  let
wenzelm@26074
   121
    val proc = Unix.execute (cmdline, []);
wenzelm@26074
   122
    val (proc_stdout, proc_stdin) = Unix.streamsOf proc;
wenzelm@26074
   123
    val _ = TextIO.closeOut proc_stdin;
wenzelm@26074
   124
wenzelm@26074
   125
    (*finished state*)
wenzelm@26074
   126
    val finished = ref false;
wenzelm@26074
   127
    val finished_mutex = Mutex.mutex ();
wenzelm@26074
   128
    val finished_cond = ConditionVar.conditionVar ();
wenzelm@26074
   129
    fun signal_finished () =
wenzelm@26074
   130
      (Mutex.lock finished_mutex; finished := true; Mutex.unlock finished_mutex;
wenzelm@26074
   131
        ConditionVar.signal finished_cond);
wenzelm@26074
   132
wenzelm@26074
   133
    val _ = Mutex.lock finished_mutex;
wenzelm@26074
   134
wenzelm@26074
   135
    (*reader thread*)
wenzelm@26074
   136
    val buffer = ref [];
wenzelm@26074
   137
    fun reader () =
wenzelm@26074
   138
      (case Exn.capture TextIO.input proc_stdout of
wenzelm@26074
   139
        Exn.Exn Interrupt => ()
wenzelm@26074
   140
      | Exn.Exn _ => signal_finished ()
wenzelm@26074
   141
      | Exn.Result "" => signal_finished ()
wenzelm@26074
   142
      | Exn.Result txt => (change buffer (cons txt); reader ()));
wenzelm@26074
   143
    val reader_thread = Thread.fork (reader, []);
wenzelm@26074
   144
wenzelm@26074
   145
    (*main thread*)
wenzelm@26074
   146
    val () =
wenzelm@26074
   147
      while not (! finished) do with_attributes atts (fn _ => fn () =>
wenzelm@26074
   148
        ((ConditionVar.waitUntil (finished_cond, finished_mutex, Time.now () + Time.fromSeconds 1); ())
wenzelm@26074
   149
          handle Interrupt => Unix.kill (proc, Posix.Signal.int))) ();  (* FIXME lock!?! *)
wenzelm@26074
   150
    val _ = Thread.interrupt reader_thread handle Thread _ => ();
wenzelm@26074
   151
wenzelm@26074
   152
    val status = OS.Process.isSuccess (Unix.reap proc);
wenzelm@26074
   153
    val output = implode (rev (! buffer));
wenzelm@26074
   154
  in (output, status) end) ();
wenzelm@26074
   155
wenzelm@26074
   156
wenzelm@23961
   157
(* critical section -- may be nested within the same thread *)
wenzelm@23961
   158
wenzelm@23961
   159
local
wenzelm@23961
   160
wenzelm@24063
   161
val critical_lock = Mutex.mutex ();
wenzelm@24063
   162
val critical_thread = ref (NONE: Thread.thread option);
wenzelm@24063
   163
val critical_name = ref "";
wenzelm@24063
   164
wenzelm@23961
   165
in
wenzelm@23961
   166
wenzelm@23961
   167
fun self_critical () =
wenzelm@23961
   168
  (case ! critical_thread of
wenzelm@23961
   169
    NONE => false
wenzelm@23961
   170
  | SOME id => Thread.equal (id, Thread.self ()));
wenzelm@23961
   171
wenzelm@23991
   172
fun NAMED_CRITICAL name e =
wenzelm@23961
   173
  if self_critical () then e ()
wenzelm@23961
   174
  else
wenzelm@24214
   175
    uninterruptible (fn atts => fn () =>
wenzelm@24208
   176
      let
wenzelm@24208
   177
        val name' = ! critical_name;
wenzelm@24208
   178
        val _ =
wenzelm@24208
   179
          if Mutex.trylock critical_lock then ()
wenzelm@24208
   180
          else
wenzelm@24208
   181
            let
wenzelm@24208
   182
              val timer = Timer.startRealTimer ();
wenzelm@24208
   183
              val _ = tracing 4 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
wenzelm@24208
   184
              val _ = Mutex.lock critical_lock;
wenzelm@24208
   185
              val time = Timer.checkRealTimer timer;
wenzelm@24208
   186
              val _ = tracing (if Time.> (time, Time.fromMilliseconds 10) then 3 else 4) (fn () =>
wenzelm@24208
   187
                "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
wenzelm@24208
   188
            in () end;
wenzelm@24208
   189
        val _ = critical_thread := SOME (Thread.self ());
wenzelm@24208
   190
        val _ = critical_name := name;
wenzelm@24214
   191
        val result = Exn.capture (with_attributes atts (fn _ => e)) ();
wenzelm@24208
   192
        val _ = critical_name := "";
wenzelm@24208
   193
        val _ = critical_thread := NONE;
wenzelm@24208
   194
        val _ = Mutex.unlock critical_lock;
wenzelm@24208
   195
      in Exn.release result end) ();
wenzelm@23961
   196
wenzelm@23991
   197
fun CRITICAL e = NAMED_CRITICAL "" e;
wenzelm@23981
   198
wenzelm@23961
   199
end;
wenzelm@23961
   200
wenzelm@23973
   201
wenzelm@24208
   202
(* scheduling -- multiple threads working on a queue of tasks *)
wenzelm@24208
   203
wenzelm@24208
   204
datatype 'a task =
wenzelm@24208
   205
  Task of {body: unit -> unit, cont: 'a -> 'a, fail: 'a -> 'a} | Wait | Terminate;
wenzelm@23973
   206
wenzelm@24214
   207
fun schedule n next_task = uninterruptible (fn _ => fn tasks =>
wenzelm@23973
   208
  let
wenzelm@23973
   209
    (*protected execution*)
wenzelm@23973
   210
    val lock = Mutex.mutex ();
wenzelm@24672
   211
    val protected_name = ref "";
wenzelm@24063
   212
    fun PROTECTED name e =
wenzelm@23973
   213
      let
wenzelm@24144
   214
        val name' = ! protected_name;
wenzelm@23981
   215
        val _ =
wenzelm@23981
   216
          if Mutex.trylock lock then ()
wenzelm@23981
   217
          else
wenzelm@24144
   218
            let
wenzelm@24144
   219
              val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": waiting");
wenzelm@24144
   220
              val _ = Mutex.lock lock;
wenzelm@24144
   221
              val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": passed");
wenzelm@24144
   222
            in () end;
wenzelm@24069
   223
        val _ = protected_name := name;
wenzelm@23973
   224
        val res = Exn.capture e ();
wenzelm@24069
   225
        val _ = protected_name := "";
wenzelm@23973
   226
        val _ = Mutex.unlock lock;
wenzelm@23973
   227
      in Exn.release res end;
wenzelm@23973
   228
wenzelm@24072
   229
    (*wakeup condition*)
wenzelm@24072
   230
    val wakeup = ConditionVar.conditionVar ();
wenzelm@24072
   231
    fun wakeup_all () = ConditionVar.broadcast wakeup;
wenzelm@24072
   232
    fun wait () = ConditionVar.wait (wakeup, lock);
wenzelm@24291
   233
    fun wait_timeout () = ConditionVar.waitUntil (wakeup, lock, Time.now () + Time.fromSeconds 1);
wenzelm@24072
   234
wenzelm@24214
   235
    (*queue of tasks*)
wenzelm@23973
   236
    val queue = ref tasks;
wenzelm@24072
   237
    val active = ref 0;
wenzelm@24119
   238
    fun trace_active () = tracing 1 (fn () => "SCHEDULE: " ^ Int.toString (! active) ^ " active");
wenzelm@24072
   239
    fun dequeue () =
wenzelm@23973
   240
      let
wenzelm@23981
   241
        val (next, tasks') = next_task (! queue);
wenzelm@23973
   242
        val _ = queue := tasks';
wenzelm@24072
   243
      in
wenzelm@24208
   244
        (case next of Wait =>
wenzelm@24208
   245
          (dec active; trace_active ();
wenzelm@24208
   246
            wait ();
wenzelm@24208
   247
            inc active; trace_active ();
wenzelm@24208
   248
            dequeue ())
wenzelm@24208
   249
        | _ => next)
wenzelm@24072
   250
      end;
wenzelm@23973
   251
wenzelm@24208
   252
    (*pool of running threads*)
wenzelm@23973
   253
    val status = ref ([]: exn list);
wenzelm@24208
   254
    val running = ref ([]: Thread.thread list);
wenzelm@24208
   255
    fun start f =
wenzelm@24208
   256
      (inc active;
wenzelm@24208
   257
       change running (cons (Thread.fork (f, [Thread.InterruptState Thread.InterruptDefer]))));
wenzelm@24208
   258
    fun stop () =
wenzelm@24208
   259
      (dec active;
wenzelm@24208
   260
       change running (List.filter (fn t => not (Thread.equal (t, Thread.self ())))));
wenzelm@24208
   261
wenzelm@24208
   262
   (*worker thread*)
wenzelm@24208
   263
    fun worker () =
wenzelm@24072
   264
      (case PROTECTED "dequeue" dequeue of
wenzelm@24208
   265
        Task {body, cont, fail} =>
wenzelm@24214
   266
          (case Exn.capture (interruptible (fn _ => body)) () of
wenzelm@24208
   267
            Exn.Result () =>
wenzelm@24208
   268
              (PROTECTED "cont" (fn () => (change queue cont; wakeup_all ())); worker ())
wenzelm@23981
   269
          | Exn.Exn exn =>
wenzelm@24208
   270
              PROTECTED "fail" (fn () =>
wenzelm@24208
   271
                (change status (cons exn); change queue fail; stop (); wakeup_all ())))
wenzelm@24208
   272
      | Terminate => PROTECTED "terminate" (fn () => (stop (); wakeup_all ())));
wenzelm@23973
   273
wenzelm@23973
   274
    (*main control: fork and wait*)
wenzelm@23973
   275
    fun fork 0 = ()
wenzelm@24208
   276
      | fork k = (start worker; fork (k - 1));
wenzelm@24063
   277
    val _ = PROTECTED "main" (fn () =>
wenzelm@24063
   278
     (fork (Int.max (n, 1));
wenzelm@24208
   279
      while not (List.null (! running)) do
wenzelm@24208
   280
      (trace_active ();
wenzelm@24208
   281
       if not (List.null (! status)) then (List.app Thread.interrupt (! running)) else ();
wenzelm@24291
   282
       wait_timeout ())));
wenzelm@23973
   283
wenzelm@24208
   284
  in ! status end);
wenzelm@23973
   285
wenzelm@25704
   286
wenzelm@25704
   287
(* serial numbers *)
wenzelm@25704
   288
wenzelm@25704
   289
local
wenzelm@25704
   290
wenzelm@25704
   291
val serial_lock = Mutex.mutex ();
wenzelm@25704
   292
val serial_count = ref 0;
wenzelm@25704
   293
wenzelm@25704
   294
in
wenzelm@25704
   295
wenzelm@25704
   296
val serial = uninterruptible (fn _ => fn () =>
wenzelm@25704
   297
  let
wenzelm@25704
   298
    val _ = Mutex.lock serial_lock;
wenzelm@25704
   299
    val res = inc serial_count;
wenzelm@25704
   300
    val _ = Mutex.unlock serial_lock;
wenzelm@25704
   301
  in res end);
wenzelm@25704
   302
wenzelm@23961
   303
end;
wenzelm@23961
   304
wenzelm@25735
   305
wenzelm@25735
   306
(* thread data *)
wenzelm@25735
   307
wenzelm@25735
   308
val get_data = Thread.getLocal;
wenzelm@25735
   309
val put_data = Thread.setLocal;
wenzelm@25735
   310
wenzelm@25704
   311
end;
wenzelm@24688
   312
wenzelm@25704
   313
structure BasicMultithreading: BASIC_MULTITHREADING = Multithreading;
wenzelm@25704
   314
open BasicMultithreading;