src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Sat Aug 01 00:09:45 2009 +0200 (2009-08-01)
changeset 32295 400cc493d466
parent 32286 1fb5db48002d
child 32738 15bb09ca0378
permissions -rw-r--r--
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
renamed Multithreading.restricted_interrupts to Multithreading.private_interrupts;
added Multithreading.sync_interrupts;
Multithreading.sync_wait: more careful treatment of attributes;
Multithreading.tracing: uninterruptible;
Multithreading.system_out: signal within critical region, more careful sync_wait;
eliminated redundant Thread.testInterrupt;
Future.wait_timeout: uniform Multithreading.sync_wait;
future scheduler: interruptible body (sync!), to improve reactivity;
future_job: reject duplicate assignments -- system error;
misc tuning;
     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 available = true;
    33 
    34 val max_threads = ref 0;
    35 
    36 val tested_platform =
    37   let val ml_platform = getenv "ML_PLATFORM"
    38   in String.isSuffix "linux" ml_platform orelse String.isSuffix "darwin" ml_platform end;
    39 
    40 fun max_threads_value () =
    41   let val m = ! max_threads in
    42     if m > 0 then m
    43     else if not tested_platform then 1
    44     else Int.min (Int.max (Thread.numProcessors (), 1), 8)
    45   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 public_interrupts =
    70   [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce];
    71 
    72 val private_interrupts =
    73   [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptAsynchOnce];
    74 
    75 val sync_interrupts = map
    76   (fn x as Thread.InterruptState Thread.InterruptDefer => x
    77     | Thread.InterruptState _ => Thread.InterruptState Thread.InterruptSynch
    78     | x => x);
    79 
    80 val safe_interrupts = map
    81   (fn Thread.InterruptState Thread.InterruptAsynch =>
    82       Thread.InterruptState Thread.InterruptAsynchOnce
    83     | x => x);
    84 
    85 fun with_attributes new_atts e =
    86   let
    87     val orig_atts = safe_interrupts (Thread.getAttributes ());
    88     val result = Exn.capture (fn () =>
    89       (Thread.setAttributes (safe_interrupts new_atts); e orig_atts)) ();
    90     val _ = Thread.setAttributes orig_atts;
    91   in Exn.release result end;
    92 
    93 
    94 (* portable wrappers *)
    95 
    96 fun interruptible f x = with_attributes public_interrupts (fn _ => f x);
    97 
    98 fun uninterruptible f x =
    99   with_attributes no_interrupts (fn atts =>
   100     f (fn g => fn y => with_attributes atts (fn _ => g y)) x);
   101 
   102 
   103 (* synchronous wait *)
   104 
   105 fun sync_wait opt_atts time cond lock =
   106   with_attributes
   107     (sync_interrupts (case opt_atts of SOME atts => atts | NONE => Thread.getAttributes ()))
   108     (fn _ =>
   109       (case time of
   110         SOME t => Exn.Result (ConditionVar.waitUntil (cond, lock, t))
   111       | NONE => (ConditionVar.wait (cond, lock); Exn.Result true))
   112       handle exn => Exn.Exn exn);
   113 
   114 
   115 (* tracing *)
   116 
   117 val trace = ref 0;
   118 
   119 fun tracing level msg =
   120   if level > ! trace then ()
   121   else uninterruptible (fn _ => fn () =>
   122     (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
   123       handle _ (*sic*) => ()) ();
   124 
   125 fun tracing_time detailed time =
   126   tracing
   127    (if not detailed then 5
   128     else if Time.>= (time, Time.fromMilliseconds 1000) then 1
   129     else if Time.>= (time, Time.fromMilliseconds 100) then 2
   130     else if Time.>= (time, Time.fromMilliseconds 10) then 3
   131     else if Time.>= (time, Time.fromMilliseconds 1) then 4 else 5);
   132 
   133 fun real_time f x =
   134   let
   135     val timer = Timer.startRealTimer ();
   136     val () = f x;
   137     val time = Timer.checkRealTimer timer;
   138   in time end;
   139 
   140 
   141 (* execution with time limit *)
   142 
   143 structure TimeLimit =
   144 struct
   145 
   146 exception TimeOut;
   147 
   148 fun timeLimit time f x = uninterruptible (fn restore_attributes => fn () =>
   149   let
   150     val worker = Thread.self ();
   151     val timeout = ref false;
   152     val watchdog = Thread.fork (fn () =>
   153       (OS.Process.sleep time; timeout := true; Thread.interrupt worker), []);
   154 
   155     val result = Exn.capture (restore_attributes f) x;
   156     val was_timeout = (case result of Exn.Exn Exn.Interrupt => ! timeout | _ => false);
   157 
   158     val _ = Thread.interrupt watchdog handle Thread _ => ();
   159   in if was_timeout then raise TimeOut else Exn.release result end) ();
   160 
   161 end;
   162 
   163 
   164 (* system shell processes, with propagation of interrupts *)
   165 
   166 fun system_out script = with_attributes no_interrupts (fn orig_atts =>
   167   let
   168     val script_name = OS.FileSys.tmpName ();
   169     val _ = write_file script_name script;
   170 
   171     val pid_name = OS.FileSys.tmpName ();
   172     val output_name = OS.FileSys.tmpName ();
   173 
   174     (*result state*)
   175     datatype result = Wait | Signal | Result of int;
   176     val result = ref Wait;
   177     val lock = Mutex.mutex ();
   178     val cond = ConditionVar.conditionVar ();
   179     fun set_result res =
   180       (Mutex.lock lock; result := res; ConditionVar.signal cond; Mutex.unlock lock);
   181 
   182     val _ = Mutex.lock lock;
   183 
   184     (*system thread*)
   185     val system_thread = Thread.fork (fn () =>
   186       let
   187         val status =
   188           OS.Process.system ("perl -w \"$ISABELLE_HOME/lib/scripts/system.pl\" group " ^
   189             script_name ^ " " ^ pid_name ^ " " ^ output_name);
   190         val res =
   191           (case Posix.Process.fromStatus status of
   192             Posix.Process.W_EXITED => Result 0
   193           | Posix.Process.W_EXITSTATUS 0wx82 => Signal
   194           | Posix.Process.W_EXITSTATUS w => Result (Word8.toInt w)
   195           | Posix.Process.W_SIGNALED s =>
   196               if s = Posix.Signal.int then Signal
   197               else Result (256 + LargeWord.toInt (Posix.Signal.toWord s))
   198           | Posix.Process.W_STOPPED s => Result (512 + LargeWord.toInt (Posix.Signal.toWord s)));
   199       in set_result res end handle _ (*sic*) => set_result (Result 2), []);
   200 
   201     (*main thread -- proxy for interrupts*)
   202     fun kill n =
   203       (case Int.fromString (read_file pid_name) of
   204         SOME pid =>
   205           Posix.Process.kill
   206             (Posix.Process.K_GROUP (Posix.Process.wordToPid (LargeWord.fromInt pid)),
   207               Posix.Signal.int)
   208       | NONE => ())
   209       handle OS.SysErr _ => () | IO.Io _ =>
   210         (OS.Process.sleep (Time.fromMilliseconds 100); if n > 0 then kill (n - 1) else ());
   211 
   212     val _ =
   213       while ! result = Wait do
   214         let val res =
   215           sync_wait (SOME orig_atts)
   216             (SOME (Time.+ (Time.now (), Time.fromMilliseconds 100))) cond lock
   217         in case res of Exn.Exn Exn.Interrupt => kill 10 | _ => () end;
   218 
   219     (*cleanup*)
   220     val output = read_file output_name handle IO.Io _ => "";
   221     val _ = OS.FileSys.remove script_name handle OS.SysErr _ => ();
   222     val _ = OS.FileSys.remove pid_name handle OS.SysErr _ => ();
   223     val _ = OS.FileSys.remove output_name handle OS.SysErr _ => ();
   224     val _ = Thread.interrupt system_thread handle Thread _ => ();
   225     val rc = (case ! result of Signal => raise Exn.Interrupt | Result rc => rc);
   226   in (output, rc) end);
   227 
   228 
   229 (* critical section -- may be nested within the same thread *)
   230 
   231 local
   232 
   233 val critical_lock = Mutex.mutex ();
   234 val critical_thread = ref (NONE: Thread.thread option);
   235 val critical_name = ref "";
   236 
   237 in
   238 
   239 fun self_critical () =
   240   (case ! critical_thread of
   241     NONE => false
   242   | SOME t => Thread.equal (t, Thread.self ()));
   243 
   244 fun NAMED_CRITICAL name e =
   245   if self_critical () then e ()
   246   else
   247     Exn.release (uninterruptible (fn restore_attributes => fn () =>
   248       let
   249         val name' = ! critical_name;
   250         val _ =
   251           if Mutex.trylock critical_lock then ()
   252           else
   253             let
   254               val _ = tracing 5 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
   255               val time = real_time Mutex.lock critical_lock;
   256               val _ = tracing_time true time (fn () =>
   257                 "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
   258             in () end;
   259         val _ = critical_thread := SOME (Thread.self ());
   260         val _ = critical_name := name;
   261         val result = Exn.capture (restore_attributes e) ();
   262         val _ = critical_name := "";
   263         val _ = critical_thread := NONE;
   264         val _ = Mutex.unlock critical_lock;
   265       in result end) ());
   266 
   267 fun CRITICAL e = NAMED_CRITICAL "" e;
   268 
   269 end;
   270 
   271 
   272 (* serial numbers *)
   273 
   274 local
   275 
   276 val serial_lock = Mutex.mutex ();
   277 val serial_count = ref 0;
   278 
   279 in
   280 
   281 val serial = uninterruptible (fn _ => fn () =>
   282   let
   283     val _ = Mutex.lock serial_lock;
   284     val _ = serial_count := ! serial_count + 1;
   285     val res = ! serial_count;
   286     val _ = Mutex.unlock serial_lock;
   287   in res end);
   288 
   289 end;
   290 
   291 end;
   292 
   293 structure Basic_Multithreading: BASIC_MULTITHREADING = Multithreading;
   294 open Basic_Multithreading;