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