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