src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Mon Mar 31 23:08:55 2008 +0200 (2008-03-31)
changeset 26504 6e87c0a60104
parent 26493 de4764e95166
child 28124 10a1f1f4c6ae
permissions -rw-r--r--
before close: Exn.capture/release;
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@26083
    12
  val interruptible: ('a -> 'b) -> 'a -> 'b
wenzelm@26083
    13
  val uninterruptible: ((('c -> 'd) -> 'c -> 'd) -> 'a -> 'b) -> 'a -> 'b
wenzelm@26098
    14
  val system_out: string -> string * int
wenzelm@25704
    15
  structure TimeLimit: TIME_LIMIT
wenzelm@26390
    16
  val profile: int -> ('a -> 'b) -> 'a -> 'b
wenzelm@25704
    17
end;
wenzelm@25704
    18
wenzelm@25704
    19
signature BASIC_MULTITHREADING =
wenzelm@25704
    20
sig
wenzelm@25704
    21
  include BASIC_MULTITHREADING
wenzelm@25704
    22
  include MULTITHREADING_POLYML
wenzelm@25704
    23
end;
wenzelm@25704
    24
wenzelm@24208
    25
signature MULTITHREADING =
wenzelm@24208
    26
sig
wenzelm@24208
    27
  include MULTITHREADING
wenzelm@25704
    28
  include MULTITHREADING_POLYML
wenzelm@24208
    29
end;
wenzelm@24208
    30
wenzelm@23961
    31
structure Multithreading: MULTITHREADING =
wenzelm@23961
    32
struct
wenzelm@23961
    33
wenzelm@24072
    34
(* options *)
wenzelm@24069
    35
wenzelm@24119
    36
val trace = ref 0;
wenzelm@24119
    37
fun tracing level msg =
wenzelm@24119
    38
  if level <= ! trace
