src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Wed Dec 11 18:02:22 2013 +0100 (2013-12-11 ago)
changeset 54717 42c209a6c225
parent 50910 54f06ba192ef
child 54723 124432e77ecf
permissions -rw-r--r--
support for polml-5.5.2;
support Thread.numPhysicalProcessors of polyml-5.5.2 (according to SVN 1890);
clarified max_threads: store plain value internally, reproduce result only on startup, and thus avoid potential system overhead;
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@54717
    84
  else
wenzelm@54717
    85
    let val n =
wenzelm@54717
    86
      (case Thread.numPhysicalProcessors () of
wenzelm@54717
    87
        SOME n => n
wenzelm@54717
    88
      | NONE => Thread.numProcessors ())
wenzelm@54717
    89
    in Int.min (Int.max (n, 1), 8) end;
wenzelm@54717
    90
wenzelm@54717
    91
val max_threads = ref 1;
wenzelm@54717
    92
wenzelm@54717
    93
fun max_threads_value () = ! max_threads;
wenzelm@54717
    94
wenzelm@54717
    95
fun max_threads_update m = max_threads := max_threads_result m;
wenzelm@54717
    96
wenzelm@54717
    97
fun max_threads_setmp m f x =
wenzelm@54717
    98
  uninterruptible (fn restore_attributes => fn () =>
wenzelm@54717
    99
    let
wenzelm@54717
   100
      val max_threads_orig = ! max_threads;
wenzelm@54717
   101
      val _ = max_threads_update m;
wenzelm@54717
   102
      val result = Exn.capture (restore_attributes f) x;
wenzelm@54717
   103
      val _ = max_threads := max_threads_orig;
wenzelm@54717
   104
    in Exn.release result end) ();
wenzelm@54717
   105
wenzelm@54717
   106
fun enabled () = max_threads_value () > 1;
wenzelm@54717
   107
wenzelm@54717
   108
wenzelm@32286
   109
(* synchronous wait *)
wenzelm@32286
   110
wenzelm@32295
   111
fun sync_wait opt_atts time cond lock =
wenzelm@32295
   112
  with_attributes
wenzelm@32295
   113
    (sync_interrupts (case opt_atts of SOME atts => atts | NONE => Thread.getAttributes ()))
wenzelm@32295
   114
    (fn _ =>
wenzelm@32295
   115
      (case time of
wenzelm@43761
   116
        SOME t => Exn.Res (ConditionVar.waitUntil (cond, lock, t))
wenzelm@43761
   117
      | NONE => (ConditionVar.wait (cond, lock); Exn.Res true))
wenzelm@32295
   118
      handle exn => Exn.Exn exn);
wenzelm@32295
   119
wenzelm@32295
   120
wenzelm@32295
   121
(* tracing *)
wenzelm@32295
   122
wenzelm@39616
   123
val trace = ref 0;
wenzelm@32295
   124
wenzelm@32295
   125
fun tracing level msg =
wenzelm@32295
   126
  if level > ! trace then ()
