| author | wenzelm | 
| Sat, 18 Mar 2017 20:30:05 +0100 | |
| changeset 65311 | 08ebdaa34b24 | 
| parent 65264 | 7e6ecd04b5fe | 
| child 65313 | 347ed6219dab | 
| permissions | -rw-r--r-- | 
| 56210 | 1 | /* Title: Pure/PIDE/session.scala | 
| 36676 | 2 | Author: Makarius | 
| 57923 | 3 | Options: :folding=explicit: | 
| 36676 | 4 | |
| 56210 | 5 | PIDE editor session, potentially with running prover process. | 
| 36676 | 6 | */ | 
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 7 | |
| 34871 
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
 wenzelm parents: 
34859diff
changeset | 8 | package isabelle | 
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 9 | |
| 44733 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 10 | |
| 46771 
06a9b24c4a36
explicit syslog_limit reduces danger of low-level message flooding;
 wenzelm parents: 
46739diff
changeset | 11 | import scala.collection.immutable.Queue | 
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 12 | |
| 34815 
6bae73cd8e33
unified Command and Command_State, eliminated separate Accumulator;
 wenzelm parents: 
34813diff
changeset | 13 | |
| 34791 | 14 | object Session | 
| 15 | {
 | |
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 16 | /* outlets */ | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 17 | |
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 18 | object Consumer | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 19 |   {
 | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 20 | def apply[A](name: String)(consume: A => Unit): Consumer[A] = | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 21 | new Consumer[A](name, consume) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 22 | } | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 23 | final class Consumer[-A] private(val name: String, val consume: A => Unit) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 24 | |
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 25 | class Outlet[A](dispatcher: Consumer_Thread[() => Unit]) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 26 |   {
 | 
| 61590 | 27 | private val consumers = Synchronized[List[Consumer[A]]](Nil) | 
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 28 | |
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 29 |     def += (c: Consumer[A]) { consumers.change(Library.update(c)) }
 | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 30 |     def -= (c: Consumer[A]) { consumers.change(Library.remove(c)) }
 | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 31 | |
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 32 | def post(a: A) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 33 |     {
 | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 34 |       for (c <- consumers.value.iterator) {
 | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 35 | dispatcher.send(() => | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 36 |           try { c.consume(a) }
 | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 37 |           catch {
 | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 38 | case exn: Throwable => | 
| 56782 
433cf57550fa
more systematic Isabelle output, like in classic Isabelle/ML (without markup);
 wenzelm parents: 
56775diff
changeset | 39 |               Output.error_message("Consumer failed: " + quote(c.name) + "\n" + Exn.message(exn))
 | 
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 40 | }) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 41 | } | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 42 | } | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 43 | } | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 44 | |
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 45 | |
| 56315 | 46 | /* change */ | 
| 47 | ||
| 48 | sealed case class Change( | |
| 49 | previous: Document.Version, | |
| 59077 
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
 wenzelm parents: 
58928diff
changeset | 50 | syntax_changed: List[Document.Node.Name], | 
| 56316 
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
 wenzelm parents: 
56315diff
changeset | 51 | deps_changed: Boolean, | 
| 56315 | 52 | doc_edits: List[Document.Edit_Command], | 
| 53 | version: Document.Version) | |
| 54 | ||
| 57976 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 55 | case object Change_Flush | 
| 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 56 | |
| 56315 | 57 | |
| 34813 
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
 wenzelm parents: 
34810diff
changeset | 58 | /* events */ | 
| 
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
 wenzelm parents: 
34810diff
changeset | 59 | |
| 43716 | 60 |   //{{{
 | 
| 50697 
82e9178e6a98
improved Monitor_Dockable, based on ML_Statistics operations;
 wenzelm parents: 
50566diff
changeset | 61 | case class Statistics(props: Properties.T) | 
| 50117 | 62 | case class Global_Options(options: Options) | 
| 44805 | 63 | case object Caret_Focus | 
| 54521 
744ea0025e11
clarified Document.Blobs environment vs. actual edits of auxiliary files;
 wenzelm parents: 
54519diff
changeset | 64 | case class Raw_Edits(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text]) | 
| 52531 | 65 | case class Dialog_Result(id: Document_ID.Generic, serial: Long, result: String) | 
| 59364 | 66 | case class Build_Theories(id: String, master_dir: Path, theories: List[(Options, List[Path])]) | 
| 47027 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 67 | case class Commands_Changed( | 
| 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 68 | assignment: Boolean, nodes: Set[Document.Node.Name], commands: Set[Command]) | 
| 39630 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 69 | |
| 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 70 | sealed abstract class Phase | 
| 65206 | 71 |   {
 | 
| 72 | def print: String = | |
| 73 |       this match {
 | |
| 74 | case Terminated(rc) => if (rc == 0) "finished" else "failed" | |
| 75 | case _ => Word.lowercase(this.toString) | |
| 76 | } | |
| 77 | } | |
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 78 | case object Inactive extends Phase // stable | 
| 39701 | 79 | case object Startup extends Phase // transient | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 80 | case object Ready extends Phase // metastable | 
| 39701 | 81 | case object Shutdown extends Phase // transient | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 82 | case class Terminated(rc: Int) extends Phase // stable | 
| 43716 | 83 | //}}} | 
| 52111 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 84 | |
| 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 85 | |
| 56775 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 86 | /* syslog */ | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 87 | |
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 88 | private[Session] class Syslog(limit: Int) | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 89 |   {
 | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 90 | private var queue = Queue.empty[XML.Elem] | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 91 | private var length = 0 | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 92 | |
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 93 |     def += (msg: XML.Elem): Unit = synchronized {
 | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 94 | queue = queue.enqueue(msg) | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 95 | length += 1 | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 96 | if (length > limit) queue = queue.dequeue._2 | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 97 | } | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 98 | |
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 99 |     def content: String = synchronized {
 | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 100 | cat_lines(queue.iterator.map(XML.content)) + | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 101 | (if (length > limit) "\n(A total of " + length + " messages...)" else "") | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 102 | } | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 103 | } | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 104 | |
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 105 | |
| 52111 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 106 | /* protocol handlers */ | 
| 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 107 | |
| 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 108 | abstract class Protocol_Handler | 
| 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 109 |   {
 | 
| 65219 | 110 |     def init(session: Session): Unit = {}
 | 
| 111 |     def exit(): Unit = {}
 | |
| 112 | val functions: List[(String, Prover.Protocol_Output => Boolean)] | |
| 52111 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 113 | } | 
| 34791 | 114 | } | 
| 115 | ||
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 116 | |
| 65264 
7e6ecd04b5fe
dynamic session_options for tuning parameters and initial prover options;
 wenzelm parents: 