wenzelm@23981
    39
  then (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
wenzelm@23981
    40
  else ();
wenzelm@23961
    41
wenzelm@23981
    42
val available = true;
wenzelm@25775
    43
wenzelm@23973
    44
val max_threads = ref 1;
wenzelm@23973
    45
wenzelm@25775
    46
fun max_threads_value () =
wenzelm@25775
    47
  let val m = ! max_threads
wenzelm@25775
    48
  in if m <= 0 then Thread.numProcessors () else m end;
wenzelm@25775
    49
wenzelm@23973
    50
wenzelm@24069
    51
(* misc utils *)
wenzelm@24069
    52
wenzelm@24208
    53
fun cons x xs = x :: xs;
wenzelm@24069
    54
wenzelm@24208
    55
fun change r f = r := f (! r);
wenzelm@24069
    56
wenzelm@24069
    57
fun inc i = (i := ! i + 1; ! i);
wenzelm@24069
    58
fun dec i = (i := ! i - 1; ! i);
wenzelm@24069
    59
wenzelm@24208
    60
fun show "" = "" | show name = " " ^ name;
wenzelm@24208
    61
fun show' "" = "" | show' name = " [" ^ name ^ "]";
wenzelm@24208
    62
wenzelm@26098
    63
fun read_file name =
wenzelm@26098
    64
  let val is = TextIO.openIn name
wenzelm@26504
    65
  in Exn.release (Exn.capture TextIO.inputAll is before TextIO.closeIn is) end;
wenzelm@26098
    66
wenzelm@26098
    67
fun write_file name txt =
wenzelm@26098
    68
  let val os = TextIO.openOut name
wenzelm@26504
    69
  in Exn.release (Exn.capture TextIO.output (os, txt) before TextIO.closeOut os) end;
wenzelm@26098
    70
wenzelm@24208
    71
wenzelm@24208
    72
(* thread attributes *)
wenzelm@24208
    73
wenzelm@24208
    74
fun with_attributes new_atts f x =
wenzelm@24208
    75
  let
wenzelm@24208
    76
    val orig_atts = Thread.getAttributes ();
wenzelm@24208
    77
    fun restore () = Thread.setAttributes orig_atts;
wenzelm@24208
    78
  in
wenzelm@24208
    79
    Exn.release
wenzelm@24214
    80
    (*RACE for fully asynchronous interrupts!*)
wenzelm@24214
    81
    (let
wenzelm@24208
    82
        val _ = Thread.setAttributes new_atts;
wenzelm@24214
    83
        val result = Exn.capture (f orig_atts) x;
wenzelm@24208
    84
        val _ = restore ();
wenzelm@24208
    85
      in result end
wenzelm@24208
    86
      handle Interrupt => (restore (); Exn.Exn Interrupt))
wenzelm@24208
    87
  end;
wenzelm@24208
    88
wenzelm@26083
    89
fun interruptible f =
wenzelm@26083
    90
  with_attributes
wenzelm@26083
    91
    [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce]
wenzelm@26083
    92
    (fn _ => f);
wenzelm@24208
    93
wenzelm@26083
    94
fun uninterruptible f =
wenzelm@26083
    95
  with_attributes
wenzelm@26083
    96
    [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer]
wenzelm@26083
    97
    (fn atts => f (fn g => with_attributes atts (fn _ => g)));
wenzelm@24668
    98
wenzelm@24688
    99
wenzelm@24688
   100
(* execution with time limit *)
wenzelm@24688
   101
wenzelm@24688
   102
structure TimeLimit =
wenzelm@24688
   103
struct
wenzelm@24688
   104
wenzelm@24688
   105
exception TimeOut;
wenzelm@24688
   106
wenzelm@26083
   107
fun timeLimit time f x = uninterruptible (fn restore_attributes => fn () =>
wenzelm@26083
   108
  let
wenzelm@26083
   109
    val worker = Thread.self ();
wenzelm@26083
   110
    val timeout = ref false;
wenzelm@26083
   111
    val watchdog = Thread.fork (fn () =>
wenzelm@26083
   112
      (OS.Process.sleep time; timeout := true; Thread.interrupt worker), []);
wenzelm@24688
   113
wenzelm@26083
   114
    (*RACE! timeout signal vs. external Interrupt*)
wenzelm@26083
   115
    val result = Exn.capture (restore_attributes f) x;
wenzelm@26083
   116
    val was_timeout = (case result of Exn.Exn Interrupt => ! timeout | _ => false);
wenzelm@24688
   117
wenzelm@26083
   118
    val _ = Thread.interrupt watchdog handle Thread _ => ();
wenzelm@26083
   119
  in if was_timeout then raise TimeOut else Exn.release result end) ();
wenzelm@24688
   120
wenzelm@24688
   121
end;
wenzelm@24668
   122
wenzelm@24069
   123
wenzelm@26098
   124
(* system shell processes, with propagation of interrupts *)
wenzelm@26098
   125
wenzelm@26221
   126
fun system_out_threaded script = uninterruptible (fn restore_attributes => fn () =>
wenzelm@26098
   127
  let
wenzelm@26098
   128
    val script_name = OS.FileSys.tmpName ();
wenzelm@26098
   129
    val _ = write_file script_name script;
wenzelm@26098
   130
wenzelm@26098
   131
    val pid_name = OS.FileSys.tmpName ();
wenzelm@26098
   132
    val output_name = OS.FileSys.tmpName ();
wenzelm@26098
   133
wenzelm@26098
   134
    (*result state*)
wenzelm@26098
   135
    datatype result = Wait | Signal | Result of int;
wenzelm@26098
   136
    val result = ref Wait;
wenzelm@26098
   137
    val result_mutex = Mutex.mutex ();
wenzelm@26098
   138
    val result_cond = ConditionVar.conditionVar ();
wenzelm@26098
   139
    fun set_result res =
wenzelm@26098
   140
      (Mutex.lock result_mutex; result := res; Mutex.unlock result_mutex;
wenzelm@26098
   141
        ConditionVar.signal result_cond);
wenzelm@26098
   142
wenzelm@26098
   143
    val _ = Mutex.lock result_mutex;
wenzelm@26098
   144
wenzelm@26098
   145
    (*system thread*)
wenzelm@26098
   146
    val system_thread = Thread.fork (fn () =>
wenzelm@26098
   147
      let
wenzelm@26098
   148
        val status =
wenzelm@26098
   149
          OS.Process.system ("perl -w \"$ISABELLE_HOME/lib/scripts/system.pl\" group " ^
wenzelm@26098
   150
            script_name ^ " " ^ pid_name ^ " " ^ output_name);
wenzelm@26098
   151
        val res =
wenzelm@26098
   152
          (case Posix.Process.fromStatus status of
wenzelm@26098
   153
            Posix.Process.W_EXITED => Result 0
wenzelm@26098
   154
          | Posix.Process.W_EXITSTATUS 0wx82 => Signal
wenzelm@26098
   155
          | Posix.Process.W_EXITSTATUS w => Result (Word8.toInt w)
wenzelm@26098
   156
          | Posix.Process.W_SIGNALED s =>
wenzelm@26098
   157
              if s = Posix.Signal.int then Signal
wenzelm@26098
   158
              else Result (256 + LargeWord.toInt (Posix.Signal.toWord s))
wenzelm@26098
   159
          | Posix.Process.W_STOPPED s => Result (512 + LargeWord.toInt (Posix.Signal.toWord s)));
wenzelm@26098
   160
      in set_result res end handle _ => set_result (Result 2), []);
wenzelm@26098
   161
wenzelm@26098
   162
    (*main thread -- proxy for interrupts*)
wenzelm@26098
   163
    fun kill n =
wenzelm@26098
   164
      (case Int.fromString (read_file pid_name) of
wenzelm@26098
   165
        SOME pid =>
wenzelm@26098
   166
          Posix.Process.kill
wenzelm@26098
   167
            (Posix.Process.K_GROUP (Posix.Process.wordToPid (LargeWord.fromInt pid)),
wenzelm@26098
   168
              Posix.Signal.int)
wenzelm@26098
   169
      | NONE => ())
wenzelm@26098
   170
      handle OS.SysErr _ => () | IO.Io _ =>
wenzelm@26098
   171
        (OS.Process.sleep (Time.fromMilliseconds 100); if n > 0 then kill (n - 1) else ());
wenzelm@26098
   172
wenzelm@26098
   173
    val _ = while ! result = Wait do
wenzelm@26098
   174
      restore_attributes (fn () =>
wenzelm@26098
   175
        (ConditionVar.waitUntil (result_cond, result_mutex, Time.now () + Time.fromMilliseconds 100); ())
wenzelm@26098
   176
          handle Interrupt => kill 10) ();
wenzelm@26098
   177
wenzelm@26098
   178
    (*cleanup*)
wenzelm@26098
   179
    val output = read_file output_name handle IO.Io _ => "";
wenzelm@26098
   180
    val _ = OS.FileSys.remove script_name handle OS.SysErr _ => ();
wenzelm@26098
   181
    val _ = OS.FileSys.remove pid_name handle OS.SysErr _ => ();
wenzelm@26098
   182
    val _ = OS.FileSys.remove output_name handle OS.SysErr _ => ();
wenzelm@26098
   183
    val _ = Thread.interrupt system_thread handle Thread _ => ();
wenzelm@26098
   184
    val rc = (case ! result of Signal => raise Interrupt | Result rc => rc);
wenzelm@26098
   185
  in (output, rc) end) ();
wenzelm@26098
   186
wenzelm@26221
   187
val system_out =
wenzelm@26221
   188
  if ml_system = "polyml-5.1" then system_out  (*signals not propagated from root thread!*)
wenzelm@26221
   189
  else system_out_threaded;
wenzelm@26221
   190
wenzelm@26098
   191
wenzelm@23961
   192
(* critical section -- may be nested within the same thread *)
wenzelm@23961
   193
wenzelm@23961
   194
local
wenzelm@23961
   195
wenzelm@24063
   196
val critical_lock = Mutex.mutex ();
wenzelm@24063
   197
val critical_thread = ref (NONE: Thread.thread option);
wenzelm@24063
   198
val critical_name = ref "";
wenzelm@24063
   199
wenzelm@23961
   200
in
wenzelm@23961
   201
wenzelm@23961
   202
fun self_critical () =
wenzelm@23961
   203
  (case ! critical_thread of
wenzelm@23961
   204
    NONE => false
wenzelm@23961
   205
  | SOME id => Thread.equal (id, Thread.self ()));
wenzelm@23961
   206
wenzelm@23991
   207
fun NAMED_CRITICAL name e =
wenzelm@23961
   208
  if self_critical () then e ()
wenzelm@23961
   209
  else
wenzelm@26083
   210
    uninterruptible (fn restore_attributes => fn () =>
wenzelm@24208
   211
      let
wenzelm@24208
   212
        val name' = ! critical_name;
wenzelm@24208
   213
        val _ =
wenzelm@24208
   214
          if Mutex.trylock critical_lock then ()
wenzelm@24208
   215
          else
wenzelm@24208
   216
            let
wenzelm@24208
   217
              val timer = Timer.startRealTimer ();
wenzelm@24208
   218
              val _ = tracing 4 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
wenzelm@24208
   219
              val _ = Mutex.lock critical_lock;
wenzelm@24208
   220
              val time = Timer.checkRealTimer timer;
wenzelm@26493
   221
              val trace_time =
wenzelm@26493
   222
                if Time.>= (time, Time.fromMilliseconds 1000) then 1
wenzelm@26493
   223
                else if Time.>= (time, Time.fromMilliseconds 100) then 2
wenzelm@26493
   224
                else if Time.>= (time, Time.fromMilliseconds 10) then 3 else 4;
wenzelm@26493
   225
              val _ = tracing trace_time (fn () =>
wenzelm@24208
   226
                "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
wenzelm@24208
   227
            in () end;
wenzelm@24208
   228
        val _ = critical_thread := SOME (Thread.self ());
wenzelm@24208
   229
        val _ = critical_name := name;
wenzelm@26083
   230
        val result = Exn.capture (restore_attributes e) ();
wenzelm@24208
   231
        val _ = critical_name := "";
wenzelm@24208
   232
        val _ = critical_thread := NONE;
wenzelm@24208
   233
        val _ = Mutex.unlock critical_lock;
wenzelm@24208
   234
      in Exn.release result end) ();
wenzelm@23961
   235
wenzelm@23991
   236
fun CRITICAL e = NAMED_CRITICAL "" e;
wenzelm@23981
   237
wenzelm@23961
   238
end;
wenzelm@23961
   239
wenzelm@23973
   240
wenzelm@24208
   241
(* scheduling -- multiple threads working on a queue of tasks *)
wenzelm@24208
   242
wenzelm@24208
   243
datatype 'a task =
wenzelm@24208
   244
  Task of {body: unit -> unit, cont: 'a -> 'a, fail: 'a -> 'a} | Wait | Terminate;
wenzelm@23973
   245
wenzelm@26083
   246
fun schedule n next_task = uninterruptible (fn restore_attributes => fn tasks =>
wenzelm@23973
   247
  let
wenzelm@23973
   248
    (*protected execution*)
wenzelm@23973
   249
    val lock = Mutex.mutex ();
wenzelm@24672
   250
    val protected_name = ref "";
wenzelm@24063
   251
    fun PROTECTED name e =
wenzelm@23973
   252
      let
wenzelm@24144
   253
        val name' = ! protected_name;
wenzelm@23981
   254
        val _ =
wenzelm@23981
   255
          if Mutex.trylock lock then ()
wenzelm@23981
   256
          else
wenzelm@24144
   257
            let
wenzelm@24144
   258
              val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": waiting");
wenzelm@24144
   259
              val _ = Mutex.lock lock;
wenzelm@24144
   260
              val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": passed");
wenzelm@24144
   261
            in () end;
wenzelm@24069
   262
        val _ = protected_name := name;
wenzelm@23973
   263
        val res = Exn.capture e ();
wenzelm@24069
   264
        val _ = protected_name := "";
wenzelm@23973
   265
        val _ = Mutex.unlock lock;
wenzelm@23973
   266
      in Exn.release res end;
wenzelm@23973
   267
wenzelm@24072
   268
    (*wakeup condition*)
wenzelm@24072
   269
    val wakeup = ConditionVar.conditionVar ();
wenzelm@24072
   270
    fun wakeup_all () = ConditionVar.broadcast wakeup;
wenzelm@24072
   271
    fun wait () = ConditionVar.wait (wakeup, lock);
wenzelm@24291
   272
    fun wait_timeout () = ConditionVar.waitUntil (wakeup, lock, Time.now () + Time.fromSeconds 1);
wenzelm@24072
   273
wenzelm@24214
   274
    (*queue of tasks*)
wenzelm@23973
   275
    val queue = ref tasks;
wenzelm@24072
   276
    val active = ref 0;
wenzelm@24119
   277
    fun trace_active () = tracing 1 (fn () => "SCHEDULE: " ^ Int.toString (! active) ^ " active");
wenzelm@24072
   278
    fun dequeue () =
wenzelm@23973
   279
      let
wenzelm@23981
   280
        val (next, tasks') = next_task (! queue);
wenzelm@23973
   281
        val _ = queue := tasks';
wenzelm@24072
   282
      in
wenzelm@24208
   283
        (case next of Wait =>
wenzelm@24208
   284
          (dec active; trace_active ();
wenzelm@24208
   285
            wait ();
wenzelm@24208
   286
            inc active; trace_active ();
wenzelm@24208
   287
            dequeue ())
wenzelm@24208
   288
        | _ => next)
wenzelm@24072
   289
      end;
wenzelm@23973
   290
wenzelm@24208
   291
    (*pool of running threads*)
wenzelm@23973
   292
    val status = ref ([]: exn list);
wenzelm@24208
   293
    val running = ref ([]: Thread.thread list);
wenzelm@24208
   294
    fun start f =
wenzelm@24208
   295
      (inc active;
wenzelm@24208
   296
       change running (cons (Thread.fork (f, [Thread.InterruptState Thread.InterruptDefer]))));
wenzelm@24208
   297
    fun stop () =
wenzelm@24208
   298
      (dec active;
wenzelm@24208
   299
       change running (List.filter (fn t => not (Thread.equal (t, Thread.self ())))));
wenzelm@24208
   300
wenzelm@24208
   301
   (*worker thread*)
wenzelm@24208
   302
    fun worker () =
wenzelm@24072
   303
      (case PROTECTED "dequeue" dequeue of
wenzelm@24208
   304
        Task {body, cont, fail} =>
wenzelm@26083
   305
          (case Exn.capture (restore_attributes body) () of
wenzelm@24208
   306
            Exn.Result () =>
wenzelm@24208
   307
              (PROTECTED "cont" (fn () => (change queue cont; wakeup_all ())); worker ())
wenzelm@23981
   308
          | Exn.Exn exn =>
wenzelm@24208
   309
              PROTECTED "fail" (fn () =>
wenzelm@24208
   310
                (change status (cons exn); change queue fail; stop (); wakeup_all ())))
wenzelm@24208
   311
      | Terminate => PROTECTED "terminate" (fn () => (stop (); wakeup_all ())));
wenzelm@23973
   312
wenzelm@23973
   313
    (*main control: fork and wait*)
wenzelm@23973
   314
    fun fork 0 = ()
wenzelm@24208
   315
      | fork k = (start worker; fork (k - 1));
wenzelm@24063
   316
    val _ = PROTECTED "main" (fn () =>
wenzelm@24063
   317
     (fork (Int.max (n, 1));
wenzelm@24208
   318
      while not (List.null (! running)) do
wenzelm@24208
   319
      (trace_active ();
wenzelm@26254
   320
       if not (List.null (! status))
wenzelm@26254
   321
       then (List.app (fn t => Thread.interrupt t handle Thread _ => ()) (! running))
wenzelm@26254
   322
       else ();
wenzelm@24291
   323
       wait_timeout ())));
wenzelm@23973
   324
wenzelm@24208
   325
  in ! status end);
wenzelm@23973
   326
wenzelm@25704
   327
wenzelm@26390
   328
(* profiling *)
wenzelm@26390
   329
wenzelm@26390
   330
local val profile_orig = profile in
wenzelm@26390
   331
wenzelm@26390
   332
fun profile 0 f x = f x
wenzelm@26390
   333
  | profile n f x = NAMED_CRITICAL "profile" (fn () => profile_orig n f x);
wenzelm@26390
   334
wenzelm@26390
   335
end;
wenzelm@26390
   336
wenzelm@26390
   337
wenzelm@25704
   338
(* serial numbers *)
wenzelm@25704
   339
wenzelm@25704
   340
local
wenzelm@25704
   341
wenzelm@25704
   342
val serial_lock = Mutex.mutex ();
wenzelm@25704
   343
val serial_count = ref 0;
wenzelm@25704
   344
wenzelm@25704
   345
in
wenzelm@25704
   346
wenzelm@25704
   347
val serial = uninterruptible (fn _ => fn () =>
wenzelm@25704
   348
  let
wenzelm@25704
   349
    val _ = Mutex.lock serial_lock;
wenzelm@25704
   350
    val res = inc serial_count;
wenzelm@25704
   351
    val _ = Mutex.unlock serial_lock;
wenzelm@25704
   352
  in res end);
wenzelm@25704
   353
wenzelm@23961
   354
end;
wenzelm@23961
   355
wenzelm@25735
   356
wenzelm@25735
   357
(* thread data *)
wenzelm@25735
   358
wenzelm@25735
   359
val get_data = Thread.getLocal;
wenzelm@25735
   360
val put_data = Thread.setLocal;
wenzelm@25735
   361
wenzelm@25704
   362
end;
wenzelm@24688
   363
wenzelm@25704
   364
structure BasicMultithreading: BASIC_MULTITHREADING = Multithreading;
wenzelm@25704
   365
open BasicMultithreading;