src/Pure/Concurrent/multithreading.ML
changeset 78650 47d0c333d155
parent 68130 6fb85346cb79
child 78715 9506b852ebdf
equal deleted inserted replaced
78649:d46006355819 78650:47d0c333d155
     7 signature MULTITHREADING =
     7 signature MULTITHREADING =
     8 sig
     8 sig
     9   val max_threads: unit -> int
     9   val max_threads: unit -> int
    10   val max_threads_update: int -> unit
    10   val max_threads_update: int -> unit
    11   val parallel_proofs: int ref
    11   val parallel_proofs: int ref
    12   val sync_wait: Time.time option -> ConditionVar.conditionVar -> Mutex.mutex -> bool Exn.result
    12   val sync_wait: Time.time option -> Thread.ConditionVar.conditionVar -> Thread.Mutex.mutex ->
       
    13     bool Exn.result
    13   val trace: int ref
    14   val trace: int ref
    14   val tracing: int -> (unit -> string) -> unit
    15   val tracing: int -> (unit -> string) -> unit
    15   val tracing_time: bool -> Time.time -> (unit -> string) -> unit
    16   val tracing_time: bool -> Time.time -> (unit -> string) -> unit
    16   val synchronized: string -> Mutex.mutex -> (unit -> 'a) -> 'a
    17   val synchronized: string -> Thread.Mutex.mutex -> (unit -> 'a) -> 'a
    17 end;
    18 end;
    18 
    19 
    19 structure Multithreading: MULTITHREADING =
    20 structure Multithreading: MULTITHREADING =
    20 struct
    21 struct
    21 
    22 
    22 (* max_threads *)
    23 (* max_threads *)
    23 
    24 
    24 local
    25 local
    25 
    26 
    26 fun num_processors () =
    27 fun num_processors () =
    27   (case Thread.numPhysicalProcessors () of
    28   (case Thread.Thread.numPhysicalProcessors () of
    28     SOME n => n
    29     SOME n => n
    29   | NONE => Thread.numProcessors ());
    30   | NONE => Thread.Thread.numProcessors ());
    30 
    31 
    31 fun max_threads_result m =
    32 fun max_threads_result m =
    32   if Thread_Data.is_virtual then 1
    33   if Thread_Data.is_virtual then 1
    33   else if m > 0 then m
    34   else if m > 0 then m
    34   else Int.min (Int.max (num_processors (), 1), 8);
    35   else Int.min (Int.max (num_processors (), 1), 8);
    53 fun sync_wait time cond lock =
    54 fun sync_wait time cond lock =
    54   Thread_Attributes.with_attributes
    55   Thread_Attributes.with_attributes
    55     (Thread_Attributes.sync_interrupts (Thread_Attributes.get_attributes ()))
    56     (Thread_Attributes.sync_interrupts (Thread_Attributes.get_attributes ()))
    56     (fn _ =>
    57     (fn _ =>
    57       (case time of
    58       (case time of
    58         SOME t => Exn.Res (ConditionVar.waitUntil (cond, lock, t))
    59         SOME t => Exn.Res (Thread.ConditionVar.waitUntil (cond, lock, t))
    59       | NONE => (ConditionVar.wait (cond, lock); Exn.Res true))
    60       | NONE => (Thread.ConditionVar.wait (cond, lock); Exn.Res true))
    60       handle exn => Exn.Exn exn);
    61       handle exn => Exn.Exn exn);
    61 
    62 
    62 
    63 
    63 (* tracing *)
    64 (* tracing *)
    64 
    65 
    84 fun synchronized name lock e =
    85 fun synchronized name lock e =
    85   Exn.release (Thread_Attributes.uninterruptible (fn restore_attributes => fn () =>
    86   Exn.release (Thread_Attributes.uninterruptible (fn restore_attributes => fn () =>
    86     if ! trace > 0 then
    87     if ! trace > 0 then
    87       let
    88       let
    88         val immediate =
    89         val immediate =
    89           if Mutex.trylock lock then true
    90           if Thread.Mutex.trylock lock then true
    90           else
    91           else
    91             let
    92             let
    92               val _ = tracing 5 (fn () => name ^ ": locking ...");
    93               val _ = tracing 5 (fn () => name ^ ": locking ...");
    93               val timer = Timer.startRealTimer ();
    94               val timer = Timer.startRealTimer ();
    94               val _ = Mutex.lock lock;
    95               val _ = Thread.Mutex.lock lock;
    95               val time = Timer.checkRealTimer timer;
    96               val time = Timer.checkRealTimer timer;
    96               val _ = tracing_time true time (fn () => name ^ ": locked after " ^ Time.toString time);
    97               val _ = tracing_time true time (fn () => name ^ ": locked after " ^ Time.toString time);
    97             in false end;
    98             in false end;
    98         val result = Exn.capture (restore_attributes e) ();
    99         val result = Exn.capture (restore_attributes e) ();
    99         val _ = if immediate then () else tracing 5 (fn () => name ^ ": unlocking ...");
   100         val _ = if immediate then () else tracing 5 (fn () => name ^ ": unlocking ...");
   100         val _ = Mutex.unlock lock;
   101         val _ = Thread.Mutex.unlock lock;
   101       in result end
   102       in result end
   102     else
   103     else
   103       let
   104       let
   104         val _ = Mutex.lock lock;
   105         val _ = Thread.Mutex.lock lock;
   105         val result = Exn.capture (restore_attributes e) ();
   106         val result = Exn.capture (restore_attributes e) ();
   106         val _ = Mutex.unlock lock;
   107         val _ = Thread.Mutex.unlock lock;
   107       in result end) ());
   108       in result end) ());
   108 
   109 
   109 end;
   110 end;