src/Pure/Concurrent/simple_thread.ML
author wenzelm
Wed, 11 Nov 2009 00:09:15 +0100
changeset 33605 f91ec14e20b6
parent 33602 d25e6bd6ca95
child 37216 3165bc303f66
permissions -rw-r--r--
admit dummy implementation;
     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 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
    13 end;
    14 
    15 structure SimpleThread: SIMPLE_THREAD =
    16 struct
    17 
    18 fun attributes interrupts =
    19   if interrupts then Multithreading.public_interrupts else Multithreading.no_interrupts;
    20 
    21 fun fork interrupts body =
    22   Thread.fork (fn () => exception_trace (fn () => body () handle Exn.Interrupt => ()),
    23     attributes interrupts);
    24 
    25 fun interrupt thread = Thread.interrupt thread handle Thread _ => ();
    26 
    27 
    28 (* basic synchronization *)
    29 
    30 fun synchronized name lock e =
    31   if Multithreading.available then
    32     Exn.release (uninterruptible (fn restore_attributes => fn () =>
    33     let
    34       val immediate =
    35         if Mutex.trylock lock then true
    36         else
    37           let
    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);
    42           in false end;
    43       val result = Exn.capture (restore_attributes e) ();
    44       val _ =
    45         if immediate then () else Multithreading.tracing 5 (fn () => name ^ ": unlocking ...");
    46       val _ = Mutex.unlock lock;
    47     in result end) ())
    48   else e ();
    49 
    50 end;