65223diff
changeset | 117 | class Session(session_options: => Options, val resources: Resources) extends Document.Session | 
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 118 | {
 | 
| 65214 | 119 | session => | 
| 120 | ||
| 65218 | 121 | val xml_cache: XML.Cache = new XML.Cache() | 
| 122 | ||
| 65214 | 123 | |
| 47653 | 124 | /* global flags */ | 
| 125 | ||
| 126 | @volatile var timing: Boolean = false | |
| 127 | @volatile var verbose: Boolean = false | |
| 128 | ||
| 129 | ||
| 65264 
7e6ecd04b5fe
dynamic session_options for tuning parameters and initial prover options;
 wenzelm parents: 
65223diff
changeset | 130 | /* dynamic session options */ | 
| 49288 | 131 | |
| 65264 
7e6ecd04b5fe
dynamic session_options for tuning parameters and initial prover options;
 wenzelm parents: 
65223diff
changeset | 132 |   def output_delay: Time = session_options.seconds("editor_output_delay")
 | 
| 
7e6ecd04b5fe
dynamic session_options for tuning parameters and initial prover options;
 wenzelm parents: 
65223diff
changeset | 133 |   def prune_delay: Time = session_options.seconds("editor_prune_delay")
 | 
| 
7e6ecd04b5fe
dynamic session_options for tuning parameters and initial prover options;
 wenzelm parents: 
65223diff
changeset | 134 |   def prune_size: Int = session_options.int("editor_prune_size")
 | 
| 
7e6ecd04b5fe
dynamic session_options for tuning parameters and initial prover options;
 wenzelm parents: 
65223diff
changeset | 135 |   def syslog_limit: Int = session_options.int("editor_syslog_limit")
 | 
| 
7e6ecd04b5fe
dynamic session_options for tuning parameters and initial prover options;
 wenzelm parents: 
65223diff
changeset | 136 |   def reparse_limit: Int = session_options.int("editor_reparse_limit")
 | 
| 37849 | 137 | |
| 138 | ||
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 139 | /* outlets */ | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 140 | |
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 141 | private val dispatcher = | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 142 |     Consumer_Thread.fork[() => Unit]("Session.dispatcher", daemon = true) { case e => e(); true }
 | 
| 34809 | 143 | |
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 144 | val statistics = new Session.Outlet[Session.Statistics](dispatcher) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 145 | val global_options = new Session.Outlet[Session.Global_Options](dispatcher) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 146 | val caret_focus = new Session.Outlet[Session.Caret_Focus.type](dispatcher) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 147 | val raw_edits = new Session.Outlet[Session.Raw_Edits](dispatcher) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 148 | val commands_changed = new Session.Outlet[Session.Commands_Changed](dispatcher) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 149 | val phase_changed = new Session.Outlet[Session.Phase](dispatcher) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 150 | val syslog_messages = new Session.Outlet[Prover.Output](dispatcher) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 151 | val raw_output_messages = new Session.Outlet[Prover.Output](dispatcher) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 152 | val trace_events = new Session.Outlet[Simplifier_Trace.Event.type](dispatcher) | 
| 60900 | 153 | val debugger_updates = new Session.Outlet[Debugger.Update.type](dispatcher) | 
| 34809 | 154 | |
| 60749 | 155 | val all_messages = new Session.Outlet[Prover.Message](dispatcher) // potential bottle-neck! | 
| 34809 | 156 | |
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 157 | |
| 56799 | 158 | /** main protocol manager **/ | 
| 45635 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 159 | |
| 56799 | 160 | /* internal messages */ | 
| 45635 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 161 | |
| 62556 
c115e69f457f
more abstract Session.start, without prover command-line;
 wenzelm parents: 
62545diff
changeset | 162 | private case class Start(start_prover: Prover.Receiver => Prover) | 
| 56799 | 163 | private case object Stop | 
| 164 | private case class Cancel_Exec(exec_id: Document_ID.Exec) | |
| 165 | private case class Protocol_Command(name: String, args: List[String]) | |
| 166 | private case class Update_Options(options: Options) | |
| 167 | private case object Prune_History | |
| 45635 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 168 | |
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 169 | |
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 170 | /* phase */ | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 171 | |
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 172 | private def post_phase(new_phase: Session.Phase): Session.Phase = | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 173 |   {
 | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 174 | phase_changed.post(new_phase) | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 175 | new_phase | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 176 | } | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 177 | private val _phase = Synchronized[Session.Phase](Session.Inactive) | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 178 | private def phase_=(new_phase: Session.Phase): Unit = _phase.change(_ => post_phase(new_phase)) | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 179 | |
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 180 | def phase = _phase.value | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 181 | def is_ready: Boolean = phase == Session.Ready | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 182 | |
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 183 | |
| 43651 
511df47bcadc
some support for theory files within Isabelle/Scala session;
 wenzelm parents: 
43649diff
changeset | 184 | /* global state */ | 
| 43644 | 185 | |
| 56775 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 186 | private val syslog = new Session.Syslog(syslog_limit) | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 187 | def syslog_content(): String = syslog.content | 
| 39626 
a5d0bcfb95a3
manage persistent syslog via Session, not Isabelle_Process;
 wenzelm parents: 
39625diff
changeset | 188 | |
| 56690 | 189 | private val global_state = Synchronized(Document.State.init) | 
| 56687 | 190 | def current_state(): Document.State = global_state.value | 
| 46944 
9fc22eb6408c
more recent recent_syntax, e.g. relevant for document rendering during startup;
 wenzelm parents: 
46942diff
changeset | 191 | |
| 63584 
68751fe1c036
tuned signature -- prover-independence is presently theoretical;
 wenzelm parents: 
62556diff
changeset | 192 | def recent_syntax(name: Document.Node.Name): Outer_Syntax = | 
| 60934 | 193 | global_state.value.recent_finished.version.get_finished.nodes(name).syntax getOrElse | 
| 64854 | 194 | resources.base.syntax | 
| 34816 
d33312514220
maintain generic session entities -- cover commands, states, etc. (but not yet documents);
 wenzelm parents: 
34815diff
changeset | 195 | |
| 43651 
511df47bcadc
some support for theory files within Isabelle/Scala session;
 wenzelm parents: 
43649diff
changeset | 196 | |
| 56799 | 197 | /* pipelined change parsing */ | 
| 198 | ||
| 199 | private case class Text_Edits( | |
| 200 | previous: Future[Document.Version], | |
| 201 | doc_blobs: Document.Blobs, | |
| 202 | text_edits: List[Document.Edit_Text], | |
| 203 | version_result: Promise[Document.Version]) | |
| 43651 
511df47bcadc
some support for theory files within Isabelle/Scala session;
 wenzelm parents: 
43649diff
changeset | 204 | |
| 56799 | 205 |   private val change_parser = Consumer_Thread.fork[Text_Edits]("change_parser", daemon = true)
 | 
| 206 |   {
 | |
| 207 | case Text_Edits(previous, doc_blobs, text_edits, version_result) => | |
| 208 | val prev = previous.get_finished | |
| 209 | val change = | |
| 210 |         Timing.timeit("parse_change", timing) {
 | |
| 211 | resources.parse_change(reparse_limit, prev, doc_blobs, text_edits) | |
| 212 | } | |
| 213 | version_result.fulfill(change.version) | |
| 214 | manager.send(change) | |
| 215 | true | |
| 216 | } | |
| 41534 | 217 | |
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 218 | |
| 56719 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 219 | /* buffered changes */ | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 220 | |
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 221 | private object change_buffer | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 222 |   {
 | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 223 | private var assignment: Boolean = false | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 224 | private var nodes: Set[Document.Node.Name] = Set.empty | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 225 | private var commands: Set[Command] = Set.empty | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 226 | |
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 227 |     def flush(): Unit = synchronized {
 | 
| 59319 | 228 | if (assignment || nodes.nonEmpty || commands.nonEmpty) | 
| 56719 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 229 | commands_changed.post(Session.Commands_Changed(assignment, nodes, commands)) | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 230 | assignment = false | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 231 | nodes = Set.empty | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 232 | commands = Set.empty | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 233 | } | 
| 61556 | 234 |     private val delay_flush = Standard_Thread.delay_first(output_delay) { flush() }
 | 
| 56719 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 235 | |
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 236 |     def invoke(assign: Boolean, cmds: List[Command]): Unit = synchronized {
 | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 237 | assignment |= assign | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 238 |       for (command <- cmds) {
 | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 239 | nodes += command.node_name | 
| 64803 
27328dcaf64c
emit Commands_Changed for blobs as well, e.g. relevant for isabelle.vscode.Server.prover_output;
 wenzelm parents: 
63584diff
changeset | 240 | command.blobs_names.foreach(nodes += _) | 
| 56719 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 241 | commands += command | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 242 | } | 
| 56771 
28d62a5b07e8
more systematic delay_first discipline for change_buffer and prune_history;
 wenzelm parents: 
56735diff
changeset | 243 | delay_flush.invoke() | 
| 56719 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 244 | } | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 245 | |
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 246 | def shutdown() | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 247 |     {
 | 
| 56771 
28d62a5b07e8
more systematic delay_first discipline for change_buffer and prune_history;
 wenzelm parents: 
56735diff
changeset | 248 | delay_flush.revoke() | 
| 56719 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 249 | flush() | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 250 | } | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 251 | } | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 252 | |
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 253 | |
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 254 | /* postponed changes */ | 
| 44733 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 255 | |
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 256 | private object postponed_changes | 
| 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 257 |   {
 | 
| 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 258 | private var postponed: List[Session.Change] = Nil | 
| 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 259 | |
| 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 260 |     def store(change: Session.Change): Unit = synchronized { postponed ::= change }
 | 
| 56705 
937826d702d5
simplified commands_changed_buffer (in contrast to a8331fb5c959): rely on better performance of Consumer_Thread/Mailbox and more direct Timer (like session_actor.receiver);
 wenzelm parents: 
56704diff
changeset | 261 | |
| 57976 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 262 |     def flush(state: Document.State): List[Session.Change] = synchronized {
 | 
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 263 | val (assigned, unassigned) = postponed.partition(change => state.is_assigned(change.previous)) | 
| 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 264 | postponed = unassigned | 
| 57976 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 265 | assigned.reverse | 
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 266 | } | 
| 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 267 | } | 
| 44733 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 268 | |
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 269 | |
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 270 | /* prover process */ | 
| 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 271 | |
| 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 272 | private object prover | 
| 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 273 |   {
 | 
| 61590 | 274 | private val variable = Synchronized[Option[Prover]](None) | 
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 275 | |
| 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 276 | def defined: Boolean = variable.value.isDefined | 
| 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 277 | def get: Prover = variable.value.get | 
| 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 278 |     def set(p: Prover) { variable.change(_ => Some(p)) }
 | 
| 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 279 |     def reset { variable.change(_ => None) }
 | 
| 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 280 |     def await_reset() { variable.guarded_access({ case None => Some((), None) case _ => None }) }
 | 
| 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 281 | } | 
| 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 282 | |
| 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 283 | |
| 59366 
e94df7f6b608
clarified build_theories: proper protocol handler;
 wenzelm parents: 
59364diff
changeset | 284 | /* protocol handlers */ | 
| 
e94df7f6b608
clarified build_theories: proper protocol handler;
 wenzelm parents: 
59364diff
changeset | 285 | |
| 65215 | 286 | private val protocol_handlers = Protocol_Handlers.init(session) | 
| 59366 
e94df7f6b608
clarified build_theories: proper protocol handler;
 wenzelm parents: 
59364diff
changeset | 287 | |
| 
e94df7f6b608
clarified build_theories: proper protocol handler;
 wenzelm parents: 
59364diff
changeset | 288 | def get_protocol_handler(name: String): Option[Session.Protocol_Handler] = | 
| 65214 | 289 | protocol_handlers.get(name) | 
| 59366 
e94df7f6b608
clarified build_theories: proper protocol handler;
 wenzelm parents: 
59364diff
changeset | 290 | |
| 59367 | 291 | def add_protocol_handler(handler: Session.Protocol_Handler): Unit = | 
| 65219 | 292 | protocol_handlers.add(handler) | 
| 65214 | 293 | |
| 294 | def add_protocol_handler(name: String): Unit = | |
| 65219 | 295 | protocol_handlers.add(name) | 
| 59366 
e94df7f6b608
clarified build_theories: proper protocol handler;
 wenzelm parents: 
59364diff
changeset | 296 | |
| 
e94df7f6b608
clarified build_theories: proper protocol handler;
 wenzelm parents: 
59364diff
changeset | 297 | |
| 65222 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 298 | /* debugger */ | 
| 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 299 | |
| 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 300 | private val debugger_handler = new Debugger.Handler(this) | 
| 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 301 | add_protocol_handler(debugger_handler) | 
| 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 302 | |
| 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 303 | def debugger: Debugger = debugger_handler.debugger | 
| 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 304 | |
| 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 305 | |
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 306 | /* manager thread */ | 
| 44733 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 307 | |
| 62115 
57895801cb57
prune old versions more often, to reduce overall heap requirements;
 wenzelm parents: 
62050diff
changeset | 308 | private val delay_prune = | 
| 
57895801cb57
prune old versions more often, to reduce overall heap requirements;
 wenzelm parents: 
62050diff
changeset | 309 |     Standard_Thread.delay_first(prune_delay) { manager.send(Prune_History) }
 | 
| 56771 
28d62a5b07e8
more systematic delay_first discipline for change_buffer and prune_history;
 wenzelm parents: 
56735diff
changeset | 310 | |
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 311 | private val manager: Consumer_Thread[Any] = | 
| 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 312 |   {
 | 
| 52649 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 313 | /* raw edits */ | 
| 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 314 | |
| 54521 
744ea0025e11
clarified Document.Blobs environment vs. actual edits of auxiliary files;
 wenzelm parents: 
54519diff
changeset | 315 | def handle_raw_edits(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text]) | 
| 52649 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 316 |     //{{{
 | 
| 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 317 |     {
 | 
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 318 | require(prover.defined) | 
| 56712 | 319 | |
| 52649 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 320 | prover.get.discontinue_execution() | 
| 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 321 | |
| 56687 | 322 | val previous = global_state.value.history.tip.version | 
| 52649 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 323 | val version = Future.promise[Document.Version] | 
| 56711 | 324 | global_state.change(_.continue_history(previous, edits, version)) | 
| 52649 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 325 | |
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 326 | raw_edits.post(Session.Raw_Edits(doc_blobs, edits)) | 
| 56704 | 327 | change_parser.send(Text_Edits(previous, doc_blobs, edits, version)) | 
| 52649 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 328 | } | 
| 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 329 | //}}} | 
| 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 330 | |
| 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 331 | |
| 43716 | 332 | /* resulting changes */ | 
| 34809 | 333 | |
| 56315 | 334 | def handle_change(change: Session.Change) | 
| 38221 | 335 |     //{{{
 | 
| 34809 | 336 |     {
 | 
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 337 | require(prover.defined) | 
| 56712 | 338 | |
| 44383 | 339 | def id_command(command: Command) | 
| 43720 | 340 |       {
 | 
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54443diff
changeset | 341 |         for {
 | 
| 57842 
8e4ae2db1849
more direct access to persistent blobs (see also 8953d4cc060a), avoiding fragile digest lookup from later version (which might have removed unused blobs already);
 wenzelm parents: 
57651diff
changeset | 342 | (name, digest) <- command.blobs_defined | 
| 56687 | 343 | if !global_state.value.defined_blob(digest) | 
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54443diff
changeset | 344 |         } {
 | 
| 57842 
8e4ae2db1849
more direct access to persistent blobs (see also 8953d4cc060a), avoiding fragile digest lookup from later version (which might have removed unused blobs already);
 wenzelm parents: 
57651diff
changeset | 345 |           change.version.nodes(name).get_blob match {
 | 
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54443diff
changeset | 346 | case Some(blob) => | 
| 56687 | 347 | global_state.change(_.define_blob(digest)) | 
| 56335 
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
 wenzelm parents: 
56316diff
changeset | 348 | prover.get.define_blob(digest, blob.bytes) | 
| 55783 | 349 | case None => | 
| 57842 
8e4ae2db1849
more direct access to persistent blobs (see also 8953d4cc060a), avoiding fragile digest lookup from later version (which might have removed unused blobs already);
 wenzelm parents: 
57651diff
changeset | 350 |               Output.error_message("Missing blob " + quote(name.toString))
 | 
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54443diff
changeset | 351 | } | 
| 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54443diff
changeset | 352 | } | 
| 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54443diff
changeset | 353 | |
| 56687 | 354 |         if (!global_state.value.defined_command(command.id)) {
 | 
| 355 | global_state.change(_.define_command(command)) | |
| 44644 
317e4962dd0f
clarified define_command: store name as structural information;
 wenzelm parents: 
44616diff
changeset | 356 | prover.get.define_command(command) | 
| 43720 | 357 | } | 
| 358 | } | |
| 56315 | 359 |       change.doc_edits foreach {
 | 
| 44383 | 360 | case (_, edit) => | 
| 361 |           edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command }
 | |
| 362 | } | |
| 43722 | 363 | |
| 56687 | 364 | val assignment = global_state.value.the_assignment(change.previous).check_finished | 
| 365 | global_state.change(_.define_version(change.version, assignment)) | |
| 56315 | 366 | prover.get.update(change.previous.id, change.version.id, change.doc_edits) | 
| 56316 
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
 wenzelm parents: 
56315diff
changeset | 367 | resources.commit(change) | 
| 34809 | 368 | } | 
| 38221 | 369 | //}}} | 
| 34809 | 370 | |
| 371 | ||
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 372 | /* prover output */ | 
| 34809 | 373 | |
| 56385 | 374 | def handle_output(output: Prover.Output) | 
| 38221 | 375 |     //{{{
 | 
