author | wenzelm |
Wed, 06 Sep 2023 14:09:27 +0200 | |
changeset 78648 | 852ec09aef13 |
parent 78647 | 27380538d632 |
child 78650 | 47d0c333d155 |
permissions | -rw-r--r-- |
71692 | 1 |
(* Title: Pure/Concurrent/isabelle_thread.ML |
28241 | 2 |
Author: Makarius |
3 |
||
71692 | 4 |
Isabelle-specific thread management. |
28241 | 5 |
*) |
6 |
||
71692 | 7 |
signature ISABELLE_THREAD = |
28241 | 8 |
sig |
78648 | 9 |
type T |
10 |
val get_thread: T -> Thread.thread |
|
11 |
val get_name: T -> string |
|
12 |
val get_id: T -> int |
|
13 |
val equal: T * T -> bool |
|
14 |
val print: T -> string |
|
15 |
val self: unit -> T |
|
16 |
val is_self: T -> bool |
|
71883 | 17 |
val stack_limit: unit -> int option |
60764 | 18 |
type params = {name: string, stack_limit: int option, interrupts: bool} |
59468
fe6651760643
explicit threads_stack_limit (for recent Poly/ML SVN versions), which leads to soft interrupt instead of exhaustion of virtual memory, which is particularly relevant for the bigger address space of x86_64;
wenzelm
parents:
59055
diff
changeset
|
19 |
val attributes: params -> Thread.threadAttribute list |
78648 | 20 |
val fork: params -> (unit -> unit) -> T |
21 |
val is_active: T -> bool |
|
22 |
val join: T -> unit |
|
23 |
val interrupt_unsynchronized: T -> unit |
|
28241 | 24 |
end; |
25 |
||
71692 | 26 |
structure Isabelle_Thread: ISABELLE_THREAD = |
28241 | 27 |
struct |
28 |
||
78648 | 29 |
(* abstract type *) |
30 |
||
31 |
abstype T = T of {thread: Thread.thread, name: string, id: int} |
|
32 |
with |
|
33 |
val make = T; |
|
34 |
fun dest (T args) = args; |
|
35 |
end; |
|
36 |
||
37 |
val get_thread = #thread o dest; |
|
38 |
val get_name = #name o dest; |
|
39 |
val get_id = #id o dest; |
|
40 |
||
41 |
val equal = Thread.equal o apply2 get_thread; |
|
42 |
||
43 |
fun print t = |
|
44 |
(case get_name t of "" => "ML" | a => "Isabelle." ^ a) ^ |
|
45 |
"-" ^ string_of_int (get_id t); |
|
46 |
||
47 |
||
60764 | 48 |
(* self *) |
49 |
||
78648 | 50 |
val make_id = Counter.make (); |
60764 | 51 |
|
52 |
local |
|
78648 | 53 |
val self_var = Thread_Data.var () : T Thread_Data.var; |
60764 | 54 |
in |
55 |
||
78648 | 56 |
fun set_self t = Thread_Data.put self_var (SOME t); |
60830 | 57 |
|
78648 | 58 |
fun self () = |
59 |
(case Thread_Data.get self_var of |
|
60 |
SOME t => t |
|
61 |
| NONE => |
|
62 |
let val t = make {thread = Thread.self (), name = "", id = make_id ()} |
|
63 |
in set_self t; t end); |
|
64 |
||
65 |
fun is_self t = equal (t, self ()); |
|
60764 | 66 |
|
67 |
end; |
|
68 |
||
69 |
||
70 |
(* fork *) |
|
71 |
||
71883 | 72 |
fun stack_limit () = |
73 |
let |
|
74 |
val threads_stack_limit = |
|
75 |
Real.floor (Options.default_real "threads_stack_limit" * 1024.0 * 1024.0 * 1024.0); |
|
76 |
in if threads_stack_limit <= 0 then NONE else SOME threads_stack_limit end; |
|
77 |
||
60764 | 78 |
type params = {name: string, stack_limit: int option, interrupts: bool}; |
79 |
||
80 |
fun attributes ({stack_limit, interrupts, ...}: params) = |
|
62501 | 81 |
Thread.MaximumMLStack stack_limit :: |
64557
37074e22e8be
more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents:
62923
diff
changeset
|
82 |
Thread_Attributes.convert_attributes |
37074e22e8be
more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents:
62923
diff
changeset
|
83 |
(if interrupts then Thread_Attributes.public_interrupts else Thread_Attributes.no_interrupts); |
59468
fe6651760643
explicit threads_stack_limit (for recent Poly/ML SVN versions), which leads to soft interrupt instead of exhaustion of virtual memory, which is particularly relevant for the bigger address space of x86_64;
wenzelm
parents:
59055
diff
changeset
|
84 |
|
60764 | 85 |
fun fork (params: params) body = |
78648 | 86 |
let |
87 |
val name = #name params; |
|
88 |
val id = make_id (); |
|
89 |
fun main () = (set_self (make {thread = Thread.self (), name = name, id = id}); body ()); |
|
90 |
val thread = Thread.fork (main, attributes params); |
|
91 |
in make {thread = thread, name = name, id = id} end; |
|
28241 | 92 |
|
60764 | 93 |
|
94 |
(* join *) |
|
95 |
||
78648 | 96 |
val is_active = Thread.isActive o get_thread; |
97 |
||
98 |
fun join t = |
|
99 |
while is_active t |
|
52583 | 100 |
do OS.Process.sleep (seconds 0.1); |
101 |
||
60764 | 102 |
|
103 |
(* interrupt *) |
|
104 |
||
78648 | 105 |
fun interrupt_unsynchronized t = |
106 |
Thread.interrupt (get_thread t) handle Thread _ => (); |
|
28550 | 107 |
|
28241 | 108 |
end; |