| author | wenzelm | 
| Sun, 24 Aug 2025 20:26:02 +0200 | |
| changeset 83053 | c1ccd17fb70f | 
| parent 82956 | e5fa061b9570 | 
| child 83198 | 7f46426e69ab | 
| 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 | 
| 70664 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 12 | import scala.collection.mutable | 
| 69640 
af09cc4792dc
more robust: no assumptions about GUI thread or document model;
 wenzelm parents: 
69492diff
changeset | 13 | import scala.annotation.tailrec | 
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 14 | |
| 82793 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 15 | import java.util.{Collections, WeakHashMap, Map => JMap}
 | 
| 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 16 | import java.lang.ref.WeakReference | 
| 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 17 | |
| 34815 
6bae73cd8e33
unified Command and Command_State, eliminated separate Accumulator;
 wenzelm parents: 
34813diff
changeset | 18 | |
| 75393 | 19 | object Session {
 | 
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 20 | /* outlets */ | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 21 | |
| 75393 | 22 |   object Consumer {
 | 
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 23 | 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 | 24 | new Consumer[A](name, consume) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 25 | } | 
| 80301 
f5055150b70b
clarified output, following Consumer_Thread.failure;
 wenzelm parents: 
80300diff
changeset | 26 |   final class Consumer[-A] private(val name: String, val consume: A => Unit) {
 | 
| 80302 | 27 | private def failure(exn: Throwable): Unit = | 
| 80301 
f5055150b70b
clarified output, following Consumer_Thread.failure;
 wenzelm parents: 
80300diff
changeset | 28 | Output.error_message( | 
| 
f5055150b70b
clarified output, following Consumer_Thread.failure;
 wenzelm parents: 
80300diff
changeset | 29 | "Session consumer failure: " + quote(name) + "\n" + Exn.print(exn)) | 
| 80302 | 30 | |
| 31 | def consume_robust(a: A): Unit = | |
| 32 |       try { consume(a) }
 | |
| 33 |       catch { case exn: Throwable => failure(exn) }
 | |
| 80301 
f5055150b70b
clarified output, following Consumer_Thread.failure;
 wenzelm parents: 
80300diff
changeset | 34 | } | 
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 35 | |
| 75393 | 36 |   class Outlet[A](dispatcher: Consumer_Thread[() => Unit]) {
 | 
| 61590 | 37 | private val consumers = Synchronized[List[Consumer[A]]](Nil) | 
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 38 | |
| 73340 | 39 | def += (c: Consumer[A]): Unit = consumers.change(Library.update(c)) | 
| 40 | def -= (c: Consumer[A]): Unit = consumers.change(Library.remove(c)) | |
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 41 | |
| 75393 | 42 |     def post(a: A): Unit = {
 | 
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 43 |       for (c <- consumers.value.iterator) {
 | 
| 80302 | 44 | dispatcher.send(() => c.consume_robust(a)) | 
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 45 | } | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 46 | } | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 47 | } | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 48 | |
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 49 | |
| 56315 | 50 | /* change */ | 
| 51 | ||
| 52 | sealed case class Change( | |
| 53 | previous: Document.Version, | |
| 59077 
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
 wenzelm parents: 
58928diff
changeset | 54 | 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 | 55 | deps_changed: Boolean, | 
| 56315 | 56 | doc_edits: List[Document.Edit_Command], | 
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 57 | consolidate: List[Document.Node.Name], | 
| 56315 | 58 | version: Document.Version) | 
| 59 | ||
| 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 | 60 | 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 | 61 | |
| 56315 | 62 | |
| 34813 
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
 wenzelm parents: 
34810diff
changeset | 63 | /* events */ | 
| 
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
 wenzelm parents: 
34810diff
changeset | 64 | |
| 43716 | 65 |   //{{{
 | 
| 71652 | 66 | case class Command_Timing(props: Properties.T) | 
| 67 | case class Theory_Timing(props: Properties.T) | |
| 68 | case class Runtime_Statistics(props: Properties.T) | |
| 69 | case class Task_Statistics(props: Properties.T) | |
| 50117 | 70 | case class Global_Options(options: Options) | 
| 44805 | 71 | case object Caret_Focus | 
| 70796 
2739631ac368
discontinued pointless dump_checkpoint and share_common_data -- superseded by base logic image in Isabelle/MMT;
 wenzelm parents: 
70778diff
changeset | 72 | case class Raw_Edits(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text]) | 
| 52531 | 73 | case class Dialog_Result(id: Document_ID.Generic, serial: Long, result: String) | 
| 47027 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 74 | case class Commands_Changed( | 
| 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 75 | assignment: Boolean, nodes: Set[Document.Node.Name], commands: Set[Command]) | 
| 39630 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 76 | |
| 75393 | 77 |   sealed abstract class Phase {
 | 
| 65206 | 78 | def print: String = | 
| 79 |       this match {
 | |
| 65317 | 80 | case Terminated(result) => if (result.ok) "finished" else "failed" | 
| 65206 | 81 | case _ => Word.lowercase(this.toString) | 
| 82 | } | |
| 83 | } | |
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 84 | case object Inactive extends Phase // stable | 
| 39701 | 85 | case object Startup extends Phase // transient | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 86 | case object Ready extends Phase // metastable | 
| 39701 | 87 | case object Shutdown extends Phase // transient | 
| 65317 | 88 | case class Terminated(result: Process_Result) extends Phase // stable | 
| 43716 | 89 | //}}} | 
| 52111 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 90 | |
| 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 91 | |
| 56775 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 92 | /* syslog */ | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 93 | |
| 76488 | 94 |   class Syslog(limit: Int) {
 | 
| 95 | private var queue = Queue.empty[String] | |
| 56775 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 96 | 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 | 97 | |
| 76488 | 98 |     def += (msg: String): Unit = synchronized {
 | 
| 56775 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 99 | 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 | 100 | length += 1 | 
| 
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) 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 | 102 | } | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 103 | |
| 76488 | 104 |     def content(): String = synchronized {
 | 
| 105 | cat_lines(queue.iterator) + | |
| 56775 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 106 | (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 | 107 | } | 
| 76488 | 108 | |
| 109 |     override def toString: String = "Syslog(" + length + ")"
 | |
| 56775 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 110 | } | 
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 111 | |
| 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 112 | |
| 52111 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 113 | /* protocol handlers */ | 
| 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 114 | |
| 71970 | 115 | type Protocol_Function = Prover.Protocol_Output => Boolean | 
| 75440 | 116 | type Protocol_Functions = List[(String, Protocol_Function)] | 
| 71970 | 117 | |
| 75393 | 118 |   abstract class Protocol_Handler extends Isabelle_System.Service {
 | 
| 65219 | 119 |     def init(session: Session): Unit = {}
 | 
| 120 |     def exit(): Unit = {}
 | |
| 75440 | 121 | def functions: Protocol_Functions = Nil | 
| 72216 | 122 | def prover_options(options: Options): Options = options | 
| 52111 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 123 | } | 
| 82784 
0751d363fd0e
clarified signature: avoid Session with accidental Resources.bootstrap, which is mostly undefined;
 wenzelm parents: 
82783diff
changeset | 124 | |
| 
0751d363fd0e
clarified signature: avoid Session with accidental Resources.bootstrap, which is mostly undefined;
 wenzelm parents: 
82783diff
changeset | 125 | |
| 
0751d363fd0e
clarified signature: avoid Session with accidental Resources.bootstrap, which is mostly undefined;
 wenzelm parents: 
82783diff
changeset | 126 | /* bootstrap session */ | 
| 
0751d363fd0e
clarified signature: avoid Session with accidental Resources.bootstrap, which is mostly undefined;
 wenzelm parents: 
82783diff
changeset | 127 | |
| 
0751d363fd0e
clarified signature: avoid Session with accidental Resources.bootstrap, which is mostly undefined;
 wenzelm parents: 
82783diff
changeset | 128 | def bootstrap(options: Options): Session = | 
| 
0751d363fd0e
clarified signature: avoid Session with accidental Resources.bootstrap, which is mostly undefined;
 wenzelm parents: 
