| author | wenzelm |
| Mon, 13 Apr 2020 17:40:44 +0200 | |
| changeset 71749 | 77232ff6b8f6 |
| parent 71694 | 16aa085f9353 |
| child 71883 | 44ba78056790 |
| 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 |
|
49894
69bfd86cc711
more robust cancel_now: avoid shooting yourself in the foot;
wenzelm
parents:
44112
diff
changeset
|
9 |
val is_self: Thread.thread -> bool |
| 71694 | 10 |
val get_name: unit -> string |
| 60764 | 11 |
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
|
12 |
val attributes: params -> Thread.threadAttribute list |
|
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
|
13 |
val fork: params -> (unit -> unit) -> Thread.thread |
| 52583 | 14 |
val join: Thread.thread -> unit |
|
44112
ef876972fdc1
more explicit Simple_Thread.interrupt_unsynchronized, to emphasize its meaning;
wenzelm
parents:
40232
diff
changeset
|
15 |
val interrupt_unsynchronized: Thread.thread -> unit |
| 28241 | 16 |
end; |
17 |
||
| 71692 | 18 |
structure Isabelle_Thread: ISABELLE_THREAD = |
| 28241 | 19 |
struct |
20 |
||
| 60764 | 21 |
(* self *) |
22 |
||
|
49894
69bfd86cc711
more robust cancel_now: avoid shooting yourself in the foot;
wenzelm
parents:
44112
diff
changeset
|
23 |
fun is_self thread = Thread.equal (Thread.self (), thread); |
|
69bfd86cc711
more robust cancel_now: avoid shooting yourself in the foot;
wenzelm
parents:
44112
diff
changeset
|
24 |
|
| 60764 | 25 |
|
| 60829 | 26 |
(* unique name *) |
| 60764 | 27 |
|
28 |
local |
|
| 62889 | 29 |
val name_var = Thread_Data.var () : string Thread_Data.var; |
| 60764 | 30 |
val count = Counter.make (); |
31 |
in |
|
32 |
||
| 71694 | 33 |
fun get_name () = |
34 |
(case Thread_Data.get name_var of |
|
35 |
SOME name => name |
|
36 |
| NONE => raise Fail "Isabelle-specific thread required"); |
|
| 60830 | 37 |
|
38 |
fun set_name base = |
|
| 71693 | 39 |
Thread_Data.put name_var (SOME ("Isabelle." ^ base ^ "-" ^ string_of_int (count ())));
|
| 60764 | 40 |
|
41 |
end; |
|
42 |
||
43 |
||
44 |
(* fork *) |
|
45 |
||
46 |
type params = {name: string, stack_limit: int option, interrupts: bool};
|
|
47 |
||
48 |
fun attributes ({stack_limit, interrupts, ...}: params) =
|
|
| 62501 | 49 |
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
|
50 |
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
|
51 |
(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
|
52 |
|
| 60764 | 53 |
fun fork (params: params) body = |
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
37216
diff
changeset
|
54 |
Thread.fork (fn () => |
| 62505 | 55 |
Exn.trace General.exnMessage tracing (fn () => |
| 60829 | 56 |
(set_name (#name params); body ()) |
| 62505 | 57 |
handle exn => if Exn.is_interrupt exn then () (*sic!*) else Exn.reraise exn), |
|
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
|
58 |
attributes params); |
| 28241 | 59 |
|
| 60764 | 60 |
|
61 |
(* join *) |
|
62 |
||
| 52583 | 63 |
fun join thread = |
64 |
while Thread.isActive thread |
|
65 |
do OS.Process.sleep (seconds 0.1); |
|
66 |
||
| 60764 | 67 |
|
68 |
(* interrupt *) |
|
69 |
||
70 |
fun interrupt_unsynchronized thread = |
|
71 |
Thread.interrupt thread handle Thread _ => (); |
|
| 28550 | 72 |
|
| 28241 | 73 |
end; |