wenzelm@32295
   127
  else uninterruptible (fn _ => fn () =>
wenzelm@32295
   128
    (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
wenzelm@32295
   129
      handle _ (*sic*) => ()) ();
wenzelm@32295
   130
wenzelm@32295
   131
fun tracing_time detailed time =
wenzelm@32295
   132
  tracing
wenzelm@32295
   133
   (if not detailed then 5
wenzelm@40301
   134
    else if Time.>= (time, seconds 1.0) then 1
wenzelm@40301
   135
    else if Time.>= (time, seconds 0.1) then 2
wenzelm@40301
   136
    else if Time.>= (time, seconds 0.01) then 3
wenzelm@40301
   137
    else if Time.>= (time, seconds 0.001) then 4 else 5);
wenzelm@32295
   138
wenzelm@32295
   139
fun real_time f x =
wenzelm@32286
   140
  let
wenzelm@32295
   141
    val timer = Timer.startRealTimer ();
wenzelm@32295
   142
    val () = f x;
wenzelm@32295
   143
    val time = Timer.checkRealTimer timer;
wenzelm@32295
   144
  in time end;
wenzelm@32286
   145
wenzelm@32286
   146
wenzelm@23961
   147
(* critical section -- may be nested within the same thread *)
wenzelm@23961
   148
wenzelm@23961
   149
local
wenzelm@23961
   150
wenzelm@24063
   151
val critical_lock = Mutex.mutex ();
wenzelm@39616
   152
val critical_thread = ref (NONE: Thread.thread option);
wenzelm@39616
   153
val critical_name = ref "";
wenzelm@24063
   154
wenzelm@40748
   155
fun show "" = "" | show name = " " ^ name;
wenzelm@40748
   156
fun show' "" = "" | show' name = " [" ^ name ^ "]";
wenzelm@40748
   157
wenzelm@23961
   158
in
wenzelm@23961
   159
wenzelm@23961
   160
fun self_critical () =
wenzelm@23961
   161
  (case ! critical_thread of
wenzelm@23961
   162
    NONE => false
wenzelm@28150
   163
  | SOME t => Thread.equal (t, Thread.self ()));
wenzelm@23961
   164
wenzelm@23991
   165
fun NAMED_CRITICAL name e =
wenzelm@23961
   166
  if self_critical () then e ()
wenzelm@23961
   167
  else
wenzelm@32184
   168
    Exn.release (uninterruptible (fn restore_attributes => fn () =>
wenzelm@24208
   169
      let
wenzelm@24208
   170
        val name' = ! critical_name;
wenzelm@24208
   171
        val _ =
wenzelm@24208
   172
          if Mutex.trylock critical_lock then ()
wenzelm@24208
   173
          else
wenzelm@24208
   174
            let
wenzelm@32184
   175
              val _ = tracing 5 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
wenzelm@32185
   176
              val time = real_time Mutex.lock critical_lock;
wenzelm@32186
   177
              val _ = tracing_time true time (fn () =>
wenzelm@24208
   178
                "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
wenzelm@24208
   179
            in () end;
wenzelm@24208
   180
        val _ = critical_thread := SOME (Thread.self ());
wenzelm@24208
   181
        val _ = critical_name := name;
wenzelm@26083
   182
        val result = Exn.capture (restore_attributes e) ();
wenzelm@24208
   183
        val _ = critical_name := "";
wenzelm@24208
   184
        val _ = critical_thread := NONE;
wenzelm@24208
   185
        val _ = Mutex.unlock critical_lock;
wenzelm@32184
   186
      in result end) ());
wenzelm@23961
   187
wenzelm@23991
   188
fun CRITICAL e = NAMED_CRITICAL "" e;
wenzelm@23981
   189
wenzelm@23961
   190
end;
wenzelm@23961
   191
wenzelm@23973
   192
wenzelm@25704
   193
(* serial numbers *)
wenzelm@25704
   194
wenzelm@25704
   195
local
wenzelm@25704
   196
wenzelm@25704
   197
val serial_lock = Mutex.mutex ();
wenzelm@39616
   198
val serial_count = ref 0;
wenzelm@25704
   199
wenzelm@25704
   200
in
wenzelm@25704
   201
wenzelm@25704
   202
val serial = uninterruptible (fn _ => fn () =>
wenzelm@25704
   203
  let
wenzelm@25704
   204
    val _ = Mutex.lock serial_lock;
wenzelm@28124
   205
    val _ = serial_count := ! serial_count + 1;
wenzelm@28124
   206
    val res = ! serial_count;
wenzelm@25704
   207
    val _ = Mutex.unlock serial_lock;
wenzelm@25704
   208
  in res end);
wenzelm@25704
   209
wenzelm@23961
   210
end;
wenzelm@23961
   211
wenzelm@25704
   212
end;
wenzelm@24688
   213
wenzelm@32286
   214
structure Basic_Multithreading: BASIC_MULTITHREADING = Multithreading;
wenzelm@32286
   215
open Basic_Multithreading;