1 (* Title: Pure/Concurrent/simple_thread.ML
4 Simplified thread operations.
7 signature SIMPLE_THREAD =
9 val attributes: bool -> Thread.threadAttribute list
10 val fork: bool -> (unit -> unit) -> Thread.thread
11 val interrupt: Thread.thread -> unit
12 val synchronized: string -> Mutex.mutex -> (unit -> 'a) -> 'a
15 structure SimpleThread: SIMPLE_THREAD =
18 fun attributes interrupts =
19 if interrupts then Multithreading.public_interrupts else Multithreading.no_interrupts;
21 fun fork interrupts body =
22 Thread.fork (fn () => exception_trace (fn () => body () handle Exn.Interrupt => ()),
23 attributes interrupts);
25 fun interrupt thread = Thread.interrupt thread handle Thread _ => ();
28 (* basic synchronization *)
30 fun synchronized name lock e =
31 if Multithreading.available then
32 Exn.release (uninterruptible (fn restore_attributes => fn () =>
35 if Mutex.trylock lock then true
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);
43 val result = Exn.capture (restore_attributes e) ();
45 if immediate then () else Multithreading.tracing 5 (fn () => name ^ ": unlocking ...");
46 val _ = Mutex.unlock lock;