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