| author | wenzelm | 
| Tue, 12 Aug 2014 17:28:07 +0200 | |
| changeset 57915 | 448325de6e4f | 
| parent 53709 | 84522727f9d3 | 
| child 59054 | 61b723761dff | 
| 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  | 
| 33602 | 10  | 
val attributes: bool -> Thread.threadAttribute list  | 
| 28241 | 11  | 
val fork: bool -> (unit -> unit) -> Thread.thread  | 
| 52583 | 12  | 
val join: Thread.thread -> unit  | 
| 
44112
 
ef876972fdc1
more explicit Simple_Thread.interrupt_unsynchronized, to emphasize its meaning;
 
wenzelm 
parents: 
40232 
diff
changeset
 | 
13  | 
val interrupt_unsynchronized: Thread.thread -> unit  | 
| 
28577
 
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
 
wenzelm 
parents: 
28550 
diff
changeset
 | 
14  | 
val synchronized: string -> Mutex.mutex -> (unit -> 'a) -> 'a  | 
| 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  | 
|
| 33602 | 22  | 
fun attributes interrupts =  | 
23  | 
if interrupts then Multithreading.public_interrupts else Multithreading.no_interrupts;  | 
|
24  | 
||
| 28241 | 25  | 
fun fork interrupts body =  | 
| 
39232
 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 
wenzelm 
parents: 
37216 
diff
changeset
 | 
26  | 
Thread.fork (fn () =>  | 
| 
53709
 
84522727f9d3
improved printing of exception trace in Poly/ML 5.5.1;
 
wenzelm 
parents: 
53527 
diff
changeset
 | 
27  | 
print_exception_trace General.exnMessage (fn () =>  | 
| 53527 | 28  | 
body () handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn),  | 
| 33602 | 29  | 
attributes interrupts);  | 
| 28241 | 30  | 
|
| 52583 | 31  | 
fun join thread =  | 
32  | 
while Thread.isActive thread  | 
|
33  | 
do OS.Process.sleep (seconds 0.1);  | 
|
34  | 
||
| 
44112
 
ef876972fdc1
more explicit Simple_Thread.interrupt_unsynchronized, to emphasize its meaning;
 
wenzelm 
parents: 
40232 
diff
changeset
 | 
35  | 
fun interrupt_unsynchronized thread = Thread.interrupt thread handle Thread _ => ();  | 
| 28550 | 36  | 
|
| 
28577
 
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
 
wenzelm 
parents: 
28550 
diff
changeset
 | 
37  | 
|
| 
 
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
 
wenzelm 
parents: 
28550 
diff
changeset
 | 
38  | 
(* basic synchronization *)  | 
| 
 
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
 
wenzelm 
parents: 
28550 
diff
changeset
 | 
39  | 
|
| 33605 | 40  | 
fun synchronized name lock e =  | 
41  | 
if Multithreading.available then  | 
|
42  | 
Exn.release (uninterruptible (fn restore_attributes => fn () =>  | 
|
| 40232 | 43  | 
let  | 
44  | 
val immediate =  | 
|
45  | 
if Mutex.trylock lock then true  | 
|
46  | 
else  | 
|
47  | 
let  | 
|
48  | 
val _ = Multithreading.tracing 5 (fn () => name ^ ": locking ...");  | 
|
49  | 
val time = Multithreading.real_time Mutex.lock lock;  | 
|
50  | 
val _ = Multithreading.tracing_time true time  | 
|
51  | 
(fn () => name ^ ": locked after " ^ Time.toString time);  | 
|
52  | 
in false end;  | 
|
53  | 
val result = Exn.capture (restore_attributes e) ();  | 
|
54  | 
val _ =  | 
|
55  | 
if immediate then () else Multithreading.tracing 5 (fn () => name ^ ": unlocking ...");  | 
|
56  | 
val _ = Mutex.unlock lock;  | 
|
57  | 
in result end) ())  | 
|
| 33605 | 58  | 
else e ();  | 
| 
28577
 
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
 
wenzelm 
parents: 
28550 
diff
changeset
 | 
59  | 
|
| 28241 | 60  | 
end;  |