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