| author | wenzelm | 
| Tue, 20 Dec 2016 10:06:18 +0100 | |
| changeset 64614 | 88211daacf93 | 
| parent 62923 | 3a122e1e352a | 
| child 64677 | 8dc24130e8fe | 
| permissions | -rw-r--r-- | 
| 52605 | 1 | (* Title: Pure/PIDE/execution.ML | 
| 52596 | 2 | Author: Makarius | 
| 3 | ||
| 52606 | 4 | Global management of execution. Unique running execution serves as | 
| 53192 | 5 | barrier for further exploration of forked command execs. | 
| 52596 | 6 | *) | 
| 7 | ||
| 52605 | 8 | signature EXECUTION = | 
| 52596 | 9 | sig | 
| 52606 | 10 | val start: unit -> Document_ID.execution | 
| 11 | val discontinue: unit -> unit | |
| 12 | val is_running: Document_ID.execution -> bool | |
| 52784 
4ba2e8b9972f
de-assign execs that were not registered as running yet -- observe change of perspective more thoroughly;
 wenzelm parents: 
52775diff
changeset | 13 | val is_running_exec: Document_ID.exec -> bool | 
| 53375 
78693e46a237
Execution.fork formally requires registered Execution.running;
 wenzelm parents: 
53193diff
changeset | 14 | val running: Document_ID.execution -> Document_ID.exec -> Future.group list -> bool | 
| 53192 | 15 | val peek: Document_ID.exec -> Future.group list | 
| 52655 
3b2b1ef13979
more careful termination of removed execs, leaving running execs undisturbed;
 wenzelm parents: 
52608diff
changeset | 16 | val cancel: Document_ID.exec -> unit | 
| 56292 | 17 |   type params = {name: string, pos: Position.T, pri: int}
 | 