| 34809 | 376 |     {
 | 
| 51662 
3391a493f39a
just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
 wenzelm parents: 
51083diff
changeset | 377 | def bad_output() | 
| 43716 | 378 |       {
 | 
| 379 | if (verbose) | |
| 56782 
433cf57550fa
more systematic Isabelle output, like in classic Isabelle/ML (without markup);
 wenzelm parents: 
56775diff
changeset | 380 |           Output.warning("Ignoring bad prover output: " + output.message.toString)
 | 
| 43716 | 381 | } | 
| 382 | ||
| 52531 | 383 | def accumulate(state_id: Document_ID.Generic, message: XML.Elem) | 
| 51662 
3391a493f39a
just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
 wenzelm parents: 
51083diff
changeset | 384 |       {
 | 
| 
3391a493f39a
just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
 wenzelm parents: 
51083diff
changeset | 385 |         try {
 | 
| 56687 | 386 | val st = global_state.change_result(_.accumulate(state_id, message)) | 
| 56719 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 387 | change_buffer.invoke(false, List(st.command)) | 
| 51662 
3391a493f39a
just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
 wenzelm parents: 
51083diff
changeset | 388 | } | 
| 
3391a493f39a
just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
 wenzelm parents: 
51083diff
changeset | 389 |         catch {
 | 
| 
3391a493f39a
just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
 wenzelm parents: 
51083diff
changeset | 390 | case _: Document.State.Fail => bad_output() | 
| 
3391a493f39a
just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
 wenzelm parents: 
51083diff
changeset | 391 | } | 
| 
3391a493f39a
just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
 wenzelm parents: 
51083diff
changeset | 392 | } | 
| 
3391a493f39a
just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
 wenzelm parents: 
51083diff
changeset | 393 | |
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 394 |       output match {
 | 
| 56385 | 395 | case msg: Prover.Protocol_Output => | 
| 65214 | 396 | val handled = protocol_handlers.invoke(msg) | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 397 |           if (!handled) {
 | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 398 |             msg.properties match {
 | 
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 399 | case Markup.Protocol_Handler(name) if prover.defined => | 
| 65214 | 400 | add_protocol_handler(name) | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 401 | |
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 402 | case Protocol.Command_Timing(state_id, timing) if prover.defined => | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 403 | val message = XML.elem(Markup.STATUS, List(XML.Elem(Markup.Timing(timing), Nil))) | 
| 65218 | 404 | accumulate(state_id, xml_cache.elem(message)) | 
| 46122 | 405 | |
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 406 | case Markup.Assign_Update => | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 407 |                 msg.text match {
 | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 408 | case Protocol.Assign_Update(id, update) => | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 409 |                     try {
 | 
| 56687 | 410 | val cmds = global_state.change_result(_.assign(id, update)) | 
| 56719 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 411 | change_buffer.invoke(true, cmds) | 
| 57976 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 412 | manager.send(Session.Change_Flush) | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 413 | } | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 414 |                     catch { case _: Document.State.Fail => bad_output() }
 | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 415 | case _ => bad_output() | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 416 | } | 
| 56771 
28d62a5b07e8
more systematic delay_first discipline for change_buffer and prune_history;
 wenzelm parents: 
56735diff
changeset | 417 | delay_prune.invoke() | 
| 46122 | 418 | |
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 419 | case Markup.Removed_Versions => | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 420 |                 msg.text match {
 | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 421 | case Protocol.Removed(removed) => | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 422 |                     try {
 | 
| 56687 | 423 | global_state.change(_.removed_versions(removed)) | 
| 57976 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 424 | manager.send(Session.Change_Flush) | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 425 | } | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 426 |                     catch { case _: Document.State.Fail => bad_output() }
 | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 427 | case _ => bad_output() | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 428 | } | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 429 | |
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 430 | case Markup.ML_Statistics(props) => | 
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 431 | statistics.post(Session.Statistics(props)) | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 432 | |
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 433 | case Markup.Task_Statistics(props) => | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 434 | // FIXME | 
| 52111 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 435 | |
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 436 | case _ => bad_output() | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 437 | } | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 438 | } | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 439 | case _ => | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 440 |           output.properties match {
 | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 441 | case Position.Id(state_id) => | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 442 | accumulate(state_id, output.message) | 
| 56210 | 443 | |
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 444 | case _ if output.is_init => | 
| 65264 
7e6ecd04b5fe
dynamic session_options for tuning parameters and initial prover options;
 wenzelm parents: 
65223diff
changeset | 445 | prover.get.options(session_options) | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 446 | phase = Session.Ready | 
| 65223 
844c067bc3d4
more robust startup, despite remaining race condition of debugger.is_active vs. session.is_ready;
 wenzelm parents: 
65222diff
changeset | 447 | debugger.ready() | 
| 56210 | 448 | |
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 449 | case Markup.Return_Code(rc) if output.is_exit => | 
| 65206 | 450 | phase = Session.Terminated(rc) | 
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 451 | prover.reset | 
| 56210 | 452 | |
| 61376 
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
 wenzelm parents: 
60934diff
changeset | 453 | case _ => | 
| 
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
 wenzelm parents: 
60934diff
changeset | 454 | raw_output_messages.post(output) | 
| 44661 
383c9d758a56
raw message function "assign_execs" avoids full overhead of decoding and caching message body;
 wenzelm parents: 
44644diff
changeset | 455 | } | 
| 52111 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 456 | } | 
| 34809 | 457 | } | 
| 38221 | 458 | //}}} | 
| 34809 | 459 | |
| 34820 
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
 wenzelm parents: 
