src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Sat Nov 27 16:29:53 2010 +0100 (2010-11-27 ago)
changeset 40748 591b6778d076
parent 40301 bf39a257b3d3
child 41710 11ae688e4e30
permissions -rw-r--r--
removed bash from ML system bootstrap, and past the Secure ML barrier;
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@25704
    11
  structure TimeLimit: TIME_LIMIT
wenzelm@25704
    12
end;
wenzelm@25704
    13
wenzelm@25704
    14
signature BASIC_MULTITHREADING =
wenzelm@25704
    15
sig
wenzelm@25704
    16
  include BASIC_MULTITHREADING
wenzelm@25704
    17
  include MULTITHREADING_POLYML
wenzelm@25704
    18
end;
wenzelm@25704
    19
wenzelm@24208
    20
signature MULTITHREADING =
wenzelm@24208
    21
sig
wenzelm@24208
    22
  include MULTITHREADING
wenzelm@25704
    23
  include MULTITHREADING_POLYML
wenzelm@24208
    24
end;
wenzelm@24208
    25
wenzelm@23961
    26
structure Multithreading: MULTITHREADING =
wenzelm@23961
    27
struct
wenzelm@23961
    28
wenzelm@32185
    29
(* options *)
wenzelm@32184
    30
wenzelm@23981
    31
val available = true;
wenzelm@25775
    32
wenzelm@39616
    33
val max_threads = ref 0;
wenzelm@23973
    34
wenzelm@25775
    35
fun max_threads_value () =
wenzelm@31630
    36
  let val m = ! max_threads in
wenzelm@31630
    37
    if m > 0 then m
wenzelm@33180
    38
    else Int.min (Int.max (Thread.numProcessors (), 1), 4)
wenzelm@31630
    39
  end;
wenzelm@25775
    40
wenzelm@28555
    41
fun enabled () = max_threads_value () > 1;
wenzelm@28555
    42
wenzelm@23973
    43
wenzelm@24208
    44
(* thread attributes *)
wenzelm@24208
    45
wenzelm@28161
    46
val no_interrupts =
wenzelm@28161
    47
  [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer];
wenzelm@28161
    48
wenzelm@32295
    49
val public_interrupts =
wenzelm@28161
    50
  [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce];
wenzelm@28161
    51
wenzelm@32295
    52
val private_interrupts =
wenzelm@30612
    53
  [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptAsynchOnce];
wenzelm@30612
    54
wenzelm@32295
    55
val sync_interrupts = map
wenzelm@32295
    56
  (fn x as Thread.InterruptState Thread.InterruptDefer => x
wenzelm@32295
    57
    | Thread.InterruptState _ => Thread.InterruptState Thread.InterruptSynch
wenzelm@32295
    58
    | x => x);
wenzelm@32295
    59
wenzelm@28466
    60
val safe_interrupts = map
wenzelm@28466
    61
  (fn Thread.InterruptState Thread.InterruptAsynch =>
wenzelm@28466
    62
      Thread.InterruptState Thread.InterruptAsynchOnce
wenzelm@28466
    63
    | x => x);
wenzelm@28466
    64
wenzelm@32295
    65
fun with_attributes new_atts e =
wenzelm@24208
    66
  let
wenzelm@29550
    67
    val orig_atts = safe_interrupts (Thread.getAttributes ());
wenzelm@30602
    68
    val result = Exn.capture (fn () =>
wenzelm@32295
    69
      (Thread.setAttributes (safe_interrupts new_atts); e orig_atts)) ();
wenzelm@30602
    70
    val _ = Thread.setAttributes orig_atts;
wenzelm@28466
    71
  in Exn.release result end;
wenzelm@24208
    72
wenzelm@32286
    73
wenzelm@32295
    74
(* portable wrappers *)
wenzelm@32295
    75
wenzelm@32295
    76
fun interruptible f x = with_attributes public_interrupts (fn _ => f x);
wenzelm@32286
    77
