src/Pure/Concurrent/simple_thread.ML
author wenzelm
Sat, 25 Jul 2009 00:13:39 +0200
changeset 32184 cfa0ef0c0c5f
parent 32051 82a9875b8707
child 32185 57ecfab3bcfe
permissions -rw-r--r--
simplified/unified Multithreading.tracing_time;
tuned;
     1 (*  Title:      Pure/Concurrent/simple_thread.ML
     2     Author:     Makarius
     3 
     4 Simplified thread operations.
     5 *)
     6 
     7 signature SIMPLE_THREAD =
     8 sig
     9   val fork: bool -> (unit -> unit) -> Thread.thread
    10   val interrupt: Thread.thread -> unit
    11   val synchronized: string -> Mutex.mutex -> (unit -> 'a) -> 'a
    12 end;
    13 
    14 structure SimpleThread: SIMPLE_THREAD =
    15 struct
    16 
    17 fun fork interrupts body =
    18   Thread.fork (fn () => exception_trace (fn () => body ()),
    19     if interrupts then Multithreading.regular_interrupts else Multithreading.no_interrupts);
    20 
    21 fun interrupt thread = Thread.interrupt thread handle Thread _ => ();
    22 
    23 
    24 (* basic synchronization *)
    25 
    26 fun synchronized name lock e = Exn.release (uninterruptible (fn restore_attributes => fn () =>
    27   let
    28     val immediate =
    29       if Mutex.trylock lock then true
    30       else
    31         let
    32           val timer = Timer.startRealTimer ();
    33           val _ = Multithreading.tracing 5 (fn () => name ^ ": locking ...");
    34           val _ = Mutex.lock lock;
    35           val time = Timer.checkRealTimer timer;
    36           val _ = Multithreading.tracing_time time (fn () =>
    37             name ^ ": locked after " ^ Time.toString time);
    38         in false end;
    39     val result = Exn.capture (restore_attributes e) ();
    40     val _ =
    41       if immediate then () else Multithreading.tracing 5 (fn () => name ^ ": unlocking ...");
    42     val _ = Mutex.unlock lock;
    43   in result end) ());
    44 
    45 end;