src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Sat Jul 25 00:13:39 2009 +0200 (2009-07-25)
changeset 32184 cfa0ef0c0c5f
parent 32107 47d0da617fcc
child 32185 57ecfab3bcfe
permissions -rw-r--r--
simplified/unified Multithreading.tracing_time;
tuned;
     1 (*  Title:      Pure/ML-Systems/multithreading_polyml.ML
     2     Author:     Makarius
     3 
     4 Multithreading in Poly/ML 5.2.1 or later (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   val system_out: string -> string * int
    12   structure TimeLimit: TIME_LIMIT
    13 end;
    14 
    15 signature BASIC_MULTITHREADING =
    16 sig
    17   include BASIC_MULTITHREADING
    18   include MULTITHREADING_POLYML
    19 end;
    20 
    21 signature MULTITHREADING =
    22 sig
    23   include MULTITHREADING
    24   include MULTITHREADING_POLYML
    25 end;
    26 
    27 structure Multithreading: MULTITHREADING =
    28 struct
    29 
    30 (* options *)
    31 
    32 val trace = ref 0;
    33 
    34 fun tracing level msg =
    35   if level > ! trace then ()
    36   else (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
    37     handle _ (*sic*) => ();
    38 
    39 fun tracing_time time =
    40   tracing
    41    (if Time.>= (time, Time.fromMilliseconds 1000) then 1
    42     else if Time.>= (time, Time.fromMilliseconds 100) then 2
    43     else if Time.>= (time, Time.fromMilliseconds 10) then 3
    44     else if Time.>= (time, Time.fromMilliseconds 1) then 4 else 5);
    45 
    46 
    47 val available = true;
    48 
    49 val max_threads = ref 0;
    50 
    51 val tested_platform =
    52   let val ml_platform = getenv "ML_PLATFORM"
    53   in String.isSuffix "linux" ml_platform orelse String.isSuffix "darwin" ml_platform end;
    54 
    55 fun max_threads_value () =
    56   let val m = ! max_threads in
    57     if m > 0 then m
    58     else if not tested_platform then 1
    59     else Int.min (Int.max (Thread.numProcessors (), 1), 8)
    60   end;
    61 
    62 fun enabled () = max_threads_value () > 1;
    63 
    64 
    65 (* misc utils *)
    66 
    67 fun show "" = "" | show name = " " ^ name;
    68 fun show' "" = "" | show' name = " [" ^ name ^ "]";
    69 
    70 fun read_file name =
    71   let val is = TextIO.openIn name
    72   in Exn.release (Exn.capture TextIO.inputAll is before TextIO.closeIn is) end;
    73 
    74 fun write_file name txt =
    75   let val os = TextIO.openOut name
    76   in Exn.release (Exn.capture TextIO.output (os, txt) before TextIO.closeOut os) end;
    77 
    78 
    79 (* thread attributes *)
    80 
    81 val no_interrupts =
    82   [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer];
    83 
    84 val regular_interrupts =
    85   [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce];
    86 
    87 val restricted_interrupts =
    88   [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptAsynchOnce];
    89 
    90 val safe_interrupts = map
    91   (fn Thread.InterruptState Thread.InterruptAsynch =>
    92       Thread.InterruptState Thread.InterruptAsynchOnce
    93     | x => x);
    94 
    95 fun with_attributes new_atts f x =
    96   let
    97     val orig_atts = safe_interrupts (Thread.getAttributes ());
    98     val result = Exn.capture (fn () =>
    99       (Thread.setAttributes (safe_interrupts new_atts); f orig_atts x)) ();
   100     val _ = Thread.setAttributes orig_atts;
   101   in Exn.release result end;
   102 
   103 fun interruptible f =
   104   with_attributes regular_interrupts (fn _ => fn x => f x);
   105 
   106 fun uninterruptible f =
   107   with_attributes no_interrupts (fn atts => fn x =>
   108     f (fn g => with_attributes atts (fn _ => fn y => g y)) x);
   109 
   110 
   111 (* execution with time limit *)
   112 
   113 structure TimeLimit =
   114 struct
   115 
   116 exception TimeOut;
   117 
   118 fun timeLimit time f x = uninterruptible (fn restore_attributes => fn () =>
   119   let
   120     val worker = Thread.self ();
   121     val timeout = ref false;
   122     val watchdog = Thread.fork (fn () =>
   123       (OS.Process.sleep time; timeout := true; Thread.interrupt worker), []);
   124 
   125     val result = Exn.capture (restore_attributes f) x;
   126     val was_timeout = (case result of Exn.Exn Exn.Interrupt => ! timeout | _ => false);
   127 
   128     val _ = Thread.interrupt watchdog handle Thread _ => ();
   129   in if was_timeout then raise TimeOut else Exn.release result end) ();
   130 
   131 end;
   132 
   133 
   134 (* system shell processes, with propagation of interrupts *)
   135 
   136 fun system_out script = uninterruptible (fn restore_attributes => fn () =>
   137   let
   138     val script_name = OS.FileSys.tmpName ();
   139     val _ = write_file script_name script;
   140 
   141     val pid_name = OS.FileSys.tmpName ();
   142     val output_name = OS.FileSys.tmpName ();
   143 
   144     (*result state*)
   145     datatype result = Wait | Signal | Result of int;
   146     val result = ref Wait;
   147     val result_mutex = Mutex.mutex ();
   148     val result_cond = ConditionVar.conditionVar ();
   149     fun set_result res =
   150       (Mutex.lock result_mutex; result := res; Mutex.unlock result_mutex;
   151         ConditionVar.signal result_cond);
   152 
   153     val _ = Mutex.lock result_mutex;
   154 
   155     (*system thread*)
   156     val system_thread = Thread.fork (fn () =>
   157       let
   158         val status =
   159           OS.Process.system ("perl -w \"$ISABELLE_HOME/lib/scripts/system.pl\" group " ^
   160             script_name ^ " " ^ pid_name ^ " " ^ output_name);
   161         val res =
   162           (case Posix.Process.fromStatus status of
   163             Posix.Process.W_EXITED => Result 0
   164           | Posix.Process.W_EXITSTATUS 0wx82 => Signal
   165           | Posix.Process.W_EXITSTATUS w => Result (Word8.toInt w)
   166           | Posix.Process.W_SIGNALED s =>
   167               if s = Posix.Signal.int then Signal
   168               else Result (256 + LargeWord.toInt (Posix.Signal.toWord s))
   169           | Posix.Process.W_STOPPED s => Result (512 + LargeWord.toInt (Posix.Signal.toWord s)));
   170       in set_result res end handle _ (*sic*) => set_result (Result 2), []);
   171 
   172     (*main thread -- proxy for interrupts*)
   173     fun kill n =
   174       (case Int.fromString (read_file pid_name) of
   175         SOME pid =>
   176           Posix.Process.kill
   177             (Posix.Process.K_GROUP (Posix.Process.wordToPid (LargeWord.fromInt pid)),
   178               Posix.Signal.int)
   179       | NONE => ())
   180       handle OS.SysErr _ => () | IO.Io _ =>
   181         (OS.Process.sleep (Time.fromMilliseconds 100); if n > 0 then kill (n - 1) else ());
   182 
   183     val _ = while ! result = Wait do
   184       restore_attributes (fn () =>
   185         (ConditionVar.waitUntil (result_cond, result_mutex, Time.now () + Time.fromMilliseconds 100); ())
   186           handle Exn.Interrupt => kill 10) ();
   187 
   188     (*cleanup*)
   189     val output = read_file output_name handle IO.Io _ => "";
   190     val _ = OS.FileSys.remove script_name handle OS.SysErr _ => ();
   191     val _ = OS.FileSys.remove pid_name handle OS.SysErr _ => ();
   192     val _ = OS.FileSys.remove output_name handle OS.SysErr _ => ();
   193     val _ = Thread.interrupt system_thread handle Thread _ => ();
   194     val rc = (case ! result of Signal => raise Exn.Interrupt | Result rc => rc);
   195   in (output, rc) end) ();
   196 
   197 
   198 (* critical section -- may be nested within the same thread *)
   199 
   200 local
   201 
   202 val critical_lock = Mutex.mutex ();
   203 val critical_thread = ref (NONE: Thread.thread option);
   204 val critical_name = ref "";
   205 
   206 in
   207 
   208 fun self_critical () =
   209   (case ! critical_thread of
   210     NONE => false
   211   | SOME t => Thread.equal (t, Thread.self ()));
   212 
   213 fun NAMED_CRITICAL name e =
   214   if self_critical () then e ()
   215   else
   216     Exn.release (uninterruptible (fn restore_attributes => fn () =>
   217       let
   218         val name' = ! critical_name;
   219         val _ =
   220           if Mutex.trylock critical_lock then ()
   221           else
   222             let
   223               val timer = Timer.startRealTimer ();
   224               val _ = tracing 5 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
   225               val _ = Mutex.lock critical_lock;
   226               val time = Timer.checkRealTimer timer;
   227               val _ = tracing_time time (fn () =>
   228                 "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
   229             in () end;
   230         val _ = critical_thread := SOME (Thread.self ());
   231         val _ = critical_name := name;
   232         val result = Exn.capture (restore_attributes e) ();
   233         val _ = critical_name := "";
   234         val _ = critical_thread := NONE;
   235         val _ = Mutex.unlock critical_lock;
   236       in result end) ());
   237 
   238 fun CRITICAL e = NAMED_CRITICAL "" e;
   239 
   240 end;
   241 
   242 
   243 (* serial numbers *)
   244 
   245 local
   246 
   247 val serial_lock = Mutex.mutex ();
   248 val serial_count = ref 0;
   249 
   250 in
   251 
   252 val serial = uninterruptible (fn _ => fn () =>
   253   let
   254     val _ = Mutex.lock serial_lock;
   255     val _ = serial_count := ! serial_count + 1;
   256     val res = ! serial_count;
   257     val _ = Mutex.unlock serial_lock;
   258   in res end);
   259 
   260 end;
   261 
   262 end;
   263 
   264 structure BasicMultithreading: BASIC_MULTITHREADING = Multithreading;
   265 open BasicMultithreading;