src/Pure/ML-Systems/multithreading_polyml.ML
author haftmann
Tue Oct 20 16:13:01 2009 +0200 (2009-10-20)
changeset 33037 b22e44496dc2
parent 32738 15bb09ca0378
child 33180 e1b0280f0299
permissions -rw-r--r--
replaced old_style infixes eq_set, subset, union, inter and variants by generic versions
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@32185
    30
(* options *)
wenzelm@32184
    31
wenzelm@23981
    32
val available = true;
wenzelm@25775
    33
wenzelm@32738
    34
val max_threads = Unsynchronized.ref 0;
wenzelm@23973
    35
wenzelm@31630
    36
val tested_platform =
wenzelm@31630
    37
  let val ml_platform = getenv "ML_PLATFORM"
wenzelm@31630
    38
  in String.isSuffix "linux" ml_platform orelse String.isSuffix "darwin" ml_platform end;
wenzelm@31630
    39
wenzelm@25775
    40
fun max_threads_value () =
wenzelm@31630
    41
  let val m = ! max_threads in
wenzelm@31630
    42
    if m > 0 then m
wenzelm@31630
    43
    else if not tested_platform then 1
wenzelm@31630
    44
    else Int.min (Int.max (Thread.numProcessors (), 1), 8)
wenzelm@31630
    45
  end;
wenzelm@25775
    46
wenzelm@28555
    47
fun enabled () = max_threads_value () > 1;
wenzelm@28555
    48
wenzelm@23973
    49
wenzelm@24069
    50
(* misc utils *)
wenzelm@24069
    51
wenzelm@24208
    52
fun show "" = "" | show name = " " ^ name;
wenzelm@24208
    53
fun show' "" = "" | show' name = " [" ^ name ^ "]";
wenzelm@24208
    54
wenzelm@26098
    55
fun read_file name =
wenzelm@26098
    56
  let val is = TextIO.openIn name
wenzelm@26504
    57
  in Exn.release (Exn.capture TextIO.inputAll is before TextIO.closeIn is) end;
wenzelm@26098
    58
wenzelm@26098
    59
fun write_file name txt =
wenzelm@26098
    60
  let val os = TextIO.openOut name
wenzelm@26504
    61
  in Exn.release (Exn.capture TextIO.output (os, txt) before TextIO.closeOut os) end;
wenzelm@26098
    62
wenzelm@24208
    63
wenzelm@24208
    64
(* thread attributes *)
wenzelm@24208
    65
wenzelm@28161
    66
val no_interrupts =
wenzelm@28161
    67
  [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer];
wenzelm@28161
    68
wenzelm@32295
    69
val public_interrupts =
wenzelm@28161
    70
  [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce];
wenzelm@28161
    71
wenzelm@32295
    72
val private_interrupts =
wenzelm@30612
    73
  [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptAsynchOnce];
wenzelm@30612
    74
wenzelm@32295
    75
val sync_interrupts = map
wenzelm@32295
    76
  (fn x as Thread.InterruptState Thread.InterruptDefer => x
wenzelm@32295
    77
    | Thread.InterruptState _ => Thread.InterruptState Thread.InterruptSynch
wenzelm@32295
    78
    | x => x);
wenzelm@32295
    79
wenzelm@28466
    80
val safe_interrupts = map
wenzelm@28466
    81
  (fn Thread.InterruptState Thread.InterruptAsynch =>
wenzelm@28466
    82
      Thread.InterruptState Thread.InterruptAsynchOnce
wenzelm@28466
    83
    | x => x);
wenzelm@28466
    84
wenzelm@32295
    85
fun with_attributes new_atts e =
wenzelm@24208
    86
  let
wenzelm@29550
    87
    val orig_atts = safe_interrupts (Thread.getAttributes ());
wenzelm@30602
    88
    val result = Exn.capture (fn () =>
wenzelm@32295
    89
      (Thread.setAttributes (safe_interrupts new_atts); e orig_atts)) ();
wenzelm@30602
    90
    val _ = Thread.setAttributes orig_atts;
wenzelm@28466
    91
  in Exn.release result end;
wenzelm@24208
    92
wenzelm@32286
    93
wenzelm@32295
    94
(* portable wrappers *)
wenzelm@32295
    95
