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