author | wenzelm |
Thu, 17 Dec 2015 17:32:01 +0100 | |
changeset 61862 | e2a9e46ac0fb |
parent 59180 | c0fa3b3bdabd |
permissions | -rw-r--r-- |
23961 | 1 |
(* Title: Pure/ML-Systems/multithreading_polyml.ML |
2 |
Author: Makarius |
|
3 |
||
50910 | 4 |
Multithreading in Poly/ML (cf. polyml/basis/Thread.sml). |
23961 | 5 |
*) |
6 |
||
24208 | 7 |
signature MULTITHREADING = |
8 |
sig |
|
9 |
include MULTITHREADING |
|
59180
c0fa3b3bdabd
discontinued central critical sections: NAMED_CRITICAL / CRITICAL;
wenzelm
parents:
59136
diff
changeset
|
10 |
val interruptible: ('a -> 'b) -> 'a -> 'b |
c0fa3b3bdabd
discontinued central critical sections: NAMED_CRITICAL / CRITICAL;
wenzelm
parents:
59136
diff
changeset
|
11 |
val uninterruptible: ((('c -> 'd) -> 'c -> 'd) -> 'a -> 'b) -> 'a -> 'b |
24208 | 12 |
end; |
13 |
||
23961 | 14 |
structure Multithreading: MULTITHREADING = |
15 |
struct |
|
16 |
||
24208 | 17 |
(* thread attributes *) |
18 |
||
28161 | 19 |
val no_interrupts = |
20 |
[Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer]; |
|
21 |
||
41713
a21084741b37
added Multithreading.interrupted (cf. java.lang.Thread.interrupted);
wenzelm
parents:
41710
diff
changeset
|
22 |
val test_interrupts = |
a21084741b37
added Multithreading.interrupted (cf. java.lang.Thread.interrupted);
wenzelm
parents:
41710
diff
changeset
|
23 |
[Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptSynch]; |
a21084741b37
added Multithreading.interrupted (cf. java.lang.Thread.interrupted);
wenzelm
parents:
41710
diff
changeset
|
24 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
25 |
val public_interrupts = |
28161 | 26 |
[Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce]; |
27 |
||
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
28 |
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
|
29 |
[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
|
30 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
31 |
val sync_interrupts = map |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
32 |
(fn x as Thread.InterruptState Thread.InterruptDefer => x |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
33 |
| Thread.InterruptState _ => Thread.InterruptState Thread.InterruptSynch |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
34 |
| x => x); |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
35 |
|
28466
6e35fbfc32b8
with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents:
28465
diff
changeset
|
36 |
val safe_interrupts = map |
6e35fbfc32b8
with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents:
28465
diff
changeset
|
37 |
(fn Thread.InterruptState Thread.InterruptAsynch => |
6e35fbfc32b8
with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents:
28465
diff
changeset
|
38 |
Thread.InterruptState Thread.InterruptAsynchOnce |
6e35fbfc32b8
with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents:
28465
diff
changeset
|
39 |
| x => x); |
6e35fbfc32b8
with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents:
28465
diff
changeset
|
40 |
|
41713
a21084741b37
added Multithreading.interrupted (cf. java.lang.Thread.interrupted);
wenzelm
parents:
41710
diff
changeset
|
41 |
fun interrupted () = |
a21084741b37
added Multithreading.interrupted (cf. java.lang.Thread.interrupted);
wenzelm
parents:
41710
diff
changeset
|
42 |
let |
a21084741b37
added Multithreading.interrupted (cf. java.lang.Thread.interrupted);
wenzelm
parents:
41710
diff
changeset
|
43 |
val orig_atts = safe_interrupts (Thread.getAttributes ()); |
a21084741b37
added Multithreading.interrupted (cf. java.lang.Thread.interrupted);
wenzelm
parents:
41710
diff
changeset
|
44 |
val _ = Thread.setAttributes test_interrupts; |
a21084741b37
added Multithreading.interrupted (cf. java.lang.Thread.interrupted);
wenzelm
parents:
41710
diff
changeset
|
45 |
val test = Exn.capture Thread.testInterrupt (); |
a21084741b37
added Multithreading.interrupted (cf. java.lang.Thread.interrupted);
wenzelm
parents:
41710
diff
changeset
|
46 |
val _ = Thread.setAttributes orig_atts; |
a21084741b37
added Multithreading.interrupted (cf. java.lang.Thread.interrupted);
wenzelm
parents:
41710
diff
changeset
|
47 |
in Exn.release test end; |
a21084741b37
added Multithreading.interrupted (cf. java.lang.Thread.interrupted);
wenzelm
parents:
41710
diff
changeset
|
48 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
49 |
fun with_attributes new_atts e = |
24208 | 50 |
let |
29550
67ec51c032cb
with_attributes: make double sure that unsafe attributes are avoided;
wenzelm
parents:
28555
diff
changeset
|
51 |
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
|
52 |
val result = Exn.capture (fn () => |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
53 |
(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
|
54 |
val _ = Thread.setAttributes orig_atts; |
28466
6e35fbfc32b8
with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents:
28465
diff
changeset
|
55 |
in Exn.release result end; |
24208 | 56 |
|
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
|
57 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
58 |
(* portable wrappers *) |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
59 |
|
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
60 |
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
|
61 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
62 |
fun uninterruptible f x = |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
63 |
with_attributes no_interrupts (fn atts => |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
64 |
f (fn g => fn y => with_attributes atts (fn _ => g y)) x); |
24668 | 65 |
|
24688
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
66 |
|
54717 | 67 |
(* options *) |
68 |
||
69 |
val available = true; |
|
70 |
||
71 |
fun max_threads_result m = |
|
59136
c2b23cb8a677
added Par_List in Scala, in accordance to ML version;
wenzelm
parents:
59054
diff
changeset
|
72 |
if m > 0 then m else Int.min (Int.max (Thread.numProcessors (), 1), 8); |
54717 | 73 |
|
74 |
val max_threads = ref 1; |
|
75 |
||
76 |
fun max_threads_value () = ! max_threads; |
|
77 |
||
78 |
fun max_threads_update m = max_threads := max_threads_result m; |
|
79 |
||
80 |
fun max_threads_setmp m f x = |
|
81 |
uninterruptible (fn restore_attributes => fn () => |
|
82 |
let |
|
83 |
val max_threads_orig = ! max_threads; |
|
84 |
val _ = max_threads_update m; |
|
85 |
val result = Exn.capture (restore_attributes f) x; |
|
86 |
val _ = max_threads := max_threads_orig; |
|
87 |
in Exn.release result end) (); |
|
88 |
||
89 |
fun enabled () = max_threads_value () > 1; |
|
90 |
||
91 |
||
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
|
92 |
(* 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
|
93 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
94 |
fun sync_wait opt_atts time cond lock = |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
95 |
with_attributes |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
96 |
(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
|
97 |
(fn _ => |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
98 |
(case time of |
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
41713
diff
changeset
|
99 |
SOME t => Exn.Res (ConditionVar.waitUntil (cond, lock, t)) |
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
41713
diff
changeset
|
100 |
| NONE => (ConditionVar.wait (cond, lock); Exn.Res true)) |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
101 |
handle exn => Exn.Exn exn); |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
102 |
|
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
103 |
|
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
104 |
(* tracing *) |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
105 |
|
39616
8052101883c3
renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
wenzelm
parents:
39583
diff
changeset
|
106 |
val trace = ref 0; |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
107 |
|
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
108 |
fun tracing level msg = |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
109 |
if level > ! trace then () |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
110 |
else uninterruptible (fn _ => fn () => |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
111 |
(TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr) |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
112 |
handle _ (*sic*) => ()) (); |
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 |
fun tracing_time detailed time = |
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 |
(if not detailed then 5 |
40301 | 117 |
else if Time.>= (time, seconds 1.0) then 1 |
118 |
else if Time.>= (time, seconds 0.1) then 2 |
|
119 |
else if Time.>= (time, seconds 0.01) then 3 |
|
120 |
else if Time.>= (time, seconds 0.001) then 4 else 5); |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
121 |
|
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
122 |
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
|
123 |
let |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
124 |
val timer = Timer.startRealTimer (); |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
125 |
val () = f x; |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
126 |
val time = Timer.checkRealTimer timer; |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
127 |
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
|
128 |
|
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
|
129 |
|
59180
c0fa3b3bdabd
discontinued central critical sections: NAMED_CRITICAL / CRITICAL;
wenzelm
parents:
59136
diff
changeset
|
130 |
(* synchronized evaluation *) |
23961 | 131 |
|
59054
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
132 |
fun synchronized name lock e = |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
133 |
Exn.release (uninterruptible (fn restore_attributes => fn () => |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
134 |
let |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
135 |
val immediate = |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
136 |
if Mutex.trylock lock then true |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
137 |
else |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
138 |
let |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
139 |
val _ = tracing 5 (fn () => name ^ ": locking ..."); |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
140 |
val time = real_time Mutex.lock lock; |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
141 |
val _ = tracing_time true time (fn () => name ^ ": locked after " ^ Time.toString time); |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
142 |
in false end; |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
143 |
val result = Exn.capture (restore_attributes e) (); |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
144 |
val _ = if immediate then () else tracing 5 (fn () => name ^ ": unlocking ..."); |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
145 |
val _ = Mutex.unlock lock; |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
146 |
in result end) ()); |
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54723
diff
changeset
|
147 |
|
23973 | 148 |
|
25704 | 149 |
(* serial numbers *) |
150 |
||
151 |
local |
|
152 |
||
153 |
val serial_lock = Mutex.mutex (); |
|
39616
8052101883c3
renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
wenzelm
parents:
39583
diff
changeset
|
154 |
val serial_count = ref 0; |
25704 | 155 |
|
156 |
in |
|
157 |
||
158 |
val serial = uninterruptible (fn _ => fn () => |
|
159 |
let |
|
160 |
val _ = Mutex.lock serial_lock; |
|
28124
10a1f1f4c6ae
moved Multithreading.task/schedule to Concurrent/schedule.ML;
wenzelm
parents:
26504
diff
changeset
|
161 |
val _ = serial_count := ! serial_count + 1; |
10a1f1f4c6ae
moved Multithreading.task/schedule to Concurrent/schedule.ML;
wenzelm
parents:
26504
diff
changeset
|
162 |
val res = ! serial_count; |
25704 | 163 |
val _ = Mutex.unlock serial_lock; |
164 |
in res end); |
|
165 |
||
23961 | 166 |
end; |
167 |
||
25704 | 168 |
end; |
24688
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24672
diff
changeset
|
169 |
|
59180
c0fa3b3bdabd
discontinued central critical sections: NAMED_CRITICAL / CRITICAL;
wenzelm
parents:
59136
diff
changeset
|
170 |
val interruptible = Multithreading.interruptible; |
c0fa3b3bdabd
discontinued central critical sections: NAMED_CRITICAL / CRITICAL;
wenzelm
parents:
59136
diff
changeset
|
171 |
val uninterruptible = Multithreading.uninterruptible; |
c0fa3b3bdabd
discontinued central critical sections: NAMED_CRITICAL / CRITICAL;
wenzelm
parents:
59136
diff
changeset
|
172 |