author | bulwahn |
Mon, 02 Jul 2012 12:23:30 +0200 | |
changeset 48179 | 18461f745b4a |
parent 44112 | ef876972fdc1 |
child 49894 | 69bfd86cc711 |
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 |
44112
ef876972fdc1
more explicit Simple_Thread.interrupt_unsynchronized, to emphasize its meaning;
wenzelm
parents:
40232
diff
changeset
|
11 |
val interrupt_unsynchronized: 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 = |
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
37216
diff
changeset
|
22 |
Thread.fork (fn () => |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
37216
diff
changeset
|
23 |
exception_trace (fn () => |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
37216
diff
changeset
|
24 |
body () handle exn => if Exn.is_interrupt exn then () else reraise exn), |
33602 | 25 |
attributes interrupts); |
28241 | 26 |
|
44112
ef876972fdc1
more explicit Simple_Thread.interrupt_unsynchronized, to emphasize its meaning;
wenzelm
parents:
40232
diff
changeset
|
27 |
fun interrupt_unsynchronized thread = Thread.interrupt thread handle Thread _ => (); |
28550 | 28 |
|
28577
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
29 |
|
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
30 |
(* basic synchronization *) |
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
31 |
|
33605 | 32 |
fun synchronized name lock e = |
33 |
if Multithreading.available then |
|
34 |
Exn.release (uninterruptible (fn restore_attributes => fn () => |
|
40232 | 35 |
let |
36 |
val immediate = |
|
37 |
if Mutex.trylock lock then true |
|
38 |
else |
|
39 |
let |
|
40 |
val _ = Multithreading.tracing 5 (fn () => name ^ ": locking ..."); |
|
41 |
val time = Multithreading.real_time Mutex.lock lock; |
|
42 |
val _ = Multithreading.tracing_time true time |
|
43 |
(fn () => name ^ ": locked after " ^ Time.toString time); |
|
44 |
in false end; |
|
45 |
val result = Exn.capture (restore_attributes e) (); |
|
46 |
val _ = |
|
47 |
if immediate then () else Multithreading.tracing 5 (fn () => name ^ ": unlocking ..."); |
|
48 |
val _ = Mutex.unlock lock; |
|
49 |
in result end) ()) |
|
33605 | 50 |
else e (); |
28577
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
51 |
|
28241 | 52 |
end; |