src/Pure/PIDE/execution.ML
author wenzelm
Wed Mar 26 20:08:07 2014 +0100 (2014-03-26 ago)
changeset 56296 5413b6379c0e
parent 56292 1a91a0da65ab
child 56297 3925634718fb
permissions -rw-r--r--
less markup by default -- this is stored persistently in Isabelle/Scala;
removed dead code;
     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
    91     val props =
    92       if ! Multithreading.trace >= 2
    93       then [(Markup.taskN, Task_Queue.str_of_task task)] else [];
    94   in Output.status (implode (map (Markup.markup_only o Markup.properties props) markups)) end;
    95 
    96 type params = {name: string, pos: Position.T, pri: int};
    97 
    98 fun fork ({name, pos, pri}: params) e =
    99   uninterruptible (fn _ => Position.setmp_thread_data pos (fn () =>
   100     let
   101       val exec_id = the_default 0 (Position.parse_id pos);
   102       val group = Future.worker_subgroup ();
   103       val _ = change_state (apsnd (fn execs =>
   104         (case Inttab.lookup execs exec_id of
   105           SOME (groups, prints) =>
   106             Inttab.update (exec_id, (group :: groups, prints)) execs
   107         | NONE => raise Fail (unregistered exec_id))));
   108 
   109       val future =
   110         (singleton o Future.forks)
   111           {name = name, group = SOME group, deps = [], pri = pri, interrupts = false}
   112           (fn () =>
   113             let
   114               val task = the (Future.worker_task ());
   115               val _ = status task [Markup.running];
   116               val result =
   117                 Exn.capture (Future.interruptible_task e) ()
   118                 |> Future.identify_result pos;
   119               val _ = status task [Markup.joined];
   120               val _ =
   121                 (case result of
   122                   Exn.Exn exn =>
   123                    (status task [Markup.failed];
   124                     status task [Markup.finished];
   125                     Output.report (Markup.markup_only Markup.bad);
   126                     if exec_id = 0 then ()
   127                     else List.app (Future.error_msg pos) (ML_Compiler.exn_messages_ids exn))
   128                 | Exn.Res _ =>
   129                     status task [Markup.finished])
   130             in Exn.release result end);
   131 
   132       val _ = status (Future.task_of future) [Markup.forked];
   133     in future end)) ();
   134 
   135 
   136 (* print *)
   137 
   138 fun print ({name, pos, pri}: params) pr =
   139   change_state (apsnd (fn execs =>
   140     let
   141       val exec_id = the_default 0 (Position.parse_id pos);
   142       val i = serial ();
   143       val print = {name = name, pri = pri, body = fn () => pr i};
   144     in
   145       (case Inttab.lookup execs exec_id of
   146         SOME (groups, prints) => Inttab.update (exec_id, (groups, print :: prints)) execs
   147       | NONE => raise Fail (unregistered exec_id))
   148     end));
   149 
   150 fun fork_prints exec_id =
   151   (case Inttab.lookup (#2 (get_state ())) exec_id of
   152     SOME (_, prints) =>
   153       if null prints orelse null (tl prints) orelse not (Multithreading.enabled ())
   154       then List.app (fn {body, ...} => body ()) (rev prints)
   155       else
   156         let val pos = Position.thread_data () in
   157           List.app (fn {name, pri, body} =>
   158             ignore (fork {name = name, pos = pos, pri = pri} body)) (rev prints)
   159         end
   160   | NONE => raise Fail (unregistered exec_id));
   161 
   162 
   163 (* cleanup *)
   164 
   165 fun purge exec_ids =
   166   (change_state o apsnd) (fn execs =>
   167     let
   168       val execs' = fold Inttab.delete_safe exec_ids execs;
   169       val () =
   170         (execs', ()) |-> Inttab.fold (fn (exec_id, (groups, _)) => fn () =>
   171           if Inttab.defined execs' exec_id then ()
   172           else groups |> List.app (fn group =>
   173             if Task_Queue.is_canceled group then ()
   174             else raise Fail ("Attempt to purge valid execution: " ^ Document_ID.print exec_id)));
   175     in execs' end);
   176 
   177 fun reset () =
   178   change_state_result (fn (_, execs) =>
   179     let val groups = Inttab.fold (append o #1 o #2) execs []
   180     in (groups, init_state) end);
   181 
   182 fun shutdown () =
   183   (Future.shutdown ();
   184     (case maps Task_Queue.group_status (reset ()) of
   185       [] => ()
   186     | exns => raise Par_Exn.make exns));
   187 
   188 end;
   189