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