--- a/src/Pure/Concurrent/simple_thread.ML Mon Oct 13 15:48:38 2008 +0200
+++ b/src/Pure/Concurrent/simple_thread.ML Mon Oct 13 15:48:39 2008 +0200
@@ -2,13 +2,14 @@
ID: $Id$
Author: Makarius
-Simplified thread fork interface.
+Simplified thread operations.
*)
signature SIMPLE_THREAD =
sig
val fork: bool -> (unit -> unit) -> Thread.thread
val interrupt: Thread.thread -> unit
+ val synchronized: string -> Mutex.mutex -> (unit -> 'a) -> 'a
end;
structure SimpleThread: SIMPLE_THREAD =
@@ -20,4 +21,19 @@
fun interrupt thread = Thread.interrupt thread handle Thread _ => ();
+
+(* basic synchronization *)
+
+fun synchronized name lock e = Exn.release (uninterruptible (fn restore_attributes => fn () =>
+ let
+ val _ =
+ if Mutex.trylock lock then ()
+ else
+ (Multithreading.tracing 3 (fn () => name ^ ": locking ...");
+ Mutex.lock lock;
+ Multithreading.tracing 3 (fn () => name ^ ": ... locked"));
+ val result = Exn.capture (restore_attributes e) ();
+ val _ = Mutex.unlock lock;
+ in result end) ());
+
end;