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