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