src/Pure/PIDE/execution.ML
author wenzelm
Sun Aug 25 20:32:26 2013 +0200 (2013-08-25 ago)
changeset 53192 04df1d236e1c
parent 52787 c143ad7811fc
child 53193 2ddc5e788f7c
permissions -rw-r--r--
maintain goal forks as part of global execution;
tuned;
     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 -> 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   val fork: {name: string, pos: Position.T, pri: int} -> (unit -> 'a) -> 'a future
    19   val purge: Document_ID.exec list -> unit
    20   val reset: unit -> Future.group list
    21   val shutdown: unit -> unit
    22 end;
    23 
    24 structure Execution: EXECUTION =
    25 struct
    26 
    27 (* global state *)
    28 
    29 datatype state = State of
    30   {execution: Document_ID.execution,  (*overall document execution*)
    31    execs: Future.group list Inttab.table};  (*command execs with registered forks*)
    32 
    33 fun make_state (execution, execs) = State {execution = execution, execs = execs};
    34 fun map_state f (State {execution, execs}) = make_state (f (execution, execs));
    35 
    36 val init_state = make_state (Document_ID.none, Inttab.empty);
    37 val state = Synchronized.var "Execution.state" init_state;
    38 
    39 fun get_state () = let val State args = Synchronized.value state in args end;
    40 fun change_state f = Synchronized.change state (map_state f);
    41 
    42 
    43 (* unique running execution *)
    44 
    45 fun start () =
    46   let
    47     val execution_id = Document_ID.make ();
    48     val _ = change_state (apfst (K execution_id));
    49   in execution_id end;
    50 
    51 fun discontinue () = change_state (apfst (K Document_ID.none));
    52 
    53 fun is_running execution_id = execution_id = #execution (get_state ());
    54 
    55 
    56 (* execs *)
    57 
    58 fun is_running_exec exec_id =
    59   Inttab.defined (#execs (get_state ())) exec_id;
    60 
    61 fun running execution_id exec_id =
    62   Synchronized.guarded_access state
    63     (fn State {execution = execution_id', execs} =>
    64       let
    65         val continue = execution_id = execution_id';
    66         val execs' =
    67           if continue then
    68             Inttab.update_new (exec_id, [Future.the_worker_group ()]) execs
    69               handle Inttab.DUP dup =>
    70                 error ("Execution already registered: " ^ Document_ID.print dup)
    71           else execs;
    72       in SOME (continue, make_state (execution_id', execs')) end);
    73 
    74 fun peek exec_id =
    75   Inttab.lookup_list (#execs (get_state ())) exec_id;
    76 
    77 fun cancel exec_id = List.app Future.cancel_group (peek exec_id);
    78 fun terminate exec_id = List.app Future.terminate (peek exec_id);
    79 
    80 
    81 (* fork *)
    82 
    83 local
    84 
    85 fun status task markups =
    86   let val props = Markup.properties [(Markup.taskN, Task_Queue.str_of_task task)]
    87   in Output.status (implode (map (Markup.markup_only o props) markups)) end;
    88 
    89 fun register exec =
    90   change_state (fn (execution, execs) => (execution, Inttab.cons_list exec execs));
    91 
    92 in
    93 
    94 fun fork {name, pos, pri} e =
    95   uninterruptible (fn _ => Position.setmp_thread_data pos (fn () =>
    96     let
    97       val exec_id = the_default 0 (Position.parse_id pos);
    98       val group = Future.worker_subgroup ();
    99       val _ = register (exec_id, group);
   100 
   101       val future =
   102         (singleton o Future.forks)
   103           {name = name, group = SOME group, deps = [], pri = pri, interrupts = false}
   104           (fn () =>
   105             let
   106               val task = the (Future.worker_task ());
   107               val _ = status task [Markup.running];
   108               val result =
   109                 Exn.capture (Future.interruptible_task e) ()
   110                 |> Future.identify_result pos;
   111               val _ = status task [Markup.finished, Markup.joined];
   112               val _ =
   113                 (case result of
   114                   Exn.Res _ => ()
   115                 | Exn.Exn exn =>
   116                     if exec_id = 0 orelse Exn.is_interrupt exn then ()
   117                     else
   118                       (status task [Markup.failed];
   119                        Output.report (Markup.markup_only Markup.bad);
   120                        List.app (Future.error_msg pos) (ML_Compiler.exn_messages_ids exn)));
   121             in Exn.release result end);
   122       val _ = status (Future.task_of future) [Markup.forked];
   123     in future end)) ();
   124 
   125 end;
   126 
   127 
   128 (* cleanup *)
   129 
   130 fun purge exec_ids =
   131   (change_state o apsnd) (fn execs =>
   132     let
   133       val execs' = fold Inttab.delete_safe exec_ids execs;
   134       val () =
   135         (execs', ()) |-> Inttab.fold (fn (exec_id, groups) => fn () =>
   136           if Inttab.defined execs' exec_id then ()
   137           else groups |> List.app (fn group =>
   138             if Task_Queue.is_canceled group then ()
   139             else error ("Attempt to purge valid execution: " ^ Document_ID.print exec_id)));
   140     in execs' end);
   141 
   142 fun reset () =
   143   Synchronized.change_result state (fn State {execs, ...} =>
   144     let val groups = Inttab.fold (append o #2) execs []
   145     in (groups, init_state) end);
   146 
   147 fun shutdown () =
   148   (Future.shutdown ();
   149     (case maps Task_Queue.group_status (reset ()) of
   150       [] => ()
   151     | exns => raise Par_Exn.make exns));
   152 
   153 end;
   154