src/Pure/PIDE/session.scala
author wenzelm
Sun May 19 18:10:45 2019 +0200 (5 months ago)
changeset 70284 3e17c3a5fd39
parent 69640 af09cc4792dc
child 70625 1ae987cc052f
permissions -rw-r--r--
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
wenzelm@56210
     1
/*  Title:      Pure/PIDE/session.scala
wenzelm@36676
     2
    Author:     Makarius
wenzelm@57923
     3
    Options:    :folding=explicit:
wenzelm@36676
     4
wenzelm@56210
     5
PIDE editor session, potentially with running prover process.
wenzelm@36676
     6
*/
wenzelm@34777
     7
wenzelm@34871
     8
package isabelle
wenzelm@34777
     9
wenzelm@44733
    10
wenzelm@46771
    11
import scala.collection.immutable.Queue
wenzelm@69640
    12
import scala.annotation.tailrec
wenzelm@34777
    13
wenzelm@34815
    14
wenzelm@34791
    15
object Session
wenzelm@34791
    16
{
wenzelm@56715
    17
  /* outlets */
wenzelm@56715
    18
wenzelm@56715
    19
  object Consumer
wenzelm@56715
    20
  {
wenzelm@56715
    21
    def apply[A](name: String)(consume: A => Unit): Consumer[A] =
wenzelm@56715
    22
      new Consumer[A](name, consume)
wenzelm@56715
    23
  }
wenzelm@56715
    24
  final class Consumer[-A] private(val name: String, val consume: A => Unit)
wenzelm@56715
    25
wenzelm@56715
    26
  class Outlet[A](dispatcher: Consumer_Thread[() => Unit])
wenzelm@56715
    27
  {
wenzelm@61590
    28
    private val consumers = Synchronized[List[Consumer[A]]](Nil)
wenzelm@56715
    29
wenzelm@56715
    30
    def += (c: Consumer[A]) { consumers.change(Library.update(c)) }
wenzelm@56715
    31
    def -= (c: Consumer[A]) { consumers.change(Library.remove(c)) }
wenzelm@56715
    32
wenzelm@56715
    33
    def post(a: A)
wenzelm@56715
    34
    {
wenzelm@56715
    35
      for (c <- consumers.value.iterator) {
wenzelm@56715
    36
        dispatcher.send(() =>
wenzelm@56715
    37
          try { c.consume(a) }
wenzelm@56715
    38
          catch {
wenzelm@56715
    39
            case exn: Throwable =>
wenzelm@56782
    40
              Output.error_message("Consumer failed: " + quote(c.name) + "\n" + Exn.message(exn))
wenzelm@56715
    41
          })
wenzelm@56715
    42
      }
wenzelm@56715
    43
    }
wenzelm@56715
    44
  }
wenzelm@56715
    45
wenzelm@56715
    46
wenzelm@56315
    47
  /* change */
wenzelm@56315
    48
wenzelm@56315
    49
  sealed case class Change(
wenzelm@56315
    50
    previous: Document.Version,
wenzelm@59077
    51
    syntax_changed: List[Document.Node.Name],
wenzelm@56316
    52
    deps_changed: Boolean,
wenzelm@56315
    53
    doc_edits: List[Document.Edit_Command],
wenzelm@68381
    54
    consolidate: List[Document.Node.Name],
wenzelm@56315
    55
    version: Document.Version)
wenzelm@56315
    56
wenzelm@57976
    57
  case object Change_Flush
wenzelm@57976
    58
wenzelm@56315
    59
wenzelm@34813
    60
  /* events */
wenzelm@34813
    61
wenzelm@43716
    62
  //{{{
wenzelm@50697
    63
  case class Statistics(props: Properties.T)
wenzelm@50117
    64
  case class Global_Options(options: Options)
wenzelm@44805
    65
  case object Caret_Focus
wenzelm@54521
    66
  case class Raw_Edits(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
wenzelm@52531
    67
  case class Dialog_Result(id: Document_ID.Generic, serial: Long, result: String)
wenzelm@59364
    68
  case class Build_Theories(id: String, master_dir: Path, theories: List[(Options, List[Path])])
wenzelm@47027
    69
  case class Commands_Changed(
wenzelm@47027
    70
    assignment: Boolean, nodes: Set[Document.Node.Name], commands: Set[Command])
wenzelm@39630
    71
wenzelm@39630
    72
  sealed abstract class Phase
wenzelm@65206
    73
  {
wenzelm@65206
    74
    def print: String =
wenzelm@65206
    75
      this match {
wenzelm@65317
    76
        case Terminated(result) => if (result.ok) "finished" else "failed"
wenzelm@65206
    77
        case _ => Word.lowercase(this.toString)
wenzelm@65206
    78
      }
wenzelm@65206
    79
  }
wenzelm@65208
    80
  case object Inactive extends Phase  // stable
wenzelm@39701
    81
  case object Startup extends Phase  // transient
wenzelm@65208
    82
  case object Ready extends Phase  // metastable
wenzelm@39701
    83
  case object Shutdown extends Phase  // transient
wenzelm@65317
    84
  case class Terminated(result: Process_Result) extends Phase  // stable
wenzelm@43716
    85
  //}}}
wenzelm@52111
    86
wenzelm@52111
    87
wenzelm@56775
    88
  /* syslog */
wenzelm@56775
    89
wenzelm@56775
    90
  private[Session] class Syslog(limit: Int)
wenzelm@56775
    91
  {
wenzelm@56775
    92
    private var queue = Queue.empty[XML.Elem]
wenzelm@56775
    93
    private var length = 0
wenzelm@56775
    94
wenzelm@56775
    95
    def += (msg: XML.Elem): Unit = synchronized {
wenzelm@56775
    96
      queue = queue.enqueue(msg)
wenzelm@56775
    97
      length += 1
wenzelm@56775
    98
      if (length > limit) queue = queue.dequeue._2
wenzelm@56775
    99
    }
wenzelm@56775
   100
wenzelm@56775
   101
    def content: String = synchronized {
wenzelm@56775
   102
      cat_lines(queue.iterator.map(XML.content)) +
wenzelm@56775
   103
      (if (length > limit) "\n(A total of " + length + " messages...)" else "")
wenzelm@56775
   104
    }
wenzelm@56775
   105
  }
wenzelm@56775
   106
wenzelm@56775
   107
wenzelm@52111
   108
  /* protocol handlers */
wenzelm@52111
   109
wenzelm@52111
   110
  abstract class Protocol_Handler
wenzelm@52111
   111
  {
wenzelm@65219
   112
    def init(session: Session): Unit = {}
wenzelm@65219
   113
    def exit(): Unit = {}
wenzelm@65219
   114
    val functions: List[(String, Prover.Protocol_Output => Boolean)]
wenzelm@52111
   115
  }
wenzelm@34791
   116
}
wenzelm@34791
   117
wenzelm@34777
   118
wenzelm@69492
   119
class Session(_session_options: => Options, val resources: Resources) extends Document.Session
wenzelm@34777
   120
{
wenzelm@65214
   121
  session =>
wenzelm@65214
   122
wenzelm@68169
   123
  val xml_cache: XML.Cache = XML.make_cache()
wenzelm@68168
   124
  val xz_cache: XZ.Cache = XZ.make_cache()
wenzelm@65218
   125
wenzelm@65214
   126
wenzelm@47653
   127
  /* global flags */
wenzelm@47653
   128
wenzelm@47653
   129
  @volatile var timing: Boolean = false
wenzelm@47653
   130
  @volatile var verbose: Boolean = false
wenzelm@47653
   131
wenzelm@47653
   132
wenzelm@65264
   133
  /* dynamic session options */
wenzelm@49288
   134
wenzelm@69492
   135
  def session_options: Options = _session_options
wenzelm@69492
   136
wenzelm@65264
   137
  def output_delay: Time = session_options.seconds("editor_output_delay")
wenzelm@66379
   138
  def consolidate_delay: Time = session_options.seconds("editor_consolidate_delay")
wenzelm@65264
   139
  def prune_delay: Time = session_options.seconds("editor_prune_delay")
wenzelm@65264
   140
  def prune_size: Int = session_options.int("editor_prune_size")
wenzelm@65264
   141
  def syslog_limit: Int = session_options.int("editor_syslog_limit")
wenzelm@65264
   142
  def reparse_limit: Int = session_options.int("editor_reparse_limit")
wenzelm@37849
   143
wenzelm@37849
   144
wenzelm@66094
   145
  /* dispatcher */
wenzelm@56715
   146
wenzelm@56715
   147
  private val dispatcher =
wenzelm@56715
   148
    Consumer_Thread.fork[() => Unit]("Session.dispatcher", daemon = true) { case e => e(); true }
wenzelm@34809
   149
wenzelm@66094
   150
  def assert_dispatcher[A](body: => A): A =
wenzelm@66094
   151
  {
wenzelm@66094
   152
    assert(dispatcher.check_thread)
wenzelm@66094
   153
    body
wenzelm@66094
   154
  }
wenzelm@66094
   155
wenzelm@66094
   156
  def require_dispatcher[A](body: => A): A =
wenzelm@66094
   157
  {
wenzelm@66094
   158
    require(dispatcher.check_thread)
wenzelm@66094
   159
    body
wenzelm@66094
   160
  }
wenzelm@66094
   161
wenzelm@66094
   162
  def send_dispatcher(body: => Unit): Unit =
wenzelm@66094
   163
  {
wenzelm@66094
   164
    if (dispatcher.check_thread) body
wenzelm@66094
   165
    else dispatcher.send(() => body)
wenzelm@66094
   166
  }
wenzelm@66094
   167
wenzelm@66094
   168
  def send_wait_dispatcher(body: => Unit): Unit =
wenzelm@66094
   169
  {
wenzelm@66094
   170
    if (dispatcher.check_thread) body
wenzelm@66094
   171
    else dispatcher.send_wait(() => body)
wenzelm@66094
   172
  }
wenzelm@66094
   173
wenzelm@66094
   174
wenzelm@66094
   175
  /* outlets */
wenzelm@66094
   176
wenzelm@56715
   177
  val statistics = new Session.Outlet[Session.Statistics](dispatcher)
wenzelm@56715
   178
  val global_options = new Session.Outlet[Session.Global_Options](dispatcher)
wenzelm@56715
   179
  val caret_focus = new Session.Outlet[Session.Caret_Focus.type](dispatcher)
wenzelm@56715
   180
  val raw_edits = new Session.Outlet[Session.Raw_Edits](dispatcher)
wenzelm@56715
   181
  val commands_changed = new Session.Outlet[Session.Commands_Changed](dispatcher)
wenzelm@56715
   182
  val phase_changed = new Session.Outlet[Session.Phase](dispatcher)
wenzelm@56715
   183
  val syslog_messages = new Session.Outlet[Prover.Output](dispatcher)
wenzelm@56715
   184
  val raw_output_messages = new Session.Outlet[Prover.Output](dispatcher)
wenzelm@56715
   185
  val trace_events = new Session.Outlet[Simplifier_Trace.Event.type](dispatcher)
wenzelm@60900
   186
  val debugger_updates = new Session.Outlet[Debugger.Update.type](dispatcher)
wenzelm@34809
   187
wenzelm@60749
   188
  val all_messages = new Session.Outlet[Prover.Message](dispatcher)  // potential bottle-neck!
wenzelm@34809
   189
wenzelm@56706
   190
wenzelm@56799
   191
  /** main protocol manager **/
wenzelm@45635
   192
wenzelm@56799
   193
  /* internal messages */
wenzelm@45635
   194
wenzelm@62556
   195
  private case class Start(start_prover: Prover.Receiver => Prover)
wenzelm@56799
   196
  private case object Stop
wenzelm@56799
   197
  private case class Cancel_Exec(exec_id: Document_ID.Exec)
wenzelm@56799
   198
  private case class Protocol_Command(name: String, args: List[String])
wenzelm@56799
   199
  private case class Update_Options(options: Options)
wenzelm@66379
   200
  private case object Consolidate_Execution
wenzelm@56799
   201
  private case object Prune_History
wenzelm@45635
   202
wenzelm@38841
   203
wenzelm@65208
   204
  /* phase */
wenzelm@65208
   205
wenzelm@65208
   206
  private def post_phase(new_phase: Session.Phase): Session.Phase =
wenzelm@65208
   207
  {
wenzelm@65208
   208
    phase_changed.post(new_phase)
wenzelm@65208
   209
    new_phase
wenzelm@65208
   210
  }
wenzelm@65208
   211
  private val _phase = Synchronized[Session.Phase](Session.Inactive)
wenzelm@65208
   212
  private def phase_=(new_phase: Session.Phase): Unit = _phase.change(_ => post_phase(new_phase))
wenzelm@65208
   213
wenzelm@65208
   214
  def phase = _phase.value
wenzelm@65208
   215
  def is_ready: Boolean = phase == Session.Ready
wenzelm@65208
   216
wenzelm@65208
   217
wenzelm@43651
   218
  /* global state */
wenzelm@43644
   219
wenzelm@56775
   220
  private val syslog = new Session.Syslog(syslog_limit)
wenzelm@56775
   221
  def syslog_content(): String = syslog.content
wenzelm@39626
   222
wenzelm@56690
   223
  private val global_state = Synchronized(Document.State.init)
wenzelm@56687
   224
  def current_state(): Document.State = global_state.value
wenzelm@46944
   225
wenzelm@63584
   226
  def recent_syntax(name: Document.Node.Name): Outer_Syntax =
wenzelm@60934
   227
    global_state.value.recent_finished.version.get_finished.nodes(name).syntax getOrElse
wenzelm@66720
   228
    resources.session_base.overall_syntax
wenzelm@34816
   229
wenzelm@43651
   230
wenzelm@56799
   231
  /* pipelined change parsing */
wenzelm@56799
   232
wenzelm@56799
   233
  private case class Text_Edits(
wenzelm@56799
   234
    previous: Future[Document.Version],
wenzelm@56799
   235
    doc_blobs: Document.Blobs,
wenzelm@56799
   236
    text_edits: List[Document.Edit_Text],
wenzelm@68381
   237
    consolidate: List[Document.Node.Name],
wenzelm@56799
   238
    version_result: Promise[Document.Version])
wenzelm@43651
   239
wenzelm@56799
   240
  private val change_parser = Consumer_Thread.fork[Text_Edits]("change_parser", daemon = true)
wenzelm@56799
   241
  {
wenzelm@68336
   242
    case Text_Edits(previous, doc_blobs, text_edits, consolidate, version_result) =>
wenzelm@56799
   243
      val prev = previous.get_finished
wenzelm@56799
   244
      val change =
wenzelm@56799
   245
        Timing.timeit("parse_change", timing) {
wenzelm@68336
   246
          resources.parse_change(reparse_limit, prev, doc_blobs, text_edits, consolidate)
wenzelm@56799
   247
        }
wenzelm@56799
   248
      version_result.fulfill(change.version)
wenzelm@56799
   249
      manager.send(change)
wenzelm@56799
   250
      true
wenzelm@56799
   251
  }
wenzelm@41534
   252
wenzelm@56706
   253
wenzelm@56719
   254
  /* buffered changes */
wenzelm@56719
   255
wenzelm@56719
   256
  private object change_buffer
wenzelm@56719
   257
  {
wenzelm@56719
   258
    private var assignment: Boolean = false
wenzelm@56719
   259
    private var nodes: Set[Document.Node.Name] = Set.empty
wenzelm@56719
   260
    private var commands: Set[Command] = Set.empty
wenzelm@56719
   261
wenzelm@56719
   262
    def flush(): Unit = synchronized {
wenzelm@59319
   263
      if (assignment || nodes.nonEmpty || commands.nonEmpty)
wenzelm@56719
   264
        commands_changed.post(Session.Commands_Changed(assignment, nodes, commands))
wenzelm@68381
   265
      if (nodes.nonEmpty) consolidation.update(nodes)
wenzelm@56719
   266
      assignment = false
wenzelm@56719
   267
      nodes = Set.empty
wenzelm@56719
   268
      commands = Set.empty
wenzelm@56719
   269
    }
wenzelm@61556
   270
    private val delay_flush = Standard_Thread.delay_first(output_delay) { flush() }
wenzelm@56719
   271
wenzelm@70284
   272
    def invoke(assign: Boolean, edited_nodes: List[Document.Node.Name], cmds: List[Command]): Unit =
wenzelm@70284
   273
      synchronized {
wenzelm@70284
   274
        assignment |= assign
wenzelm@70284
   275
        for (node <- edited_nodes) {
wenzelm@70284
   276
          nodes += node
wenzelm@70284
   277
        }
wenzelm@70284
   278
        for (command <- cmds) {
wenzelm@70284
   279
          nodes += command.node_name
wenzelm@70284
   280
          command.blobs_names.foreach(nodes += _)
wenzelm@70284
   281
          commands += command
wenzelm@70284
   282
        }
wenzelm@70284
   283
        delay_flush.invoke()
wenzelm@56719
   284
      }
wenzelm@56719
   285
wenzelm@56719
   286
    def shutdown()
wenzelm@56719
   287
    {
wenzelm@56771
   288
      delay_flush.revoke()
wenzelm@56719
   289
      flush()
wenzelm@56719
   290
    }
wenzelm@56719
   291
  }
wenzelm@56719
   292
wenzelm@56719
   293
wenzelm@56706
   294
  /* postponed changes */
wenzelm@44733
   295
wenzelm@56706
   296
  private object postponed_changes
wenzelm@56706
   297
  {
wenzelm@56706
   298
    private var postponed: List[Session.Change] = Nil
wenzelm@56706
   299
wenzelm@56706
   300
    def store(change: Session.Change): Unit = synchronized { postponed ::= change }
wenzelm@56705
   301
wenzelm@57976
   302
    def flush(state: Document.State): List[Session.Change] = synchronized {
wenzelm@56706
   303
      val (assigned, unassigned) = postponed.partition(change => state.is_assigned(change.previous))
wenzelm@56706
   304
      postponed = unassigned
wenzelm@57976
   305
      assigned.reverse
wenzelm@56706
   306
    }
wenzelm@56706
   307
  }
wenzelm@44733
   308
wenzelm@56706
   309
wenzelm@68381
   310
  /* node consolidation */
wenzelm@68381
   311
wenzelm@68381
   312
  private object consolidation
wenzelm@68381
   313
  {
wenzelm@68381
   314
    private val delay =
wenzelm@68381
   315
      Standard_Thread.delay_first(consolidate_delay) { manager.send(Consolidate_Execution) }
wenzelm@68381
   316
wenzelm@68807
   317
    private val init_state: Option[Set[Document.Node.Name]] = Some(Set.empty)
wenzelm@68807
   318
    private val state = Synchronized(init_state)
wenzelm@68807
   319
wenzelm@68807
   320
    def exit()
wenzelm@68807
   321
    {
wenzelm@68807
   322
      delay.revoke()
wenzelm@68807
   323
      state.change(_ => None)
wenzelm@68807
   324
    }
wenzelm@68381
   325
wenzelm@68381
   326
    def update(new_nodes: Set[Document.Node.Name] = Set.empty)
wenzelm@68381
   327
    {
wenzelm@68807
   328
      val active =
wenzelm@68807
   329
        state.change_result(st =>
wenzelm@68807
   330
          (st.isDefined, st.map(nodes => if (nodes.isEmpty) new_nodes else nodes ++ new_nodes)))
wenzelm@68807
   331
      if (active) delay.invoke()
wenzelm@68381
   332
    }
wenzelm@68381
   333
wenzelm@68381
   334
    def flush(): Set[Document.Node.Name] =
wenzelm@68807
   335
      state.change_result(st => if (st.isDefined) (st.get, init_state) else (Set.empty, None))
wenzelm@68381
   336
  }
wenzelm@68381
   337
wenzelm@68381
   338
wenzelm@56793
   339
  /* prover process */
wenzelm@56793
   340
wenzelm@56793
   341
  private object prover
wenzelm@56793
   342
  {
wenzelm@61590
   343
    private val variable = Synchronized[Option[Prover]](None)
wenzelm@56793
   344
wenzelm@56793
   345
    def defined: Boolean = variable.value.isDefined
wenzelm@56793
   346
    def get: Prover = variable.value.get
wenzelm@56793
   347
    def set(p: Prover) { variable.change(_ => Some(p)) }
wenzelm@56793
   348
    def reset { variable.change(_ => None) }
wenzelm@56793
   349
    def await_reset() { variable.guarded_access({ case None => Some((), None) case _ => None }) }
wenzelm@56793
   350
  }
wenzelm@56793
   351
wenzelm@56793
   352
wenzelm@69488
   353
  /* file formats */
wenzelm@69488
   354
wenzelm@69488
   355
  lazy val file_formats: File_Format.Session =
wenzelm@69488
   356
    resources.file_formats.start_session(session)
wenzelm@69488
   357
wenzelm@69488
   358
wenzelm@59366
   359
  /* protocol handlers */
wenzelm@59366
   360
wenzelm@65215
   361
  private val protocol_handlers = Protocol_Handlers.init(session)
wenzelm@59366
   362
wenzelm@59366
   363
  def get_protocol_handler(name: String): Option[Session.Protocol_Handler] =
wenzelm@65214
   364
    protocol_handlers.get(name)
wenzelm@59366
   365
wenzelm@65315
   366
  def init_protocol_handler(handler: Session.Protocol_Handler): Unit =
wenzelm@65315
   367
    protocol_handlers.init(handler)
wenzelm@65214
   368
wenzelm@65315
   369
  def init_protocol_handler(name: String): Unit =
wenzelm@65315
   370
    protocol_handlers.init(name)
wenzelm@59366
   371
wenzelm@59366
   372
wenzelm@65222
   373
  /* debugger */
wenzelm@65222
   374
wenzelm@65222
   375
  private val debugger_handler = new Debugger.Handler(this)
wenzelm@65315
   376
  init_protocol_handler(debugger_handler)
wenzelm@65222
   377
wenzelm@65222
   378
  def debugger: Debugger = debugger_handler.debugger
wenzelm@65222
   379
wenzelm@65222
   380
wenzelm@56706
   381
  /* manager thread */
wenzelm@44733
   382
wenzelm@62115
   383
  private val delay_prune =
wenzelm@62115
   384
    Standard_Thread.delay_first(prune_delay) { manager.send(Prune_History) }
wenzelm@56771
   385
wenzelm@56706
   386
  private val manager: Consumer_Thread[Any] =
wenzelm@56706
   387
  {
wenzelm@52649
   388
    /* raw edits */
wenzelm@52649
   389
wenzelm@68336
   390
    def handle_raw_edits(
wenzelm@68336
   391
      doc_blobs: Document.Blobs = Document.Blobs.empty,
wenzelm@68336
   392
      edits: List[Document.Edit_Text] = Nil,
wenzelm@68381
   393
      consolidate: List[Document.Node.Name] = Nil)
wenzelm@52649
   394
    //{{{
wenzelm@52649
   395
    {
wenzelm@56793
   396
      require(prover.defined)
wenzelm@56712
   397
wenzelm@52649
   398
      prover.get.discontinue_execution()
wenzelm@52649
   399
wenzelm@56687
   400
      val previous = global_state.value.history.tip.version
wenzelm@52649
   401
      val version = Future.promise[Document.Version]
wenzelm@56711
   402
      global_state.change(_.continue_history(previous, edits, version))
wenzelm@52649
   403
wenzelm@56715
   404
      raw_edits.post(Session.Raw_Edits(doc_blobs, edits))
wenzelm@68336
   405
      change_parser.send(Text_Edits(previous, doc_blobs, edits, consolidate, version))
wenzelm@52649
   406
    }
wenzelm@52649
   407
    //}}}
wenzelm@52649
   408
wenzelm@52649
   409
wenzelm@43716
   410
    /* resulting changes */
wenzelm@34809
   411
wenzelm@56315
   412
    def handle_change(change: Session.Change)
wenzelm@38221
   413
    //{{{
wenzelm@34809
   414
    {
wenzelm@56793
   415
      require(prover.defined)
wenzelm@56712
   416
wenzelm@44383
   417
      def id_command(command: Command)
wenzelm@43720
   418
      {
wenzelm@54519
   419
        for {
wenzelm@57842
   420
          (name, digest) <- command.blobs_defined
wenzelm@56687
   421
          if !global_state.value.defined_blob(digest)
wenzelm@54519
   422
        } {
wenzelm@57842
   423
          change.version.nodes(name).get_blob match {
wenzelm@54519
   424
            case Some(blob) =>
wenzelm@56687
   425
              global_state.change(_.define_blob(digest))
wenzelm@56335
   426
              prover.get.define_blob(digest, blob.bytes)
wenzelm@55783
   427
            case None =>
wenzelm@57842
   428
              Output.error_message("Missing blob " + quote(name.toString))
wenzelm@54519
   429
          }
wenzelm@54519
   430
        }
wenzelm@54519
   431
wenzelm@56687
   432
        if (!global_state.value.defined_command(command.id)) {
wenzelm@56687
   433
          global_state.change(_.define_command(command))
wenzelm@44644
   434
          prover.get.define_command(command)
wenzelm@43720
   435
        }
wenzelm@43720
   436
      }
wenzelm@68293
   437
      for { (_, edit) <- change.doc_edits } {
wenzelm@68293
   438
        edit.foreach({ case (c1, c2) => c1.foreach(id_command); c2.foreach(id_command) })
wenzelm@44383
   439
      }
wenzelm@43722
   440
wenzelm@56687
   441
      val assignment = global_state.value.the_assignment(change.previous).check_finished
wenzelm@56687
   442
      global_state.change(_.define_version(change.version, assignment))
wenzelm@68336
   443
      prover.get.update(change.previous.id, change.version.id, change.doc_edits, change.consolidate)
wenzelm@56316
   444
      resources.commit(change)
wenzelm@34809
   445
    }
wenzelm@38221
   446
    //}}}
wenzelm@34809
   447
wenzelm@34809
   448
wenzelm@46772
   449
    /* prover output */
wenzelm@34809
   450
wenzelm@56385
   451
    def handle_output(output: Prover.Output)
wenzelm@38221
   452
    //{{{
wenzelm@34809
   453
    {
wenzelm@68103
   454
      def bad_output()
wenzelm@68103
   455
      {
wenzelm@68103
   456
        if (verbose)
wenzelm@68103
   457
          Output.warning("Ignoring bad prover output: " + output.message.toString)
wenzelm@68103
   458
      }
wenzelm@68103
   459
wenzelm@68103
   460
      def change_command(f: Document.State => (Command.State, Document.State))
wenzelm@68103
   461
      {
wenzelm@68103
   462
        try {
wenzelm@68103
   463
          val st = global_state.change_result(f)
wenzelm@70284
   464
          change_buffer.invoke(false, Nil, List(st.command))
wenzelm@68103
   465
        }
wenzelm@68103
   466
        catch { case _: Document.State.Fail => bad_output() }
wenzelm@68103
   467
      }
wenzelm@51662
   468
wenzelm@54442
   469
      output match {
wenzelm@56385
   470
        case msg: Prover.Protocol_Output =>
wenzelm@65214
   471
          val handled = protocol_handlers.invoke(msg)
wenzelm@54442
   472
          if (!handled) {
wenzelm@54442
   473
            msg.properties match {
wenzelm@56793
   474
              case Markup.Protocol_Handler(name) if prover.defined =>
wenzelm@65315
   475
                init_protocol_handler(name)
wenzelm@54442
   476
wenzelm@56793
   477
              case Protocol.Command_Timing(state_id, timing) if prover.defined =>
wenzelm@54442
   478
                val message = XML.elem(Markup.STATUS, List(XML.Elem(Markup.Timing(timing), Nil)))
wenzelm@68103
   479
                change_command(_.accumulate(state_id, xml_cache.elem(message), xml_cache))
wenzelm@46122
   480
wenzelm@66873
   481
              case Protocol.Theory_Timing(_, _) =>
wenzelm@66873
   482
                // FIXME
wenzelm@66873
   483
wenzelm@68101
   484
              case Markup.Export(args)
wenzelm@68101
   485
              if args.id.isDefined && Value.Long.unapply(args.id.get).isDefined =>
wenzelm@68103
   486
                val id = Value.Long.unapply(args.id.get).get
wenzelm@68168
   487
                val export = Export.make_entry("", args, msg.bytes, cache = xz_cache)
wenzelm@68168
   488
                change_command(_.add_export(id, (args.serial, export)))
wenzelm@68088
   489
wenzelm@54442
   490
              case Markup.Assign_Update =>
wenzelm@54442
   491
                msg.text match {
wenzelm@70284
   492
                  case Protocol.Assign_Update(id, edited, update) =>
wenzelm@54442
   493
                    try {
wenzelm@70284
   494
                      val (edited_nodes, cmds) =
wenzelm@70284
   495
                        global_state.change_result(_.assign(id, edited, update))
wenzelm@70284
   496
                      change_buffer.invoke(true, edited_nodes, cmds)
wenzelm@57976
   497
                      manager.send(Session.Change_Flush)
wenzelm@54442
   498
                    }
wenzelm@68103
   499
                    catch { case _: Document.State.Fail => bad_output() }
wenzelm@68103
   500
                  case _ => bad_output()
wenzelm@54442
   501
                }
wenzelm@56771
   502
                delay_prune.invoke()
wenzelm@46122
   503
wenzelm@54442
   504
              case Markup.Removed_Versions =>
wenzelm@54442
   505
                msg.text match {
wenzelm@54442
   506
                  case Protocol.Removed(removed) =>
wenzelm@54442
   507
                    try {
wenzelm@56687
   508
                      global_state.change(_.removed_versions(removed))
wenzelm@57976
   509
                      manager.send(Session.Change_Flush)
wenzelm@54442
   510
                    }
wenzelm@68103
   511
                    catch { case _: Document.State.Fail => bad_output() }
wenzelm@68103
   512
                  case _ => bad_output()
wenzelm@54442
   513
                }
wenzelm@54442
   514
wenzelm@54442
   515
              case Markup.ML_Statistics(props) =>
wenzelm@56715
   516
                statistics.post(Session.Statistics(props))
wenzelm@54442
   517
wenzelm@54442
   518
              case Markup.Task_Statistics(props) =>
wenzelm@54442
   519
                // FIXME
wenzelm@52111
   520
wenzelm@68103
   521
              case _ => bad_output()
wenzelm@54442
   522
            }
wenzelm@54442
   523
          }
wenzelm@54442
   524
        case _ =>
wenzelm@54442
   525
          output.properties match {
wenzelm@54442
   526
            case Position.Id(state_id) =>
wenzelm@68103
   527
              change_command(_.accumulate(state_id, output.message, xml_cache))
wenzelm@56210
   528
wenzelm@54442
   529
            case _ if output.is_init =>
wenzelm@69488
   530
              prover.get.options(file_formats.prover_options(session_options))
wenzelm@65470
   531
              prover.get.session_base(resources)
wenzelm@54442
   532
              phase = Session.Ready
wenzelm@65223
   533
              debugger.ready()
wenzelm@56210
   534
wenzelm@65317
   535
            case Markup.Process_Result(result) if output.is_exit =>
wenzelm@69488
   536
              file_formats.stop_session
wenzelm@65317
   537
              phase = Session.Terminated(result)
wenzelm@56793
   538
              prover.reset
wenzelm@56210
   539
wenzelm@61376
   540
            case _ =>
wenzelm@61376
   541
              raw_output_messages.post(output)
wenzelm@44661
   542
          }
wenzelm@52111
   543
        }
wenzelm@34809
   544
    }
wenzelm@38221
   545
    //}}}
wenzelm@34809
   546
wenzelm@34820
   547
wenzelm@56706
   548
    /* main thread */
wenzelm@56706
   549
wenzelm@56710
   550
    Consumer_Thread.fork[Any]("Session.manager", daemon = true)
wenzelm@56709
   551
    {
wenzelm@56709
   552
      case arg: Any =>
wenzelm@56709
   553
        //{{{
wenzelm@56709
   554
        arg match {
wenzelm@56733
   555
          case output: Prover.Output =>
wenzelm@56775
   556
            if (output.is_stdout || output.is_stderr)
wenzelm@56775
   557
              raw_output_messages.post(output)
wenzelm@56733
   558
            else handle_output(output)
wenzelm@56775
   559
wenzelm@56733
   560
            if (output.is_syslog) {
wenzelm@56775
   561
              syslog += output.message
wenzelm@56733
   562
              syslog_messages.post(output)
wenzelm@56733
   563
            }
wenzelm@56775
   564
wenzelm@56733
   565
            all_messages.post(output)
wenzelm@56733
   566
wenzelm@56733
   567
          case input: Prover.Input =>
wenzelm@56733
   568
            all_messages.post(input)
wenzelm@56733
   569
wenzelm@62556
   570
          case Start(start_prover) if !prover.defined =>
wenzelm@65207
   571
            prover.set(start_prover(manager.send(_)))
wenzelm@56706
   572
wenzelm@56709
   573
          case Stop =>
wenzelm@68807
   574
            consolidation.exit()
wenzelm@65208
   575
            delay_prune.revoke()
wenzelm@65208
   576
            if (prover.defined) {
wenzelm@65219
   577
              protocol_handlers.exit()
wenzelm@56772
   578
              global_state.change(_ => Document.State.init)
wenzelm@56709
   579
              prover.get.terminate
wenzelm@56709
   580
            }
wenzelm@56706
   581
wenzelm@66379
   582
          case Consolidate_Execution =>
wenzelm@68381
   583
            if (prover.defined) {
wenzelm@68381
   584
              val state = global_state.value
wenzelm@68381
   585
              state.stable_tip_version match {
wenzelm@68381
   586
                case None => consolidation.update()
wenzelm@68381
   587
                case Some(version) =>
wenzelm@68381
   588
                  val consolidate =
wenzelm@68381
   589
                    consolidation.flush().iterator.filter(name =>
wenzelm@68381
   590
                      !resources.session_base.loaded_theory(name) &&
wenzelm@68381
   591
                      !state.node_consolidated(version, name) &&
wenzelm@68381
   592
                      state.node_maybe_consolidated(version, name)).toList
wenzelm@68381
   593
                  if (consolidate.nonEmpty) handle_raw_edits(consolidate = consolidate)
wenzelm@68381
   594
              }
wenzelm@68381
   595
            }
wenzelm@66379
   596
wenzelm@56771
   597
          case Prune_History =>
wenzelm@56793
   598
            if (prover.defined) {
wenzelm@57976
   599
              val old_versions = global_state.change_result(_.remove_versions(prune_size))
wenzelm@59319
   600
              if (old_versions.nonEmpty) prover.get.remove_versions(old_versions)
wenzelm@56771
   601
            }
wenzelm@56771
   602
wenzelm@56709
   603
          case Update_Options(options) =>
wenzelm@56793
   604
            if (prover.defined && is_ready) {
wenzelm@69488
   605
              prover.get.options(file_formats.prover_options(options))
wenzelm@68336
   606
              handle_raw_edits()
wenzelm@56709
   607
            }
wenzelm@56715
   608
            global_options.post(Session.Global_Options(options))
wenzelm@34809
   609
wenzelm@56793
   610
          case Cancel_Exec(exec_id) if prover.defined =>
wenzelm@56709
   611
            prover.get.cancel_exec(exec_id)
wenzelm@56706
   612
wenzelm@56793
   613
          case Session.Raw_Edits(doc_blobs, edits) if prover.defined =>
wenzelm@68336
   614
            handle_raw_edits(doc_blobs = doc_blobs, edits = edits)
wenzelm@56709
   615
wenzelm@56793
   616
          case Session.Dialog_Result(id, serial, result) if prover.defined =>
wenzelm@56709
   617
            prover.get.dialog_result(serial, result)
wenzelm@56709
   618
            handle_output(new Prover.Output(Protocol.Dialog_Result(id, serial, result)))
wenzelm@56706
   619
wenzelm@56793
   620
          case Protocol_Command(name, args) if prover.defined =>
wenzelm@56709
   621
            prover.get.protocol_command(name, args:_*)
wenzelm@56706
   622
wenzelm@56793
   623
          case change: Session.Change if prover.defined =>
wenzelm@57976
   624
            val state = global_state.value
wenzelm@57976
   625
            if (!state.removing_versions && state.is_assigned(change.previous))
wenzelm@56709
   626
              handle_change(change)
wenzelm@56709
   627
            else postponed_changes.store(change)
wenzelm@57651
   628
wenzelm@57976
   629
          case Session.Change_Flush if prover.defined =>
wenzelm@57976
   630
            val state = global_state.value
wenzelm@57976
   631
            if (!state.removing_versions)
wenzelm@57976
   632
              postponed_changes.flush(state).foreach(handle_change(_))
wenzelm@57976
   633
wenzelm@57651
   634
          case bad =>
wenzelm@57651
   635
            if (verbose) Output.warning("Ignoring bad message: " + bad.toString)
wenzelm@56706
   636
        }
wenzelm@56709
   637
        true
wenzelm@56709
   638
        //}}}
wenzelm@56709
   639
    }
wenzelm@34777
   640
  }
wenzelm@34777
   641
wenzelm@34809
   642
wenzelm@56799
   643
  /* main operations */
wenzelm@56799
   644
wenzelm@56799
   645
  def snapshot(name: Document.Node.Name = Document.Node.Name.empty,
wenzelm@56799
   646
      pending_edits: List[Text.Edit] = Nil): Document.Snapshot =
wenzelm@56799
   647
    global_state.value.snapshot(name, pending_edits)
wenzelm@34809
   648
wenzelm@69640
   649
  @tailrec final def await_stable_snapshot(): Document.Snapshot =
wenzelm@69640
   650
  {
wenzelm@69640
   651
    val snapshot = this.snapshot()
wenzelm@69640
   652
    if (snapshot.is_outdated) {
wenzelm@69640
   653
      Thread.sleep(output_delay.ms)
wenzelm@69640
   654
      await_stable_snapshot()
wenzelm@69640
   655
    }
wenzelm@69640
   656
    else snapshot
wenzelm@69640
   657
  }
wenzelm@69640
   658
wenzelm@62556
   659
  def start(start_prover: Prover.Receiver => Prover)
wenzelm@65207
   660
  {
wenzelm@69488
   661
    file_formats
wenzelm@65207
   662
    _phase.change(
wenzelm@65207
   663
      {
wenzelm@65207
   664
        case Session.Inactive =>
wenzelm@65207
   665
          manager.send(Start(start_prover))
wenzelm@65208
   666
          post_phase(Session.Startup)
wenzelm@65207
   667
        case phase => error("Cannot start prover in phase " + quote(phase.print))
wenzelm@65207
   668
      })
wenzelm@65207
   669
  }
wenzelm@45076
   670
wenzelm@65311
   671
  def send_stop()
wenzelm@50117
   672
  {
wenzelm@65208
   673
    val was_ready =
wenzelm@65208
   674
      _phase.guarded_access(phase =>
wenzelm@65208
   675
        phase match {
wenzelm@65208
   676
          case Session.Startup | Session.Shutdown => None
wenzelm@65208
   677
          case Session.Terminated(_) => Some((false, phase))
wenzelm@65317
   678
          case Session.Inactive => Some((false, post_phase(Session.Terminated(Process_Result(0)))))
wenzelm@65208
   679
          case Session.Ready => Some((true, post_phase(Session.Shutdown)))
wenzelm@65208
   680
        })
wenzelm@65311
   681
    if (was_ready) manager.send(Stop)
wenzelm@65311
   682
  }
wenzelm@65311
   683
wenzelm@65317
   684
  def stop(): Process_Result =
wenzelm@65311
   685
  {
wenzelm@65311
   686
    send_stop()
wenzelm@56793
   687
    prover.await_reset()
wenzelm@65208
   688
wenzelm@56704
   689
    change_parser.shutdown()
wenzelm@56706
   690
    change_buffer.shutdown()
wenzelm@56706
   691
    manager.shutdown()
wenzelm@56715
   692
    dispatcher.shutdown()
wenzelm@65209
   693
wenzelm@65209
   694
    phase match {
wenzelm@65317
   695
      case Session.Terminated(result) => result
wenzelm@65209
   696
      case phase => error("Bad session phase after shutdown: " + quote(phase.print))
wenzelm@65209
   697
    }
wenzelm@50117
   698
  }
wenzelm@38841
   699
wenzelm@53054
   700
  def protocol_command(name: String, args: String*)
wenzelm@56706
   701
  { manager.send(Protocol_Command(name, args.toList)) }
wenzelm@53054
   702
wenzelm@56706
   703
  def cancel_exec(exec_id: Document_ID.Exec)
wenzelm@56706
   704
  { manager.send(Cancel_Exec(exec_id)) }
wenzelm@52931
   705
wenzelm@54521
   706
  def update(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
wenzelm@59319
   707
  { if (edits.nonEmpty) manager.send_wait(Session.Raw_Edits(doc_blobs, edits)) }
wenzelm@50498
   708
wenzelm@52084
   709
  def update_options(options: Options)
wenzelm@56706
   710
  { manager.send_wait(Update_Options(options)) }
wenzelm@52084
   711
wenzelm@52531
   712
  def dialog_result(id: Document_ID.Generic, serial: Long, result: String)
wenzelm@56706
   713
  { manager.send(Session.Dialog_Result(id, serial, result)) }
wenzelm@34777
   714
}