82783diff
changeset | 129 |     new Session {
 | 
| 
0751d363fd0e
clarified signature: avoid Session with accidental Resources.bootstrap, which is mostly undefined;
 wenzelm parents: 
82783diff
changeset | 130 | override def session_options: Options = options | 
| 
0751d363fd0e
clarified signature: avoid Session with accidental Resources.bootstrap, which is mostly undefined;
 wenzelm parents: 
82783diff
changeset | 131 | override def resources: Resources = Resources.bootstrap | 
| 
0751d363fd0e
clarified signature: avoid Session with accidental Resources.bootstrap, which is mostly undefined;
 wenzelm parents: 
82783diff
changeset | 132 | } | 
| 82956 
e5fa061b9570
more accurate treatment unicode_symbols (for main theory file);
 wenzelm parents: 
82955diff
changeset | 133 | |
| 
e5fa061b9570
more accurate treatment unicode_symbols (for main theory file);
 wenzelm parents: 
82955diff
changeset | 134 | |
| 
e5fa061b9570
more accurate treatment unicode_symbols (for main theory file);
 wenzelm parents: 
82955diff
changeset | 135 | /* read_theory cache */ | 
| 
e5fa061b9570
more accurate treatment unicode_symbols (for main theory file);
 wenzelm parents: 
82955diff
changeset | 136 | |
| 
e5fa061b9570
more accurate treatment unicode_symbols (for main theory file);
 wenzelm parents: 
