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;
     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
    85     let val n =
    86       (case Thread.numPhysicalProcessors () of
    87         SOME n => n
    88       | NONE => Thread.numProcessors ())
    89     in Int.min (Int.max (n, 1), 8) end;
    90 
    91 val max_threads = ref 1;
    92 
    93 fun max_threads_value () = ! max_threads;
    94 
    95 fun max_threads_update m = max_threads := max_threads_result m;
    96 
    97 fun max_threads_setmp m f x =
    98   uninterruptible (fn restore_attributes => fn () =>
    99     let
   100       val max_threads_orig = ! max_threads;
   101       val _ = max_threads_update m;
   102       val result = Exn.capture (restore_attributes f) x;
   103       val _ = max_threads := max_threads_orig;
   104     in Exn.release result end) ();
   105 
   106 fun enabled () = max_threads_value () > 1;
   107 
   108 
   109 (* synchronous wait *)
   110 
   111 fun sync_wait opt_atts time cond lock =
   112   with_attributes
   113     (sync_interrupts (case opt_atts of SOME atts => atts | NONE => Thread.getAttributes ()))
   114     (fn _ =>
   115       (case time of
   116         SOME t => Exn.Res (ConditionVar.waitUntil (cond, lock, t))
   117       | NONE => (ConditionVar.wait (cond, lock); Exn.Res true))
   118       handle exn => Exn.Exn exn);
   119 
   120 
   121 (* tracing *)
   122 
   123 val trace = ref 0;
   124 
   125 fun tracing level msg =
   126   if level > ! trace then ()
   127   else uninterruptible (fn _ => fn () =>
   128     (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
   129       handle _ (*sic*) => ()) ();
   130 
   131 fun tracing_time detailed time =
   132   tracing
   133    (if not detailed then 5
   134     else if Time.>= (time, seconds 1.0) then 1
   135     else if Time.>= (time, seconds 0.1) then 2
   136     else if Time.>= (time, seconds 0.01) then 3
   137     else if Time.>= (time, seconds 0.001) then 4 else 5);
   138 
   139 fun real_time f x =
   140   let
   141     val timer = Timer.startRealTimer ();
   142     val () = f x;
   143     val time = Timer.checkRealTimer timer;
   144   in time end;
   145 
   146 
   147 (* critical section -- may be nested within the same thread *)
   148 
   149 local
   150 
   151 val critical_lock = Mutex.mutex ();
   152 val critical_thread = ref (NONE: Thread.thread option);
   153 val critical_name = ref "";
   154 
   155 fun show "" = "" | show name = " " ^ name;
   156 fun show' "" = "" | show' name = " [" ^ name ^ "]";
   157 
   158 in
   159 
   160 fun self_critical () =
   161   (case ! critical_thread of
   162     NONE => false
   163   | SOME t => Thread.equal (t, Thread.self ()));
   164 
   165 fun NAMED_CRITICAL name e =
   166   if self_critical () then e ()
   167   else
   168     Exn.release (uninterruptible (fn restore_attributes => fn () =>
   169       let
   170         val name' = ! critical_name;
   171         val _ =
   172           if Mutex.trylock critical_lock then ()
   173           else
   174             let
   175               val _ = tracing 5 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
   176               val time = real_time Mutex.lock critical_lock;
   177               val _ = tracing_time true time (fn () =>
   178                 "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
   179             in () end;
   180         val _ = critical_thread := SOME (Thread.self ());
   181         val _ = critical_name := name;
   182         val result = Exn.capture (restore_attributes e) ();
   183         val _ = critical_name := "";
   184         val _ = critical_thread := NONE;
   185         val _ = Mutex.unlock critical_lock;
   186       in result end) ());
   187 
   188 fun CRITICAL e = NAMED_CRITICAL "" e;
   189 
   190 end;
   191 
   192 
   193 (* serial numbers *)
   194 
   195 local
   196 
   197 val serial_lock = Mutex.mutex ();
   198 val serial_count = ref 0;
   199 
   200 in
   201 
   202 val serial = uninterruptible (fn _ => fn () =>
   203   let
   204     val _ = Mutex.lock serial_lock;
   205     val _ = serial_count := ! serial_count + 1;
   206     val res = ! serial_count;
   207     val _ = Mutex.unlock serial_lock;
   208   in res end);
   209 
   210 end;
   211 
   212 end;
   213 
   214 structure Basic_Multithreading: BASIC_MULTITHREADING = Multithreading;
   215 open Basic_Multithreading;