back to default -M max, with more robust interpretation of corresponding max_threads value;
1 (* Title: Pure/ML-Systems/multithreading_polyml.ML
4 Multithreading in Poly/ML 5.2.1 or later (cf. polyml/basis/Thread.sml).
7 signature MULTITHREADING_POLYML =
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
15 signature BASIC_MULTITHREADING =
17 include BASIC_MULTITHREADING
18 include MULTITHREADING_POLYML
21 signature MULTITHREADING =
23 include MULTITHREADING
24 include MULTITHREADING_POLYML
27 structure Multithreading: MULTITHREADING =
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*) => ();
41 val max_threads = ref 1;
44 let val ml_platform = getenv "ML_PLATFORM"
45 in String.isSuffix "linux" ml_platform orelse String.isSuffix "darwin" ml_platform end;
47 fun max_threads_value () =
48 let val m = ! max_threads in
50 else if not tested_platform then 1
51 else Int.min (Int.max (Thread.numProcessors (), 1), 8)
54 fun enabled () = max_threads_value () > 1;
59 fun show "" = "" | show name = " " ^ name;
60 fun show' "" = "" | show' name = " [" ^ name ^ "]";
63 let val is = TextIO.openIn name
64 in Exn.release (Exn.capture TextIO.inputAll is before TextIO.closeIn is) end;
66 fun write_file name txt =
67 let val os = TextIO.openOut name
68 in Exn.release (Exn.capture TextIO.output (os, txt) before TextIO.closeOut os) end;
71 (* thread attributes *)
74 [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer];
76 val regular_interrupts =
77 [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce];
79 val restricted_interrupts =
80 [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptAsynchOnce];
82 val safe_interrupts = map
83 (fn Thread.InterruptState Thread.InterruptAsynch =>
84 Thread.InterruptState Thread.InterruptAsynchOnce
87 fun with_attributes new_atts f x =
89 val orig_atts = safe_interrupts (Thread.getAttributes ());
90 val result = Exn.capture (fn () =>
91 (Thread.setAttributes (safe_interrupts new_atts); f orig_atts x)) ();
92 val _ = Thread.setAttributes orig_atts;
93 in Exn.release result end;
95 fun interruptible f = with_attributes regular_interrupts (fn _ => f);
97 fun uninterruptible f =
98 with_attributes no_interrupts (fn atts => f (fn g => with_attributes atts (fn _ => g)));
101 (* execution with time limit *)
103 structure TimeLimit =
108 fun timeLimit time f x = uninterruptible (fn restore_attributes => fn () =>
110 val worker = Thread.self ();
111 val timeout = ref false;
112 val watchdog = Thread.fork (fn () =>
113 (OS.Process.sleep time; timeout := true; Thread.interrupt worker), []);
115 val result = Exn.capture (restore_attributes f) x;
116 val was_timeout = (case result of Exn.Exn Exn.Interrupt => ! timeout | _ => false);
118 val _ = Thread.interrupt watchdog handle Thread _ => ();
119 in if was_timeout then raise TimeOut else Exn.release result end) ();
124 (* system shell processes, with propagation of interrupts *)
126 fun system_out script = uninterruptible (fn restore_attributes => fn () =>
128 val script_name = OS.FileSys.tmpName ();
129 val _ = write_file script_name script;
131 val pid_name = OS.FileSys.tmpName ();
132 val output_name = OS.FileSys.tmpName ();
135 datatype result = Wait | Signal | Result of int;
136 val result = ref Wait;
137 val result_mutex = Mutex.mutex ();
138 val result_cond = ConditionVar.conditionVar ();
140 (Mutex.lock result_mutex; result := res; Mutex.unlock result_mutex;
141 ConditionVar.signal result_cond);
143 val _ = Mutex.lock result_mutex;
146 val system_thread = Thread.fork (fn () =>
149 OS.Process.system ("perl -w \"$ISABELLE_HOME/lib/scripts/system.pl\" group " ^
150 script_name ^ " " ^ pid_name ^ " " ^ output_name);
152 (case Posix.Process.fromStatus status of
153 Posix.Process.W_EXITED => Result 0
154 | Posix.Process.W_EXITSTATUS 0wx82 => Signal
155 | Posix.Process.W_EXITSTATUS w => Result (Word8.toInt w)
156 | Posix.Process.W_SIGNALED s =>
157 if s = Posix.Signal.int then Signal
158 else Result (256 + LargeWord.toInt (Posix.Signal.toWord s))
159 | Posix.Process.W_STOPPED s => Result (512 + LargeWord.toInt (Posix.Signal.toWord s)));
160 in set_result res end handle _ (*sic*) => set_result (Result 2), []);
162 (*main thread -- proxy for interrupts*)
164 (case Int.fromString (read_file pid_name) of
167 (Posix.Process.K_GROUP (Posix.Process.wordToPid (LargeWord.fromInt pid)),
170 handle OS.SysErr _ => () | IO.Io _ =>
171 (OS.Process.sleep (Time.fromMilliseconds 100); if n > 0 then kill (n - 1) else ());
173 val _ = while ! result = Wait do
174 restore_attributes (fn () =>
175 (ConditionVar.waitUntil (result_cond, result_mutex, Time.now () + Time.fromMilliseconds 100); ())
176 handle Exn.Interrupt => kill 10) ();
179 val output = read_file output_name handle IO.Io _ => "";
180 val _ = OS.FileSys.remove script_name handle OS.SysErr _ => ();
181 val _ = OS.FileSys.remove pid_name handle OS.SysErr _ => ();
182 val _ = OS.FileSys.remove output_name handle OS.SysErr _ => ();
183 val _ = Thread.interrupt system_thread handle Thread _ => ();
184 val rc = (case ! result of Signal => raise Exn.Interrupt | Result rc => rc);
185 in (output, rc) end) ();
188 (* critical section -- may be nested within the same thread *)
192 val critical_lock = Mutex.mutex ();
193 val critical_thread = ref (NONE: Thread.thread option);
194 val critical_name = ref "";
198 fun self_critical () =
199 (case ! critical_thread of
201 | SOME t => Thread.equal (t, Thread.self ()));
203 fun NAMED_CRITICAL name e =
204 if self_critical () then e ()
206 uninterruptible (fn restore_attributes => fn () =>
208 val name' = ! critical_name;
210 if Mutex.trylock critical_lock then ()
213 val timer = Timer.startRealTimer ();
214 val _ = tracing 4 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
215 val _ = Mutex.lock critical_lock;
216 val time = Timer.checkRealTimer timer;
218 if Time.>= (time, Time.fromMilliseconds 1000) then 1
219 else if Time.>= (time, Time.fromMilliseconds 100) then 2
220 else if Time.>= (time, Time.fromMilliseconds 10) then 3 else 4;
221 val _ = tracing trace_time (fn () =>
222 "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
224 val _ = critical_thread := SOME (Thread.self ());
225 val _ = critical_name := name;
226 val result = Exn.capture (restore_attributes e) ();
227 val _ = critical_name := "";
228 val _ = critical_thread := NONE;
229 val _ = Mutex.unlock critical_lock;
230 in Exn.release result end) ();
232 fun CRITICAL e = NAMED_CRITICAL "" e;
241 val serial_lock = Mutex.mutex ();
242 val serial_count = ref 0;
246 val serial = uninterruptible (fn _ => fn () =>
248 val _ = Mutex.lock serial_lock;
249 val _ = serial_count := ! serial_count + 1;
250 val res = ! serial_count;
251 val _ = Mutex.unlock serial_lock;
258 structure BasicMultithreading: BASIC_MULTITHREADING = Multithreading;
259 open BasicMultithreading;