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