author | wenzelm |
Sat, 02 Apr 2016 23:29:05 +0200 | |
changeset 62826 | eb94e570c1a4 |
parent 62715 | 8312e5d8d217 |
child 62891 | 7a11ea5c9626 |
permissions | -rw-r--r-- |
62508
d0b68218ea55
discontinued RAW session: bootstrap directly from isabelle_process RAW_ML_SYSTEM;
wenzelm
parents:
62501
diff
changeset
|
1 |
(* Title: Pure/Concurrent/multithreading.ML |
24690
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
3 |
|
62359 | 4 |
Multithreading in Poly/ML (cf. polyml/basis/Thread.sml). |
24690
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
5 |
*) |
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
6 |
|
62359 | 7 |
signature BASIC_MULTITHREADING = |
8 |
sig |
|
9 |
val interruptible: ('a -> 'b) -> 'a -> 'b |
|
10 |
val uninterruptible: ((('c -> 'd) -> 'c -> 'd) -> 'a -> 'b) -> 'a -> 'b |
|
11 |
end; |
|
12 |
||
24690
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
13 |
signature MULTITHREADING = |
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
14 |
sig |
62359 | 15 |
include BASIC_MULTITHREADING |
28149 | 16 |
val no_interrupts: Thread.threadAttribute list |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
17 |
val public_interrupts: Thread.threadAttribute list |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
18 |
val private_interrupts: Thread.threadAttribute list |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
19 |
val sync_interrupts: Thread.threadAttribute list -> Thread.threadAttribute list |
41713
a21084741b37
added Multithreading.interrupted (cf. java.lang.Thread.interrupted);
wenzelm
parents:
39616
diff
changeset
|
20 |
val interrupted: unit -> unit (*exception Interrupt*) |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
21 |
val with_attributes: Thread.threadAttribute list -> (Thread.threadAttribute list -> 'a) -> 'a |
62359 | 22 |
val max_threads_value: unit -> int |
23 |
val max_threads_update: int -> unit |
|
24 |
val enabled: unit -> bool |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
25 |
val sync_wait: Thread.threadAttribute list option -> Time.time option -> |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
26 |
ConditionVar.conditionVar -> Mutex.mutex -> bool Exn.result |
39616
8052101883c3
renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
wenzelm
parents:
32777
diff
changeset
|
27 |
val trace: int ref |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
28 |
val tracing: int -> (unit -> string) -> unit |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
29 |
val tracing_time: bool -> Time.time -> (unit -> string) -> unit |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
30 |
val real_time: ('a -> unit) -> 'a -> Time.time |
59054
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54717
diff
changeset
|
31 |
val synchronized: string -> Mutex.mutex -> (unit -> 'a) -> 'a |
25704 | 32 |
val serial: unit -> int |
24690
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
33 |
end; |
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
34 |
|
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
35 |
structure Multithreading: MULTITHREADING = |
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
36 |
struct |
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
37 |
|
62359 | 38 |
(* thread attributes *) |
39 |
||
40 |
val no_interrupts = |
|
41 |
[Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer]; |
|
42 |
||
43 |
val test_interrupts = |
|
44 |
[Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptSynch]; |
|
45 |
||
46 |
val public_interrupts = |
|
47 |
[Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce]; |
|
48 |
||
49 |
val private_interrupts = |
|
50 |
[Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptAsynchOnce]; |
|
51 |
||
52 |
val sync_interrupts = map |
|
53 |
(fn x as Thread.InterruptState Thread.InterruptDefer => x |
|
54 |
| Thread.InterruptState _ => Thread.InterruptState Thread.InterruptSynch |
|
55 |
| x => x); |
|
24690
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
56 |
|
62359 | 57 |
val safe_interrupts = map |
58 |
(fn Thread.InterruptState Thread.InterruptAsynch => |
|
59 |
Thread.InterruptState Thread.InterruptAsynchOnce |
|
60 |
| x => x); |
|
61 |
||
62 |
fun interrupted () = |
|
63 |
let |
|
64 |
val orig_atts = safe_interrupts (Thread.getAttributes ()); |
|
65 |
val _ = Thread.setAttributes test_interrupts; |
|
66 |
val test = Exn.capture Thread.testInterrupt (); |
|
67 |
val _ = Thread.setAttributes orig_atts; |
|
68 |
in Exn.release test end; |
|
69 |
||
70 |
fun with_attributes new_atts e = |
|
71 |
let |
|
72 |
val orig_atts = safe_interrupts (Thread.getAttributes ()); |
|
73 |
val result = Exn.capture (fn () => |
|
74 |
(Thread.setAttributes (safe_interrupts new_atts); e orig_atts)) (); |
|
75 |
val _ = Thread.setAttributes orig_atts; |
|
76 |
in Exn.release result end; |
|
24690
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
77 |
|
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:
32186
diff
changeset
|
78 |
|
62359 | 79 |
(* portable wrappers *) |
80 |
||
81 |
fun interruptible f x = with_attributes public_interrupts (fn _ => f x); |
|
82 |
||
83 |
fun uninterruptible f x = |
|
84 |
with_attributes no_interrupts (fn atts => |
|
85 |
f (fn g => fn y => with_attributes atts (fn _ => g y)) x); |
|
86 |
||
87 |
||
88 |
(* options *) |
|
89 |
||
62501 | 90 |
fun num_processors () = |
91 |
(case Thread.numPhysicalProcessors () of |
|
92 |
SOME n => n |
|
93 |
| NONE => Thread.numProcessors ()); |
|
94 |
||
62359 | 95 |
fun max_threads_result m = |
62501 | 96 |
if m > 0 then m else Int.min (Int.max (num_processors (), 1), 8); |
62359 | 97 |
|
98 |
val max_threads = ref 1; |
|
99 |
fun max_threads_value () = ! max_threads; |
|
100 |
fun max_threads_update m = max_threads := max_threads_result m; |
|
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:
32186
diff
changeset
|
101 |
|
62359 | 102 |
fun enabled () = max_threads_value () > 1; |
28187
4062882c7df3
proper values of no_interrupts, regular_interrupts;
wenzelm
parents:
28169
diff
changeset
|
103 |
|
62359 | 104 |
|
105 |
(* synchronous wait *) |
|
41713
a21084741b37
added Multithreading.interrupted (cf. java.lang.Thread.interrupted);
wenzelm
parents:
39616
diff
changeset
|
106 |
|
62359 | 107 |
fun sync_wait opt_atts time cond lock = |
108 |
with_attributes |
|
109 |
(sync_interrupts (case opt_atts of SOME atts => atts | NONE => Thread.getAttributes ())) |
|
110 |
(fn _ => |
|
111 |
(case time of |
|
112 |
SOME t => Exn.Res (ConditionVar.waitUntil (cond, lock, t)) |
|
113 |
| NONE => (ConditionVar.wait (cond, lock); Exn.Res true)) |
|
114 |
handle exn => Exn.Exn exn); |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32286
diff
changeset
|
115 |
|
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 |
(* tracing *) |
30612
cb6421b6a18f
future_job: do not inherit attributes, but enforce restricted interrupts -- attempt to prevent interrupt race conditions;
wenzelm
parents:
29564
diff
changeset
|
118 |
|
62359 | 119 |
val trace = ref 0; |
120 |
||
121 |
fun tracing level msg = |
|
122 |
if level > ! trace then () |
|
123 |
else uninterruptible (fn _ => fn () => |
|
124 |
(TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr) |
|
125 |
handle _ (*sic*) => ()) (); |
|
126 |
||
127 |
fun tracing_time detailed time = |
|
128 |
tracing |
|
129 |
(if not detailed then 5 |
|
62826 | 130 |
else if time >= seconds 1.0 then 1 |
131 |
else if time >= seconds 0.1 then 2 |
|
132 |
else if time >= seconds 0.01 then 3 |
|
133 |
else if time >= seconds 0.001 then 4 else 5); |
|
62359 | 134 |
|
135 |
fun real_time f x = |
|
136 |
let |
|
137 |
val timer = Timer.startRealTimer (); |
|
138 |
val () = f x; |
|
139 |
val time = Timer.checkRealTimer timer; |
|
140 |
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:
32186
diff
changeset
|
141 |
|
25735 | 142 |
|
59180
c0fa3b3bdabd
discontinued central critical sections: NAMED_CRITICAL / CRITICAL;
wenzelm
parents:
59054
diff
changeset
|
143 |
(* synchronized evaluation *) |
24690
c661dae1070a
renamed ML-Systems/multithreading_dummy.ML to ML-Systems/multithreading.ML;
wenzelm
parents:
diff
changeset
|
144 |
|
62359 | 145 |
fun synchronized name lock e = |
146 |
Exn.release (uninterruptible (fn restore_attributes => fn () => |
|
147 |
let |
|
148 |
val immediate = |
|
149 |
if Mutex.trylock lock then true |
|
150 |
else |
|
151 |
let |
|
152 |
val _ = tracing 5 (fn () => name ^ ": locking ..."); |
|
153 |
val time = real_time Mutex.lock lock; |
|
154 |
val _ = tracing_time true time (fn () => name ^ ": locked after " ^ Time.toString time); |
|
155 |
in false end; |
|
156 |
val result = Exn.capture (restore_attributes e) (); |
|
157 |
val _ = if immediate then () else tracing 5 (fn () => name ^ ": unlocking ..."); |
|
158 |
val _ = Mutex.unlock lock; |
|
159 |
in result end) ()); |
|
59054
61b723761dff
load simple_thread.ML later, such that it benefits from redefined print_exception_trace;
wenzelm
parents:
54717
diff
changeset
|
160 |
|
25735 | 161 |
|
162 |
(* serial numbers *) |
|
163 |
||
62359 | 164 |
local |
165 |
||
166 |
val serial_lock = Mutex.mutex (); |
|
167 |
val serial_count = ref 0; |
|
168 |
||
169 |
in |
|
170 |
||
171 |
val serial = uninterruptible (fn _ => fn () => |
|
172 |
let |
|
173 |
val _ = Mutex.lock serial_lock; |
|
174 |
val _ = serial_count := ! serial_count + 1; |
|
175 |
val res = ! serial_count; |
|
176 |
val _ = Mutex.unlock serial_lock; |
|
177 |
in res end); |
|
25704 | 178 |
|
28123
53cd972d7db9
provide dummy thread structures, including proper Thread.getLocal/setLocal;
wenzelm
parents:
26082
diff
changeset
|
179 |
end; |
53cd972d7db9
provide dummy thread structures, including proper Thread.getLocal/setLocal;
wenzelm
parents:
26082
diff
changeset
|
180 |
|
62359 | 181 |
end; |
182 |
||
183 |
structure Basic_Multithreading: BASIC_MULTITHREADING = Multithreading; |
|
184 |
open Basic_Multithreading; |