author | smolkas |
Wed, 28 Nov 2012 12:25:43 +0100 | |
changeset 50278 | 05f8ec128e83 |
parent 49894 | 69bfd86cc711 |
child 52583 | 0a7240d88e09 |
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 |
44112
ef876972fdc1
more explicit Simple_Thread.interrupt_unsynchronized, to emphasize its meaning;
wenzelm
parents:
40232
diff
changeset
|
12 |
val interrupt_unsynchronized: Thread.thread -> unit |
28577
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
13 |
val synchronized: string -> Mutex.mutex -> (unit -> 'a) -> 'a |
28241 | 14 |
end; |
15 |
||
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
33605
diff
changeset
|
16 |
structure Simple_Thread: SIMPLE_THREAD = |
28241 | 17 |
struct |
18 |
||
49894
69bfd86cc711
more robust cancel_now: avoid shooting yourself in the foot;
wenzelm
parents:
44112
diff
changeset
|
19 |
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
|
20 |
|
33602 | 21 |
fun attributes interrupts = |
22 |
if interrupts then Multithreading.public_interrupts else Multithreading.no_interrupts; |
|
23 |
||
28241 | 24 |
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
|
25 |
Thread.fork (fn () => |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
37216
diff
changeset
|
26 |
exception_trace (fn () => |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
37216
diff
changeset
|
27 |
body () handle exn => if Exn.is_interrupt exn then () else reraise exn), |
33602 | 28 |
attributes interrupts); |
28241 | 29 |
|
44112
ef876972fdc1
more explicit Simple_Thread.interrupt_unsynchronized, to emphasize its meaning;
wenzelm
parents:
40232
diff
changeset
|
30 |
fun interrupt_unsynchronized thread = Thread.interrupt thread handle Thread _ => (); |
28550 | 31 |
|
28577
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
32 |
|
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
33 |
(* basic synchronization *) |
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
34 |
|
33605 | 35 |
fun synchronized name lock e = |
36 |
if Multithreading.available then |
|
37 |
Exn.release (uninterruptible (fn restore_attributes => fn () => |
|
40232 | 38 |
let |
39 |
val immediate = |
|
40 |
if Mutex.trylock lock then true |
|
41 |
else |
|
42 |
let |
|
43 |
val _ = Multithreading.tracing 5 (fn () => name ^ ": locking ..."); |
|
44 |
val time = Multithreading.real_time Mutex.lock lock; |
|
45 |
val _ = Multithreading.tracing_time true time |
|
46 |
(fn () => name ^ ": locked after " ^ Time.toString time); |
|
47 |
in false end; |
|
48 |
val result = Exn.capture (restore_attributes e) (); |
|
49 |
val _ = |
|
50 |
if immediate then () else Multithreading.tracing 5 (fn () => name ^ ": unlocking ..."); |
|
51 |
val _ = Mutex.unlock lock; |
|
52 |
in result end) ()) |
|
33605 | 53 |
else e (); |
28577
bd2456e0d944
added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents:
28550
diff
changeset
|
54 |
|
28241 | 55 |
end; |