82955diff
changeset | 137 | sealed case class Read_Theory_Key(name: String, unicode_symbols: Boolean) | 
| 34791 | 138 | } | 
| 139 | ||
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 140 | |
| 82780 | 141 | abstract class Session extends Document.Session {
 | 
| 65214 | 142 | session => | 
| 143 | ||
| 82940 | 144 | override def toString: String = resources.session_base.session_name | 
| 145 | ||
| 82780 | 146 | def session_options: Options | 
| 82784 
0751d363fd0e
clarified signature: avoid Session with accidental Resources.bootstrap, which is mostly undefined;
 wenzelm parents: 
82783diff
changeset | 147 | def resources: Resources | 
| 82744 
0ca8b1861fa3
clarified signature: more explicit subtypes of Session, with corresponding subtypes of Resources;
 wenzelm parents: 
82742diff
changeset | 148 | |
| 82780 | 149 | val store: Store = Store(session_options) | 
| 82769 
7cb5ef6da1f0
proper build_context.store, instead of circular null value (amending 0e36478a1b6a and e891ff63e6db);
 wenzelm parents: 
82765diff
changeset | 150 | def cache: Rich_Text.Cache = store.cache | 
| 65218 | 151 | |
| 76914 | 152 | def build_blobs_info(name: Document.Node.Name): Command.Blobs_Info = Command.Blobs_Info.empty | 
| 76912 
ca872f20cf5b
clarified session sources: theory and blobs are read from database, instead of physical file-system;
 wenzelm parents: 
76851diff
changeset | 153 | def build_blobs(name: Document.Node.Name): Document.Blobs = Document.Blobs.empty | 
| 72816 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72721diff
changeset | 154 | |
| 65214 | 155 | |
| 82756 
c3c8e84f63c6
clarified signature: general Session.open_session_context;
 wenzelm parents: 
82750diff
changeset | 156 | /* session exports */ | 
| 
c3c8e84f63c6
clarified signature: general Session.open_session_context;
 wenzelm parents: 
82750diff
changeset | 157 | |
| 
c3c8e84f63c6
clarified signature: general Session.open_session_context;
 wenzelm parents: 
82750diff
changeset | 158 | def open_session_context( | 
| 
c3c8e84f63c6
clarified signature: general Session.open_session_context;
 wenzelm parents: 
82750diff
changeset | 159 | document_snapshot: Option[Document.Snapshot] = None | 
| 
c3c8e84f63c6
clarified signature: general Session.open_session_context;
 wenzelm parents: 
82750diff
changeset | 160 |   ): Export.Session_Context = {
 | 
| 
c3c8e84f63c6
clarified signature: general Session.open_session_context;
 wenzelm parents: 
82750diff
changeset | 161 | Export.open_session_context( | 
| 
c3c8e84f63c6
clarified signature: general Session.open_session_context;
 wenzelm parents: 
82750diff
changeset | 162 | store, resources.session_background, document_snapshot = document_snapshot) | 
| 
c3c8e84f63c6
clarified signature: general Session.open_session_context;
 wenzelm parents: 
82750diff
changeset | 163 | } | 
| 
c3c8e84f63c6
clarified signature: general Session.open_session_context;
 wenzelm parents: 
82750diff
changeset | 164 | |
| 82956 
e5fa061b9570
more accurate treatment unicode_symbols (for main theory file);
 wenzelm parents: 
82955diff
changeset | 165 | private val read_theory_cache = | 
| 
e5fa061b9570
more accurate treatment unicode_symbols (for main theory file);
 wenzelm parents: 
82955diff
changeset | 166 | new WeakHashMap[Session.Read_Theory_Key, WeakReference[Document.Snapshot]] | 
| 82793 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 167 | |
| 82956 
e5fa061b9570
more accurate treatment unicode_symbols (for main theory file);
 wenzelm parents: 
82955diff
changeset | 168 | def read_theory(name: String, unicode_symbols: Boolean = false): Document.Snapshot = | 
| 82793 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 169 |     read_theory_cache.synchronized {
 | 
| 82956 
e5fa061b9570
more accurate treatment unicode_symbols (for main theory file);
 wenzelm parents: 
82955diff
changeset | 170 | val key = Session.Read_Theory_Key(name, unicode_symbols) | 
| 
e5fa061b9570
more accurate treatment unicode_symbols (for main theory file);
 wenzelm parents: 
82955diff
changeset | 171 |       Option(read_theory_cache.get(key)).map(_.get) match {
 | 
| 82955 
7185406956cd
proper comparison of actual source (amending fe8598c92be7), e.g. relevant for "File / Reload with Encoding / UTF-8" in Isabelle/jEdit;
 wenzelm parents: 
82940diff
changeset | 172 | case Some(snapshot: Document.Snapshot) => snapshot | 
| 82793 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 173 | case _ => | 
| 82955 
7185406956cd
proper comparison of actual source (amending fe8598c92be7), e.g. relevant for "File / Reload with Encoding / UTF-8" in Isabelle/jEdit;
 wenzelm parents: 
82940diff
changeset | 174 | val maybe_snapshot = | 
| 82793 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 175 |             using(open_session_context()) { session_context =>
 | 
| 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 176 | Build.read_theory(session_context.theory(name), | 
| 82956 
e5fa061b9570
more accurate treatment unicode_symbols (for main theory file);
 wenzelm parents: 
82955diff
changeset | 177 | unicode_symbols = unicode_symbols, | 
| 82793 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 178 |                 migrate_file = (a: String) => session.resources.append_path("", Path.explode(a)))
 | 
| 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 179 | } | 
| 82955 
7185406956cd
proper comparison of actual source (amending fe8598c92be7), e.g. relevant for "File / Reload with Encoding / UTF-8" in Isabelle/jEdit;
 wenzelm parents: 
82940diff
changeset | 180 |           maybe_snapshot.map(_.snippet_commands) match {
 | 
| 
7185406956cd
proper comparison of actual source (amending fe8598c92be7), e.g. relevant for "File / Reload with Encoding / UTF-8" in Isabelle/jEdit;
 wenzelm parents: 
82940diff
changeset | 181 | case Some(List(_)) => | 
| 82956 
e5fa061b9570
more accurate treatment unicode_symbols (for main theory file);
 wenzelm parents: 
82955diff
changeset | 182 | read_theory_cache.put(key, new WeakReference(maybe_snapshot.get)) | 
| 82955 
7185406956cd
proper comparison of actual source (amending fe8598c92be7), e.g. relevant for "File / Reload with Encoding / UTF-8" in Isabelle/jEdit;
 wenzelm parents: 
82940diff
changeset | 183 | maybe_snapshot.get | 
| 82793 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 184 |             case _ => error("Failed to load theory " + quote(name) + " from session database")
 | 
| 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 185 | } | 
| 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 186 | } | 
| 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 187 | } | 
| 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 188 | |
| 82756 
c3c8e84f63c6
clarified signature: general Session.open_session_context;
 wenzelm parents: 
82750diff
changeset | 189 | |
| 47653 | 190 | /* global flags */ | 
| 191 | ||
| 192 | @volatile var timing: Boolean = false | |
| 193 | @volatile var verbose: Boolean = false | |
| 194 | ||
| 195 | ||
| 65264 
7e6ecd04b5fe
dynamic session_options for tuning parameters and initial prover options;
 wenzelm parents: 
65223diff
changeset | 196 | /* dynamic session options */ | 
| 49288 | 197 | |
| 76610 | 198 |   def load_delay: Time = session_options.seconds("editor_load_delay")
 | 
| 199 |   def input_delay: Time = session_options.seconds("editor_input_delay")
 | |
| 200 |   def generated_input_delay: Time = session_options.seconds("editor_generated_input_delay")
 | |
| 65264 
7e6ecd04b5fe
dynamic session_options for tuning parameters and initial prover options;
 wenzelm parents: 
65223diff
changeset | 201 |   def output_delay: Time = session_options.seconds("editor_output_delay")
 | 
| 66379 
6392766f3c25
maintain "consolidated" status of theory nodes, which means all evals are finished (but not necessarily prints nor imports);
 wenzelm parents: 
66094diff
changeset | 202 |   def consolidate_delay: Time = session_options.seconds("editor_consolidate_delay")
 | 
| 65264 
7e6ecd04b5fe
dynamic session_options for tuning parameters and initial prover options;
 wenzelm parents: 
65223diff
changeset | 203 |   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 | 204 |   def prune_size: Int = session_options.int("editor_prune_size")
 | 
| 76610 | 205 |   def update_delay: Time = session_options.seconds("editor_update_delay")
 | 
| 77149 
3991a35cd740
automatically build document when selected theories are finished;
 wenzelm parents: 
76914diff
changeset | 206 |   def document_delay: Time = session_options.seconds("editor_document_delay")
 | 
| 76610 | 207 |   def chart_delay: Time = session_options.seconds("editor_chart_delay")
 | 
| 65264 
7e6ecd04b5fe
dynamic session_options for tuning parameters and initial prover options;
 wenzelm parents: 
65223diff
changeset | 208 |   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 | 209 |   def reparse_limit: Int = session_options.int("editor_reparse_limit")
 | 
| 37849 | 210 | |
| 211 | ||
| 66094 | 212 | /* dispatcher */ | 
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 213 | |
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 214 | private val dispatcher = | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 215 |     Consumer_Thread.fork[() => Unit]("Session.dispatcher", daemon = true) { case e => e(); true }
 | 
| 34809 | 216 | |
| 75393 | 217 |   def assert_dispatcher[A](body: => A): A = {
 | 
| 74254 | 218 | assert(dispatcher.check_thread()) | 
| 66094 | 219 | body | 
| 220 | } | |
| 221 | ||
| 75393 | 222 |   def require_dispatcher[A](body: => A): A = {
 | 
| 74254 | 223 | require(dispatcher.check_thread(), "not on dispatcher thread") | 
| 66094 | 224 | body | 
| 225 | } | |
| 226 | ||
| 75393 | 227 |   def send_dispatcher(body: => Unit): Unit = {
 | 
| 74254 | 228 | if (dispatcher.check_thread()) body | 
| 66094 | 229 | else dispatcher.send(() => body) | 
| 230 | } | |
| 231 | ||
| 75393 | 232 |   def send_wait_dispatcher(body: => Unit): Unit = {
 | 
| 74254 | 233 | if (dispatcher.check_thread()) body | 
| 66094 | 234 | else dispatcher.send_wait(() => body) | 
| 235 | } | |
| 236 | ||
| 237 | ||
| 238 | /* outlets */ | |
| 239 | ||
| 72721 
79f5e843e5ec
clarified signature: prefer high-level Snapshot over low-level Command.State;
 wenzelm parents: 
72692diff
changeset | 240 | val finished_theories = new Session.Outlet[Document.Snapshot](dispatcher) | 
| 71652 | 241 | val command_timings = new Session.Outlet[Session.Command_Timing](dispatcher) | 
| 242 | val theory_timings = new Session.Outlet[Session.Theory_Timing](dispatcher) | |
| 243 | val runtime_statistics = new Session.Outlet[Session.Runtime_Statistics](dispatcher) | |
| 244 | val task_statistics = new Session.Outlet[Session.Task_Statistics](dispatcher) | |
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 245 | 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 | 246 | 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 | 247 | 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 | 248 | 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 | 249 | val phase_changed = new Session.Outlet[Session.Phase](dispatcher) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 250 | val syslog_messages = new Session.Outlet[Prover.Output](dispatcher) | 
| 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 251 | 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 | 252 | val trace_events = new Session.Outlet[Simplifier_Trace.Event.type](dispatcher) | 
| 60900 | 253 | val debugger_updates = new Session.Outlet[Debugger.Update.type](dispatcher) | 
| 34809 | 254 | |
| 60749 | 255 | val all_messages = new Session.Outlet[Prover.Message](dispatcher) // potential bottle-neck! | 
| 34809 | 256 | |
| 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 | 257 | |
| 56799 | 258 | /** 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 | 259 | |
| 56799 | 260 | /* 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 | 261 | |
| 62556 
c115e69f457f
more abstract Session.start, without prover command-line;
 wenzelm parents: 
62545diff
changeset | 262 | private case class Start(start_prover: Prover.Receiver => Prover) | 
| 56799 | 263 | private case object Stop | 
| 70775 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 264 | private case class Get_State(promise: Promise[Document.State]) | 
| 56799 | 265 | private case class Cancel_Exec(exec_id: Document_ID.Exec) | 
| 73565 | 266 | private case class Protocol_Command_Raw(name: String, args: List[Bytes]) | 
| 80462 
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
 wenzelm parents: 
80302diff
changeset | 267 | private case class Protocol_Command_Args(name: String, args: List[XML.Body]) | 
| 56799 | 268 | private case class Update_Options(options: Options) | 
| 66379 
6392766f3c25
maintain "consolidated" status of theory nodes, which means all evals are finished (but not necessarily prints nor imports);
 wenzelm parents: 
66094diff
changeset | 269 | private case object Consolidate_Execution | 
| 56799 | 270 | 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 | 271 | |
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 272 | |
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 273 | /* phase */ | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 274 | |
| 75393 | 275 |   private def post_phase(new_phase: Session.Phase): Session.Phase = {
 | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 276 | phase_changed.post(new_phase) | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 277 | new_phase | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 278 | } | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 279 | private val _phase = Synchronized[Session.Phase](Session.Inactive) | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 280 | 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 | 281 | |
| 71601 | 282 | def phase: Session.Phase = _phase.value | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 283 | def is_ready: Boolean = phase == Session.Ready | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 284 | |
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 285 | |
| 70775 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 286 | /* syslog */ | 
| 43644 | 287 | |
| 76488 | 288 | def make_syslog(): Session.Syslog = new Session.Syslog(syslog_limit) | 
| 289 | ||
| 290 | val syslog: Session.Syslog = make_syslog() | |
| 39626 
a5d0bcfb95a3
manage persistent syslog via Session, not Isabelle_Process;
 wenzelm parents: 
39625diff
changeset | 291 | |
| 43651 
511df47bcadc
some support for theory files within Isabelle/Scala session;
 wenzelm parents: 
43649diff
changeset | 292 | |
| 56799 | 293 | /* pipelined change parsing */ | 
| 294 | ||
| 295 | private case class Text_Edits( | |
| 296 | previous: Future[Document.Version], | |
| 297 | doc_blobs: Document.Blobs, | |
| 298 | text_edits: List[Document.Edit_Text], | |
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 299 | consolidate: List[Document.Node.Name], | 
| 56799 | 300 | version_result: Promise[Document.Version]) | 
| 43651 
511df47bcadc
some support for theory files within Isabelle/Scala session;
 wenzelm parents: 
43649diff
changeset | 301 | |
| 75393 | 302 |   private val change_parser = Consumer_Thread.fork[Text_Edits]("change_parser", daemon = true) {
 | 
| 70796 
2739631ac368
discontinued pointless dump_checkpoint and share_common_data -- superseded by base logic image in Isabelle/MMT;
 wenzelm parents: 
70778diff
changeset | 303 | case Text_Edits(previous, doc_blobs, text_edits, consolidate, version_result) => | 
| 56799 | 304 | val prev = previous.get_finished | 
| 305 | val change = | |
| 82789 | 306 | Timing.timeit(Thy_Syntax.parse_change(session, prev, doc_blobs, text_edits, consolidate), | 
| 76593 
badb5264f7b9
clarified signature: just one level of arguments to avoid type-inference problems;
 wenzelm parents: 
76590diff
changeset | 307 | message = _ => "parse_change", | 
| 
badb5264f7b9
clarified signature: just one level of arguments to avoid type-inference problems;
 wenzelm parents: 
76590diff
changeset | 308 | enabled = timing) | 
| 56799 | 309 | version_result.fulfill(change.version) | 
| 310 | manager.send(change) | |
| 311 | true | |
| 312 | } | |
| 41534 | 313 | |
| 82778 | 314 | def auto_resolve: Boolean = true | 
| 315 |   def deps_changed(): Unit = {}
 | |
| 316 | ||
| 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 | 317 | |
| 56719 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 318 | /* buffered changes */ | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 319 | |
| 75393 | 320 |   private object change_buffer {
 | 
| 56719 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 321 | 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 | 322 | 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 | 323 | 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 | 324 | |
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 325 |     def flush(): Unit = synchronized {
 | 
| 59319 | 326 | 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 | 327 | commands_changed.post(Session.Commands_Changed(assignment, nodes, commands)) | 
| 76322 | 328 | if (nodes.nonEmpty) consolidation.update(more_nodes = nodes) | 
| 56719 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 329 | assignment = false | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 330 | nodes = Set.empty | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 331 | commands = Set.empty | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 332 | } | 
| 71704 | 333 |     private val delay_flush = 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 | 334 | |
| 70284 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 335 | def invoke(assign: Boolean, edited_nodes: List[Document.Node.Name], cmds: List[Command]): Unit = | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 336 |       synchronized {
 | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 337 | assignment |= assign | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 338 |         for (node <- edited_nodes) {
 | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 339 | nodes += node | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 340 | } | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 341 |         for (command <- cmds) {
 | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 342 | nodes += command.node_name | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 343 | command.blobs_names.foreach(nodes += _) | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 344 | commands += command | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 345 | } | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 346 | 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 | 347 | } | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 348 | |
| 75393 | 349 |     def shutdown(): Unit = {
 | 
| 56771 
28d62a5b07e8
more systematic delay_first discipline for change_buffer and prune_history;
 wenzelm parents: 
56735diff
changeset | 350 | 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 | 351 | flush() | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 352 | } | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 353 | } | 
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 354 | |
| 
80eb2192516a
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
 wenzelm parents: 
56715diff
changeset | 355 | |
| 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 | 356 | /* 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 | 357 | |
| 75393 | 358 |   private object postponed_changes {
 | 
| 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 | 359 | 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 | 360 | |
| 
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 | 361 |     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 | 362 | |
| 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 | 363 |     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 | 364 | 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 | 365 | 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 | 366 | 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 | 367 | } | 
| 
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 | 368 | } | 
| 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 | 369 | |
| 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 | 370 | |
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 371 | /* node consolidation */ | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 372 | |
| 75393 | 373 |   private object consolidation {
 | 
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 374 | private val delay = | 
| 71704 | 375 |       Delay.first(consolidate_delay) { manager.send(Consolidate_Execution) }
 | 
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 376 | |
| 68807 
e28978310a2a
more robust exit: avoid later Consolidate_Execution with handle_raw_edits (cf. 2fd3a6d6ba2e);
 wenzelm parents: 
68382diff
changeset | 377 | private val init_state: Option[Set[Document.Node.Name]] = Some(Set.empty) | 
| 
e28978310a2a
more robust exit: avoid later Consolidate_Execution with handle_raw_edits (cf. 2fd3a6d6ba2e);
 wenzelm parents: 
68382diff
changeset | 378 | private val state = Synchronized(init_state) | 
| 
e28978310a2a
more robust exit: avoid later Consolidate_Execution with handle_raw_edits (cf. 2fd3a6d6ba2e);
 wenzelm parents: 
68382diff
changeset | 379 | |
| 75393 | 380 |     def exit(): Unit = {
 | 
| 68807 
e28978310a2a
more robust exit: avoid later Consolidate_Execution with handle_raw_edits (cf. 2fd3a6d6ba2e);
 wenzelm parents: 
68382diff
changeset | 381 | delay.revoke() | 
| 
e28978310a2a
more robust exit: avoid later Consolidate_Execution with handle_raw_edits (cf. 2fd3a6d6ba2e);
 wenzelm parents: 
68382diff
changeset | 382 | state.change(_ => None) | 
| 
e28978310a2a
more robust exit: avoid later Consolidate_Execution with handle_raw_edits (cf. 2fd3a6d6ba2e);
 wenzelm parents: 
68382diff
changeset | 383 | } | 
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 384 | |
| 76322 | 385 |     def update(more_nodes: Set[Document.Node.Name] = Set.empty): Unit = {
 | 
| 68807 
e28978310a2a
more robust exit: avoid later Consolidate_Execution with handle_raw_edits (cf. 2fd3a6d6ba2e);
 wenzelm parents: 
68382diff
changeset | 386 | val active = | 
| 
e28978310a2a
more robust exit: avoid later Consolidate_Execution with handle_raw_edits (cf. 2fd3a6d6ba2e);
 wenzelm parents: 
68382diff
changeset | 387 | state.change_result(st => | 
| 76322 | 388 | (st.isDefined, st.map(nodes => if (nodes.isEmpty) more_nodes else nodes ++ more_nodes))) | 
| 68807 
e28978310a2a
more robust exit: avoid later Consolidate_Execution with handle_raw_edits (cf. 2fd3a6d6ba2e);
 wenzelm parents: 
68382diff
changeset | 389 | if (active) delay.invoke() | 
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 390 | } | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 391 | |
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 392 | def flush(): Set[Document.Node.Name] = | 
| 68807 
e28978310a2a
more robust exit: avoid later Consolidate_Execution with handle_raw_edits (cf. 2fd3a6d6ba2e);
 wenzelm parents: 
68382diff
changeset | 393 | state.change_result(st => if (st.isDefined) (st.get, init_state) else (Set.empty, None)) | 
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 394 | } | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 395 | |
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 396 | |
| 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 | 397 | /* 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 | 398 | |
| 75393 | 399 |   private object prover {
 | 
| 61590 | 400 | 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 | 401 | |
| 
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 | 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 | 403 | def get: Prover = variable.value.get | 
| 73340 | 404 | def set(p: Prover): Unit = variable.change(_ => Some(p)) | 
| 76321 
3e1e2f9198bb
tuned signature, following hints by IntelliJ IDEA;
 wenzelm parents: 
76045diff
changeset | 405 | def reset(): Unit = variable.change(_ => None) | 
| 73340 | 406 |     def await_reset(): Unit = variable.guarded_access({ case None => Some((), None) case _ => 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 | 407 | } | 
| 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 408 | |
| 
d5ab6a8799ce
more synchronized treatment of prover process, which might emit more messages before shutdown and requires manager to accept them;
 wenzelm parents: 
56782diff
changeset | 409 | |
| 72216 | 410 | /* file formats and protocol handlers */ | 
| 69488 
b05c0bb47f6d
support for File_Format.Session, e.g. server process accessible via prover options;
 wenzelm parents: 
68807diff
changeset | 411 | |
| 72216 | 412 | private lazy val file_formats: File_Format.Session = | 
| 71733 | 413 | File_Format.registry.start_session(session) | 
| 69488 
b05c0bb47f6d
support for File_Format.Session, e.g. server process accessible via prover options;
 wenzelm parents: 
68807diff
changeset | 414 | |
| 65215 | 415 | private val protocol_handlers = Protocol_Handlers.init(session) | 
| 59366 
e94df7f6b608
clarified build_theories: proper protocol handler;
 wenzelm parents: 
59364diff
changeset | 416 | |
| 72215 | 417 | def get_protocol_handler[C <: Session.Protocol_Handler](c: Class[C]): Option[C] = | 
| 76788 | 418 | protocol_handlers.get(c.getName).flatMap(Library.as_subclass(c)) | 
| 59366 
e94df7f6b608
clarified build_theories: proper protocol handler;
 wenzelm parents: 
59364diff
changeset | 419 | |
| 65315 | 420 | def init_protocol_handler(handler: Session.Protocol_Handler): Unit = | 
| 421 | protocol_handlers.init(handler) | |
| 65214 | 422 | |
| 72216 | 423 | def prover_options(options: Options): Options = | 
| 424 | protocol_handlers.prover_options(file_formats.prover_options(options)) | |
| 425 | ||
| 59366 
e94df7f6b608
clarified build_theories: proper protocol handler;
 wenzelm parents: 
59364diff
changeset | 426 | |
| 65222 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 427 | /* debugger */ | 
| 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 428 | |
| 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 429 | private val debugger_handler = new Debugger.Handler(this) | 
| 65315 | 430 | init_protocol_handler(debugger_handler) | 
| 65222 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 431 | |
| 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 432 | 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 | 433 | |
| 
fb8253564483
more robust debugger initialization, e.g. required for GUI components before actual session startup;
 wenzelm parents: 
65221diff
changeset | 434 | |
| 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 | 435 | /* 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 | 436 | |
| 76407 | 437 | private lazy val delay_prune = | 
| 71704 | 438 |     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 | 439 | |
| 75393 | 440 |   private val manager: Consumer_Thread[Any] = {
 | 
| 70775 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 441 | /* global state */ | 
| 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 442 | val global_state = Synchronized(Document.State.init) | 
| 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 443 | |
| 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 444 | |
| 52649 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 445 | /* raw edits */ | 
| 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 446 | |
| 68336 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
68293diff
changeset | 447 | def handle_raw_edits( | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
68293diff
changeset | 448 | doc_blobs: Document.Blobs = Document.Blobs.empty, | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
68293diff
changeset | 449 | edits: List[Document.Edit_Text] = Nil, | 
| 75393 | 450 |       consolidate: List[Document.Node.Name] = Nil): Unit = {
 | 
| 52649 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 451 |     //{{{
 | 
| 73120 
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
 wenzelm parents: 
73031diff
changeset | 452 | require(prover.defined, "prover process not defined (handle_raw_edits)") | 
| 56712 | 453 | |
| 70778 
f326596f5752
consolidate less aggressively: avoid live-lock when PIDE round-trip takes too long (e.g. in complex theory hierarchies);
 wenzelm parents: 
70776diff
changeset | 454 | if (edits.nonEmpty) prover.get.discontinue_execution() | 
| 52649 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 455 | |
| 56687 | 456 | 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 | 457 | val version = Future.promise[Document.Version] | 
| 56711 | 458 | 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 | 459 | |
| 70796 
2739631ac368
discontinued pointless dump_checkpoint and share_common_data -- superseded by base logic image in Isabelle/MMT;
 wenzelm parents: 
70778diff
changeset | 460 | raw_edits.post(Session.Raw_Edits(doc_blobs, edits)) | 
| 
2739631ac368
discontinued pointless dump_checkpoint and share_common_data -- superseded by base logic image in Isabelle/MMT;
 wenzelm parents: 
70778diff
changeset | 461 | change_parser.send(Text_Edits(previous, doc_blobs, edits, consolidate, version)) | 
| 75393 | 462 | //}}} | 
| 52649 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 463 | } | 
| 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 464 | |
| 
f45ab3e8211b
update_options with full update, e.g. required for re-assignment of Command.prints;
 wenzelm parents: 
52563diff
changeset | 465 | |
| 43716 | 466 | /* resulting changes */ | 
| 34809 | 467 | |
| 75393 | 468 |     def handle_change(change: Session.Change): Unit = {
 | 
| 38221 | 469 |     //{{{
 | 
| 73120 
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
 wenzelm parents: 
73031diff
changeset | 470 | require(prover.defined, "prover process not defined (handle_change)") | 
| 56712 | 471 | |
| 70664 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 472 | // define commands | 
| 43720 | 473 |       {
 | 
| 70664 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 474 | val id_commands = new mutable.ListBuffer[Command] | 
| 75393 | 475 |         def id_command(command: Command): Unit = {
 | 
| 70664 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 476 |           for {
 | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 477 | (name, digest) <- command.blobs_defined | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 478 | if !global_state.value.defined_blob(digest) | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 479 |           } {
 | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 480 |             change.version.nodes(name).get_blob match {
 | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 481 | case Some(blob) => | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 482 | global_state.change(_.define_blob(digest)) | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 483 | prover.get.define_blob(digest, blob.bytes) | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 484 | case None => | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 485 |                 Output.error_message("Missing blob " + quote(name.toString))
 | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 486 | } | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 487 | } | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 488 | |
| 82793 
fe8598c92be7
basic support to reload theory markup from session store;
 wenzelm parents: 
82789diff
changeset | 489 |           if (!command.span.is_theory && !global_state.value.defined_command(command.id)) {
 | 
| 70664 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 490 | global_state.change(_.define_command(command)) | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 491 | id_commands += command | 
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54443diff
changeset | 492 | } | 
| 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54443diff
changeset | 493 | } | 
| 70664 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 494 |         for { (_, edit) <- change.doc_edits } {
 | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70661diff
changeset | 495 |           edit.foreach({ case (c1, c2) => c1.foreach(id_command); c2.foreach(id_command) })
 | 
| 43720 | 496 | } | 
| 72946 | 497 |         if (id_commands.nonEmpty) {
 | 
| 498 | prover.get.define_commands_bulk(resources, id_commands.toList) | |
| 499 | } | |
| 44383 | 500 | } | 
| 43722 | 501 | |
| 56687 | 502 | val assignment = global_state.value.the_assignment(change.previous).check_finished | 
| 503 | global_state.change(_.define_version(change.version, assignment)) | |
| 70625 
1ae987cc052f
support for share_common_data after define_command and before actual update: this affects string particles of command tokens;
 wenzelm parents: 
70284diff
changeset | 504 | |
| 68336 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
68293diff
changeset | 505 | prover.get.update(change.previous.id, change.version.id, change.doc_edits, change.consolidate) | 
| 82783 
98d9abefd9b0
clarified signature: prefer private operation (see also 803731b62180);
 wenzelm parents: 
82782diff
changeset | 506 | |
| 
98d9abefd9b0
clarified signature: prefer private operation (see also 803731b62180);
 wenzelm parents: 
82782diff
changeset | 507 |       if (change.deps_changed || auto_resolve && resources.undefined_blobs(change.version).nonEmpty) {
 | 
| 
98d9abefd9b0
clarified signature: prefer private operation (see also 803731b62180);
 wenzelm parents: 
82782diff
changeset | 508 | deps_changed() | 
| 
98d9abefd9b0
clarified signature: prefer private operation (see also 803731b62180);
 wenzelm parents: 
82782diff
changeset | 509 | } | 
| 75393 | 510 | //}}} | 
| 34809 | 511 | } | 
| 512 | ||
| 513 | ||
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 514 | /* prover output */ | 
| 34809 | 515 | |
| 75393 | 516 |     def handle_output(output: Prover.Output): Unit = {
 | 
| 38221 | 517 |     //{{{
 | 
| 75393 | 518 |       def bad_output(): Unit = {
 | 
| 68103 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 519 | if (verbose) | 
| 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 520 |           Output.warning("Ignoring bad prover output: " + output.message.toString)
 | 
| 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 521 | } | 
| 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 522 | |
| 75393 | 523 |       def change_command(f: Document.State => (Command.State, Document.State)): Unit = {
 | 
| 68103 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 524 |         try {
 | 
| 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 525 | val st = global_state.change_result(f) | 
| 72692 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72217diff
changeset | 526 |           if (!st.command.span.is_theory) {
 | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72217diff
changeset | 527 | change_buffer.invoke(false, Nil, List(st.command)) | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72217diff
changeset | 528 | } | 
| 68103 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 529 | } | 
| 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 530 |         catch { case _: Document.State.Fail => bad_output() }
 | 
| 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 531 | } | 
| 51662 
3391a493f39a
just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
 wenzelm parents: 
51083diff
changeset | 532 | |
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 533 |       output match {
 | 
| 56385 | 534 | case msg: Prover.Protocol_Output => | 
| 65214 | 535 | val handled = protocol_handlers.invoke(msg) | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 536 |           if (!handled) {
 | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 537 |             msg.properties match {
 | 
| 71673 | 538 | case Protocol.Command_Timing(props, state_id, timing) if prover.defined => | 
| 539 | command_timings.post(Session.Command_Timing(props)) | |
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 540 | val message = XML.elem(Markup.STATUS, List(XML.Elem(Markup.Timing(timing), Nil))) | 
| 73031 
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
 wenzelm parents: 
73024diff
changeset | 541 | change_command(_.accumulate(state_id, cache.elem(message), cache)) | 
| 46122 | 542 | |
| 71673 | 543 | case Markup.Theory_Timing(props) => | 
| 544 | theory_timings.post(Session.Theory_Timing(props)) | |
| 66873 
9953ae603a23
provide theory timing information, similar to command timing but always considered relevant;
 wenzelm parents: 
66720diff
changeset | 545 | |
| 71671 | 546 | case Markup.Task_Statistics(props) => | 
| 547 | task_statistics.post(Session.Task_Statistics(props)) | |
| 548 | ||
| 71624 | 549 | case Protocol.Export(args) | 
| 68101 | 550 | if args.id.isDefined && Value.Long.unapply(args.id.get).isDefined => | 
| 71968 
ec0ef3ebe75e
removed pointless pide_exports: unused during "build_session" process (reverting 6a64205b491a);
 wenzelm parents: 
71960diff
changeset | 551 | val id = Value.Long.unapply(args.id.get).get | 
| 76851 | 552 | val entry = Export.Entry.make(Sessions.DRAFT, args, msg.chunk, cache) | 
| 74685 | 553 | change_command(_.add_export(id, (args.serial, entry))) | 
| 68088 | 554 | |
| 72692 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72217diff
changeset | 555 | case Protocol.Loading_Theory(node_name, id) => | 
| 72816 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72721diff
changeset | 556 | val blobs_info = build_blobs_info(node_name) | 
| 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72721diff
changeset | 557 |                 try { global_state.change(_.begin_theory(node_name, id, msg.text, blobs_info)) }
 | 
| 72692 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72217diff
changeset | 558 |                 catch { case _: Document.State.Fail => bad_output() }
 | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72217diff
changeset | 559 | |
| 76394 | 560 | case List(Markup.Commands_Accepted.THIS) => | 
| 70665 
94442fce40a5
prefer commands_accepted: fewer protocol messages;
 wenzelm parents: 
70664diff
changeset | 561 |                 msg.text match {
 | 
| 
94442fce40a5
prefer commands_accepted: fewer protocol messages;
 wenzelm parents: 
70664diff
changeset | 562 | case Protocol.Commands_Accepted(ids) => | 
| 
94442fce40a5
prefer commands_accepted: fewer protocol messages;
 wenzelm parents: 
70664diff
changeset | 563 | ids.foreach(id => | 
| 73031 
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
 wenzelm parents: 
73024diff
changeset | 564 | change_command(_.accumulate(id, Protocol.Commands_Accepted.message, cache))) | 
| 70665 
94442fce40a5
prefer commands_accepted: fewer protocol messages;
 wenzelm parents: 
70664diff
changeset | 565 | case _ => bad_output() | 
| 
94442fce40a5
prefer commands_accepted: fewer protocol messages;
 wenzelm parents: 
70664diff
changeset | 566 | } | 
| 
94442fce40a5
prefer commands_accepted: fewer protocol messages;
 wenzelm parents: 
70664diff
changeset | 567 | |
| 76394 | 568 | case List(Markup.Assign_Update.THIS) => | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 569 |                 msg.text match {
 | 
| 70284 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 570 | case Protocol.Assign_Update(id, edited, update) => | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 571 |                     try {
 | 
| 70284 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 572 | val (edited_nodes, cmds) = | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 573 | global_state.change_result(_.assign(id, edited, update)) | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69640diff
changeset | 574 | change_buffer.invoke(true, edited_nodes, 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 | 575 | manager.send(Session.Change_Flush) | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 576 | } | 
| 68103 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 577 |                     catch { case _: Document.State.Fail => bad_output() }
 | 
| 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 578 | case _ => bad_output() | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 579 | } | 
| 56771 
28d62a5b07e8
more systematic delay_first discipline for change_buffer and prune_history;
 wenzelm parents: 
56735diff
changeset | 580 | delay_prune.invoke() | 
| 46122 | 581 | |
| 76394 | 582 | case List(Markup.Removed_Versions.THIS) => | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 583 |                 msg.text match {
 | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 584 | case Protocol.Removed(removed) => | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 585 |                     try {
 | 
| 56687 | 586 | 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 | 587 | manager.send(Session.Change_Flush) | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 588 | } | 
| 68103 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 589 |                     catch { case _: Document.State.Fail => bad_output() }
 | 
| 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 590 | case _ => bad_output() | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 591 | } | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 592 | |
| 68103 
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
 wenzelm parents: 
68101diff
changeset | 593 | case _ => bad_output() | 
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 594 | } | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 595 | } | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 596 | case _ => | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 597 |           output.properties match {
 | 
| 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 598 | case Position.Id(state_id) => | 
| 73031 
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
 wenzelm parents: 
73024diff
changeset | 599 | change_command(_.accumulate(state_id, output.message, cache)) | 
| 56210 | 600 | |
| 54442 
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
 wenzelm parents: 
53054diff
changeset | 601 | case _ if output.is_init => | 
| 72217 | 602 | val init_ok = | 
| 603 |                 try {
 | |
| 604 | Isabelle_System.make_services(classOf[Session.Protocol_Handler]) | |
| 605 | .foreach(init_protocol_handler) | |
| 606 | true | |
| 607 | } | |
| 608 |                 catch {
 | |
| 609 | case exn: Throwable => | |
| 80462 
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
 wenzelm parents: 
80302diff
changeset | 610 | prover.get.protocol_command( | 
| 
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
 wenzelm parents: 
80302diff
changeset | 611 | "Prover.stop", XML.Encode.int(1), XML.string(Exn.message(exn))) | 
| 72217 | 612 | false | 
| 613 | } | |
| 72156 
065dcd80293e
provide protocol handlers via isabelle_system_service;
 wenzelm parents: 
72116diff
changeset | 614 | |
| 72217 | 615 |               if (init_ok) {
 | 
| 616 | prover.get.options(prover_options(session_options)) | |
| 617 | prover.get.init_session(resources) | |
| 72216 | 618 | |
| 72217 | 619 | phase = Session.Ready | 
| 620 | debugger.ready() | |
| 621 | } | |
| 56210 | 622 | |
| 65317 | 623 | case Markup.Process_Result(result) if output.is_exit => | 
| 72115 
c998827f1df9
more thorough protocol_handlers.exit, like file_formats.stop_session;
 wenzelm parents: 
71970diff
changeset | 624 | if (prover.defined) protocol_handlers.exit() | 
| 72861 
3f5e6da08687
clarified protocol: support "isabelle log" on failed theories as well;
 wenzelm parents: 
72816diff
changeset | 625 |               for (id <- global_state.value.theories.keys) {
 | 
| 76912 
ca872f20cf5b
clarified session sources: theory and blobs are read from database, instead of physical file-system;
 wenzelm parents: 
76851diff
changeset | 626 | val snapshot = global_state.change_result(_.end_theory(id, build_blobs)) | 
| 72861 
3f5e6da08687
clarified protocol: support "isabelle log" on failed theories as well;
 wenzelm parents: 
72816diff
changeset | 627 | finished_theories.post(snapshot) | 
| 
3f5e6da08687
clarified protocol: support "isabelle log" on failed theories as well;
 wenzelm parents: 
72816diff
changeset | 628 | } | 
| 76842 | 629 | file_formats.stop_session() | 
| 65317 | 630 | phase = Session.Terminated(result) | 
| 76321 
3e1e2f9198bb
tuned signature, following hints by IntelliJ IDEA;
 wenzelm parents: 
76045diff
changeset | 631 | prover.reset() | 
| 56210 | 632 | |
| 61376 
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
 wenzelm parents: 
60934diff
changeset | 633 | case _ => | 
| 
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
 wenzelm parents: 
60934diff
changeset | 634 | 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 | 635 | } | 
| 52111 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 wenzelm parents: 
52084diff
changeset | 636 | } | 
| 75393 | 637 | //}}} | 
| 34809 | 638 | } | 
| 639 | ||
| 34820 
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
 wenzelm parents: 
34819diff
changeset | 640 | |
| 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 | 641 | /* 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 | 642 | |
| 75393 | 643 |     Consumer_Thread.fork[Any]("Session.manager", daemon = true) {
 | 
| 56709 | 644 | case arg: Any => | 
| 645 |         //{{{
 | |
| 646 |         arg match {
 | |
| 56733 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 647 | case output: Prover.Output => | 
| 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 648 |             if (output.is_syslog) {
 | 
| 76488 | 649 | syslog += XML.content(output.message) | 
| 56733 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 650 | syslog_messages.post(output) | 
| 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 651 | } | 
| 56775 
59f70b89e5fd
improved syslog performance -- avoid denial-of-service e.g. with threads_trace = 5 and active Syslog dockable;
 wenzelm parents: 
56772diff
changeset | 652 | |
| 71606 
b3b0d87edd20
clarified order: update syslog before handling exit;
 wenzelm parents: 
71601diff
changeset | 653 | if (output.is_stdout || output.is_stderr) | 
| 
b3b0d87edd20
clarified order: update syslog before handling exit;
 wenzelm parents: 
71601diff
changeset | 654 | raw_output_messages.post(output) | 
| 
b3b0d87edd20
clarified order: update syslog before handling exit;
 wenzelm parents: 
71601diff
changeset | 655 | else handle_output(output) | 
| 
b3b0d87edd20
clarified order: update syslog before handling exit;
 wenzelm parents: 
71601diff
changeset | 656 | |
| 56733 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 657 | all_messages.post(output) | 
| 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 658 | |
| 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 659 | case input: Prover.Input => | 
| 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 660 | all_messages.post(input) | 
| 
f7700146678d
manager is direct receiver of prover output -- discontinued old performance tuning (329320fc88df, 1baa5d19ac44);
 wenzelm parents: 
56719diff
changeset | 661 | |
| 62556 
c115e69f457f
more abstract Session.start, without prover command-line;
 wenzelm parents: 
62545diff
changeset | 662 | case Start(start_prover) if !prover.defined => | 
| 65207 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 663 | 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 | 664 | |
| 56709 | 665 | case Stop => | 
| 68807 
e28978310a2a
more robust exit: avoid later Consolidate_Execution with handle_raw_edits (cf. 2fd3a6d6ba2e);
 wenzelm parents: 
68382diff
changeset | 666 | consolidation.exit() | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 667 | delay_prune.revoke() | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 668 |             if (prover.defined) {
 | 
| 56772 | 669 | global_state.change(_ => Document.State.init) | 
| 73367 | 670 | prover.get.terminate() | 
| 56709 | 671 | } | 
| 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 | 672 | |
| 70775 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 673 | case Get_State(promise) => | 
| 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 674 | promise.fulfill(global_state.value) | 
| 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 675 | |
| 66379 
6392766f3c25
maintain "consolidated" status of theory nodes, which means all evals are finished (but not necessarily prints nor imports);
 wenzelm parents: 
66094diff
changeset | 676 | case Consolidate_Execution => | 
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 677 |             if (prover.defined) {
 | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 678 | val state = global_state.value | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 679 |               state.stable_tip_version match {
 | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 680 | case None => consolidation.update() | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 681 | case Some(version) => | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 682 | val consolidate = | 
| 77198 
9b35c1171d9a
more thorough consolidation: follow dependencies of forked proofs (e.g. see theories MaxPrefix vs. MaxChop in AFP/Functional-Automata);
 wenzelm parents: 
77149diff
changeset | 683 |                     version.nodes.descendants(consolidation.flush().toList).filter { name =>
 | 
| 82920 | 684 | !resources.loaded_theory(name) && | 
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 685 | !state.node_consolidated(version, name) && | 
| 77198 
9b35c1171d9a
more thorough consolidation: follow dependencies of forked proofs (e.g. see theories MaxPrefix vs. MaxChop in AFP/Functional-Automata);
 wenzelm parents: 
77149diff
changeset | 686 | state.node_maybe_consolidated(version, name) | 
| 
9b35c1171d9a
more thorough consolidation: follow dependencies of forked proofs (e.g. see theories MaxPrefix vs. MaxChop in AFP/Functional-Automata);
 wenzelm parents: 
77149diff
changeset | 687 | } | 
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 688 | if (consolidate.nonEmpty) handle_raw_edits(consolidate = consolidate) | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 689 | } | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68336diff
changeset | 690 | } | 
| 66379 
6392766f3c25
maintain "consolidated" status of theory nodes, which means all evals are finished (but not necessarily prints nor imports);
 wenzelm parents: 
66094diff
changeset | 691 | |
| 56771 
28d62a5b07e8
more systematic delay_first discipline for change_buffer and prune_history;
 wenzelm parents: 
56735diff
changeset | 692 | 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 | 693 |             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 | 694 | val old_versions = global_state.change_result(_.remove_versions(prune_size)) | 
| 59319 | 695 | 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 | 696 | } | 
| 
28d62a5b07e8
more systematic delay_first discipline for change_buffer and prune_history;
 wenzelm parents: 
56735diff
changeset | 697 | |
| 56709 | 698 | 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 | 699 |             if (prover.defined && is_ready) {
 | 
| 72216 | 700 | prover.get.options(prover_options(options)) | 
| 68336 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
68293diff
changeset | 701 | handle_raw_edits() | 
| 56709 | 702 | } | 
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 703 | global_options.post(Session.Global_Options(options)) | 
| 34809 | 704 | |
| 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 | 705 | case Cancel_Exec(exec_id) if prover.defined => | 
| 56709 | 706 | 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 | 707 | |
| 70796 
2739631ac368
discontinued pointless dump_checkpoint and share_common_data -- superseded by base logic image in Isabelle/MMT;
 wenzelm parents: 
70778diff
changeset | 708 | case Session.Raw_Edits(doc_blobs, edits) if prover.defined => | 
| 
2739631ac368
discontinued pointless dump_checkpoint and share_common_data -- superseded by base logic image in Isabelle/MMT;
 wenzelm parents: 
70778diff
changeset | 709 | handle_raw_edits(doc_blobs = doc_blobs, edits = edits) | 
| 56709 | 710 | |
| 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 | 711 | case Session.Dialog_Result(id, serial, result) if prover.defined => | 
| 56709 | 712 | prover.get.dialog_result(serial, result) | 
| 713 | 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 | 714 | |
| 73565 | 715 | case Protocol_Command_Raw(name, args) if prover.defined => | 
| 716 | prover.get.protocol_command_raw(name, args) | |
| 717 | ||
| 718 | case Protocol_Command_Args(name, args) if prover.defined => | |
| 70661 | 719 | prover.get.protocol_command_args(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 | 720 | |
| 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 | 721 | 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 | 722 | 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 | 723 | if (!state.removing_versions && state.is_assigned(change.previous)) | 
| 56709 | 724 | handle_change(change) | 
| 725 | else postponed_changes.store(change) | |
| 57651 
10df45dd14da
less warnings -- ignore potential prover startup/shutdown races;
 wenzelm parents: 
56864diff
changeset | 726 | |
| 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 | 727 | 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 | 728 | 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 | 729 | if (!state.removing_versions) | 
| 71601 | 730 | postponed_changes.flush(state).foreach(handle_change) | 
| 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 | 731 | |
| 57651 
10df45dd14da
less warnings -- ignore potential prover startup/shutdown races;
 wenzelm parents: 
56864diff
changeset | 732 | case bad => | 
| 
10df45dd14da
less warnings -- ignore potential prover startup/shutdown races;
 wenzelm parents: 
56864diff
changeset | 733 |             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 | 734 | } | 
| 56709 | 735 | true | 
| 736 | //}}} | |
| 737 | } | |
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 738 | } | 
| 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 739 | |
| 34809 | 740 | |
| 56799 | 741 | /* main operations */ | 
| 742 | ||
| 75393 | 743 |   def get_state(): Document.State = {
 | 
| 74253 | 744 |     if (manager.is_active()) {
 | 
| 70776 | 745 | val promise = Future.promise[Document.State] | 
| 746 | manager.send_wait(Get_State(promise)) | |
| 747 | promise.join | |
| 748 | } | |
| 749 | else Document.State.init | |
| 70775 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 750 | } | 
| 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 751 | |
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76761diff
changeset | 752 | def snapshot( | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76761diff
changeset | 753 | node_name: Document.Node.Name = Document.Node.Name.empty, | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76761diff
changeset | 754 | pending_edits: Document.Pending_Edits = Document.Pending_Edits.empty | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76761diff
changeset | 755 | ): Document.Snapshot = get_state().snapshot(node_name = node_name, pending_edits = pending_edits) | 
| 70775 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 756 | |
| 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 757 | def recent_syntax(name: Document.Node.Name): Outer_Syntax = | 
| 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 758 | get_state().recent_finished.version.get_finished.nodes(name).syntax getOrElse | 
| 
97d3485028b6
more sequential access to Session.manager.global_state: avoid minor divergence of tip version;
 wenzelm parents: 
70774diff
changeset | 759 | resources.session_base.overall_syntax | 
| 34809 | 760 | |
| 76766 | 761 | def stable_tip_version[A](models: Iterable[Document.Model]): Option[Document.Version] = | 
| 762 | if (models.forall(model => model.pending_edits.isEmpty)) get_state().stable_tip_version | |
| 76590 | 763 | else None | 
| 764 | ||
| 75393 | 765 |   @tailrec final def await_stable_snapshot(): Document.Snapshot = {
 | 
| 69640 
af09cc4792dc
more robust: no assumptions about GUI thread or document model;
 wenzelm parents: 
69492diff
changeset | 766 | val snapshot = this.snapshot() | 
| 
af09cc4792dc
more robust: no assumptions about GUI thread or document model;
 wenzelm parents: 
69492diff
changeset | 767 |     if (snapshot.is_outdated) {
 | 
| 73702 
7202e12cb324
tuned signature --- following hints by IntelliJ IDEA;
 wenzelm parents: 
73565diff
changeset | 768 | output_delay.sleep() | 
| 69640 
af09cc4792dc
more robust: no assumptions about GUI thread or document model;
 wenzelm parents: 
69492diff
changeset | 769 | await_stable_snapshot() | 
| 
af09cc4792dc
more robust: no assumptions about GUI thread or document model;
 wenzelm parents: 
69492diff
changeset | 770 | } | 
| 
af09cc4792dc
more robust: no assumptions about GUI thread or document model;
 wenzelm parents: 
69492diff
changeset | 771 | else snapshot | 
| 
af09cc4792dc
more robust: no assumptions about GUI thread or document model;
 wenzelm parents: 
69492diff
changeset | 772 | } | 
| 
af09cc4792dc
more robust: no assumptions about GUI thread or document model;
 wenzelm parents: 
69492diff
changeset | 773 | |
| 82765 | 774 | def build( | 
| 775 | progress: Progress = new Progress, | |
| 776 | dirs: List[Path] = Nil, | |
| 777 | no_build: Boolean = false | |
| 778 |   ): Build.Results = {
 | |
| 779 | Build.build(store.options, | |
| 780 | selection = Sessions.Selection.session(resources.session_base.session_name), | |
| 781 | progress = progress, build_heap = true, no_build = no_build, dirs = dirs, | |
| 782 | infos = resources.session_background.infos) | |
| 783 | } | |
| 784 | ||
| 785 | def build_ok(dirs: List[Path] = Nil): Boolean = | |
| 786 | build(dirs = dirs, no_build = true).ok | |
| 787 | ||
| 75393 | 788 |   def start(start_prover: Prover.Receiver => Prover): Unit = {
 | 
| 69488 
b05c0bb47f6d
support for File_Format.Session, e.g. server process accessible via prover options;
 wenzelm parents: 
68807diff
changeset | 789 | file_formats | 
| 65207 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 790 | _phase.change( | 
| 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 791 |       {
 | 
| 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 792 | case Session.Inactive => | 
| 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 793 | manager.send(Start(start_prover)) | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 794 | post_phase(Session.Startup) | 
| 65207 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 795 |         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 | 796 | }) | 
| 
004bc5968c2a
more strict Session.start: no restart from terminated session;
 wenzelm parents: 
65206diff
changeset | 797 | } | 
| 45076 | 798 | |
| 75393 | 799 |   def stop(): Process_Result = {
 | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 800 | val was_ready = | 
| 71667 
4d2de35214c5
proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
 wenzelm parents: 
71655diff
changeset | 801 | _phase.guarded_access( | 
| 
4d2de35214c5
proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
 wenzelm parents: 
71655diff
changeset | 802 |         {
 | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 803 | case Session.Startup | Session.Shutdown => None | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 804 | case Session.Terminated(_) => Some((false, phase)) | 
| 77243 | 805 | case Session.Inactive => Some((false, post_phase(Session.Terminated(Process_Result.ok)))) | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 806 | case Session.Ready => Some((true, post_phase(Session.Shutdown))) | 
| 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 807 | }) | 
| 65311 | 808 | if (was_ready) manager.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 | 809 | prover.await_reset() | 
| 65208 
91c528cd376a
more robust Session.stop: idempotent, avoid conflict with startup;
 wenzelm parents: 
65207diff
changeset | 810 | |
| 56704 | 811 | 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 | 812 | 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 | 813 | manager.shutdown() | 
| 56715 
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
 wenzelm parents: 
56713diff
changeset | 814 | dispatcher.shutdown() | 
| 65209 | 815 | |
| 816 |     phase match {
 | |
| 65317 | 817 | case Session.Terminated(result) => result | 
| 65209 | 818 |       case phase => error("Bad session phase after shutdown: " + quote(phase.print))
 | 
| 819 | } | |
| 50117 | 820 | } | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 821 | |
| 82794 | 822 | def system_output(text: String): Unit = | 
| 823 | manager.send(new Prover.System_Output(text)) | |
| 824 | ||
| 73565 | 825 | def protocol_command_raw(name: String, args: List[Bytes]): Unit = | 
| 826 | manager.send(Protocol_Command_Raw(name, args)) | |
| 827 | ||
| 80462 
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
 wenzelm parents: 
80302diff
changeset | 828 | def protocol_command_args(name: String, args: List[XML.Body]): Unit = | 
| 73565 | 829 | manager.send(Protocol_Command_Args(name, args)) | 
| 830 | ||
| 80462 
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
 wenzelm parents: 
80302diff
changeset | 831 | def protocol_command(name: String, args: XML.Body*): Unit = | 
| 73565 | 832 | protocol_command_args(name, args.toList) | 
| 53054 
8365d7fca3de
public access for protocol handlers and protocol commands -- to be used within reason;
 wenzelm parents: 
52931diff
changeset | 833 | |
| 73340 | 834 | def cancel_exec(exec_id: Document_ID.Exec): Unit = | 
| 835 | 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 | 836 | |
| 73340 | 837 | def update(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text]): Unit = | 
| 70796 
2739631ac368
discontinued pointless dump_checkpoint and share_common_data -- superseded by base logic image in Isabelle/MMT;
 wenzelm parents: 
70778diff
changeset | 838 | if (edits.nonEmpty) manager.send_wait(Session.Raw_Edits(doc_blobs, edits)) | 
| 73340 | 839 | |
| 840 | def update_options(options: Options): Unit = | |
| 841 | manager.send_wait(Update_Options(options)) | |
| 50498 | 842 | |
| 73340 | 843 | def dialog_result(id: Document_ID.Generic, serial: Long, result: String): Unit = | 
| 844 | manager.send(Session.Dialog_Result(id, serial, result)) | |
| 82782 | 845 | |
| 846 | ||
| 847 | /* diagnostics */ | |
| 848 | ||
| 849 | val init_time: Time = Time.now() | |
| 850 | def print_now(): String = (Time.now() - init_time).toString | |
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 851 | } |