wenzelm@32295
    96
fun interruptible f x = with_attributes public_interrupts (fn _ => f x);
wenzelm@32286
    97
wenzelm@32295
    98
fun uninterruptible f x =
wenzelm@32295
    99
  with_attributes no_interrupts (fn atts =>
wenzelm@32295
   100
    f (fn g => fn y => with_attributes atts (fn _ => g y)) x);
wenzelm@24668
   101
wenzelm@24688
   102
wenzelm@32286
   103
(* synchronous wait *)
wenzelm@32286
   104
wenzelm@32295
   105
fun sync_wait opt_atts time cond lock =
wenzelm@32295
   106
  with_attributes
wenzelm@32295
   107
    (sync_interrupts (case opt_atts of SOME atts => atts | NONE => Thread.getAttributes ()))
wenzelm@32295
   108
    (fn _ =>
wenzelm@32295
   109
      (case time of
wenzelm@32295
   110
        SOME t => Exn.Result (ConditionVar.waitUntil (cond, lock, t))
wenzelm@32295
   111
      | NONE => (ConditionVar.wait (cond, lock); Exn.Result true))
wenzelm@32295
   112
      handle exn => Exn.Exn exn);
wenzelm@32295
   113
wenzelm@32295
   114
wenzelm@32295
   115
(* tracing *)
wenzelm@32295
   116
wenzelm@32738
   117
val trace = Unsynchronized.ref 0;
wenzelm@32295
   118
wenzelm@32295
   119
fun tracing level msg =
wenzelm@32295
   120
  if level > ! trace then ()
