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