src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Sat Jul 25 00:13:39 2009 +0200 (2009-07-25 ago)
changeset 32184 cfa0ef0c0c5f
parent 32107 47d0da617fcc
child 32185 57ecfab3bcfe
permissions -rw-r--r--
simplified/unified Multithreading.tracing_time;
tuned;
wenzelm@23961
     1
(*  Title:      Pure/ML-Systems/multithreading_polyml.ML
wenzelm@23961
     2
    Author:     Makarius
wenzelm@23961
     3
wenzelm@31311
     4
Multithreading in Poly/ML 5.2.1 or later (cf. polyml/basis/Thread.sml).
wenzelm@23961
     5
*)
wenzelm@23961
     6
wenzelm@25704
     7
signature MULTITHREADING_POLYML =
wenzelm@25704
     8
sig
wenzelm@26083
     9
  val interruptible: ('a -> 'b) -> 'a -> 'b
wenzelm@26083
    10
  val uninterruptible: ((('c -> 'd) -> 'c -> 'd) -> 'a -> 'b) -> 'a -> 'b
wenzelm@26098
    11
  val system_out: string -> string * int
wenzelm@25704
    12
  structure TimeLimit: TIME_LIMIT
wenzelm@25704
    13
end;
wenzelm@25704
    14
wenzelm@25704
    15
signature BASIC_MULTITHREADING =
wenzelm@25704
    16
sig
wenzelm@25704
    17
  include BASIC_MULTITHREADING
wenzelm@25704
    18
  include MULTITHREADING_POLYML
wenzelm@25704
    19
end;
wenzelm@25704
    20
wenzelm@24208
    21
signature MULTITHREADING =
wenzelm@24208
    22
sig
wenzelm@24208
    23
  include MULTITHREADING
wenzelm@25704
    24
  include MULTITHREADING_POLYML
wenzelm@24208
    25
end;
wenzelm@24208
    26
wenzelm@23961
    27
structure Multithreading: MULTITHREADING =
wenzelm@23961
    28
struct
wenzelm@23961
    29
wenzelm@24072
    30
(* options *)
wenzelm@24069
    31
wenzelm@24119
    32
val trace = ref 0;
wenzelm@28465
    33
wenzelm@24119
    34
fun tracing level msg =
wenzelm@28465
    35
  if level > ! trace then ()
