src/Pure/ML-Systems/multithreading_polyml.ML
changeset 54717 42c209a6c225
parent 50910 54f06ba192ef
child 54723 124432e77ecf
equal deleted inserted replaced
54713:6666fc0b9ebc 54717:42c209a6c225
    22   include MULTITHREADING_POLYML
    22   include MULTITHREADING_POLYML
    23 end;
    23 end;
    24 
    24 
    25 structure Multithreading: MULTITHREADING =
    25 structure Multithreading: MULTITHREADING =
    26 struct
    26 struct
    27 
       
    28 (* options *)
       
    29 
       
    30 val available = true;
       
    31 
       
    32 val max_threads = ref 1;
       
    33 
       
    34 fun max_threads_value () =
       
    35   let val m = ! max_threads in
       
    36     if m > 0 then m
       
    37     else Int.min (Int.max (Thread.numProcessors (), 1), 8)
       
    38   end;
       
    39 
       
    40 fun enabled () = max_threads_value () > 1;
       
    41 
       
    42 
    27 
    43 (* thread attributes *)
    28 (* thread attributes *)
    44 
    29 
    45 val no_interrupts =
    30 val no_interrupts =
    46   [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer];
    31   [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer];
    86 fun interruptible f x = with_attributes public_interrupts (fn _ => f x);
    71 fun interruptible f x = with_attributes public_interrupts (fn _ => f x);
    87 
    72 
    88 fun uninterruptible f x =
    73 fun uninterruptible f x =
    89   with_attributes no_interrupts (fn atts =>
    74   with_attributes no_interrupts (fn atts =>
    90     f (fn g => fn y => with_attributes atts (fn _ => g y)) x);
    75     f (fn g => fn y => with_attributes atts (fn _ => g y)) x);
       
    76 
       
    77 
       
    78 (* options *)
       
    79 
       
    80 val available = true;
       
    81 
       
    82 fun max_threads_result m =
       
    83   if m > 0 then m
       
    84   else
       
    85     let val n =
       
    86       (case Thread.numPhysicalProcessors () of
       
    87         SOME n => n
       
    88       | NONE => Thread.numProcessors ())
       
    89     in Int.min (Int.max (n, 1), 8) end;
       
    90 
       
    91 val max_threads = ref 1;
       
    92 
       
    93 fun max_threads_value () = ! max_threads;
       
    94 
       
    95 fun max_threads_update m = max_threads := max_threads_result m;
       
    96 
       
    97 fun max_threads_setmp m f x =
       
    98   uninterruptible (fn restore_attributes => fn () =>
       
    99     let
       
   100       val max_threads_orig = ! max_threads;
       
   101       val _ = max_threads_update m;
       
   102       val result = Exn.capture (restore_attributes f) x;
       
   103       val _ = max_threads := max_threads_orig;
       
   104     in Exn.release result end) ();
       
   105 
       
   106 fun enabled () = max_threads_value () > 1;
    91 
   107 
    92 
   108 
    93 (* synchronous wait *)
   109 (* synchronous wait *)
    94 
   110 
    95 fun sync_wait opt_atts time cond lock =
   111 fun sync_wait opt_atts time cond lock =