wenzelm@32295
   121
  else uninterruptible (fn _ => fn () =>
wenzelm@32295
   122
    (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
wenzelm@32295
   123
      handle _ (*sic*) => ()) ();
wenzelm@32295
   124
wenzelm@32295
   125
fun tracing_time detailed time =
wenzelm@32295
   126
  tracing
wenzelm@32295
   127
   (if not detailed then 5
wenzelm@32295
   128
    else if Time.>= (time, Time.fromMilliseconds 1000) then 1
wenzelm@32295
   129
    else if Time.>= (time, Time.fromMilliseconds 100) then 2
wenzelm@32295
   130
    else if Time.>= (time, Time.fromMilliseconds 10) then 3
wenzelm@32295
   131
    else if Time.>= (time, Time.fromMilliseconds 1) then 4 else 5);
wenzelm@32295
   132
wenzelm@32295
   133
fun real_time f x =
wenzelm@32286
   134
  let
wenzelm@32295
   135
    val timer = Timer.startRealTimer ();
wenzelm@32295
   136
    val () = f x;
wenzelm@32295
   137
    val time = Timer.checkRealTimer timer;
wenzelm@32295
   138
  in time end;
wenzelm@32286
   139
wenzelm@32286
   140
wenzelm@24688
   141
(* execution with time limit *)
wenzelm@24688
   142
wenzelm@24688
   143
structure TimeLimit =
wenzelm@24688
   144
struct
wenzelm@24688
   145
wenzelm@24688
   146
exception TimeOut;
wenzelm@24688
   147
wenzelm@26083
   148
fun timeLimit time f x = uninterruptible (fn restore_attributes => fn () =>
wenzelm@26083
   149
  let
wenzelm@26083
   150
    val worker = Thread.self ();
wenzelm@32738
   151
    val timeout = Unsynchronized.ref false;
wenzelm@26083
   152
    val watchdog = Thread.fork (fn () =>
wenzelm@26083
   153
      (OS.Process.sleep time; timeout := true; Thread.interrupt worker), []);
wenzelm@24688
   154
wenzelm@26083
   155
    val result = Exn.capture (restore_attributes f) x;
wenzelm@28443
   156
    val was_timeout = (case result of Exn.Exn Exn.Interrupt => ! timeout | _ => false);
wenzelm@24688
   157
wenzelm@26083
   158
    val _ = Thread.interrupt watchdog handle Thread _ => ();
wenzelm@26083
   159
  in if was_timeout then raise TimeOut else Exn.release result end) ();
wenzelm@24688
   160
wenzelm@24688
   161
end;
wenzelm@24668
   162
wenzelm@24069
   163
wenzelm@26098
   164
(* system shell processes, with propagation of interrupts *)
wenzelm@26098
   165
wenzelm@32295
   166
fun system_out script = with_attributes no_interrupts (fn orig_atts =>
wenzelm@26098
   167
  let
wenzelm@26098
   168
    val script_name = OS.FileSys.tmpName ();
wenzelm@26098
   169
    val _ = write_file script_name script;
wenzelm@26098
   170
wenzelm@26098
   171
    val pid_name = OS.FileSys.tmpName ();
wenzelm@26098
   172
    val output_name = OS.FileSys.tmpName ();
wenzelm@26098
   173
wenzelm@26098
   174
    (*result state*)
wenzelm@26098
   175
    datatype result = Wait | Signal | Result of int;
wenzelm@32738
   176
    val result = Unsynchronized.ref Wait;
wenzelm@32295
   177
    val lock = Mutex.mutex ();
wenzelm@32295
   178
    val cond = ConditionVar.conditionVar ();
wenzelm@26098
   179
    fun set_result res =
wenzelm@32295
   180
      (Mutex.lock lock; result := res; ConditionVar.signal cond; Mutex.unlock lock);
wenzelm@26098
   181
wenzelm@32295
   182
    val _ = Mutex.lock lock;
wenzelm@26098
   183
wenzelm@26098
   184
    (*system thread*)
wenzelm@26098
   185
    val system_thread = Thread.fork (fn () =>
wenzelm@26098
   186
      let
wenzelm@26098
   187
        val status =
wenzelm@26098
   188
          OS.Process.system ("perl -w \"$ISABELLE_HOME/lib/scripts/system.pl\" group " ^
wenzelm@26098
   189
            script_name ^ " " ^ pid_name ^ " " ^ output_name);
wenzelm@26098
   190
        val res =
wenzelm@26098
   191
          (case Posix.Process.fromStatus status of
wenzelm@26098
   192
            Posix.Process.W_EXITED => Result 0
wenzelm@26098
   193
          | Posix.Process.W_EXITSTATUS 0wx82 => Signal
wenzelm@26098
   194
          | Posix.Process.W_EXITSTATUS w => Result (Word8.toInt w)
wenzelm@26098
   195
          | Posix.Process.W_SIGNALED s =>
wenzelm@26098
   196
              if s = Posix.Signal.int then Signal
wenzelm@26098
   197
              else Result (256 + LargeWord.toInt (Posix.Signal.toWord s))
wenzelm@26098
   198
          | Posix.Process.W_STOPPED s => Result (512 + LargeWord.toInt (Posix.Signal.toWord s)));
wenzelm@28398
   199
      in set_result res end handle _ (*sic*) => set_result (Result 2), []);
wenzelm@26098
   200
wenzelm@26098
   201
    (*main thread -- proxy for interrupts*)
wenzelm@26098
   202
    fun kill n =
wenzelm@26098
   203
      (case Int.fromString (read_file pid_name) of
wenzelm@26098
   204
        SOME pid =>
wenzelm@26098
   205
          Posix.Process.kill
wenzelm@26098
   206
            (Posix.Process.K_GROUP (Posix.Process.wordToPid (LargeWord.fromInt pid)),
wenzelm@26098
   207
              Posix.Signal.int)
wenzelm@26098
   208
      | NONE => ())
wenzelm@26098
   209
      handle OS.SysErr _ => () | IO.Io _ =>
wenzelm@26098
   210
        (OS.Process.sleep (Time.fromMilliseconds 100); if n > 0 then kill (n - 1) else ());
wenzelm@26098
   211
wenzelm@32295
   212
    val _ =
wenzelm@32295
   213
      while ! result = Wait do
wenzelm@32295
   214
        let val res =
wenzelm@32295
   215
          sync_wait (SOME orig_atts)
wenzelm@32295
   216
            (SOME (Time.+ (Time.now (), Time.fromMilliseconds 100))) cond lock
wenzelm@32295
   217
        in case res of Exn.Exn Exn.Interrupt => kill 10 | _ => () end;
wenzelm@26098
   218
wenzelm@26098
   219
    (*cleanup*)
wenzelm@26098
   220
    val output = read_file output_name handle IO.Io _ => "";
wenzelm@26098
   221
    val _ = OS.FileSys.remove script_name handle OS.SysErr _ => ();
wenzelm@26098
   222
    val _ = OS.FileSys.remove pid_name handle OS.SysErr _ => ();
wenzelm@26098
   223
    val _ = OS.FileSys.remove output_name handle OS.SysErr _ => ();
wenzelm@26098
   224
    val _ = Thread.interrupt system_thread handle Thread _ => ();
wenzelm@28443
   225
    val rc = (case ! result of Signal => raise Exn.Interrupt | Result rc => rc);
wenzelm@32295
   226
  in (output, rc) end);
wenzelm@26098
   227
wenzelm@26098
   228
wenzelm@23961
   229
(* critical section -- may be nested within the same thread *)
wenzelm@23961
   230
wenzelm@23961
   231
local
wenzelm@23961
   232
wenzelm@24063
   233
val critical_lock = Mutex.mutex ();
wenzelm@32738
   234
val critical_thread = Unsynchronized.ref (NONE: Thread.thread option);
wenzelm@32738
   235
val critical_name = Unsynchronized.ref "";
wenzelm@24063
   236
wenzelm@23961
   237
in
wenzelm@23961
   238
wenzelm@23961
   239
fun self_critical () =
wenzelm@23961
   240
  (case ! critical_thread of
wenzelm@23961
   241
    NONE => false
wenzelm@28150
   242
  | SOME t => Thread.equal (t, Thread.self ()));
wenzelm@23961
   243
wenzelm@23991
   244
fun NAMED_CRITICAL name e =
wenzelm@23961
   245
  if self_critical () then e ()
wenzelm@23961
   246
  else
wenzelm@32184
   247
    Exn.release (uninterruptible (fn restore_attributes => fn () =>
wenzelm@24208
   248
      let
wenzelm@24208
   249
        val name' = ! critical_name;
wenzelm@24208
   250
        val _ =
wenzelm@24208
   251
          if Mutex.trylock critical_lock then ()
wenzelm@24208
   252
          else
wenzelm@24208
   253
            let
wenzelm@32184
   254
              val _ = tracing 5 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
wenzelm@32185
   255
              val time = real_time Mutex.lock critical_lock;
wenzelm@32186
   256
              val _ = tracing_time true time (fn () =>
wenzelm@24208
   257
                "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
wenzelm@24208
   258
            in () end;
wenzelm@24208
   259
        val _ = critical_thread := SOME (Thread.self ());
wenzelm@24208
   260
        val _ = critical_name := name;
wenzelm@26083
   261
        val result = Exn.capture (restore_attributes e) ();
wenzelm@24208
   262
        val _ = critical_name := "";
wenzelm@24208
   263
        val _ = critical_thread := NONE;
wenzelm@24208
   264
        val _ = Mutex.unlock critical_lock;
wenzelm@32184
   265
      in result end) ());
wenzelm@23961
   266
wenzelm@23991
   267
fun CRITICAL e = NAMED_CRITICAL "" e;
wenzelm@23981
   268
wenzelm@23961
   269
end;
wenzelm@23961
   270
wenzelm@23973
   271
wenzelm@25704
   272
(* serial numbers *)
wenzelm@25704
   273
wenzelm@25704
   274
local
wenzelm@25704
   275
wenzelm@25704
   276
val serial_lock = Mutex.mutex ();
wenzelm@32738
   277
val serial_count = Unsynchronized.ref 0;
wenzelm@25704
   278
wenzelm@25704
   279
in
wenzelm@25704
   280
wenzelm@25704
   281
val serial = uninterruptible (fn _ => fn () =>
wenzelm@25704
   282
  let
wenzelm@25704
   283
    val _ = Mutex.lock serial_lock;
wenzelm@28124
   284
    val _ = serial_count := ! serial_count + 1;
wenzelm@28124
   285
    val res = ! serial_count;
wenzelm@25704
   286
    val _ = Mutex.unlock serial_lock;
wenzelm@25704
   287
  in res end);
wenzelm@25704
   288
wenzelm@23961
   289
end;
wenzelm@23961
   290
wenzelm@25704
   291
end;
wenzelm@24688
   292
wenzelm@32286
   293
structure Basic_Multithreading: BASIC_MULTITHREADING = Multithreading;
wenzelm@32286
   294
open Basic_Multithreading;