62889
|
1 |
(* Title: Pure/Concurrent/thread_data.ML
|
|
2 |
Author: Makarius
|
|
3 |
|
62890
|
4 |
Thread-local data -- physical version without context management.
|
62889
|
5 |
*)
|
|
6 |
|
|
7 |
signature THREAD_DATA =
|
|
8 |
sig
|
|
9 |
type 'a var
|
|
10 |
val var: unit -> 'a var
|
|
11 |
val get: 'a var -> 'a option
|
|
12 |
val put: 'a var -> 'a option -> unit
|
|
13 |
val setmp: 'a var -> 'a option -> ('b -> 'c) -> 'b -> 'c
|
62890
|
14 |
val is_virtual: bool
|
62889
|
15 |
end;
|
|
16 |
|
|
17 |
structure Thread_Data: THREAD_DATA =
|
|
18 |
struct
|
|
19 |
|
|
20 |
abstype 'a var = Var of 'a option Universal.tag
|
|
21 |
with
|
|
22 |
|
|
23 |
fun var () : 'a var = Var (Universal.tag ());
|
|
24 |
|
|
25 |
fun get (Var tag) =
|
62923
|
26 |
(case Thread.Thread.getLocal tag of
|
62889
|
27 |
SOME data => data
|
|
28 |
| NONE => NONE);
|
|
29 |
|
62923
|
30 |
fun put (Var tag) data = Thread.Thread.setLocal (tag, data);
|
62889
|
31 |
|
|
32 |
fun setmp v data f x =
|
62923
|
33 |
Thread_Attributes.uninterruptible (fn restore_attributes => fn () =>
|
62889
|
34 |
let
|
|
35 |
val orig_data = get v;
|
|
36 |
val _ = put v data;
|
|
37 |
val result = Exn.capture (restore_attributes f) x;
|
|
38 |
val _ = put v orig_data;
|
|
39 |
in Exn.release result end) ();
|
|
40 |
|
|
41 |
end;
|
|
42 |
|
62890
|
43 |
val is_virtual = false;
|
|
44 |
|
62889
|
45 |
end;
|