wenzelm@32295
    78
fun uninterruptible f x =
wenzelm@32295
    79
  with_attributes no_interrupts (fn atts =>
wenzelm@32295
    80
    f (fn g => fn y => with_attributes atts (fn _ => g y)) x);
wenzelm@24668
    81
wenzelm@24688
    82
wenzelm@32286
    83
(* synchronous wait *)
wenzelm@32286
    84
wenzelm@32295
    85
fun sync_wait opt_atts time cond lock =
wenzelm@32295
    86
  with_attributes
wenzelm@32295
    87
    (sync_interrupts (case opt_atts of SOME atts => atts | NONE => Thread.getAttributes ()))
wenzelm@32295
    88
    (fn _ =>
wenzelm@32295
    89
      (case time of
wenzelm@32295
    90
        SOME t => Exn.Result (ConditionVar.waitUntil (cond, lock, t))
wenzelm@32295
    91
      | NONE => (ConditionVar.wait (cond, lock); Exn.Result true))
wenzelm@32295
    92
      handle exn => Exn.Exn exn);
wenzelm@32295
    93
wenzelm@32295
    94
wenzelm@32295
    95
(* tracing *)
wenzelm@32295
    96
wenzelm@39616
    97
val trace = ref 0;
wenzelm@32295
    98
wenzelm@32295
    99
fun tracing level msg =
wenzelm@32295
   100
  if level > ! trace then ()
