src/Pure/PIDE/session.scala
author wenzelm
Mon Mar 13 21:37:35 2017 +0100 (2017-03-13 ago)
changeset 65214 a2ec0db555c7
parent 65213 51c0f094dc02
child 65215 90a7876d6f4c
permissions -rw-r--r--
clarified modules;
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@34777
    12
wenzelm@34815
    13
wenzelm@34791
    14
object Session
wenzelm@34791
    15
{
wenzelm@56715
    16
  /* outlets */
wenzelm@56715
    17
wenzelm@56715
    18
  object Consumer
wenzelm@56715
    19
  {
wenzelm@56715
    20
    def apply[A](name: String)(consume: A => Unit): Consumer[A] =
wenzelm@56715
    21
      new Consumer[A](name, consume)
wenzelm@56715
    22
  }
wenzelm@56715
    23
  final class Consumer[-A] private(val name: String, val consume: A => Unit)
wenzelm@56715
    24
wenzelm@56715
    25
  class Outlet[A](dispatcher: Consumer_Thread[() => Unit])
wenzelm@56715
    26
  {
wenzelm@61590
    27
    private val consumers = Synchronized[List[Consumer[A]]](Nil)
wenzelm@56715
    28
wenzelm@56715
    29
    def += (c: Consumer[A]) { consumers.change(Library.update(c)) }
wenzelm@56715
    30
    def -= (c: Consumer[A]) { consumers.change(Library.remove(c)) }
wenzelm@56715
    31
wenzelm@56715
    32
    def post(a: A)
wenzelm@56715
    33
    {
wenzelm@56715
    34
      for (c <- consumers.value.iterator) {
wenzelm@56715
    35
        dispatcher.send(() =>
wenzelm@56715
    36
          try { c.consume(a) }
wenzelm@56715
    37
          catch {
wenzelm@56715
    38
            case exn: Throwable =>
wenzelm@56782
    39
              Output.error_message("Consumer failed: " + quote(c.name) + "\n" + Exn.message(exn))
wenzelm@56715
    40
          })
wenzelm@56715
    41
      }
wenzelm@56715
    42
    }
wenzelm@56715
    43
  }
wenzelm@56715
    44
wenzelm@56715
    45
wenzelm@56315
    46
  /* change */
wenzelm@56315
    47
wenzelm@56315
    48
  sealed case class Change(
wenzelm@56315
    49
    previous: Document.Version,
wenzelm@59077
    50
    syntax_changed: List[Document.Node.Name],
wenzelm@56316
    51
    deps_changed: Boolean,
wenzelm@56315
    52
    doc_edits: List[Document.Edit_Command],
wenzelm@56315
    53
    version: Document.Version)
wenzelm@56315
    54
wenzelm@57976
    55
  case object Change_Flush
wenzelm@57976
    56
wenzelm@56315
    57
wenzelm@34813
    58
  /* events */
wenzelm@34813
    59
wenzelm@43716
    60
  //{{{
wenzelm@50697
    61
  case class Statistics(props: Properties.T)
wenzelm@50117
    62
  case class Global_Options(options: Options)
wenzelm@44805
    63
  case object Caret_Focus
wenzelm@54521
    64
  case class Raw_Edits(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
wenzelm@52531
    65
  case class Dialog_Result(id: Document_ID.Generic, serial: Long, result: String)
wenzelm@59364
    66
  case class Build_Theories(id: String, master_dir: Path, theories: List[(Options, List[Path])])
wenzelm@47027
    67
  case class Commands_Changed(
wenzelm@47027
    68
    assignment: Boolean, nodes: Set[Document.Node.Name], commands: Set[Command])
wenzelm@39630
    69
wenzelm@39630
    70
  sealed abstract class Phase
wenzelm@65206
    71
  {
wenzelm@65206
    72
    def print: String =
wenzelm@65206
    73
      this match {
wenzelm@65206
    74
        case Terminated(rc) => if (rc == 0) "finished" else "failed"
wenzelm@65206
    75
        case _ => Word.lowercase(this.toString)
wenzelm@65206
    76
      }
wenzelm@65206
    77
  }
wenzelm@65208
    78
  case object Inactive extends Phase  // stable
wenzelm@39701
    79
  case object Startup extends Phase  // transient
wenzelm@65208
    80
  case object Ready extends Phase  // metastable
wenzelm@39701
    81
  case object Shutdown extends Phase  // transient
wenzelm@65208
    82
  case class Terminated(rc: Int) extends Phase  // stable
wenzelm@43716
    83
  //}}}
wenzelm@52111
    84
wenzelm@52111
    85
wenzelm@56775
    86
  /* syslog */
wenzelm@56775
    87
wenzelm@56775
    88
  private[Session] class Syslog(limit: Int)
wenzelm@56775
    89
  {
wenzelm@56775
    90
    private var queue = Queue.empty[XML.Elem]
wenzelm@56775
    91
    private var length = 0
wenzelm@56775
    92
wenzelm@56775
    93
    def += (msg: XML.Elem): Unit = synchronized {
wenzelm@56775
    94
      queue = queue.enqueue(msg)
wenzelm@56775
    95
      length += 1
wenzelm@56775
    96
      if (length > limit) queue = queue.dequeue._2
wenzelm@56775
    97
    }
wenzelm@56775
    98
wenzelm@56775
    99
    def content: String = synchronized {
wenzelm@56775
   100
      cat_lines(queue.iterator.map(XML.content)) +
wenzelm@56775
   101
      (if (length > limit) "\n(A total of " + length + " messages...)" else "")
wenzelm@56775
   102
    }
wenzelm@56775
   103
  }
wenzelm@56775
   104
wenzelm@56775
   105
wenzelm@52111
   106
  /* protocol handlers */
wenzelm@52111
   107
wenzelm@52111
   108
  abstract class Protocol_Handler
wenzelm@52111
   109
  {
wenzelm@65213
   110
    def start(session: Session, prover: Prover): Unit = {}
wenzelm@52111
   111
    def stop(prover: Prover): Unit = {}
wenzelm@56385
   112
    val functions: Map[String, (Prover, Prover.Protocol_Output) => Boolean]
wenzelm@52111
   113
  }
wenzelm@34791
   114
}
wenzelm@34791
   115
wenzelm@34777
   116
wenzelm@56208
   117
class Session(val resources: Resources)
wenzelm@34777
   118
{
wenzelm@65214
   119
  session =>
wenzelm@65214
   120
wenzelm@65214
   121
wenzelm@47653
   122
  /* global flags */
wenzelm@47653
   123
wenzelm@47653
   124
  @volatile var timing: Boolean = false
wenzelm@47653
   125
  @volatile var verbose: Boolean = false
wenzelm@47653
   126
wenzelm@47653
   127
wenzelm@49288
   128
  /* tuning parameters */
wenzelm@49288
   129
wenzelm@49288
   130
  def output_delay: Time = Time.seconds(0.1)  // prover output (markup, common messages)
wenzelm@62115
   131
  def prune_delay: Time = Time.seconds(15.0)  // prune history (delete old versions)
wenzelm@49293
   132
  def prune_size: Int = 0  // size of retained history
wenzelm@49293
   133
  def syslog_limit: Int = 100
wenzelm@49524
   134
  def reparse_limit: Int = 0
wenzelm@37849
   135
wenzelm@37849
   136
wenzelm@56715
   137
  /* outlets */
wenzelm@56715
   138
wenzelm@56715
   139
  private val dispatcher =
wenzelm@56715
   140
    Consumer_Thread.fork[() => Unit]("Session.dispatcher", daemon = true) { case e => e(); true }
wenzelm@34809
   141
wenzelm@56715
   142
  val statistics = new Session.Outlet[Session.Statistics](dispatcher)
wenzelm@56715
   143
  val global_options = new Session.Outlet[Session.Global_Options](dispatcher)
wenzelm@56715
   144
  val caret_focus = new Session.Outlet[Session.Caret_Focus.type](dispatcher)
wenzelm@56715
   145
  val raw_edits = new Session.Outlet[Session.Raw_Edits](dispatcher)
wenzelm@56715
   146
  val commands_changed = new Session.Outlet[Session.Commands_Changed](dispatcher)
wenzelm@56715
   147
  val phase_changed = new Session.Outlet[Session.Phase](dispatcher)
wenzelm@56715
   148
  val syslog_messages = new Session.Outlet[Prover.Output](dispatcher)
wenzelm@56715
   149
  val raw_output_messages = new Session.Outlet[Prover.Output](dispatcher)
wenzelm@56715
   150
  val trace_events = new Session.Outlet[Simplifier_Trace.Event.type](dispatcher)
wenzelm@60900
   151
  val debugger_updates = new Session.Outlet[Debugger.Update.type](dispatcher)
wenzelm@34809
   152
wenzelm@60749
   153
  val all_messages = new Session.Outlet[Prover.Message](dispatcher)  // potential bottle-neck!
wenzelm@34809
   154
wenzelm@56706
   155
wenzelm@56799
   156
  /** main protocol manager **/
wenzelm@45635
   157
wenzelm@56799
   158
  /* internal messages */
wenzelm@45635
   159
wenzelm@62556
   160
  private case class Start(start_prover: Prover.Receiver => Prover)
wenzelm@56799
   161
  private case object Stop
wenzelm@56799
   162
  private case class Cancel_Exec(exec_id: Document_ID.Exec)
wenzelm@56799
   163
  private case class Protocol_Command(name: String, args: List[String])
wenzelm@56799
   164
  private case class Update_Options(options: Options)
wenzelm@56799
   165
  private case object Prune_History
wenzelm@45635
   166
wenzelm@38841
   167
wenzelm@65208
   168
  /* phase */
wenzelm@65208
   169
wenzelm@65208
   170
  private def post_phase(new_phase: Session.Phase): Session.Phase =
wenzelm@65208
   171
  {
wenzelm@65208
   172
    phase_changed.post(new_phase)
wenzelm@65208
   173
    new_phase
wenzelm@65208
   174
  }
wenzelm@65208
   175
  private val _phase = Synchronized[Session.Phase](Session.Inactive)
wenzelm@65208
   176
  private def phase_=(new_phase: Session.Phase): Unit = _phase.change(_ => post_phase(new_phase))
wenzelm@65208
   177
wenzelm@65208
   178
  def phase = _phase.value
wenzelm@65208
   179
  def is_ready: Boolean = phase == Session.Ready
wenzelm@65208
   180
wenzelm@65208
   181
wenzelm@43651
   182
  /* global state */
wenzelm@43644
   183
wenzelm@56775
   184
  private val syslog = new Session.Syslog(syslog_limit)
wenzelm@56775
   185
  def syslog_content(): String = syslog.content
wenzelm@39626
   186
wenzelm@56690
   187
  private val global_state = Synchronized(Document.State.init)
wenzelm@56687
   188
  def current_state(): Document.State = global_state.value
wenzelm@46944
   189
wenzelm@63584
   190
  def recent_syntax(name: Document.Node.Name): Outer_Syntax =
wenzelm@60934
   191
    global_state.value.recent_finished.version.get_finished.nodes(name).syntax getOrElse
wenzelm@64854
   192
    resources.base.syntax
wenzelm@34816
   193
wenzelm@43651
   194
wenzelm@56799
   195
  /* pipelined change parsing */
wenzelm@56799
   196
wenzelm@56799
   197
  private case class Text_Edits(
wenzelm@56799
   198
    previous: Future[Document.Version],
wenzelm@56799
   199
    doc_blobs: Document.Blobs,
wenzelm@56799
   200
    text_edits: List[Document.Edit_Text],
wenzelm@56799
   201
    version_result: Promise[Document.Version])
wenzelm@43651
   202
wenzelm@56799
   203
  private val change_parser = Consumer_Thread.fork[Text_Edits]("change_parser", daemon = true)
wenzelm@56799
   204
  {
wenzelm@56799
   205
    case Text_Edits(previous, doc_blobs, text_edits, version_result) =>
wenzelm@56799
   206
      val prev = previous.get_finished
wenzelm@56799
   207
      val change =
wenzelm@56799
   208
        Timing.timeit("parse_change", timing) {
wenzelm@56799
   209
          resources.parse_change(reparse_limit, prev, doc_blobs, text_edits)
wenzelm@56799
   210
        }
wenzelm@56799
   211
      version_result.fulfill(change.version)
wenzelm@56799
   212
      manager.send(change)
wenzelm@56799
   213
      true
wenzelm@56799
   214
  }
wenzelm@41534
   215
wenzelm@56706
   216
wenzelm@56719
   217
  /* buffered changes */
wenzelm@56719
   218
wenzelm@56719
   219
  private object change_buffer
wenzelm@56719
   220
  {
wenzelm@56719
   221
    private var assignment: Boolean = false
wenzelm@56719
   222
    private var nodes: Set[Document.Node.Name] = Set.empty
wenzelm@56719
   223
    private var commands: Set[Command] = Set.empty
wenzelm@56719
   224
wenzelm@56719
   225
    def flush(): Unit = synchronized {
wenzelm@59319
   226
      if (assignment || nodes.nonEmpty || commands.nonEmpty)
wenzelm@56719
   227
        commands_changed.post(Session.Commands_Changed(assignment, nodes, commands))
wenzelm@56719
   228
      assignment = false
wenzelm@56719
   229
      nodes = Set.empty
wenzelm@56719
   230
      commands = Set.empty
wenzelm@56719
   231
    }
wenzelm@61556
   232
    private val delay_flush = Standard_Thread.delay_first(output_delay) { flush() }
wenzelm@56719
   233
wenzelm@56719
   234
    def invoke(assign: Boolean, cmds: List[Command]): Unit = synchronized {
wenzelm@56719
   235
      assignment |= assign
wenzelm@56719
   236
      for (command <- cmds) {
wenzelm@56719
   237
        nodes += command.node_name
wenzelm@64803
   238
        command.blobs_names.foreach(nodes += _)
wenzelm@56719
   239
        commands += command
wenzelm@56719
   240
      }
wenzelm@56771
   241
      delay_flush.invoke()
wenzelm@56719
   242
    }
wenzelm@56719
   243
wenzelm@56719
   244
    def shutdown()
wenzelm@56719
   245
    {
wenzelm@56771
   246
      delay_flush.revoke()
wenzelm@56719
   247
      flush()
wenzelm@56719
   248
    }
wenzelm@56719
   249
  }
wenzelm@56719
   250
wenzelm@56719
   251
wenzelm@56706
   252
  /* postponed changes */
wenzelm@44733
   253
wenzelm@56706
   254
  private object postponed_changes
wenzelm@56706
   255
  {
wenzelm@56706
   256
    private var postponed: List[Session.Change] = Nil
wenzelm@56706
   257
wenzelm@56706
   258
    def store(change: Session.Change): Unit = synchronized { postponed ::= change }
wenzelm@56705
   259
wenzelm@57976
   260
    def flush(state: Document.State): List[Session.Change] = synchronized {
wenzelm@56706
   261
      val (assigned, unassigned) = postponed.partition(change => state.is_assigned(change.previous))
wenzelm@56706
   262
      postponed = unassigned
wenzelm@57976
   263
      assigned.reverse
wenzelm@56706
   264
    }
wenzelm@56706
   265
  }
wenzelm@44733
   266
wenzelm@56706
   267
wenzelm@56793
   268
  /* prover process */
wenzelm@56793
   269
wenzelm@56793
   270
  private object prover
wenzelm@56793
   271
  {
wenzelm@61590
   272
    private val variable = Synchronized[Option[Prover]](None)
wenzelm@56793
   273
wenzelm@56793
   274
    def defined: Boolean = variable.value.isDefined
wenzelm@56793
   275
    def get: Prover = variable.value.get
wenzelm@56793
   276
    def set(p: Prover) { variable.change(_ => Some(p)) }
wenzelm@56793
   277
    def reset { variable.change(_ => None) }
wenzelm@56793
   278
    def await_reset() { variable.guarded_access({ case None => Some((), None) case _ => None }) }
wenzelm@56793
   279
  }
wenzelm@56793
   280
wenzelm@56793
   281
wenzelm@59366
   282
  /* protocol handlers */
wenzelm@59366
   283
wenzelm@65214
   284
  private val protocol_handlers = Protocol_Handlers.init()
wenzelm@59366
   285
wenzelm@59366
   286
  def get_protocol_handler(name: String): Option[Session.Protocol_Handler] =
wenzelm@65214
   287
    protocol_handlers.get(name)
wenzelm@59366
   288
wenzelm@59367
   289
  def add_protocol_handler(handler: Session.Protocol_Handler): Unit =
wenzelm@65214
   290
    protocol_handlers.add(session, prover.get, handler)
wenzelm@65214
   291
wenzelm@65214
   292
  def add_protocol_handler(name: String): Unit =
wenzelm@65214
   293
    protocol_handlers.add(session, prover.get, name)
wenzelm@59366
   294
wenzelm@59366
   295
wenzelm@56706
   296
  /* manager thread */
wenzelm@44733
   297
wenzelm@62115
   298
  private val delay_prune =
wenzelm@62115
   299
    Standard_Thread.delay_first(prune_delay) { manager.send(Prune_History) }
wenzelm@56771
   300
wenzelm@56706
   301
  private val manager: Consumer_Thread[Any] =
wenzelm@56706
   302
  {
wenzelm@52649
   303
    /* raw edits */
wenzelm@52649
   304
wenzelm@54521
   305
    def handle_raw_edits(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
wenzelm@52649
   306
    //{{{
wenzelm@52649
   307
    {
wenzelm@56793
   308
      require(prover.defined)
wenzelm@56712
   309
wenzelm@52649
   310
      prover.get.discontinue_execution()
wenzelm@52649
   311
wenzelm@56687
   312
      val previous = global_state.value.history.tip.version
wenzelm@52649
   313
      val version = Future.promise[Document.Version]
wenzelm@56711
   314
      global_state.change(_.continue_history(previous, edits, version))
wenzelm@52649
   315
wenzelm@56715
   316
      raw_edits.post(Session.Raw_Edits(doc_blobs, edits))
wenzelm@56704
   317
      change_parser.send(Text_Edits(previous, doc_blobs, edits, version))
wenzelm@52649
   318
    }
wenzelm@52649
   319
    //}}}
wenzelm@52649
   320
wenzelm@52649
   321
wenzelm@43716
   322
    /* resulting changes */
wenzelm@34809
   323
wenzelm@56315
   324
    def handle_change(change: Session.Change)
wenzelm@38221
   325
    //{{{
wenzelm@34809
   326
    {
wenzelm@56793
   327
      require(prover.defined)
wenzelm@56712
   328
wenzelm@44383
   329
      def id_command(command: Command)
wenzelm@43720
   330
      {
wenzelm@54519
   331
        for {
wenzelm@57842
   332
          (name, digest) <- command.blobs_defined
wenzelm@56687
   333
          if !global_state.value.defined_blob(digest)
wenzelm@54519
   334
        } {
wenzelm@57842
   335
          change.version.nodes(name).get_blob match {
wenzelm@54519
   336
            case Some(blob) =>
wenzelm@56687
   337
              global_state.change(_.define_blob(digest))
wenzelm@56335
   338
              prover.get.define_blob(digest, blob.bytes)
wenzelm@55783
   339
            case None =>
wenzelm@57842
   340
              Output.error_message("Missing blob " + quote(name.toString))
wenzelm@54519
   341
          }
wenzelm@54519
   342
        }
wenzelm@54519
   343
wenzelm@56687
   344
        if (!global_state.value.defined_command(command.id)) {
wenzelm@56687
   345
          global_state.change(_.define_command(command))
wenzelm@44644
   346
          prover.get.define_command(command)
wenzelm@43720
   347
        }
wenzelm@43720
   348
      }
wenzelm@56315
   349
      change.doc_edits foreach {
wenzelm@44383
   350
        case (_, edit) =>
wenzelm@44383
   351
          edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command }
wenzelm@44383
   352
      }
wenzelm@43722
   353
wenzelm@56687
   354
      val assignment = global_state.value.the_assignment(change.previous).check_finished
wenzelm@56687
   355
      global_state.change(_.define_version(change.version, assignment))
wenzelm@56315
   356
      prover.get.update(change.previous.id, change.version.id, change.doc_edits)
wenzelm@56316
   357
      resources.commit(change)
wenzelm@34809
   358
    }
wenzelm@38221
   359
    //}}}
wenzelm@34809
   360
wenzelm@34809
   361
wenzelm@46772
   362
    /* prover output */
wenzelm@34809
   363
wenzelm@56385
   364
    def handle_output(output: Prover.Output)
wenzelm@38221
   365
    //{{{
wenzelm@34809
   366
    {
wenzelm@51662
   367
      def bad_output()
wenzelm@43716
   368
      {
wenzelm@43716
   369
        if (verbose)
wenzelm@56782
   370
          Output.warning("Ignoring bad prover output: " + output.message.toString)
wenzelm@43716
   371
      }
wenzelm@43716
   372
wenzelm@52531
   373
      def accumulate(state_id: Document_ID.Generic, message: XML.Elem)
wenzelm@51662
   374
      {
wenzelm@51662
   375
        try {
wenzelm@56687
   376
          val st = global_state.change_result(_.accumulate(state_id, message))
wenzelm@56719
   377
          change_buffer.invoke(false, List(st.command))
wenzelm@51662
   378
        }
wenzelm@51662
   379
        catch {
wenzelm@51662
   380
          case _: Document.State.Fail => bad_output()
wenzelm@51662
   381
        }
wenzelm@51662
   382
      }
wenzelm@51662
   383
wenzelm@54442
   384
      output match {
wenzelm@56385
   385
        case msg: Prover.Protocol_Output =>
wenzelm@65214
   386
          val handled = protocol_handlers.invoke(msg)
wenzelm@54442
   387
          if (!handled) {
wenzelm@54442
   388
            msg.properties match {
wenzelm@56793
   389
              case Markup.Protocol_Handler(name) if prover.defined =>
wenzelm@65214
   390
                add_protocol_handler(name)
wenzelm@54442
   391
wenzelm@56793
   392
              case Protocol.Command_Timing(state_id, timing) if prover.defined =>
wenzelm@54442
   393
                val message = XML.elem(Markup.STATUS, List(XML.Elem(Markup.Timing(timing), Nil)))
wenzelm@54442
   394
                accumulate(state_id, prover.get.xml_cache.elem(message))
wenzelm@46122
   395
wenzelm@54442
   396
              case Markup.Assign_Update =>
wenzelm@54442
   397
                msg.text match {
wenzelm@54442
   398
                  case Protocol.Assign_Update(id, update) =>
wenzelm@54442
   399
                    try {
wenzelm@56687
   400
                      val cmds = global_state.change_result(_.assign(id, update))
wenzelm@56719
   401
                      change_buffer.invoke(true, cmds)
wenzelm@57976
   402
                      manager.send(Session.Change_Flush)
wenzelm@54442
   403
                    }
wenzelm@54442
   404
                    catch { case _: Document.State.Fail => bad_output() }
wenzelm@54442
   405
                  case _ => bad_output()
wenzelm@54442
   406
                }
wenzelm@56771
   407
                delay_prune.invoke()
wenzelm@46122
   408
wenzelm@54442
   409
              case Markup.Removed_Versions =>
wenzelm@54442
   410
                msg.text match {
wenzelm@54442
   411
                  case Protocol.Removed(removed) =>
wenzelm@54442
   412
                    try {
wenzelm@56687
   413
                      global_state.change(_.removed_versions(removed))
wenzelm@57976
   414
                      manager.send(Session.Change_Flush)
wenzelm@54442
   415
                    }
wenzelm@54442
   416
                    catch { case _: Document.State.Fail => bad_output() }
wenzelm@54442
   417
                  case _ => bad_output()
wenzelm@54442
   418
                }
wenzelm@54442
   419
wenzelm@54442
   420
              case Markup.ML_Statistics(props) =>
wenzelm@56715
   421
                statistics.post(Session.Statistics(props))
wenzelm@54442
   422
wenzelm@54442
   423
              case Markup.Task_Statistics(props) =>
wenzelm@54442
   424
                // FIXME
wenzelm@52111
   425
wenzelm@54442
   426
              case _ => bad_output()
wenzelm@54442
   427
            }
wenzelm@54442
   428
          }
wenzelm@54442
   429
        case _ =>
wenzelm@54442
   430
          output.properties match {
wenzelm@54442
   431
            case Position.Id(state_id) =>
wenzelm@54442
   432
              accumulate(state_id, output.message)
wenzelm@56210
   433
wenzelm@54442
   434
            case _ if output.is_init =>
wenzelm@54442
   435
              phase = Session.Ready
wenzelm@56210
   436
wenzelm@54442
   437
            case Markup.Return_Code(rc) if output.is_exit =>
wenzelm@65206
   438
              phase = Session.Terminated(rc)
wenzelm@56793
   439
              prover.reset
wenzelm@56210
   440
wenzelm@61376
   441
            case _ =>
wenzelm@61376
   442
              raw_output_messages.post(output)
wenzelm@44661
   443
          }
wenzelm@52111
   444
        }
wenzelm@34809
   445
    }
wenzelm@38221
   446
    //}}}
wenzelm@34809
   447
wenzelm@34820
   448
wenzelm@56706
   449
    /* main thread */
wenzelm@56706
   450
wenzelm@56710
   451
    Consumer_Thread.fork[Any]("Session.manager", daemon = true)
wenzelm@56709
   452
    {
wenzelm@56709
   453
      case arg: Any =>
wenzelm@56709
   454
        //{{{
wenzelm@56709
   455
        arg match {
wenzelm@56733
   456
          case output: Prover.Output =>
wenzelm@56775
   457
            if (output.is_stdout || output.is_stderr)
wenzelm@56775
   458
              raw_output_messages.post(output)
wenzelm@56733
   459
            else handle_output(output)
wenzelm@56775
   460
wenzelm@56733
   461
            if (output.is_syslog) {
wenzelm@56775
   462
              syslog += output.message
wenzelm@56733
   463
              syslog_messages.post(output)
wenzelm@56733
   464
            }
wenzelm@56775
   465
wenzelm@56733
   466
            all_messages.post(output)
wenzelm@56733
   467
wenzelm@56733
   468
          case input: Prover.Input =>
wenzelm@56733
   469
            all_messages.post(input)
wenzelm@56733
   470
wenzelm@62556
   471
          case Start(start_prover) if !prover.defined =>
wenzelm@65207
   472
            prover.set(start_prover(manager.send(_)))
wenzelm@56706
   473
wenzelm@56709
   474
          case Stop =>
wenzelm@65208
   475
            delay_prune.revoke()
wenzelm@65208
   476
            if (prover.defined) {
wenzelm@65214
   477
              protocol_handlers.stop(prover.get)
wenzelm@56772
   478
              global_state.change(_ => Document.State.init)
wenzelm@56709
   479
              prover.get.terminate
wenzelm@56709
   480
            }
wenzelm@56706
   481
wenzelm@56771
   482
          case Prune_History =>
wenzelm@56793
   483
            if (prover.defined) {
wenzelm@57976
   484
              val old_versions = global_state.change_result(_.remove_versions(prune_size))
wenzelm@59319
   485
              if (old_versions.nonEmpty) prover.get.remove_versions(old_versions)
wenzelm@56771
   486
            }
wenzelm@56771
   487
wenzelm@56709
   488
          case Update_Options(options) =>
wenzelm@56793
   489
            if (prover.defined && is_ready) {
wenzelm@56709
   490
              prover.get.options(options)
wenzelm@56709
   491
              handle_raw_edits(Document.Blobs.empty, Nil)
wenzelm@56709
   492
            }
wenzelm@56715
   493
            global_options.post(Session.Global_Options(options))
wenzelm@34809
   494
wenzelm@56793
   495
          case Cancel_Exec(exec_id) if prover.defined =>
wenzelm@56709
   496
            prover.get.cancel_exec(exec_id)
wenzelm@56706
   497
wenzelm@56793
   498
          case Session.Raw_Edits(doc_blobs, edits) if prover.defined =>
wenzelm@56709
   499
            handle_raw_edits(doc_blobs, edits)
wenzelm@56709
   500
wenzelm@56793
   501
          case Session.Dialog_Result(id, serial, result) if prover.defined =>
wenzelm@56709
   502
            prover.get.dialog_result(serial, result)
wenzelm@56709
   503
            handle_output(new Prover.Output(Protocol.Dialog_Result(id, serial, result)))
wenzelm@56706
   504
wenzelm@59364
   505
          case Session.Build_Theories(id, master_dir, theories) if prover.defined =>
wenzelm@59364
   506
            prover.get.build_theories(id, master_dir, theories)
wenzelm@59362
   507
wenzelm@56793
   508
          case Protocol_Command(name, args) if prover.defined =>
wenzelm@56709
   509
            prover.get.protocol_command(name, args:_*)
wenzelm@56706
   510
wenzelm@56793
   511
          case change: Session.Change if prover.defined =>
wenzelm@57976
   512
            val state = global_state.value
wenzelm@57976
   513
            if (!state.removing_versions && state.is_assigned(change.previous))
wenzelm@56709
   514
              handle_change(change)
wenzelm@56709
   515
            else postponed_changes.store(change)
wenzelm@57651
   516
wenzelm@57976
   517
          case Session.Change_Flush if prover.defined =>
wenzelm@57976
   518
            val state = global_state.value
wenzelm@57976
   519
            if (!state.removing_versions)
wenzelm@57976
   520
              postponed_changes.flush(state).foreach(handle_change(_))
wenzelm@57976
   521
wenzelm@57651
   522
          case bad =>
wenzelm@57651
   523
            if (verbose) Output.warning("Ignoring bad message: " + bad.toString)
wenzelm@56706
   524
        }
wenzelm@56709
   525
        true
wenzelm@56709
   526
        //}}}
wenzelm@56709
   527
    }
wenzelm@34777
   528
  }
wenzelm@34777
   529
wenzelm@34809
   530
wenzelm@56799
   531
  /* main operations */
wenzelm@56799
   532
wenzelm@56799
   533
  def snapshot(name: Document.Node.Name = Document.Node.Name.empty,
wenzelm@56799
   534
      pending_edits: List[Text.Edit] = Nil): Document.Snapshot =
wenzelm@56799
   535
    global_state.value.snapshot(name, pending_edits)
wenzelm@34809
   536
wenzelm@62556
   537
  def start(start_prover: Prover.Receiver => Prover)
wenzelm@65207
   538
  {
wenzelm@65207
   539
    _phase.change(
wenzelm@65207
   540
      {
wenzelm@65207
   541
        case Session.Inactive =>
wenzelm@65207
   542
          manager.send(Start(start_prover))
wenzelm@65208
   543
          post_phase(Session.Startup)
wenzelm@65207
   544
        case phase => error("Cannot start prover in phase " + quote(phase.print))
wenzelm@65207
   545
      })
wenzelm@65207
   546
  }
wenzelm@45076
   547
wenzelm@65209
   548
  def stop(): Int =
wenzelm@50117
   549
  {
wenzelm@65208
   550
    val was_ready =
wenzelm@65208
   551
      _phase.guarded_access(phase =>
wenzelm@65208
   552
        phase match {
wenzelm@65208
   553
          case Session.Startup | Session.Shutdown => None
wenzelm@65208
   554
          case Session.Terminated(_) => Some((false, phase))
wenzelm@65208
   555
          case Session.Inactive => Some((false, post_phase(Session.Terminated(0))))
wenzelm@65208
   556
          case Session.Ready => Some((true, post_phase(Session.Shutdown)))
wenzelm@65208
   557
        })
wenzelm@65208
   558
    if (was_ready) manager.send_wait(Stop)
wenzelm@56793
   559
    prover.await_reset()
wenzelm@65208
   560
wenzelm@56704
   561
    change_parser.shutdown()
wenzelm@56706
   562
    change_buffer.shutdown()
wenzelm@56706
   563
    manager.shutdown()
wenzelm@56715
   564
    dispatcher.shutdown()
wenzelm@65209
   565
wenzelm@65209
   566
    phase match {
wenzelm@65209
   567
      case Session.Terminated(rc) => rc
wenzelm@65209
   568
      case phase => error("Bad session phase after shutdown: " + quote(phase.print))
wenzelm@65209
   569
    }
wenzelm@50117
   570
  }
wenzelm@38841
   571
wenzelm@53054
   572
  def protocol_command(name: String, args: String*)
wenzelm@56706
   573
  { manager.send(Protocol_Command(name, args.toList)) }
wenzelm@53054
   574
wenzelm@56706
   575
  def cancel_exec(exec_id: Document_ID.Exec)
wenzelm@56706
   576
  { manager.send(Cancel_Exec(exec_id)) }
wenzelm@52931
   577
wenzelm@54521
   578
  def update(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
wenzelm@59319
   579
  { if (edits.nonEmpty) manager.send_wait(Session.Raw_Edits(doc_blobs, edits)) }
wenzelm@50498
   580
wenzelm@52084
   581
  def update_options(options: Options)
wenzelm@56706
   582
  { manager.send_wait(Update_Options(options)) }
wenzelm@52084
   583
wenzelm@52531
   584
  def dialog_result(id: Document_ID.Generic, serial: Long, result: String)
wenzelm@56706
   585
  { manager.send(Session.Dialog_Result(id, serial, result)) }
wenzelm@59362
   586
wenzelm@59364
   587
  def build_theories(id: String, master_dir: Path, theories: List[(Options, List[Path])])
wenzelm@59364
   588
  { manager.send(Session.Build_Theories(id, master_dir, theories)) }
wenzelm@34777
   589
}