author | wenzelm |
Thu, 29 Jan 2015 15:21:16 +0100 | |
changeset 59468 | fe6651760643 |
parent 59055 | 5a7157b8e870 |
child 60764 | b610ba36e02c |
permissions | -rw-r--r-- |
28241 | 1 |
(* Title: Pure/Concurrent/simple_thread.ML |
2 |
Author: Makarius |
|
3 |
||
28577
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
4 |
Simplified thread operations. |
28241 | 5 |
*) |
6 |
||
7 |
signature SIMPLE_THREAD = |
|
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 |
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
|
10 |
type params = {stack_limit: int option, interrupts: bool} |
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
|
11 |
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
|
12 |
val fork: params -> (unit -> unit) -> Thread.thread |
52583 | 13 |
val join: Thread.thread -> unit |
44112
ef876972fdc1
more explicit Simple_Thread.interrupt_unsynchronized, to emphasize its meaning;
wenzelm
parents:
40232
diff
changeset
|
14 |
val interrupt_unsynchronized: Thread.thread -> unit |
28241 | 15 |
end; |
16 |
||
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
33605
diff
changeset
|
17 |
structure Simple_Thread: SIMPLE_THREAD = |
28241 | 18 |
struct |
19 |
||
49894
69bfd86cc711
more robust cancel_now: avoid shooting yourself in the foot;
wenzelm
parents:
44112
diff
changeset
|
20 |
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
|
21 |
|
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
|
22 |
type params = {stack_limit: int option, interrupts: bool}; |
33602 | 23 |
|
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
|
24 |
fun attributes {stack_limit, interrupts} = |
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
|
25 |
maximum_ml_stack stack_limit @ |
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
|
26 |
(if interrupts then Multithreading.public_interrupts else Multithreading.no_interrupts); |
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
|
27 |
|
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
|
28 |
fun fork params body = |
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
37216
diff
changeset
|
29 |
Thread.fork (fn () => |
59055
5a7157b8e870
more informative failure of protocol commands, with exception trace;
wenzelm
parents:
59054
diff
changeset
|
30 |
print_exception_trace General.exnMessage tracing (fn () => |
53527 | 31 |
body () handle exn => if Exn.is_interrupt exn then () (*sic!*) else 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
|
32 |
attributes params); |
28241 | 33 |
|
52583 | 34 |
fun join thread = |
35 |
while Thread.isActive thread |
|
36 |
do OS.Process.sleep (seconds 0.1); |
|
37 |
||
44112
ef876972fdc1
more explicit Simple_Thread.interrupt_unsynchronized, to emphasize its meaning;
wenzelm
parents:
40232
diff
changeset
|
38 |
fun interrupt_unsynchronized thread = Thread.interrupt thread handle Thread _ => (); |
28550 | 39 |
|
28241 | 40 |
end; |