wenzelm@28465
    36
  else (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
wenzelm@28465
    37
    handle _ (*sic*) => ();
wenzelm@23961
    38
wenzelm@32184
    39
fun tracing_time time =
wenzelm@32184
    40
  tracing
wenzelm@32184
    41
   (if Time.>= (time, Time.fromMilliseconds 1000) then 1
wenzelm@32184
    42
    else if Time.>= (time, Time.fromMilliseconds 100) then 2
wenzelm@32184
    43
    else if Time.>= (time, Time.fromMilliseconds 10) then 3
wenzelm@32184
    44
    else if Time.>= (time, Time.fromMilliseconds 1) then 4 else 5);
wenzelm@32184
    45
wenzelm@32184
    46
wenzelm@23981
    47
val available = true;
wenzelm@25775
    48
wenzelm@31897
    49
val max_threads = ref 0;
wenzelm@23973
    50
wenzelm@31630
    51
val tested_platform =
wenzelm@31630
    52
  let val ml_platform = getenv "ML_PLATFORM"
wenzelm@31630
    53
  in String.isSuffix "linux" ml_platform orelse String.isSuffix "darwin" ml_platform end;
wenzelm@31630
    54
wenzelm@25775
    55
fun max_threads_value () =
wenzelm@31630
    56
  let val m = ! max_threads in
wenzelm@31630
    57
    if m > 0 then m
wenzelm@31630
    58
    else if not tested_platform then 1
wenzelm@31630
    59
    else Int.min (Int.max (Thread.numProcessors (), 1), 8)
wenzelm@31630
    60
  end;
wenzelm@25775
    61
wenzelm@28555
    62
fun enabled () = max_threads_value () > 1;
wenzelm@28555
    63
wenzelm@23973
    64
wenzelm@24069
    65
(* misc utils *)
wenzelm@24069
    66
wenzelm@24208
    67
fun show "" = "" | show name = " " ^ name;
wenzelm@24208
    68
fun show' "" = "" | show' name = " [" ^ name ^ "]";
wenzelm@24208
    69
wenzelm@26098
    70
fun read_file name =
wenzelm@26098
    71
  let val is = TextIO.openIn name
wenzelm@26504
    72
  in Exn.release (Exn.capture TextIO.inputAll is before TextIO.closeIn is) end;
wenzelm@26098
    73
wenzelm@26098
    74
fun write_file name txt =
wenzelm@26098
    75
  let val os = TextIO.openOut name
wenzelm@26504
    76
  in Exn.release (Exn.capture TextIO.output (os, txt) before TextIO.closeOut os) end;
wenzelm@26098
    77
wenzelm@24208
    78
wenzelm@24208
    79
(* thread attributes *)
wenzelm@24208
    80
wenzelm@28161
    81
val no_interrupts =
wenzelm@28161
    82
  [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer];
wenzelm@28161
    83
wenzelm@28161
    84
val regular_interrupts =
wenzelm@28161
    85
  [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce];
wenzelm@28161
    86
wenzelm@30612
    87
val restricted_interrupts =
wenzelm@30612
    88
  [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptAsynchOnce];
wenzelm@30612
    89
wenzelm@28466
    90
val safe_interrupts = map
wenzelm@28466
    91
  (fn Thread.InterruptState Thread.InterruptAsynch =>
wenzelm@28466
    92
      Thread.InterruptState Thread.InterruptAsynchOnce
wenzelm@28466
    93
    | x => x);
wenzelm@28466
    94
wenzelm@24208
    95
fun with_attributes new_atts f x =
wenzelm@24208
    96
  let
wenzelm@29550
    97
    val orig_atts = safe_interrupts (Thread.getAttributes ());
wenzelm@30602
    98
    val result = Exn.capture (fn () =>
wenzelm@30602
    99
      (Thread.setAttributes (safe_interrupts new_atts); f orig_atts x)) ();
wenzelm@30602
   100
    val _ = Thread.setAttributes orig_atts;
wenzelm@28466
   101
  in Exn.release result end;
wenzelm@24208
   102
wenzelm@32107
   103
fun interruptible f =
wenzelm@32107
   104
  with_attributes regular_interrupts (fn _ => fn x => f x);
wenzelm@28150
   105
wenzelm@26083
   106
fun uninterruptible f =
wenzelm@32107
   107
  with_attributes no_interrupts (fn atts => fn x =>
wenzelm@32107
   108
    f (fn g => with_attributes atts (fn _ => fn y => g y)) x);
wenzelm@24668
   109
wenzelm@24688
   110
wenzelm@24688
   111
(* execution with time limit *)
wenzelm@24688
   112
wenzelm@24688
   113
structure TimeLimit =
wenzelm@24688
   114
struct
wenzelm@24688
   115
wenzelm@24688
   116
exception TimeOut;
wenzelm@24688
   117
wenzelm@26083
   118
fun timeLimit time f x = uninterruptible (fn restore_attributes => fn () =>
wenzelm@26083
   119
  let
wenzelm@26083
   120
    val worker = Thread.self ();
wenzelm@26083
   121
    val timeout = ref false;
wenzelm@26083
   122
    val watchdog = Thread.fork (fn () =>
wenzelm@26083
   123
      (OS.Process.sleep time; timeout := true; Thread.interrupt worker), []);
wenzelm@24688
   124
wenzelm@26083
   125
    val result = Exn.capture (restore_attributes f) x;
wenzelm@28443
   126
    val was_timeout = (case result of Exn.Exn Exn.Interrupt => ! timeout | _ => false);
wenzelm@24688
   127
wenzelm@26083
   128
    val _ = Thread.interrupt watchdog handle Thread _ => ();
wenzelm@26083
   129
  in if was_timeout then raise TimeOut else Exn.release result end) ();
wenzelm@24688
   130
wenzelm@24688
   131
end;
wenzelm@24668
   132
wenzelm@24069
   133
wenzelm@26098
   134
(* system shell processes, with propagation of interrupts *)
wenzelm@26098
   135
wenzelm@28254
   136
fun system_out script = uninterruptible (fn restore_attributes => fn () =>
wenzelm@26098
   137
  let
wenzelm@26098
   138
    val script_name = OS.FileSys.tmpName ();
wenzelm@26098
   139
    val _ = write_file script_name script;
wenzelm@26098
   140
wenzelm@26098
   141
    val pid_name = OS.FileSys.tmpName ();
wenzelm@26098
   142
    val output_name = OS.FileSys.tmpName ();
wenzelm@26098
   143
wenzelm@26098
   144
    (*result state*)
wenzelm@26098
   145
    datatype result = Wait | Signal | Result of int;
wenzelm@26098
   146
    val result = ref Wait;
wenzelm@26098
   147
    val result_mutex = Mutex.mutex ();
wenzelm@26098
   148
    val result_cond = ConditionVar.conditionVar ();
wenzelm@26098
   149
    fun set_result res =
wenzelm@26098
   150
      (Mutex.lock result_mutex; result := res; Mutex.unlock result_mutex;
wenzelm@26098
   151
        ConditionVar.signal result_cond);
wenzelm@26098
   152
wenzelm@26098
   153
    val _ = Mutex.lock result_mutex;
wenzelm@26098
   154
wenzelm@26098
   155
    (*system thread*)
wenzelm@26098
   156
    val system_thread = Thread.fork (fn () =>
wenzelm@26098
   157
      let
wenzelm@26098
   158
        val status =
wenzelm@26098
   159
          OS.Process.system ("perl -w \"$ISABELLE_HOME/lib/scripts/system.pl\" group " ^
wenzelm@26098
   160
            script_name ^ " " ^ pid_name ^ " " ^ output_name);
wenzelm@26098
   161
        val res =
wenzelm@26098
   162
          (case Posix.Process.fromStatus status of
wenzelm@26098
   163
            Posix.Process.W_EXITED => Result 0
wenzelm@26098
   164
          | Posix.Process.W_EXITSTATUS 0wx82 => Signal
wenzelm@26098
   165
          | Posix.Process.W_EXITSTATUS w => Result (Word8.toInt w)
wenzelm@26098
   166
          | Posix.Process.W_SIGNALED s =>
wenzelm@26098
   167
              if s = Posix.Signal.int then Signal
wenzelm@26098
   168
              else Result (256 + LargeWord.toInt (Posix.Signal.toWord s))
wenzelm@26098
   169
          | Posix.Process.W_STOPPED s => Result (512 + LargeWord.toInt (Posix.Signal.toWord s)));
wenzelm@28398
   170
      in set_result res end handle _ (*sic*) => set_result (Result 2), []);
wenzelm@26098
   171
wenzelm@26098
   172
    (*main thread -- proxy for interrupts*)
wenzelm@26098
   173
    fun kill n =
wenzelm@26098
   174
      (case Int.fromString (read_file pid_name) of
wenzelm@26098
   175
        SOME pid =>
wenzelm@26098
   176
          Posix.Process.kill
wenzelm@26098
   177
            (Posix.Process.K_GROUP (Posix.Process.wordToPid (LargeWord.fromInt pid)),
wenzelm@26098
   178
              Posix.Signal.int)
wenzelm@26098
   179
      | NONE => ())
wenzelm@26098
   180
      handle OS.SysErr _ => () | IO.Io _ =>
wenzelm@26098
   181
        (OS.Process.sleep (Time.fromMilliseconds 100); if n > 0 then kill (n - 1) else ());
wenzelm@26098
   182
wenzelm@26098
   183
    val _ = while ! result = Wait do
wenzelm@26098
   184
      restore_attributes (fn () =>
wenzelm@26098
   185
        (ConditionVar.waitUntil (result_cond, result_mutex, Time.now () + Time.fromMilliseconds 100); ())
wenzelm@28443
   186
          handle Exn.Interrupt => kill 10) ();
wenzelm@26098
   187
wenzelm@26098
   188
    (*cleanup*)
wenzelm@26098
   189
    val output = read_file output_name handle IO.Io _ => "";
wenzelm@26098
   190
    val _ = OS.FileSys.remove script_name handle OS.SysErr _ => ();
wenzelm@26098
   191
    val _ = OS.FileSys.remove pid_name handle OS.SysErr _ => ();
wenzelm@26098
   192
    val _ = OS.FileSys.remove output_name handle OS.SysErr _ => ();
wenzelm@26098
   193
    val _ = Thread.interrupt system_thread handle Thread _ => ();
wenzelm@28443
   194
    val rc = (case ! result of Signal => raise Exn.Interrupt | Result rc => rc);
wenzelm@26098
   195
  in (output, rc) end) ();
wenzelm@26098
   196
wenzelm@26098
   197
wenzelm@23961
   198
(* critical section -- may be nested within the same thread *)
wenzelm@23961
   199
wenzelm@23961
   200
local
wenzelm@23961
   201
wenzelm@24063
   202
val critical_lock = Mutex.mutex ();
wenzelm@24063
   203
val critical_thread = ref (NONE: Thread.thread option);
wenzelm@24063
   204
val critical_name = ref "";
wenzelm@24063
   205
wenzelm@23961
   206
in
wenzelm@23961
   207
wenzelm@23961
   208
fun self_critical () =
wenzelm@23961
   209
  (case ! critical_thread of
wenzelm@23961
   210
    NONE => false
wenzelm@28150
   211
  | SOME t => Thread.equal (t, Thread.self ()));
wenzelm@23961
   212
wenzelm@23991
   213
fun NAMED_CRITICAL name e =
wenzelm@23961
   214
  if self_critical () then e ()
wenzelm@23961
   215
  else
wenzelm@32184
   216
    Exn.release (uninterruptible (fn restore_attributes => fn () =>
wenzelm@24208
   217
      let
wenzelm@24208
   218
        val name' = ! critical_name;
wenzelm@24208
   219
        val _ =
wenzelm@24208
   220
          if Mutex.trylock critical_lock then ()
wenzelm@24208
   221
          else
wenzelm@24208
   222
            let
wenzelm@24208
   223
              val timer = Timer.startRealTimer ();
wenzelm@32184
   224
              val _ = tracing 5 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
wenzelm@24208
   225
              val _ = Mutex.lock critical_lock;
wenzelm@24208
   226
              val time = Timer.checkRealTimer timer;
wenzelm@32184
   227
              val _ = tracing_time time (fn () =>
wenzelm@24208
   228
                "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
wenzelm@24208
   229
            in () end;
wenzelm@24208
   230
        val _ = critical_thread := SOME (Thread.self ());
wenzelm@24208
   231
        val _ = critical_name := name;
wenzelm@26083
   232
        val result = Exn.capture (restore_attributes e) ();
wenzelm@24208
   233
        val _ = critical_name := "";
wenzelm@24208
   234
        val _ = critical_thread := NONE;
wenzelm@24208
   235
        val _ = Mutex.unlock critical_lock;
wenzelm@32184
   236
      in result end) ());
wenzelm@23961
   237
wenzelm@23991
   238
fun CRITICAL e = NAMED_CRITICAL "" e;
wenzelm@23981
   239
wenzelm@23961
   240
end;
wenzelm@23961
   241
wenzelm@23973
   242
wenzelm@25704
   243
(* serial numbers *)
wenzelm@25704
   244
wenzelm@25704
   245
local
wenzelm@25704
   246
wenzelm@25704
   247
val serial_lock = Mutex.mutex ();
wenzelm@25704
   248
val serial_count = ref 0;
wenzelm@25704
   249
wenzelm@25704
   250
in
wenzelm@25704
   251
wenzelm@25704
   252
val serial = uninterruptible (fn _ => fn () =>
wenzelm@25704
   253
  let
wenzelm@25704
   254
    val _ = Mutex.lock serial_lock;
wenzelm@28124
   255
    val _ = serial_count := ! serial_count + 1;
wenzelm@28124
   256
    val res = ! serial_count;
wenzelm@25704
   257
    val _ = Mutex.unlock serial_lock;
wenzelm@25704
   258
  in res end);
wenzelm@25704
   259
wenzelm@23961
   260
end;
wenzelm@23961
   261
wenzelm@25704
   262
end;
wenzelm@24688
   263
wenzelm@25704
   264
structure BasicMultithreading: BASIC_MULTITHREADING = Multithreading;
wenzelm@25704
   265
open BasicMultithreading;