author | haftmann |
Fri, 25 Sep 2009 09:50:31 +0200 | |
changeset 32705 | 04ce6bb14d85 |
parent 32295 | 400cc493d466 |
child 32738 | 15bb09ca0378 |
permissions | -rw-r--r-- |
23961 | 1 |
(* Title: Pure/ML-Systems/multithreading_polyml.ML |
2 |
Author: Makarius |
|
3 |
||
31311 | 4 |
Multithreading in Poly/ML 5.2.1 or later (cf. polyml/basis/Thread.sml). |
23961 | 5 |
*) |
6 |
||
25704 | 7 |
signature MULTITHREADING_POLYML = |
8 |
sig |
|
26083
abb3f8dd66dc
removed managed_process (cf. General/shell_process.ML);
wenzelm
parents:
26074
diff
changeset
|
9 |
val interruptible: ('a -> 'b) -> 'a -> 'b |
abb3f8dd66dc
removed managed_process (cf. General/shell_process.ML);
wenzelm
parents:
26074
diff
changeset
|
10 |
val uninterruptible: ((('c -> 'd) -> 'c -> 'd) -> 'a -> 'b) -> 'a -> 'b |
26098
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
11 |
val system_out: string -> string * int |
25704 | 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 |
||
24208 | 21 |
signature MULTITHREADING = |
22 |
sig |
|
23 |
include MULTITHREADING |
|
25704 | 24 |
include MULTITHREADING_POLYML |
24208 | 25 |
end; |
26 |
||
23961 | 27 |
structure Multithreading: MULTITHREADING = |
28 |
struct |
|
29 |
||
32185 | 30 |
(* options *) |
32184 | 31 |
|
23981 | 32 |
val available = true; |
25775
90525e67ede7
added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
wenzelm
parents:
25735
diff
changeset
|
33 |
|
31897 | 34 |
val max_threads = ref 0; |
23973 | 35 |
|
31630
2f8ed0dca3bd
back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents:
31311
diff
changeset
|
36 |
val tested_platform = |
2f8ed0dca3bd
back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents:
31311
diff
changeset
|
37 |
let val ml_platform = getenv "ML_PLATFORM" |
2f8ed0dca3bd
back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents:
31311
diff
changeset
|
38 |
in String.isSuffix "linux" ml_platform orelse String.isSuffix "darwin" ml_platform end; |
2f8ed0dca3bd
back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents:
31311
diff
changeset
|
39 |
|
25775
90525e67ede7
added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
wenzelm
parents:
25735
diff
changeset
|
40 |
fun max_threads_value () = |
31630
2f8ed0dca3bd
back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents:
31311
diff
changeset
|
41 |
let val m = ! max_threads in |
2f8ed0dca3bd
back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents:
31311
diff
changeset
|
42 |
if m > 0 then m |
2f8ed0dca3bd
back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents:
31311
diff
changeset
|
43 |
else if not tested_platform then 1 |
2f8ed0dca3bd
back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents:
31311
diff
changeset
|
44 |
else Int.min (Int.max (Thread.numProcessors (), 1), 8) |
2f8ed0dca3bd
back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents:
31311
diff
changeset
|
45 |
end; |
25775
90525e67ede7
added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
wenzelm
parents:
25735
diff
changeset
|
46 |
|
28555 | 47 |
fun enabled () = max_threads_value () > 1; |
48 |
||
23973 | 49 |
|
24069 | 50 |
(* misc utils *) |
51 |
||
24208 | 52 |
fun show "" = "" | show name = " " ^ name; |
53 |
fun show' "" = "" | show' name = " [" ^ name ^ "]"; |
|
54 |
||
26098
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
55 |
fun read_file name = |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
56 |
let val is = TextIO.openIn name |
26504 | 57 |
in Exn.release (Exn.capture TextIO.inputAll is before TextIO.closeIn is) end; |
26098
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
58 |
|
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
59 |
fun write_file name txt = |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
60 |
let val os = TextIO.openOut name |
26504 | 61 |
in Exn.release (Exn.capture TextIO.output (os, txt) before TextIO.closeOut os) end; |
26098
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
62 |
|
24208 | 63 |
|
64 |
(* thread attributes *) |
|
65 |
||
28161 | 66 |
val no_interrupts = |
67 |
[Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer]; |
|
68 |
||
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
69 |
val public_interrupts = |
28161 | 70 |
[Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce]; |
71 |
||
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
72 |
val private_interrupts = |
30612
cb6421b6a18f
future_job: do not inherit attributes, but enforce restricted interrupts -- attempt to prevent interrupt race conditions;
wenzelm
parents:
30602
diff
changeset
|
73 |
[Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptAsynchOnce]; |
cb6421b6a18f
future_job: do not inherit attributes, but enforce restricted interrupts -- attempt to prevent interrupt race conditions;
wenzelm
parents:
30602
diff
changeset
|
74 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
75 |
val sync_interrupts = map |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
76 |
(fn x as Thread.InterruptState Thread.InterruptDefer => x |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
77 |
| Thread.InterruptState _ => Thread.InterruptState Thread.InterruptSynch |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
78 |
| x => x); |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
79 |
|
28466
6e35fbfc32b8
with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents:
28465
diff
changeset
|
80 |
val safe_interrupts = map |
6e35fbfc32b8
with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents:
28465
diff
changeset
|
81 |
(fn Thread.InterruptState Thread.InterruptAsynch => |
6e35fbfc32b8
with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents:
28465
diff
changeset
|
82 |
Thread.InterruptState Thread.InterruptAsynchOnce |
6e35fbfc32b8
with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents:
28465
diff
changeset
|
83 |
| x => x); |
6e35fbfc32b8
with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents:
28465
diff
changeset
|
84 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
85 |
fun with_attributes new_atts e = |
24208 | 86 |
let |
29550
67ec51c032cb
with_attributes: make double sure that unsafe attributes are avoided;
wenzelm
parents:
28555
diff
changeset
|
87 |
val orig_atts = safe_interrupts (Thread.getAttributes ()); |
30602
1bd90b76477a
with_attributes: canonical capture/release scheme (potentially iron out race condition);
wenzelm
parents:
29564
diff
changeset
|
88 |
val result = Exn.capture (fn () => |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
89 |
(Thread.setAttributes (safe_interrupts new_atts); e orig_atts)) (); |
30602
1bd90b76477a
with_attributes: canonical capture/release scheme (potentially iron out race condition);
wenzelm
parents:
29564
diff
changeset
|
90 |
val _ = Thread.setAttributes orig_atts; |
28466
6e35fbfc32b8
with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents:
28465
diff
changeset
|
91 |
in Exn.release result end; |
24208 | 92 |
|
32286
1fb5db48002d
added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents:
32230
diff
changeset
|
93 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
94 |
(* portable wrappers *) |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
95 |
|
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
96 |
fun interruptible f x = with_attributes public_interrupts (fn _ => f x); |
32286
1fb5db48002d
added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents:
32230
diff
changeset
|
97 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
98 |
fun uninterruptible f x = |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
99 |
with_attributes no_interrupts (fn atts => |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
100 |
f (fn g => fn y => with_attributes atts (fn _ => g y)) x); |
24668 | 101 |
|
24688
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
102 |
|
32286
1fb5db48002d
added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents:
32230
diff
changeset
|
103 |
(* synchronous wait *) |
1fb5db48002d
added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents:
32230
diff
changeset
|
104 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
105 |
fun sync_wait opt_atts time cond lock = |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
106 |
with_attributes |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
107 |
(sync_interrupts (case opt_atts of SOME atts => atts | NONE => Thread.getAttributes ())) |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
108 |
(fn _ => |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
109 |
(case time of |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
110 |
SOME t => Exn.Result (ConditionVar.waitUntil (cond, lock, t)) |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
111 |
| NONE => (ConditionVar.wait (cond, lock); Exn.Result true)) |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
112 |
handle exn => Exn.Exn exn); |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
113 |
|
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
114 |
|
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
115 |
(* tracing *) |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
116 |
|
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
117 |
val trace = ref 0; |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
118 |
|
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
119 |
fun tracing level msg = |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
120 |
if level > ! trace then () |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
121 |
else uninterruptible (fn _ => fn () => |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
122 |
(TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr) |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
123 |
handle _ (*sic*) => ()) (); |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
124 |
|
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
125 |
fun tracing_time detailed time = |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
126 |
tracing |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
127 |
(if not detailed then 5 |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
128 |
else if Time.>= (time, Time.fromMilliseconds 1000) then 1 |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
129 |
else if Time.>= (time, Time.fromMilliseconds 100) then 2 |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
130 |
else if Time.>= (time, Time.fromMilliseconds 10) then 3 |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
131 |
else if Time.>= (time, Time.fromMilliseconds 1) then 4 else 5); |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
132 |
|
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
133 |
fun real_time f x = |
32286
1fb5db48002d
added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents:
32230
diff
changeset
|
134 |
let |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
135 |
val timer = Timer.startRealTimer (); |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
136 |
val () = f x; |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
137 |
val time = Timer.checkRealTimer timer; |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
138 |
in time end; |
32286
1fb5db48002d
added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents:
32230
diff
changeset
|
139 |
|
1fb5db48002d
added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents:
32230
diff
changeset
|
140 |
|
24688
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
141 |
(* execution with time limit *) |
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
142 |
|
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
143 |
structure TimeLimit = |
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
144 |
struct |
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
145 |
|
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
146 |
exception TimeOut; |
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
147 |
|
26083
abb3f8dd66dc
removed managed_process (cf. General/shell_process.ML);
wenzelm
parents:
26074
diff
changeset
|
148 |
fun timeLimit time f x = uninterruptible (fn restore_attributes => fn () => |
abb3f8dd66dc
removed managed_process (cf. General/shell_process.ML);
wenzelm
parents:
26074
diff
changeset
|
149 |
let |
abb3f8dd66dc
removed managed_process (cf. General/shell_process.ML);
wenzelm
parents:
26074
diff
changeset
|
150 |
val worker = Thread.self (); |
abb3f8dd66dc
removed managed_process (cf. General/shell_process.ML);
wenzelm
parents:
26074
diff
changeset
|
151 |
val timeout = ref false; |
abb3f8dd66dc
removed managed_process (cf. General/shell_process.ML);
wenzelm
parents:
26074
diff
changeset
|
152 |
val watchdog = Thread.fork (fn () => |
abb3f8dd66dc
removed managed_process (cf. General/shell_process.ML);
wenzelm
parents:
26074
diff
changeset
|
153 |
(OS.Process.sleep time; timeout := true; Thread.interrupt worker), []); |
24688
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
154 |
|
26083
abb3f8dd66dc
removed managed_process (cf. General/shell_process.ML);
wenzelm
parents:
26074
diff
changeset
|
155 |
val result = Exn.capture (restore_attributes f) x; |
28443 | 156 |
val was_timeout = (case result of Exn.Exn Exn.Interrupt => ! timeout | _ => false); |
24688
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
157 |
|
26083
abb3f8dd66dc
removed managed_process (cf. General/shell_process.ML);
wenzelm
parents:
26074
diff
changeset
|
158 |
val _ = Thread.interrupt watchdog handle Thread _ => (); |
abb3f8dd66dc
removed managed_process (cf. General/shell_process.ML);
wenzelm
parents:
26074
diff
changeset
|
159 |
in if was_timeout then raise TimeOut else Exn.release result end) (); |
24688
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
160 |
|
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
161 |
end; |
24668 | 162 |
|
24069 | 163 |
|
26098
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
164 |
(* system shell processes, with propagation of interrupts *) |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
165 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
166 |
fun system_out script = with_attributes no_interrupts (fn orig_atts => |
26098
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
167 |
let |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
168 |
val script_name = OS.FileSys.tmpName (); |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
169 |
val _ = write_file script_name script; |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
170 |
|
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
171 |
val pid_name = OS.FileSys.tmpName (); |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
172 |
val output_name = OS.FileSys.tmpName (); |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
173 |
|
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
174 |
(*result state*) |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
175 |
datatype result = Wait | Signal | Result of int; |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
176 |
val result = ref Wait; |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
177 |
val lock = Mutex.mutex (); |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
178 |
val cond = ConditionVar.conditionVar (); |
26098
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
179 |
fun set_result res = |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
180 |
(Mutex.lock lock; result := res; ConditionVar.signal cond; Mutex.unlock lock); |
26098
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
181 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
182 |
val _ = Mutex.lock lock; |
26098
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
183 |
|
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
184 |
(*system thread*) |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
185 |
val system_thread = Thread.fork (fn () => |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
186 |
let |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
187 |
val status = |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
188 |
OS.Process.system ("perl -w \"$ISABELLE_HOME/lib/scripts/system.pl\" group " ^ |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
189 |
script_name ^ " " ^ pid_name ^ " " ^ output_name); |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
190 |
val res = |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
191 |
(case Posix.Process.fromStatus status of |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
192 |
Posix.Process.W_EXITED => Result 0 |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
193 |
| Posix.Process.W_EXITSTATUS 0wx82 => Signal |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
194 |
| Posix.Process.W_EXITSTATUS w => Result (Word8.toInt w) |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
195 |
| Posix.Process.W_SIGNALED s => |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
196 |
if s = Posix.Signal.int then Signal |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
197 |
else Result (256 + LargeWord.toInt (Posix.Signal.toWord s)) |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
198 |
| Posix.Process.W_STOPPED s => Result (512 + LargeWord.toInt (Posix.Signal.toWord s))); |
28398 | 199 |
in set_result res end handle _ (*sic*) => set_result (Result 2), []); |
26098
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
200 |
|
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
201 |
(*main thread -- proxy for interrupts*) |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
202 |
fun kill n = |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
203 |
(case Int.fromString (read_file pid_name) of |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
204 |
SOME pid => |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
205 |
Posix.Process.kill |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
206 |
(Posix.Process.K_GROUP (Posix.Process.wordToPid (LargeWord.fromInt pid)), |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
207 |
Posix.Signal.int) |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
208 |
| NONE => ()) |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
209 |
handle OS.SysErr _ => () | IO.Io _ => |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
210 |
(OS.Process.sleep (Time.fromMilliseconds 100); if n > 0 then kill (n - 1) else ()); |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
211 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
212 |
val _ = |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
213 |
while ! result = Wait do |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
214 |
let val res = |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
215 |
sync_wait (SOME orig_atts) |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
216 |
(SOME (Time.+ (Time.now (), Time.fromMilliseconds 100))) cond lock |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
217 |
in case res of Exn.Exn Exn.Interrupt => kill 10 | _ => () end; |
26098
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
218 |
|
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
219 |
(*cleanup*) |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
220 |
val output = read_file output_name handle IO.Io _ => ""; |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
221 |
val _ = OS.FileSys.remove script_name handle OS.SysErr _ => (); |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
222 |
val _ = OS.FileSys.remove pid_name handle OS.SysErr _ => (); |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
223 |
val _ = OS.FileSys.remove output_name handle OS.SysErr _ => (); |
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
224 |
val _ = Thread.interrupt system_thread handle Thread _ => (); |
28443 | 225 |
val rc = (case ! result of Signal => raise Exn.Interrupt | Result rc => rc); |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
226 |
in (output, rc) end); |
26098
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
227 |
|
b59d33f73aed
added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents:
26083
diff
changeset
|
228 |
|
23961 | 229 |
(* critical section -- may be nested within the same thread *) |
230 |
||
231 |
local |
|
232 |
||
24063 | 233 |
val critical_lock = Mutex.mutex (); |
234 |
val critical_thread = ref (NONE: Thread.thread option); |
|
235 |
val critical_name = ref ""; |
|
236 |
||
23961 | 237 |
in |
238 |
||
239 |
fun self_critical () = |
|
240 |
(case ! critical_thread of |
|
241 |
NONE => false |
|
28150 | 242 |
| SOME t => Thread.equal (t, Thread.self ())); |
23961 | 243 |
|
23991 | 244 |
fun NAMED_CRITICAL name e = |
23961 | 245 |
if self_critical () then e () |
246 |
else |
|
32184 | 247 |
Exn.release (uninterruptible (fn restore_attributes => fn () => |
24208 | 248 |
let |
249 |
val name' = ! critical_name; |
|
250 |
val _ = |
|
251 |
if Mutex.trylock critical_lock then () |
|
252 |
else |
|
253 |
let |
|
32184 | 254 |
val _ = tracing 5 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting"); |
32185 | 255 |
val time = real_time Mutex.lock critical_lock; |
32186 | 256 |
val _ = tracing_time true time (fn () => |
24208 | 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; |
|
26083
abb3f8dd66dc
removed managed_process (cf. General/shell_process.ML);
wenzelm
parents:
26074
diff
changeset
|
261 |
val result = Exn.capture (restore_attributes e) (); |
24208 | 262 |
val _ = critical_name := ""; |
263 |
val _ = critical_thread := NONE; |
|
264 |
val _ = Mutex.unlock critical_lock; |
|
32184 | 265 |
in result end) ()); |
23961 | 266 |
|
23991 | 267 |
fun CRITICAL e = NAMED_CRITICAL "" e; |
23981 | 268 |
|
23961 | 269 |
end; |
270 |
||
23973 | 271 |
|
25704 | 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; |
|
28124
10a1f1f4c6ae
moved Multithreading.task/schedule to Concurrent/schedule.ML;
wenzelm
parents:
26504
diff
changeset
|
284 |
val _ = serial_count := ! serial_count + 1; |
10a1f1f4c6ae
moved Multithreading.task/schedule to Concurrent/schedule.ML;
wenzelm
parents:
26504
diff
changeset
|
285 |
val res = ! serial_count; |
25704 | 286 |
val _ = Mutex.unlock serial_lock; |
287 |
in res end); |
|
288 |
||
23961 | 289 |
end; |
290 |
||
25704 | 291 |
end; |
24688
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
292 |
|
32286
1fb5db48002d
added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents:
32230
diff
changeset
|
293 |
structure Basic_Multithreading: BASIC_MULTITHREADING = Multithreading; |
1fb5db48002d
added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents:
32230
diff
changeset
|
294 |
open Basic_Multithreading; |