src/Pure/Concurrent/multithreading.ML
changeset 62927 bb2b8e915243
parent 62926 9ff9bcbc2341
child 64276 622f4e4ac388
equal deleted inserted replaced
62926:9ff9bcbc2341 62927:bb2b8e915243
    12   val sync_wait: Thread.threadAttribute list option -> Time.time option ->
    12   val sync_wait: Thread.threadAttribute list option -> Time.time option ->
    13     ConditionVar.conditionVar -> Mutex.mutex -> bool Exn.result
    13     ConditionVar.conditionVar -> Mutex.mutex -> bool Exn.result
    14   val trace: int ref
    14   val trace: int ref
    15   val tracing: int -> (unit -> string) -> unit
    15   val tracing: int -> (unit -> string) -> unit
    16   val tracing_time: bool -> Time.time -> (unit -> string) -> unit
    16   val tracing_time: bool -> Time.time -> (unit -> string) -> unit
    17   val real_time: ('a -> unit) -> 'a -> Time.time
       
    18   val synchronized: string -> Mutex.mutex -> (unit -> 'a) -> 'a
    17   val synchronized: string -> Mutex.mutex -> (unit -> 'a) -> 'a
    19 end;
    18 end;
    20 
    19 
    21 structure Multithreading: MULTITHREADING =
    20 structure Multithreading: MULTITHREADING =
    22 struct
    21 struct
    75     else if time >= seconds 1.0 then 1
    74     else if time >= seconds 1.0 then 1
    76     else if time >= seconds 0.1 then 2
    75     else if time >= seconds 0.1 then 2
    77     else if time >= seconds 0.01 then 3
    76     else if time >= seconds 0.01 then 3
    78     else if time >= seconds 0.001 then 4 else 5);
    77     else if time >= seconds 0.001 then 4 else 5);
    79 
    78 
    80 fun real_time f x =
       
    81   let
       
    82     val timer = Timer.startRealTimer ();
       
    83     val () = f x;
       
    84     val time = Timer.checkRealTimer timer;
       
    85   in time end;
       
    86 
       
    87 
    79 
    88 (* synchronized evaluation *)
    80 (* synchronized evaluation *)
    89 
    81 
    90 fun synchronized name lock e =
    82 fun synchronized name lock e =
    91   Exn.release (Thread_Attributes.uninterruptible (fn restore_attributes => fn () =>
    83   Exn.release (Thread_Attributes.uninterruptible (fn restore_attributes => fn () =>
    93       val immediate =
    85       val immediate =
    94         if Mutex.trylock lock then true
    86         if Mutex.trylock lock then true
    95         else
    87         else
    96           let
    88           let
    97             val _ = tracing 5 (fn () => name ^ ": locking ...");
    89             val _ = tracing 5 (fn () => name ^ ": locking ...");
    98             val time = real_time Mutex.lock lock;
    90             val timer = Timer.startRealTimer ();
       
    91             val _ = Mutex.lock lock;
       
    92             val time = Timer.checkRealTimer timer;
    99             val _ = tracing_time true time (fn () => name ^ ": locked after " ^ Time.toString time);
    93             val _ = tracing_time true time (fn () => name ^ ": locked after " ^ Time.toString time);
   100           in false end;
    94           in false end;
   101       val result = Exn.capture (restore_attributes e) ();
    95       val result = Exn.capture (restore_attributes e) ();
   102       val _ = if immediate then () else tracing 5 (fn () => name ^ ": unlocking ...");
    96       val _ = if immediate then () else tracing 5 (fn () => name ^ ": unlocking ...");
   103       val _ = Mutex.unlock lock;
    97       val _ = Mutex.unlock lock;