src/Pure/Concurrent/future.ML
changeset 72078 b8d0b8659e0a
parent 72038 254c324f31fd
child 72084 df99d26efeeb
equal deleted inserted replaced
72077:1d6c3cba47fe 72078:b8d0b8659e0a
   283   handle Fail msg => Multithreading.tracing 0 (fn () => "SCHEDULER: " ^ msg);
   283   handle Fail msg => Multithreading.tracing 0 (fn () => "SCHEDULER: " ^ msg);
   284 
   284 
   285 
   285 
   286 (* scheduler *)
   286 (* scheduler *)
   287 
   287 
       
   288 fun scheduler_end () = (*requires SYNCHRONIZED*)
       
   289   (report_status (); scheduler := NONE);
       
   290 
   288 fun scheduler_next () = (*requires SYNCHRONIZED*)
   291 fun scheduler_next () = (*requires SYNCHRONIZED*)
   289   let
   292   let
   290     val now = Time.now ();
   293     val now = Time.now ();
   291     val tick = ! last_round + next_round <= now;
   294     val tick = ! last_round + next_round <= now;
   292     val _ = if tick then last_round := now else ();
   295     val _ = if tick then last_round := now else ();
   352 
   355 
   353 
   356 
   354     (* shutdown *)
   357     (* shutdown *)
   355 
   358 
   356     val continue = not (! do_shutdown andalso null (! workers));
   359     val continue = not (! do_shutdown andalso null (! workers));
   357     val _ = if continue then () else (report_status (); scheduler := NONE);
   360     val _ = if continue then () else scheduler_end ();
   358 
   361 
   359     val _ = broadcast scheduler_event;
   362     val _ = broadcast scheduler_event;
   360   in continue end
   363   in continue end
   361   handle exn =>
   364   handle exn =>
       
   365    (Multithreading.tracing 1 (fn () => "SCHEDULER: " ^ General.exnMessage exn);
   362     if Exn.is_interrupt exn then
   366     if Exn.is_interrupt exn then
   363      (Multithreading.tracing 1 (fn () => "SCHEDULER: Interrupt");
   367      (List.app cancel_later (cancel_all ());
   364       List.app cancel_later (cancel_all ());
       
   365       signal work_available; true)
   368       signal work_available; true)
   366     else Exn.reraise exn;
   369     else
       
   370      (scheduler_end ();
       
   371       Exn.reraise exn));
   367 
   372 
   368 fun scheduler_loop () =
   373 fun scheduler_loop () =
   369  (while
   374  (while
   370     Thread_Attributes.with_attributes
   375     Thread_Attributes.with_attributes
   371       (Thread_Attributes.sync_interrupts Thread_Attributes.public_interrupts)
   376       (Thread_Attributes.sync_interrupts Thread_Attributes.public_interrupts)
   703   else
   708   else
   704     SYNCHRONIZED "shutdown" (fn () =>
   709     SYNCHRONIZED "shutdown" (fn () =>
   705       while scheduler_active () do
   710       while scheduler_active () do
   706        (do_shutdown := true;
   711        (do_shutdown := true;
   707         Multithreading.tracing 1 (fn () => "SHUTDOWN: wait");
   712         Multithreading.tracing 1 (fn () => "SHUTDOWN: wait");
   708         wait scheduler_event));
   713         wait_timeout next_round scheduler_event));
   709 
   714 
   710 
   715 
   711 (*final declarations of this structure!*)
   716 (*final declarations of this structure!*)
   712 val map = map_future;
   717 val map = map_future;
   713 
   718