src/Pure/Concurrent/future.ML
changeset 72112 3546dd4ade74
parent 72086 41e1e2395a67
child 74166 ff3dbb2be924
equal deleted inserted replaced
72111:b9ded33bd58c 72112:3546dd4ade74
    15   val worker_subgroup: unit -> group
    15   val worker_subgroup: unit -> group
    16   type 'a future
    16   type 'a future
    17   val task_of: 'a future -> task
    17   val task_of: 'a future -> task
    18   val peek: 'a future -> 'a Exn.result option
    18   val peek: 'a future -> 'a Exn.result option
    19   val is_finished: 'a future -> bool
    19   val is_finished: 'a future -> bool
    20   val ML_statistics: bool Unsynchronized.ref
       
    21   val interruptible_task: ('a -> 'b) -> 'a -> 'b
    20   val interruptible_task: ('a -> 'b) -> 'a -> 'b
    22   val cancel_group: group -> unit
    21   val cancel_group: group -> unit
    23   val cancel: 'a future -> unit
    22   val cancel: 'a future -> unit
    24   val error_message: Position.T -> (serial * string) * string option -> unit
    23   val error_message: Position.T -> (serial * string) * string option -> unit
    25   val identify_result: Position.T -> 'a Exn.result -> 'a Exn.result
    24   val identify_result: Position.T -> 'a Exn.result -> 'a Exn.result
   166 
   165 
   167 fun count_workers state = (*requires SYNCHRONIZED*)
   166 fun count_workers state = (*requires SYNCHRONIZED*)
   168   fold (fn (_, state_ref) => fn i => if ! state_ref = state then i + 1 else i) (! workers) 0;
   167   fold (fn (_, state_ref) => fn i => if ! state_ref = state then i + 1 else i) (! workers) 0;
   169 
   168 
   170 
   169 
   171 
   170 (* ML statistics *)
   172 (* status *)
   171 
   173 
   172 fun ML_statistics () = (*requires SYNCHRONIZED*)
   174 val ML_statistics = Unsynchronized.ref false;
   173   let
   175 
   174     val {ready, pending, running, passive, urgent} = Task_Queue.status (! queue);
   176 fun report_status () = (*requires SYNCHRONIZED*)
   175     val workers_total = length (! workers);
   177   if ! ML_statistics then
   176     val workers_active = count_workers Working;
   178     let
   177     val workers_waiting = count_workers Waiting;
   179       val {ready, pending, running, passive, urgent} = Task_Queue.status (! queue);
   178   in
   180       val workers_total = length (! workers);
   179     ML_Statistics.set
   181       val workers_active = count_workers Working;
   180      {tasks_ready = ready,
   182       val workers_waiting = count_workers Waiting;
   181       tasks_pending = pending,
   183       val _ =
   182       tasks_running = running,
   184         ML_Statistics.set
   183       tasks_passive = passive,
   185          {tasks_ready = ready,
   184       tasks_urgent = urgent,
   186           tasks_pending = pending,
   185       workers_total = workers_total,
   187           tasks_running = running,
   186       workers_active = workers_active,
   188           tasks_passive = passive,
   187       workers_waiting = workers_waiting}
   189           tasks_urgent = urgent,
   188   end;
   190           workers_total = workers_total,
       
   191           workers_active = workers_active,
       
   192           workers_waiting = workers_waiting};
       
   193       val stats = ML_Statistics.get ();
       
   194     in Output.try_protocol_message (Markup.ML_statistics :: stats) [] end
       
   195   else ();
       
   196 
   189 
   197 
   190 
   198 (* cancellation primitives *)
   191 (* cancellation primitives *)
   199 
   192 
   200 fun cancel_now group = (*requires SYNCHRONIZED*)
   193 fun cancel_now group = (*requires SYNCHRONIZED*)
   284 
   277 
   285 
   278 
   286 (* scheduler *)
   279 (* scheduler *)
   287 
   280 
   288 fun scheduler_end () = (*requires SYNCHRONIZED*)
   281 fun scheduler_end () = (*requires SYNCHRONIZED*)
   289   (report_status (); scheduler := NONE);
   282   (ML_statistics (); scheduler := NONE);
   290 
   283 
   291 fun scheduler_next () = (*requires SYNCHRONIZED*)
   284 fun scheduler_next () = (*requires SYNCHRONIZED*)
   292   let
   285   let
   293     val now = Time.now ();
   286     val now = Time.now ();
   294     val tick = ! last_round + next_round <= now;
   287     val tick = ! last_round + next_round <= now;
   298     (* runtime status *)
   291     (* runtime status *)
   299 
   292 
   300     val _ =
   293     val _ =
   301       if tick then Unsynchronized.change status_ticks (fn i => i + 1) else ();
   294       if tick then Unsynchronized.change status_ticks (fn i => i + 1) else ();
   302     val _ =
   295     val _ =
   303       if tick andalso ! status_ticks mod (if ! Multithreading.trace >= 1 then 2 else 10) = 0
   296       if tick andalso ! status_ticks mod (if ! Multithreading.trace >= 1 then 2 else 5) = 0
   304       then report_status () else ();
   297       then ML_statistics () else ();
   305 
   298 
   306     val _ =
   299     val _ =
   307       if not tick orelse forall (Thread.isActive o #1) (! workers) then ()
   300       if not tick orelse forall (Thread.isActive o #1) (! workers) then ()
   308       else
   301       else
   309         let
   302         let