34819diff
changeset | 460 | |
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 461 | /* main thread */ | 
| 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 462 | |
| 56710 
8f102c18eab7
more uniform warning/error handling, potentially with propagation to send_wait caller;
 wenzelm parents: 
56709diff
changeset | 463 |     Consumer_Thread.fork[Any]("Session.manager", daemon = true)
 | 
| 56709 | 464 |     {
 | 
| 465 | case arg: Any => | |
| 466 |         //{{{
 | |
| 467 |         arg match {
 | |
| 56733 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 468 | case output: Prover.Output => | 
| 56775 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 469 | if (output.is_stdout || output.is_stderr) | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 470 | raw_output_messages.post(output) | 
| 56733 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 471 | else handle_output(output) | 
| 56775 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 472 | |
| 56733 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 473 |             if (output.is_syslog) {
 | 
| 56775 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 474 | syslog += output.message | 
| 56733 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 475 | syslog_messages.post(output) | 
| 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 476 | } | 
| 56775 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 477 | |
| 56733 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 478 | all_messages.post(output) | 
| 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 479 | |
| 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 480 | case input: Prover.Input => | 
| 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 481 | all_messages.post(input) | 
| 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 482 | |
| 62556 
c115e69f457f
more abstract Session.start, without prover command-line;
 wenzelm parents: 
62545diff
changeset | 483 | case Start(start_prover) if !prover.defined => | 
| 65207 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 484 | prover.set(start_prover(manager.send(_))) | 
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 485 | |
| 56709 | 486 | case Stop => | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 487 | delay_prune.revoke() | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 488 |             if (prover.defined) {
 | 
| 65219 | 489 | protocol_handlers.exit() | 
| 56772 | 490 | global_state.change(_ => Document.State.init) | 
| 56709 | 491 | prover.get.terminate | 
| 492 | } | |
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 493 | |
| 56771 
28d62a5b07e8
more systematic delay_first discipline for change_buffer and prune_history;
 wenzelm parents: 
56735diff
changeset | 494 | case Prune_History => | 
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 495 |             if (prover.defined) {
 | 
| 57976 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 496 | val old_versions = global_state.change_result(_.remove_versions(prune_size)) | 
| 59319 | 497 | if (old_versions.nonEmpty) prover.get.remove_versions(old_versions) | 
| 56771 
28d62a5b07e8
more systematic delay_first discipline for change_buffer and prune_history;
 wenzelm parents: 
56735diff
changeset | 498 | } | 
| 
28d62a5b07e8
more systematic delay_first discipline for change_buffer and prune_history;
 wenzelm parents: 
56735diff
changeset | 499 | |
| 56709 | 500 | case Update_Options(options) => | 
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 501 |             if (prover.defined && is_ready) {
 | 
| 56709 | 502 | prover.get.options(options) | 
| 503 | handle_raw_edits(Document.Blobs.empty, Nil) | |
| 504 | } | |
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 505 | global_options.post(Session.Global_Options(options)) | 
| 34809 | 506 | |
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 507 | case Cancel_Exec(exec_id) if prover.defined => | 
| 56709 | 508 | prover.get.cancel_exec(exec_id) | 
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 509 | |
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 510 | case Session.Raw_Edits(doc_blobs, edits) if prover.defined => | 
| 56709 | 511 | handle_raw_edits(doc_blobs, edits) | 
| 512 | ||
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 513 | case Session.Dialog_Result(id, serial, result) if prover.defined => | 
| 56709 | 514 | prover.get.dialog_result(serial, result) | 
| 515 | handle_output(new Prover.Output(Protocol.Dialog_Result(id, serial, result))) | |
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 516 | |
| 59364 | 517 | case Session.Build_Theories(id, master_dir, theories) if prover.defined => | 
| 518 | prover.get.build_theories(id, master_dir, theories) | |
| 59362 | 519 | |
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 520 | case Protocol_Command(name, args) if prover.defined => | 
| 56709 | 521 | prover.get.protocol_command(name, args:_*) | 
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 522 | |
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 523 | case change: Session.Change if prover.defined => | 
| 57976 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 524 | val state = global_state.value | 
| 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 525 | if (!state.removing_versions && state.is_assigned(change.previous)) | 
| 56709 | 526 | handle_change(change) | 
| 527 | else postponed_changes.store(change) | |
| 57651 
10df45dd14da
less warnings -- ignore potential prover startup/shutdown races;
 wenzelm parents: 
56864diff
changeset | 528 | |
| 57976 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 529 | case Session.Change_Flush if prover.defined => | 
| 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 530 | val state = global_state.value | 
| 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 531 | if (!state.removing_versions) | 
| 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 532 | postponed_changes.flush(state).foreach(handle_change(_)) | 
| 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57867diff
changeset | 533 | |
| 57651 
10df45dd14da
less warnings -- ignore potential prover startup/shutdown races;
 wenzelm parents: 
56864diff
changeset | 534 | case bad => | 
| 
10df45dd14da
less warnings -- ignore potential prover startup/shutdown races;
 wenzelm parents: 
56864diff
changeset | 535 |             if (verbose) Output.warning("Ignoring bad message: " + bad.toString)
 | 
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 536 | } | 
| 56709 | 537 | true | 
| 538 | //}}} | |
| 539 | } | |
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 540 | } | 
| 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 541 | |
| 34809 | 542 | |
| 56799 | 543 | /* main operations */ | 
| 544 | ||
| 545 | def snapshot(name: Document.Node.Name = Document.Node.Name.empty, | |
| 546 | pending_edits: List[Text.Edit] = Nil): Document.Snapshot = | |
| 547 | global_state.value.snapshot(name, pending_edits) | |
| 34809 | 548 | |
| 62556 
c115e69f457f
more abstract Session.start, without prover command-line;
 wenzelm parents: 
