src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Thu Mar 06 20:17:51 2008 +0100 (2008-03-06)
changeset 26221 e557c20158e2
parent 26098 b59d33f73aed
child 26254 3def1a1fea4e
permissions -rw-r--r--
system_out: threaded version does not work for 5.1;
     1 (*  Title:      Pure/ML-Systems/multithreading_polyml.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Multithreading in Poly/ML 5.1 (cf. polyml/basis/Thread.sml).
     6 *)
     7 
     8 open Thread;
     9 
    10 signature MULTITHREADING_POLYML =
    11 sig
    12   val interruptible: ('a -> 'b) -> 'a -> 'b
    13   val uninterruptible: ((('c -> 'd) -> 'c -> 'd) -> 'a -> 'b) -> 'a -> 'b
    14   val system_out: string -> string * int
    15   structure TimeLimit: TIME_LIMIT
    16 end;
    17 
    18 signature BASIC_MULTITHREADING =
    19 sig
    20   include BASIC_MULTITHREADING
    21   include MULTITHREADING_POLYML
    22 end;
    23 
    24 signature MULTITHREADING =
    25 sig
    26   include MULTITHREADING
    27   include MULTITHREADING_POLYML
    28 end;
    29 
    30 structure Multithreading: MULTITHREADING =
    31 struct
    32 
    33 (* options *)
    34 
    35 val trace = ref 0;
    36 fun tracing level msg =
    37   if level <= ! trace
    38   then (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
    39   else ();
    40 
    41 val available = true;
    42 
    43 val max_threads = ref 1;
    44 
    45 fun max_threads_value () =
    46   let val m = ! max_threads
    47   in if m <= 0 then Thread.numProcessors () else m end;
    48 
    49 
    50 (* misc utils *)
    51 
    52 fun cons x xs = x :: xs;
    53 
    54 fun change r f = r := f (! r);
    55 
    56 fun inc i = (i := ! i + 1; ! i);
    57 fun dec i = (i := ! i - 1; ! i);
    58 
    59 fun show "" = "" | show name = " " ^ name;
    60 fun show' "" = "" | show' name = " [" ^ name ^ "]";
    61 
    62 fun read_file name =
    63   let val is = TextIO.openIn name
    64   in TextIO.inputAll is before TextIO.closeIn is end;
    65 
    66 fun write_file name txt =
    67   let val os = TextIO.openOut name
    68   in TextIO.output (os, txt) before TextIO.closeOut os end;
    69 
    70 
    71 (* thread attributes *)
    72 
    73 fun with_attributes new_atts f x =
    74   let
    75     val orig_atts = Thread.getAttributes ();
    76     fun restore () = Thread.setAttributes orig_atts;
    77   in
    78     Exn.release
    79     (*RACE for fully asynchronous interrupts!*)
    80     (let
    81         val _ = Thread.setAttributes new_atts;
    82         val result = Exn.capture (f orig_atts) x;
    83         val _ = restore ();
    84       in result end
    85       handle Interrupt => (restore (); Exn.Exn Interrupt))
    86   end;
    87 
    88 fun interruptible f =
    89   with_attributes
    90     [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce]
    91     (fn _ => f);
    92 
    93 fun uninterruptible f =
    94   with_attributes
    95     [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer]
    96     (fn atts => f (fn g => with_attributes atts (fn _ => g)));
    97 
    98 
    99 (* execution with time limit *)
   100 
   101 structure TimeLimit =
   102 struct
   103 
   104 exception TimeOut;
   105 
   106 fun timeLimit time f x = uninterruptible (fn restore_attributes => fn () =>
   107   let
   108     val worker = Thread.self ();
   109     val timeout = ref false;
   110     val watchdog = Thread.fork (fn () =>
   111       (OS.Process.sleep time; timeout := true; Thread.interrupt worker), []);
   112 
   113     (*RACE! timeout signal vs. external Interrupt*)
   114     val result = Exn.capture (restore_attributes f) x;
   115     val was_timeout = (case result of Exn.Exn Interrupt => ! timeout | _ => false);
   116 
   117     val _ = Thread.interrupt watchdog handle Thread _ => ();
   118   in if was_timeout then raise TimeOut else Exn.release result end) ();
   119 
   120 end;
   121 
   122 
   123 (* system shell processes, with propagation of interrupts *)
   124 
   125 fun system_out_threaded script = uninterruptible (fn restore_attributes => fn () =>
   126   let
   127     val script_name = OS.FileSys.tmpName ();
   128     val _ = write_file script_name script;
   129 
   130     val pid_name = OS.FileSys.tmpName ();
   131     val output_name = OS.FileSys.tmpName ();
   132 
   133     (*result state*)
   134     datatype result = Wait | Signal | Result of int;
   135     val result = ref Wait;
   136     val result_mutex = Mutex.mutex ();
   137     val result_cond = ConditionVar.conditionVar ();
   138     fun set_result res =
   139       (Mutex.lock result_mutex; result := res; Mutex.unlock result_mutex;
   140         ConditionVar.signal result_cond);
   141 
   142     val _ = Mutex.lock result_mutex;
   143 
   144     (*system thread*)
   145     val system_thread = Thread.fork (fn () =>
   146       let
   147         val status =
   148           OS.Process.system ("perl -w \"$ISABELLE_HOME/lib/scripts/system.pl\" group " ^
   149             script_name ^ " " ^ pid_name ^ " " ^ output_name);
   150         val res =
   151           (case Posix.Process.fromStatus status of
   152             Posix.Process.W_EXITED => Result 0
   153           | Posix.Process.W_EXITSTATUS 0wx82 => Signal
   154           | Posix.Process.W_EXITSTATUS w => Result (Word8.toInt w)
   155           | Posix.Process.W_SIGNALED s =>
   156               if s = Posix.Signal.int then Signal
   157               else Result (256 + LargeWord.toInt (Posix.Signal.toWord s))
   158           | Posix.Process.W_STOPPED s => Result (512 + LargeWord.toInt (Posix.Signal.toWord s)));
   159       in set_result res end handle _ => set_result (Result 2), []);
   160 
   161     (*main thread -- proxy for interrupts*)
   162     fun kill n =
   163       (case Int.fromString (read_file pid_name) of
   164         SOME pid =>
   165           Posix.Process.kill
   166             (Posix.Process.K_GROUP (Posix.Process.wordToPid (LargeWord.fromInt pid)),
   167               Posix.Signal.int)
   168       | NONE => ())
   169       handle OS.SysErr _ => () | IO.Io _ =>
   170         (OS.Process.sleep (Time.fromMilliseconds 100); if n > 0 then kill (n - 1) else ());
   171 
   172     val _ = while ! result = Wait do
   173       restore_attributes (fn () =>
   174         (ConditionVar.waitUntil (result_cond, result_mutex, Time.now () + Time.fromMilliseconds 100); ())
   175           handle Interrupt => kill 10) ();
   176 
   177     (*cleanup*)
   178     val output = read_file output_name handle IO.Io _ => "";
   179     val _ = OS.FileSys.remove script_name handle OS.SysErr _ => ();
   180     val _ = OS.FileSys.remove pid_name handle OS.SysErr _ => ();
   181     val _ = OS.FileSys.remove output_name handle OS.SysErr _ => ();
   182     val _ = Thread.interrupt system_thread handle Thread _ => ();
   183     val rc = (case ! result of Signal => raise Interrupt | Result rc => rc);
   184   in (output, rc) end) ();
   185 
   186 val system_out =
   187   if ml_system = "polyml-5.1" then system_out  (*signals not propagated from root thread!*)
   188   else system_out_threaded;
   189 
   190 
   191 (* critical section -- may be nested within the same thread *)
   192 
   193 local
   194 
   195 val critical_lock = Mutex.mutex ();
   196 val critical_thread = ref (NONE: Thread.thread option);
   197 val critical_name = ref "";
   198 
   199 in
   200 
   201 fun self_critical () =
   202   (case ! critical_thread of
   203     NONE => false
   204   | SOME id => Thread.equal (id, Thread.self ()));
   205 
   206 fun NAMED_CRITICAL name e =
   207   if self_critical () then e ()
   208   else
   209     uninterruptible (fn restore_attributes => fn () =>
   210       let
   211         val name' = ! critical_name;
   212         val _ =
   213           if Mutex.trylock critical_lock then ()
   214           else
   215             let
   216               val timer = Timer.startRealTimer ();
   217               val _ = tracing 4 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
   218               val _ = Mutex.lock critical_lock;
   219               val time = Timer.checkRealTimer timer;
   220               val _ = tracing (if Time.> (time, Time.fromMilliseconds 10) then 3 else 4) (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 (* scheduling -- multiple threads working on a queue of tasks *)
   237 
   238 datatype 'a task =
   239   Task of {body: unit -> unit, cont: 'a -> 'a, fail: 'a -> 'a} | Wait | Terminate;
   240 
   241 fun schedule n next_task = uninterruptible (fn restore_attributes => fn tasks =>
   242   let
   243     (*protected execution*)
   244     val lock = Mutex.mutex ();
   245     val protected_name = ref "";
   246     fun PROTECTED name e =
   247       let
   248         val name' = ! protected_name;
   249         val _ =
   250           if Mutex.trylock lock then ()
   251           else
   252             let
   253               val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": waiting");
   254               val _ = Mutex.lock lock;
   255               val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": passed");
   256             in () end;
   257         val _ = protected_name := name;
   258         val res = Exn.capture e ();
   259         val _ = protected_name := "";
   260         val _ = Mutex.unlock lock;
   261       in Exn.release res end;
   262 
   263     (*wakeup condition*)
   264     val wakeup = ConditionVar.conditionVar ();
   265     fun wakeup_all () = ConditionVar.broadcast wakeup;
   266     fun wait () = ConditionVar.wait (wakeup, lock);
   267     fun wait_timeout () = ConditionVar.waitUntil (wakeup, lock, Time.now () + Time.fromSeconds 1);
   268 
   269     (*queue of tasks*)
   270     val queue = ref tasks;
   271     val active = ref 0;
   272     fun trace_active () = tracing 1 (fn () => "SCHEDULE: " ^ Int.toString (! active) ^ " active");
   273     fun dequeue () =
   274       let
   275         val (next, tasks') = next_task (! queue);
   276         val _ = queue := tasks';
   277       in
   278         (case next of Wait =>
   279           (dec active; trace_active ();
   280             wait ();
   281             inc active; trace_active ();
   282             dequeue ())
   283         | _ => next)
   284       end;
   285 
   286     (*pool of running threads*)
   287     val status = ref ([]: exn list);
   288     val running = ref ([]: Thread.thread list);
   289     fun start f =
   290       (inc active;
   291        change running (cons (Thread.fork (f, [Thread.InterruptState Thread.InterruptDefer]))));
   292     fun stop () =
   293       (dec active;
   294        change running (List.filter (fn t => not (Thread.equal (t, Thread.self ())))));
   295 
   296    (*worker thread*)
   297     fun worker () =
   298       (case PROTECTED "dequeue" dequeue of
   299         Task {body, cont, fail} =>
   300           (case Exn.capture (restore_attributes body) () of
   301             Exn.Result () =>
   302               (PROTECTED "cont" (fn () => (change queue cont; wakeup_all ())); worker ())
   303           | Exn.Exn exn =>
   304               PROTECTED "fail" (fn () =>
   305                 (change status (cons exn); change queue fail; stop (); wakeup_all ())))
   306       | Terminate => PROTECTED "terminate" (fn () => (stop (); wakeup_all ())));
   307 
   308     (*main control: fork and wait*)
   309     fun fork 0 = ()
   310       | fork k = (start worker; fork (k - 1));
   311     val _ = PROTECTED "main" (fn () =>
   312      (fork (Int.max (n, 1));
   313       while not (List.null (! running)) do
   314       (trace_active ();
   315        if not (List.null (! status)) then (List.app Thread.interrupt (! running)) else ();
   316        wait_timeout ())));
   317 
   318   in ! status end);
   319 
   320 
   321 (* serial numbers *)
   322 
   323 local
   324 
   325 val serial_lock = Mutex.mutex ();
   326 val serial_count = ref 0;
   327 
   328 in
   329 
   330 val serial = uninterruptible (fn _ => fn () =>
   331   let
   332     val _ = Mutex.lock serial_lock;
   333     val res = inc serial_count;
   334     val _ = Mutex.unlock serial_lock;
   335   in res end);
   336 
   337 end;
   338 
   339 
   340 (* thread data *)
   341 
   342 val get_data = Thread.getLocal;
   343 val put_data = Thread.setLocal;
   344 
   345 end;
   346 
   347 structure BasicMultithreading: BASIC_MULTITHREADING = Multithreading;
   348 open BasicMultithreading;