src/Pure/PIDE/execution.ML
author wenzelm
Wed Mar 26 12:32:51 2014 +0100 (2014-03-26 ago)
changeset 56292 1a91a0da65ab
parent 56291 e79f76a48449
child 56296 5413b6379c0e
permissions -rw-r--r--
more uniform Execution.fork vs. Execution.print;
asynchronous Execution.fork_prints, entrusted to Execution.fork management (NB: Command.print_finished is only relevant for persistent print functions);
     1 (*  Title:      Pure/PIDE/execution.ML
     2     Author:     Makarius
     3 
     4 Global management of execution.  Unique running execution serves as
     5 barrier for further exploration of forked command execs.
     6 *)
     7 
     8 signature EXECUTION =
     9 sig
    10   val start: unit -> Document_ID.execution
    11   val discontinue: unit -> unit
    12   val is_running: Document_ID.execution -> bool
    13   val is_running_exec: Document_ID.exec -> bool
    14   val running: Document_ID.execution -> Document_ID.exec -> Future.group list -> bool
    15   val peek: Document_ID.exec -> Future.group list
    16   val cancel: Document_ID.exec -> unit
    17   val terminate: Document_ID.exec -> unit
    18   type params = {name: string, pos: Position.T, pri: int}
    19   val fork: params -> (unit -> 'a) -> 'a future
    20   val print: params -> (serial -> unit) -> unit
    21   val fork_prints: Document_ID.exec -> unit
    22   val purge: Document_ID.exec list -> unit
    23   val reset: unit -> Future.group list
    24   val shutdown: unit -> unit
    25 end;
    26 
    27 structure Execution: EXECUTION =
    28 struct
    29 
    30 (* global state *)
    31 
    32 type print = {name: string, pri: int, body: unit -> unit};
    33 type exec_state = Future.group list * print list;  (*active forks, prints*)
    34 type state =
    35   Document_ID.execution * (*overall document execution*)
    36   exec_state Inttab.table;  (*running command execs*)
    37 
    38 val init_state: state = (Document_ID.none, Inttab.make [(Document_ID.none, ([], []))]);
    39 val state = Synchronized.var "Execution.state" init_state;
    40 
    41 fun get_state () = Synchronized.value state;
    42 fun change_state_result f = Synchronized.change_result state f;
    43 fun change_state f = Synchronized.change state f;
    44 
    45 fun unregistered exec_id = "Unregistered execution: " ^ Document_ID.print exec_id;
    46 
    47 
    48 (* unique running execution *)
    49 
    50 fun start () =
    51   let
    52     val execution_id = Document_ID.make ();
    53     val _ = change_state (apfst (K execution_id));
    54   in execution_id end;
    55 
    56 fun discontinue () = change_state (apfst (K Document_ID.none));
    57 
    58 fun is_running execution_id = execution_id = #1 (get_state ());
    59 
    60 
    61 (* execs *)
    62 
    63 fun is_running_exec exec_id =
    64   Inttab.defined (#2 (get_state ())) exec_id;
    65 
    66 fun running execution_id exec_id groups =
    67   change_state_result (fn (execution_id', execs) =>
    68     let
    69       val continue = execution_id = execution_id';
    70       val execs' =
    71         if continue then
    72           Inttab.update_new (exec_id, (groups, [])) execs
    73             handle Inttab.DUP dup =>
    74               raise Fail ("Execution already registered: " ^ Document_ID.print dup)
    75         else execs;
    76     in (continue, (execution_id', execs')) end);
    77 
    78 fun peek exec_id =
    79   (case Inttab.lookup (#2 (get_state ())) exec_id of
    80     SOME (groups, _) => groups
    81   | NONE => []);
    82 
    83 fun cancel exec_id = List.app Future.cancel_group (peek exec_id);
    84 fun terminate exec_id = List.app Future.terminate (peek exec_id);
    85 
    86 
    87 (* fork *)
    88 
    89 fun status task markups =
    90   let val props = Markup.properties [(Markup.taskN, Task_Queue.str_of_task task)]
    91   in Output.status (implode (map (Markup.markup_only o props) markups)) end;
    92 
    93 type params = {name: string, pos: Position.T, pri: int};
    94 
    95 fun fork ({name, pos, pri}: params) e =
    96   uninterruptible (fn _ => Position.setmp_thread_data pos (fn () =>
    97     let
    98       val exec_id = the_default 0 (Position.parse_id pos);
    99       val group = Future.worker_subgroup ();
   100       val _ = change_state (apsnd (fn execs =>
   101         (case Inttab.lookup execs exec_id of
   102           SOME (groups, prints) =>
   103             Inttab.update (exec_id, (group :: groups, prints)) execs
   104         | NONE => raise Fail (unregistered exec_id))));
   105 
   106       val future =
   107         (singleton o Future.forks)
   108           {name = name, group = SOME group, deps = [], pri = pri, interrupts = false}
   109           (fn () =>
   110             let
   111               val task = the (Future.worker_task ());
   112               val _ = status task [Markup.running];
   113               val result =
   114                 Exn.capture (Future.interruptible_task e) ()
   115                 |> Future.identify_result pos;
   116               val _ = status task [Markup.joined];
   117               val _ =
   118                 (case result of
   119                   Exn.Exn exn =>
   120                    (status task [Markup.failed];
   121                     status task [Markup.finished];
   122                     Output.report (Markup.markup_only Markup.bad);
   123                     if exec_id = 0 then ()
   124                     else List.app (Future.error_msg pos) (ML_Compiler.exn_messages_ids exn))
   125                 | Exn.Res _ =>
   126                     status task [Markup.finished])
   127             in Exn.release result end);
   128 
   129       val _ = status (Future.task_of future) [Markup.forked];
   130     in future end)) ();
   131 
   132 
   133 (* print *)
   134 
   135 fun print ({name, pos, pri}: params) pr =
   136   change_state (apsnd (fn execs =>
   137     let
   138       val exec_id = the_default 0 (Position.parse_id pos);
   139       val i = serial ();
   140       val print = {name = name, pri = pri, body = fn () => pr i};
   141     in
   142       (case Inttab.lookup execs exec_id of
   143         SOME (groups, prints) => Inttab.update (exec_id, (groups, print :: prints)) execs
   144       | NONE => raise Fail (unregistered exec_id))
   145     end));
   146 
   147 fun fork_prints exec_id =
   148   (case Inttab.lookup (#2 (get_state ())) exec_id of
   149     SOME (_, prints) =>
   150       if null prints orelse null (tl prints) orelse not (Multithreading.enabled ())
   151       then List.app (fn {body, ...} => body ()) (rev prints)
   152       else
   153         let
   154           val pos = Position.thread_data ();
   155           val pri =
   156             (case Future.worker_task () of
   157               SOME task => Task_Queue.pri_of_task task
   158             | NONE => 0);
   159         in
   160           List.app (fn {name, pri, body} =>
   161             ignore (fork {name = name, pos = pos, pri = pri} body)) (rev prints)
   162         end
   163   | NONE => raise Fail (unregistered exec_id));
   164 
   165 
   166 (* cleanup *)
   167 
   168 fun purge exec_ids =
   169   (change_state o apsnd) (fn execs =>
   170     let
   171       val execs' = fold Inttab.delete_safe exec_ids execs;
   172       val () =
   173         (execs', ()) |-> Inttab.fold (fn (exec_id, (groups, _)) => fn () =>
   174           if Inttab.defined execs' exec_id then ()
   175           else groups |> List.app (fn group =>
   176             if Task_Queue.is_canceled group then ()
   177             else raise Fail ("Attempt to purge valid execution: " ^ Document_ID.print exec_id)));
   178     in execs' end);
   179 
   180 fun reset () =
   181   change_state_result (fn (_, execs) =>
   182     let val groups = Inttab.fold (append o #1 o #2) execs []
   183     in (groups, init_state) end);
   184 
   185 fun shutdown () =
   186   (Future.shutdown ();
   187     (case maps Task_Queue.group_status (reset ()) of
   188       [] => ()
   189     | exns => raise Par_Exn.make exns));
   190 
   191 end;
   192