| 18 | val fork: params -> (unit -> 'a) -> 'a future | |
| 56305 | 19 | val print: params -> (unit -> unit) -> unit | 
| 56292 | 20 | val fork_prints: Document_ID.exec -> unit | 
| 53192 | 21 | val purge: Document_ID.exec list -> unit | 
| 22 | val reset: unit -> Future.group list | |
| 23 | val shutdown: unit -> unit | |
| 52596 | 24 | end; | 
| 25 | ||
| 52605 | 26 | structure Execution: EXECUTION = | 
| 52596 | 27 | struct | 
| 28 | ||
| 52787 
c143ad7811fc
pro-forma Execution.reset, despite lack of final join/commit;
 wenzelm parents: 
52784diff
changeset | 29 | (* global state *) | 
| 
c143ad7811fc
pro-forma Execution.reset, despite lack of final join/commit;
 wenzelm parents: 
52784diff
changeset | 30 | |
| 56292 | 31 | type print = {name: string, pri: int, body: unit -> unit};
 | 
| 32 | type exec_state = Future.group list * print list; (*active forks, prints*) | |
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 33 | type state = | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 34 | Document_ID.execution * (*overall document execution*) | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 35 | exec_state Inttab.table; (*running command execs*) | 
| 52787 
c143ad7811fc
pro-forma Execution.reset, despite lack of final join/commit;
 wenzelm parents: 
52784diff
changeset | 36 | |
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 37 | val init_state: state = (Document_ID.none, Inttab.make [(Document_ID.none, ([], []))]); | 
| 52787 
c143ad7811fc
pro-forma Execution.reset, despite lack of final join/commit;
 wenzelm parents: 
52784diff
changeset | 38 | val state = Synchronized.var "Execution.state" init_state; | 
| 52602 
00170ef1dc39
strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
 wenzelm parents: 
52596diff
changeset | 39 | |
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 40 | fun get_state () = Synchronized.value state; | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 41 | fun change_state_result f = Synchronized.change_result state f; | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 42 | fun change_state f = Synchronized.change state f; | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 43 | |
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 44 | fun unregistered exec_id = "Unregistered execution: " ^ Document_ID.print exec_id; | 
| 53192 | 45 | |
| 52604 | 46 | |
| 52606 | 47 | (* unique running execution *) | 
| 52604 | 48 | |
| 52606 | 49 | fun start () = | 
| 52602 
00170ef1dc39
strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
 wenzelm parents: 
52596diff
changeset | 50 | let | 
| 52606 | 51 | val execution_id = Document_ID.make (); | 
| 53192 | 52 | val _ = change_state (apfst (K execution_id)); | 
| 52606 | 53 | in execution_id end; | 
| 52604 | 54 | |
| 53192 | 55 | fun discontinue () = change_state (apfst (K Document_ID.none)); | 
| 52606 | 56 | |
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 57 | fun is_running execution_id = execution_id = #1 (get_state ()); | 
| 52604 | 58 | |
| 59 | ||
| 53192 | 60 | (* execs *) | 
| 52604 | 61 | |
| 52784 
4ba2e8b9972f
de-assign execs that were not registered as running yet -- observe change of perspective more thoroughly;
 wenzelm parents: 
52775diff
changeset | 62 | fun is_running_exec exec_id = | 
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 63 | Inttab.defined (#2 (get_state ())) exec_id; | 
| 52784 
4ba2e8b9972f
de-assign execs that were not registered as running yet -- observe change of perspective more thoroughly;
 wenzelm parents: 
52775diff
changeset | 64 | |
| 53375 
78693e46a237
Execution.fork formally requires registered Execution.running;
 wenzelm parents: 
53193diff
changeset | 65 | fun running execution_id exec_id groups = | 
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 66 | change_state_result (fn (execution_id', execs) => | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 67 | let | 
| 59348 
8a6788917b32
do not crash into already running exec, instead join its lazy result in the subsequent step (amending 59f1591a11cb);
 wenzelm parents: 
56333diff
changeset | 68 | val ok = execution_id = execution_id' andalso not (Inttab.defined execs exec_id); | 
| 
8a6788917b32
do not crash into already running exec, instead join its lazy result in the subsequent step (amending 59f1591a11cb);
 wenzelm parents: 
56333diff
changeset | 69 | val execs' = execs |> ok ? Inttab.update (exec_id, (groups, [])); | 
| 
8a6788917b32
do not crash into already running exec, instead join its lazy result in the subsequent step (amending 59f1591a11cb);
 wenzelm parents: 
56333diff
changeset | 70 | in (ok, (execution_id', execs')) end); | 
| 53192 | 71 | |
| 72 | fun peek exec_id = | |
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 73 | (case Inttab.lookup (#2 (get_state ())) exec_id of | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 74 | SOME (groups, _) => groups | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 75 | | NONE => []); | 
| 53192 | 76 | |
| 77 | fun cancel exec_id = List.app Future.cancel_group (peek exec_id); | |
| 78 | ||
| 79 | ||
| 80 | (* fork *) | |
| 81 | ||
| 82 | fun status task markups = | |
| 56296 
5413b6379c0e
less markup by default -- this is stored persistently in Isabelle/Scala;
 wenzelm parents: 
56292diff
changeset | 83 | let | 
| 
5413b6379c0e
less markup by default -- this is stored persistently in Isabelle/Scala;
 wenzelm parents: 
56292diff
changeset | 84 | val props = | 
| 
5413b6379c0e
less markup by default -- this is stored persistently in Isabelle/Scala;
 wenzelm parents: 
56292diff
changeset | 85 | if ! Multithreading.trace >= 2 | 
| 
5413b6379c0e
less markup by default -- this is stored persistently in Isabelle/Scala;
 wenzelm parents: 
56292diff
changeset | 86 | then [(Markup.taskN, Task_Queue.str_of_task task)] else []; | 
| 
5413b6379c0e
less markup by default -- this is stored persistently in Isabelle/Scala;
 wenzelm parents: 
56292diff
changeset | 87 | in Output.status (implode (map (Markup.markup_only o Markup.properties props) markups)) end; | 
| 53192 | 88 | |
| 56292 | 89 | type params = {name: string, pos: Position.T, pri: int};
 | 
| 90 | ||
| 91 | fun fork ({name, pos, pri}: params) e =
 | |
| 62923 | 92 | Thread_Attributes.uninterruptible (fn _ => Position.setmp_thread_data pos (fn () => | 
| 53192 | 93 | let | 
| 94 | val exec_id = the_default 0 (Position.parse_id pos); | |
| 95 | val group = Future.worker_subgroup (); | |
| 53375 
78693e46a237
Execution.fork formally requires registered Execution.running;
 wenzelm parents: 
53193diff
changeset | 96 | val _ = change_state (apsnd (fn execs => | 
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 97 | (case Inttab.lookup execs exec_id of | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 98 | SOME (groups, prints) => | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 99 | Inttab.update (exec_id, (group :: groups, prints)) execs | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 100 | | NONE => raise Fail (unregistered exec_id)))); | 
| 52655 
3b2b1ef13979
more careful termination of removed execs, leaving running execs undisturbed;
 wenzelm parents: 
52608diff
changeset | 101 | |
| 53192 | 102 | val future = | 
| 103 | (singleton o Future.forks) | |
| 104 |           {name = name, group = SOME group, deps = [], pri = pri, interrupts = false}
 | |
| 105 | (fn () => | |
| 106 | let | |
| 107 | val task = the (Future.worker_task ()); | |
| 108 | val _ = status task [Markup.running]; | |
| 109 | val result = | |
| 110 | Exn.capture (Future.interruptible_task e) () | |
| 61077 
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
 wenzelm parents: 
59467diff
changeset | 111 | |> Future.identify_result pos | 
| 
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
 wenzelm parents: 
59467diff
changeset | 112 | |> Exn.map_exn Runtime.thread_context; | 
| 54646 
2b38549374ba
more robust status reports: avoid loosing information about physical events (see also 28d207ba9340, 2bc5924b117f, 9edfd36a0355) -- NB: TTY and Proof General ignore Output.status and Output.report;
 wenzelm parents: 
53375diff
changeset | 113 | val _ = status task [Markup.joined]; | 
| 53192 | 114 | val _ = | 
| 115 | (case result of | |
| 54678 
87910da843d5
more uniform status -- accommodate spurious Exn.Interrupt from user code, allow ML_Compiler.exn_messages_id to crash;
 wenzelm parents: 
54646diff
changeset | 116 | Exn.Exn exn => | 
| 54646 
2b38549374ba
more robust status reports: avoid loosing information about physical events (see also 28d207ba9340, 2bc5924b117f, 9edfd36a0355) -- NB: TTY and Proof General ignore Output.status and Output.report;
 wenzelm parents: 
53375diff
changeset | 117 | (status task [Markup.failed]; | 
| 54678 
87910da843d5
more uniform status -- accommodate spurious Exn.Interrupt from user code, allow ML_Compiler.exn_messages_id to crash;
 wenzelm parents: 
54646diff
changeset | 118 | status task [Markup.finished]; | 
| 56333 
38f1422ef473
support bulk messages consisting of small string segments, which are more healthy to the Poly/ML RTS and might prevent spurious GC crashes such as MTGCProcessMarkPointers::ScanAddressesInObject;
 wenzelm parents: 
56305diff
changeset | 119 | Output.report [Markup.markup_only Markup.bad]; | 
| 54646 
2b38549374ba
more robust status reports: avoid loosing information about physical events (see also 28d207ba9340, 2bc5924b117f, 9edfd36a0355) -- NB: TTY and Proof General ignore Output.status and Output.report;
 wenzelm parents: 
53375diff
changeset | 120 | if exec_id = 0 then () | 
| 56333 
38f1422ef473
support bulk messages consisting of small string segments, which are more healthy to the Poly/ML RTS and might prevent spurious GC crashes such as MTGCProcessMarkPointers::ScanAddressesInObject;
 wenzelm parents: 
56305diff
changeset | 121 | else List.app (Future.error_message pos) (Runtime.exn_messages_ids exn)) | 
| 54678 
87910da843d5
more uniform status -- accommodate spurious Exn.Interrupt from user code, allow ML_Compiler.exn_messages_id to crash;
 wenzelm parents: 
54646diff
changeset | 122 | | Exn.Res _ => | 
| 
87910da843d5
more uniform status -- accommodate spurious Exn.Interrupt from user code, allow ML_Compiler.exn_messages_id to crash;
 wenzelm parents: 
54646diff
changeset | 123 | status task [Markup.finished]) | 
| 53192 | 124 | in Exn.release result end); | 
| 54646 
2b38549374ba
more robust status reports: avoid loosing information about physical events (see also 28d207ba9340, 2bc5924b117f, 9edfd36a0355) -- NB: TTY and Proof General ignore Output.status and Output.report;
 wenzelm parents: 
53375diff
changeset | 125 | |
| 53192 | 126 | val _ = status (Future.task_of future) [Markup.forked]; | 
| 127 | in future end)) (); | |
| 52604 | 128 | |
| 53192 | 129 | |
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 130 | (* print *) | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 131 | |
| 56305 | 132 | fun print ({name, pos, pri}: params) e =
 | 
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 133 | change_state (apsnd (fn execs => | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 134 | let | 
| 56292 | 135 | val exec_id = the_default 0 (Position.parse_id pos); | 
| 56305 | 136 |       val print = {name = name, pri = pri, body = e};
 | 
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 137 | in | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 138 | (case Inttab.lookup execs exec_id of | 
| 56292 | 139 | SOME (groups, prints) => Inttab.update (exec_id, (groups, print :: prints)) execs | 
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 140 | | NONE => raise Fail (unregistered exec_id)) | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 141 | end)); | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 142 | |
| 56292 | 143 | fun fork_prints exec_id = | 
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 144 | (case Inttab.lookup (#2 (get_state ())) exec_id of | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 145 | SOME (_, prints) => | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 146 | if null prints orelse null (tl prints) orelse not (Multithreading.enabled ()) | 
| 56292 | 147 |       then List.app (fn {body, ...} => body ()) (rev prints)
 | 
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 148 | else | 
| 56296 
5413b6379c0e
less markup by default -- this is stored persistently in Isabelle/Scala;
 wenzelm parents: 
56292diff
changeset | 149 | let val pos = Position.thread_data () in | 
| 56292 | 150 |           List.app (fn {name, pri, body} =>
 | 
| 151 |             ignore (fork {name = name, pos = pos, pri = pri} body)) (rev prints)
 | |
| 152 | end | |
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 153 | | NONE => raise Fail (unregistered exec_id)); | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 154 | |
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 155 | |
| 53192 | 156 | (* cleanup *) | 
| 157 | ||
| 158 | fun purge exec_ids = | |
| 159 | (change_state o apsnd) (fn execs => | |
| 160 | let | |
| 161 | val execs' = fold Inttab.delete_safe exec_ids execs; | |
| 162 | val () = | |
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 163 | (execs', ()) |-> Inttab.fold (fn (exec_id, (groups, _)) => fn () => | 
| 53192 | 164 | if Inttab.defined execs' exec_id then () | 
| 165 | else groups |> List.app (fn group => | |
| 166 | if Task_Queue.is_canceled group then () | |
| 53375 
78693e46a237
Execution.fork formally requires registered Execution.running;
 wenzelm parents: 
53193diff
changeset | 167 |             else raise Fail ("Attempt to purge valid execution: " ^ Document_ID.print exec_id)));
 | 
| 53192 | 168 | in execs' end); | 
| 169 | ||
| 170 | fun reset () = | |
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 171 | change_state_result (fn (_, execs) => | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
54678diff
changeset | 172 | let val groups = Inttab.fold (append o #1 o #2) execs [] | 
| 53192 | 173 | in (groups, init_state) end); | 
| 174 | ||
| 175 | fun shutdown () = | |
| 176 | (Future.shutdown (); | |
| 177 | (case maps Task_Queue.group_status (reset ()) of | |
| 178 | [] => () | |
| 179 | | exns => raise Par_Exn.make exns)); | |
| 180 | ||
| 181 | end; | |
| 182 |