62545diff
changeset | 549 | def start(start_prover: Prover.Receiver => Prover) | 
| 65207 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 550 |   {
 | 
| 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 551 | _phase.change( | 
| 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 552 |       {
 | 
| 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 553 | case Session.Inactive => | 
| 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 554 | manager.send(Start(start_prover)) | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 555 | post_phase(Session.Startup) | 
| 65207 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 556 |         case phase => error("Cannot start prover in phase " + quote(phase.print))
 | 
| 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 557 | }) | 
| 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 558 | } | 
| 45076 | 559 | |
| 65311 | 560 | def send_stop() | 
| 50117 | 561 |   {
 | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 562 | val was_ready = | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 563 | _phase.guarded_access(phase => | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 564 |         phase match {
 | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 565 | case Session.Startup | Session.Shutdown => None | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 566 | case Session.Terminated(_) => Some((false, phase)) | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 567 | case Session.Inactive => Some((false, post_phase(Session.Terminated(0)))) | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 568 | case Session.Ready => Some((true, post_phase(Session.Shutdown))) | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 569 | }) | 
| 65311 | 570 | if (was_ready) manager.send(Stop) | 
| 571 | } | |
| 572 | ||
| 573 | def stop(): Int = | |
| 574 |   {
 | |
| 575 | send_stop() | |
| 56793 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 576 | prover.await_reset() | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 577 | |
| 56704 | 578 | change_parser.shutdown() | 
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 579 | change_buffer.shutdown() | 
| 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 580 | manager.shutdown() | 
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 581 | dispatcher.shutdown() | 
| 65209 | 582 | |
| 583 |     phase match {
 | |
| 584 | case Session.Terminated(rc) => rc | |
| 585 |       case phase => error("Bad session phase after shutdown: " + quote(phase.print))
 | |
| 586 | } | |
| 50117 | 587 | } | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 588 | |
| 53054 
8365d7fca3de
public access for protocol handlers and protocol commands -- to be used within reason;
 wenzelm parents: 
