with_attributes: canonical capture/release scheme (potentially iron out race condition);
1 (* Title: Pure/ML-Systems/multithreading_polyml.ML
4 Multithreading in Poly/ML 5.2 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;
43 fun max_threads_value () =
44 let val m = ! max_threads
45 in if m <= 0 then Int.max (Thread.numProcessors (), 1) else m end;
47 fun enabled () = max_threads_value () > 1;
52 fun show "" = "" | show name = " " ^ name;
53 fun show' "" = "" | show' name = " [" ^ name ^ "]";
56 let val is = TextIO.openIn name
57 in Exn.release (Exn.capture TextIO.inputAll is before TextIO.closeIn is) end;
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;
64 (* thread attributes *)
67 [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer];
69 val regular_interrupts =
70 [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce];
72 val safe_interrupts = map
73 (fn Thread.InterruptState Thread.InterruptAsynch =>
74 Thread.InterruptState Thread.InterruptAsynchOnce
77 fun with_attributes new_atts f x =
79 val orig_atts = safe_interrupts (Thread.getAttributes ());
80 val result = Exn.capture (fn () =>
81 (Thread.setAttributes (safe_interrupts new_atts); f orig_atts x)) ();
82 val _ = Thread.setAttributes orig_atts;
83 in Exn.release result end;
85 fun interruptible f = with_attributes regular_interrupts (fn _ => f);
87 fun uninterruptible f =
88 with_attributes no_interrupts (fn atts => f (fn g => with_attributes atts (fn _ => g)));
91 (* execution with time limit *)
98 fun timeLimit time f x = uninterruptible (fn restore_attributes => fn () =>
100 val worker = Thread.self ();
101 val timeout = ref false;
102 val watchdog = Thread.fork (fn () =>
103 (OS.Process.sleep time; timeout := true; Thread.interrupt worker), []);
105 val result = Exn.capture (restore_attributes f) x;
106 val was_timeout = (case result of Exn.Exn Exn.Interrupt => ! timeout | _ => false);
108 val _ = Thread.interrupt watchdog handle Thread _ => ();
109 in if was_timeout then raise TimeOut else Exn.release result end) ();
114 (* system shell processes, with propagation of interrupts *)
116 fun system_out script = uninterruptible (fn restore_attributes => fn () =>
118 val script_name = OS.FileSys.tmpName ();
119 val _ = write_file script_name script;
121 val pid_name = OS.FileSys.tmpName ();
122 val output_name = OS.FileSys.tmpName ();
125 datatype result = Wait | Signal | Result of int;
126 val result = ref Wait;
127 val result_mutex = Mutex.mutex ();
128 val result_cond = ConditionVar.conditionVar ();
130 (Mutex.lock result_mutex; result := res; Mutex.unlock result_mutex;
131 ConditionVar.signal result_cond);
133 val _ = Mutex.lock result_mutex;
136 val system_thread = Thread.fork (fn () =>
139 OS.Process.system ("perl -w \"$ISABELLE_HOME/lib/scripts/system.pl\" group " ^
140 script_name ^ " " ^ pid_name ^ " " ^ output_name);
142 (case Posix.Process.fromStatus status of
143 Posix.Process.W_EXITED => Result 0
144 | Posix.Process.W_EXITSTATUS 0wx82 => Signal
145 | Posix.Process.W_EXITSTATUS w => Result (Word8.toInt w)
146 | Posix.Process.W_SIGNALED s =>
147 if s = Posix.Signal.int then Signal
148 else Result (256 + LargeWord.toInt (Posix.Signal.toWord s))
149 | Posix.Process.W_STOPPED s => Result (512 + LargeWord.toInt (Posix.Signal.toWord s)));
150 in set_result res end handle _ (*sic*) => set_result (Result 2), []);
152 (*main thread -- proxy for interrupts*)
154 (case Int.fromString (read_file pid_name) of
157 (Posix.Process.K_GROUP (Posix.Process.wordToPid (LargeWord.fromInt pid)),
160 handle OS.SysErr _ => () | IO.Io _ =>
161 (OS.Process.sleep (Time.fromMilliseconds 100); if n > 0 then kill (n - 1) else ());
163 val _ = while ! result = Wait do
164 restore_attributes (fn () =>
165 (ConditionVar.waitUntil (result_cond, result_mutex, Time.now () + Time.fromMilliseconds 100); ())
166 handle Exn.Interrupt => kill 10) ();
169 val output = read_file output_name handle IO.Io _ => "";
170 val _ = OS.FileSys.remove script_name handle OS.SysErr _ => ();
171 val _ = OS.FileSys.remove pid_name handle OS.SysErr _ => ();
172 val _ = OS.FileSys.remove output_name handle OS.SysErr _ => ();
173 val _ = Thread.interrupt system_thread handle Thread _ => ();
174 val rc = (case ! result of Signal => raise Exn.Interrupt | Result rc => rc);
175 in (output, rc) end) ();
178 (* critical section -- may be nested within the same thread *)
182 val critical_lock = Mutex.mutex ();
183 val critical_thread = ref (NONE: Thread.thread option);
184 val critical_name = ref "";
188 fun self_critical () =
189 (case ! critical_thread of
191 | SOME t => Thread.equal (t, Thread.self ()));
193 fun NAMED_CRITICAL name e =
194 if self_critical () then e ()
196 uninterruptible (fn restore_attributes => fn () =>
198 val name' = ! critical_name;
200 if Mutex.trylock critical_lock then ()
203 val timer = Timer.startRealTimer ();
204 val _ = tracing 4 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
205 val _ = Mutex.lock critical_lock;
206 val time = Timer.checkRealTimer timer;
208 if Time.>= (time, Time.fromMilliseconds 1000) then 1
209 else if Time.>= (time, Time.fromMilliseconds 100) then 2
210 else if Time.>= (time, Time.fromMilliseconds 10) then 3 else 4;
211 val _ = tracing trace_time (fn () =>
212 "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
214 val _ = critical_thread := SOME (Thread.self ());
215 val _ = critical_name := name;
216 val result = Exn.capture (restore_attributes e) ();
217 val _ = critical_name := "";
218 val _ = critical_thread := NONE;
219 val _ = Mutex.unlock critical_lock;
220 in Exn.release result end) ();
222 fun CRITICAL e = NAMED_CRITICAL "" e;
231 val serial_lock = Mutex.mutex ();
232 val serial_count = ref 0;
236 val serial = uninterruptible (fn _ => fn () =>
238 val _ = Mutex.lock serial_lock;
239 val _ = serial_count := ! serial_count + 1;
240 val res = ! serial_count;
241 val _ = Mutex.unlock serial_lock;
248 structure BasicMultithreading: BASIC_MULTITHREADING = Multithreading;
249 open BasicMultithreading;