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