wenzelm@32295
   101
  else uninterruptible (fn _ => fn () =>
wenzelm@32295
   102
    (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
wenzelm@32295
   103
      handle _ (*sic*) => ()) ();
wenzelm@32295
   104
wenzelm@32295
   105
fun tracing_time detailed time =
wenzelm@32295
   106
  tracing
wenzelm@32295
   107
   (if not detailed then 5
wenzelm@40301
   108
    else if Time.>= (time, seconds 1.0) then 1
wenzelm@40301
   109
    else if Time.>= (time, seconds 0.1) then 2
wenzelm@40301
   110
    else if Time.>= (time, seconds 0.01) then 3
wenzelm@40301
   111
    else if Time.>= (time, seconds 0.001) then 4 else 5);
wenzelm@32295
   112
wenzelm@32295
   113
fun real_time f x =
wenzelm@32286
   114
  let
wenzelm@32295
   115
    val timer = Timer.startRealTimer ();
wenzelm@32295
   116
    val () = f x;
wenzelm@32295
   117
    val time = Timer.checkRealTimer timer;
wenzelm@32295
   118
  in time end;
wenzelm@32286
   119
wenzelm@32286
   120
wenzelm@24688
   121
(* execution with time limit *)
wenzelm@24688
   122
wenzelm@24688
   123
structure TimeLimit =
wenzelm@24688
   124
struct
wenzelm@24688
   125
wenzelm@24688
   126
exception TimeOut;
wenzelm@24688
   127
wenzelm@26083
   128
fun timeLimit time f x = uninterruptible (fn restore_attributes => fn () =>
wenzelm@26083
   129
  let
wenzelm@26083
   130
    val worker = Thread.self ();
wenzelm@39616
   131
    val timeout = ref false;
wenzelm@26083
   132
    val watchdog = Thread.fork (fn () =>
wenzelm@26083
   133
      (OS.Process.sleep time; timeout := true; Thread.interrupt worker), []);
wenzelm@24688
   134
wenzelm@26083
   135
    val result = Exn.capture (restore_attributes f) x;
wenzelm@39232
   136
    val was_timeout = Exn.is_interrupt_exn result andalso ! timeout;
wenzelm@24688
   137
wenzelm@26083
   138
    val _ = Thread.interrupt watchdog handle Thread _ => ();
wenzelm@26083
   139
  in if was_timeout then raise TimeOut else Exn.release result end) ();
wenzelm@24688
   140
wenzelm@24688
   141
end;
wenzelm@24668
   142
wenzelm@24069
   143
wenzelm@23961
   144
(* critical section -- may be nested within the same thread *)
wenzelm@23961
   145
wenzelm@23961
   146
local
wenzelm@23961
   147
wenzelm@24063
   148
val critical_lock = Mutex.mutex ();
wenzelm@39616
   149
val critical_thread = ref (NONE: Thread.thread option);
wenzelm@39616
   150
val critical_name = ref "";
wenzelm@24063
   151
wenzelm@40748
   152
fun show "" = "" | show name = " " ^ name;
wenzelm@40748
   153
fun show' "" = "" | show' name = " [" ^ name ^ "]";
wenzelm@40748
   154
wenzelm@23961
   155
in
wenzelm@23961
   156
wenzelm@23961
   157
fun self_critical () =
wenzelm@23961
   158
  (case ! critical_thread of
wenzelm@23961
   159
    NONE => false
wenzelm@28150
   160
  | SOME t => Thread.equal (t, Thread.self ()));
wenzelm@23961
   161
wenzelm@23991
   162
fun NAMED_CRITICAL name e =
wenzelm@23961
   163
  if self_critical () then e ()
wenzelm@23961
   164
  else
wenzelm@32184
   165
    Exn.release (uninterruptible (fn restore_attributes => fn () =>
wenzelm@24208
   166
      let
wenzelm@24208
   167
        val name' = ! critical_name;
wenzelm@24208
   168
        val _ =
wenzelm@24208
   169
          if Mutex.trylock critical_lock then ()
wenzelm@24208
   170
          else
wenzelm@24208
   171
            let
wenzelm@32184
   172
              val _ = tracing 5 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
wenzelm@32185
   173
              val time = real_time Mutex.lock critical_lock;
wenzelm@32186
   174
              val _ = tracing_time true time (fn () =>
wenzelm@24208
   175
                "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
wenzelm@24208
   176
            in () end;
wenzelm@24208
   177
        val _ = critical_thread := SOME (Thread.self ());
wenzelm@24208
   178
        val _ = critical_name := name;
wenzelm@26083
   179
        val result = Exn.capture (restore_attributes e) ();
wenzelm@24208
   180
        val _ = critical_name := "";
wenzelm@24208
   181
        val _ = critical_thread := NONE;
wenzelm@24208
   182
        val _ = Mutex.unlock critical_lock;
wenzelm@32184
   183
      in result end) ());
wenzelm@23961
   184
wenzelm@23991
   185
fun CRITICAL e = NAMED_CRITICAL "" e;
wenzelm@23981
   186
wenzelm@23961
   187
end;
wenzelm@23961
   188
wenzelm@23973
   189
wenzelm@25704
   190
(* serial numbers *)
wenzelm@25704
   191
wenzelm@25704
   192
local
wenzelm@25704
   193
wenzelm@25704
   194
val serial_lock = Mutex.mutex ();
wenzelm@39616
   195
val serial_count = ref 0;
wenzelm@25704
   196
wenzelm@25704
   197
in
wenzelm@25704
   198
wenzelm@25704
   199
val serial = uninterruptible (fn _ => fn () =>
wenzelm@25704
   200
  let
wenzelm@25704
   201
    val _ = Mutex.lock serial_lock;
wenzelm@28124
   202
    val _ = serial_count := ! serial_count + 1;
wenzelm@28124
   203
    val res = ! serial_count;
wenzelm@25704
   204
    val _ = Mutex.unlock serial_lock;
wenzelm@25704
   205
  in res end);
wenzelm@25704
   206
wenzelm@23961
   207
end;
wenzelm@23961
   208
wenzelm@25704
   209
end;
wenzelm@24688
   210
wenzelm@32286
   211
structure Basic_Multithreading: BASIC_MULTITHREADING = Multithreading;
wenzelm@32286
   212
open Basic_Multithreading;