author | haftmann |
Tue, 20 Jul 2010 06:35:29 +0200 | |
changeset 37891 | c26f9d06e82c |
parent 37216 | 3165bc303f66 |
child 39232 | 69c6d3e87660 |
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 |
|
33602 | 9 |
val attributes: bool -> Thread.threadAttribute list |
28241 | 10 |
val fork: bool -> (unit -> unit) -> Thread.thread |
28550 | 11 |
val interrupt: Thread.thread -> unit |
28577
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
12 |
val synchronized: string -> Mutex.mutex -> (unit -> 'a) -> 'a |
28241 | 13 |
end; |
14 |
||
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
33605
diff
changeset
|
15 |
structure Simple_Thread: SIMPLE_THREAD = |
28241 | 16 |
struct |
17 |
||
33602 | 18 |
fun attributes interrupts = |
19 |
if interrupts then Multithreading.public_interrupts else Multithreading.no_interrupts; |
|
20 |
||
28241 | 21 |
fun fork interrupts body = |
33220
11a1af478dac
SimpleThread.fork: uniform handling of outermost Interrupt, which is not an error and should not produce exception trace;
wenzelm
parents:
32295
diff
changeset
|
22 |
Thread.fork (fn () => exception_trace (fn () => body () handle Exn.Interrupt => ()), |
33602 | 23 |
attributes interrupts); |
28241 | 24 |
|
28550 | 25 |
fun interrupt thread = Thread.interrupt thread handle Thread _ => (); |
26 |
||
28577
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
27 |
|
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
28 |
(* basic synchronization *) |
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
29 |
|
33605 | 30 |
fun synchronized name lock e = |
31 |
if Multithreading.available then |
|
32 |
Exn.release (uninterruptible (fn restore_attributes => fn () => |
|
33 |
let |
|
34 |
val immediate = |
|
35 |
if Mutex.trylock lock then true |
|
36 |
else |
|
37 |
let |
|
38 |
val _ = Multithreading.tracing 5 (fn () => name ^ ": locking ..."); |
|
39 |
val time = Multithreading.real_time Mutex.lock lock; |
|
40 |
val _ = Multithreading.tracing_time true time |
|
41 |
(fn () => name ^ ": locked after " ^ Time.toString time); |
|
42 |
in false end; |
|
43 |
val result = Exn.capture (restore_attributes e) (); |
|
44 |
val _ = |
|
45 |
if immediate then () else Multithreading.tracing 5 (fn () => name ^ ": unlocking ..."); |
|
46 |
val _ = Mutex.unlock lock; |
|
47 |
in result end) ()) |
|
48 |
else e (); |
|
28577
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
49 |
|
28241 | 50 |
end; |