author | wenzelm |
Tue, 24 May 2016 16:24:20 +0200 | |
changeset 63140 | 0644c2e5a989 |
parent 62924 | ce47945ce4fb |
child 64557 | 37074e22e8be |
permissions | -rw-r--r-- |
62923 | 1 |
(* Title: Pure/Concurrent/thread_attributes.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Thread attributes for interrupt handling. |
|
5 |
*) |
|
6 |
||
7 |
signature THREAD_ATTRIBUTES = |
|
8 |
sig |
|
9 |
type attributes = Thread.Thread.threadAttribute list |
|
10 |
val no_interrupts: attributes |
|
11 |
val test_interrupts: attributes |
|
12 |
val public_interrupts: attributes |
|
13 |
val private_interrupts: attributes |
|
14 |
val sync_interrupts: attributes -> attributes |
|
15 |
val safe_interrupts: attributes -> attributes |
|
16 |
val with_attributes: attributes -> (attributes -> 'a) -> 'a |
|
17 |
val uninterruptible: ((('c -> 'd) -> 'c -> 'd) -> 'a -> 'b) -> 'a -> 'b |
|
62924
ce47945ce4fb
tuned signature -- closer to Exn.Interrupt.expose in Scala;
wenzelm
parents:
62923
diff
changeset
|
18 |
val expose_interrupt: unit -> unit (*exception Interrupt*) |
62923 | 19 |
end; |
20 |
||
21 |
structure Thread_Attributes: THREAD_ATTRIBUTES = |
|
22 |
struct |
|
23 |
||
24 |
type attributes = Thread.Thread.threadAttribute list; |
|
25 |
||
26 |
val no_interrupts = |
|
27 |
[Thread.Thread.EnableBroadcastInterrupt false, |
|
28 |
Thread.Thread.InterruptState Thread.Thread.InterruptDefer]; |
|
29 |
||
30 |
val test_interrupts = |
|
31 |
[Thread.Thread.EnableBroadcastInterrupt false, |
|
32 |
Thread.Thread.InterruptState Thread.Thread.InterruptSynch]; |
|
33 |
||
34 |
val public_interrupts = |
|
35 |
[Thread.Thread.EnableBroadcastInterrupt true, |
|
36 |
Thread.Thread.InterruptState Thread.Thread.InterruptAsynchOnce]; |
|
37 |
||
38 |
val private_interrupts = |
|
39 |
[Thread.Thread.EnableBroadcastInterrupt false, |
|
40 |
Thread.Thread.InterruptState Thread.Thread.InterruptAsynchOnce]; |
|
41 |
||
42 |
val sync_interrupts = map |
|
43 |
(fn x as Thread.Thread.InterruptState Thread.Thread.InterruptDefer => x |
|
44 |
| Thread.Thread.InterruptState _ => Thread.Thread.InterruptState Thread.Thread.InterruptSynch |
|
45 |
| x => x); |
|
46 |
||
47 |
val safe_interrupts = map |
|
48 |
(fn Thread.Thread.InterruptState Thread.Thread.InterruptAsynch => |
|
49 |
Thread.Thread.InterruptState Thread.Thread.InterruptAsynchOnce |
|
50 |
| x => x); |
|
51 |
||
52 |
fun with_attributes new_atts e = |
|
53 |
let |
|
54 |
val orig_atts = safe_interrupts (Thread.Thread.getAttributes ()); |
|
55 |
val result = Exn.capture (fn () => |
|
56 |
(Thread.Thread.setAttributes (safe_interrupts new_atts); e orig_atts)) (); |
|
57 |
val _ = Thread.Thread.setAttributes orig_atts; |
|
58 |
in Exn.release result end; |
|
59 |
||
60 |
fun uninterruptible f x = |
|
61 |
with_attributes no_interrupts (fn atts => |
|
62 |
f (fn g => fn y => with_attributes atts (fn _ => g y)) x); |
|
63 |
||
62924
ce47945ce4fb
tuned signature -- closer to Exn.Interrupt.expose in Scala;
wenzelm
parents:
62923
diff
changeset
|
64 |
fun expose_interrupt () = |
ce47945ce4fb
tuned signature -- closer to Exn.Interrupt.expose in Scala;
wenzelm
parents:
62923
diff
changeset
|
65 |
let |
ce47945ce4fb
tuned signature -- closer to Exn.Interrupt.expose in Scala;
wenzelm
parents:
62923
diff
changeset
|
66 |
val orig_atts = safe_interrupts (Thread.Thread.getAttributes ()); |
ce47945ce4fb
tuned signature -- closer to Exn.Interrupt.expose in Scala;
wenzelm
parents:
62923
diff
changeset
|
67 |
val _ = Thread.Thread.setAttributes test_interrupts; |
ce47945ce4fb
tuned signature -- closer to Exn.Interrupt.expose in Scala;
wenzelm
parents:
62923
diff
changeset
|
68 |
val test = Exn.capture Thread.Thread.testInterrupt (); |
ce47945ce4fb
tuned signature -- closer to Exn.Interrupt.expose in Scala;
wenzelm
parents:
62923
diff
changeset
|
69 |
val _ = Thread.Thread.setAttributes orig_atts; |
ce47945ce4fb
tuned signature -- closer to Exn.Interrupt.expose in Scala;
wenzelm
parents:
62923
diff
changeset
|
70 |
in Exn.release test end; |
ce47945ce4fb
tuned signature -- closer to Exn.Interrupt.expose in Scala;
wenzelm
parents:
62923
diff
changeset
|
71 |
|
62923 | 72 |
end; |