52931diff
changeset | 589 | def protocol_command(name: String, args: String*) | 
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 590 |   { manager.send(Protocol_Command(name, args.toList)) }
 | 
| 53054 
8365d7fca3de
public access for protocol handlers and protocol commands -- to be used within reason;
 wenzelm parents: 
52931diff
changeset | 591 | |
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 592 | def cancel_exec(exec_id: Document_ID.Exec) | 
| 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 593 |   { manager.send(Cancel_Exec(exec_id)) }
 | 
| 52931 
ac6648c0c0fb
cancel_query via direct access to the exec_id of the running query process;
 wenzelm parents: 
52800diff
changeset | 594 | |
| 54521 
744ea0025e11
clarified Document.Blobs environment vs. actual edits of auxiliary files;
 wenzelm parents: 
54519diff
changeset | 595 | def update(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text]) | 
| 59319 | 596 |   { if (edits.nonEmpty) manager.send_wait(Session.Raw_Edits(doc_blobs, edits)) }
 | 
| 50498 | 597 | |
| 52084 
573e80625c78
more explicit Session.update_options as source of Global_Options event;
 wenzelm parents: 
51818diff
changeset | 598 | def update_options(options: Options) | 
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 599 |   { manager.send_wait(Update_Options(options)) }
 | 
| 52084 
573e80625c78
more explicit Session.update_options as source of Global_Options event;
 wenzelm parents: 
51818diff
changeset | 600 | |
| 52531 | 601 | def dialog_result(id: Document_ID.Generic, serial: Long, result: String) | 
| 56706 
f2f53f7046f4
converted main session manager to Consumer_Thread: messages need to be consumed immediately, postponed_changes replaces implicit actor mailbox scanning;
 wenzelm parents: 
56705diff
changeset | 602 |   { manager.send(Session.Dialog_Result(id, serial, result)) }
 | 
| 59362 | 603 | |
| 59364 | 604 | def build_theories(id: String, master_dir: Path, theories: List[(Options, List[Path])]) | 
| 605 |   { manager.send(Session.Build_Theories(id, master_dir, theories)) }
 | |
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 606 | } |