src/Pure/PIDE/session.scala
author wenzelm
Sat May 05 22:33:35 2018 +0200 (18 months ago ago)
changeset 68090 0763d4eb3ebc
parent 67825 f9c071cc958b
child 68104 0699a0bacc50
permissions -rw-r--r--
protocol message for export of theory resources;
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@65317
    74
        case Terminated(result) => if (result.ok) "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@65317
    82
  case class Terminated(result: Process_Result) 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@65219
   110
    def init(session: Session): Unit = {}
wenzelm@65219
   111
    def exit(): Unit = {}
wenzelm@65219
   112
    val functions: List[(String, Prover.Protocol_Output => Boolean)]
wenzelm@52111
   113
  }
wenzelm@34791
   114
}
wenzelm@34791
   115
wenzelm@34777
   116
wenzelm@65264
   117
class Session(session_options: => Options, val resources: Resources) extends Document.Session
wenzelm@34777
   118
{
wenzelm@65214
   119
  session =>
wenzelm@65214
   120
wenzelm@65218
   121
  val xml_cache: XML.Cache = new XML.Cache()
wenzelm@65218
   122
wenzelm@65214
   123
wenzelm@47653
   124
  /* global flags */
wenzelm@47653
   125
wenzelm@47653
   126
  @volatile var timing: Boolean = false
wenzelm@47653
   127
  @volatile var verbose: Boolean = false
wenzelm@47653
   128
wenzelm@47653
   129
wenzelm@65264
   130
  /* dynamic session options */
wenzelm@49288
   131
wenzelm@65264
   132
  def output_delay: Time = session_options.seconds("editor_output_delay")
wenzelm@66382
   133
  def consolidate_delay: Time = session_options.seconds("editor_consolidate_delay")
wenzelm@65264
   134
  def prune_delay: Time = session_options.seconds("editor_prune_delay")
wenzelm@65264
   135
  def prune_size: Int = session_options.int("editor_prune_size")
wenzelm@65264
   136
  def syslog_limit: Int = session_options.int("editor_syslog_limit")
wenzelm@65264
   137
  def reparse_limit: Int = session_options.int("editor_reparse_limit")
wenzelm@37849
   138
wenzelm@37849
   139
wenzelm@66094
   140
  /* dispatcher */
wenzelm@56715
   141
wenzelm@56715
   142
  private val dispatcher =
wenzelm@56715
   143
    Consumer_Thread.fork[() => Unit]("Session.dispatcher", daemon = true) { case e => e(); true }
wenzelm@34809
   144
wenzelm@66094
   145
  def assert_dispatcher[A](body: => A): A =
wenzelm@66094
   146
  {
wenzelm@66094
   147
    assert(dispatcher.check_thread)
wenzelm@66094
   148
    body
wenzelm@66094
   149
  }
wenzelm@66094
   150
wenzelm@66094
   151
  def require_dispatcher[A](body: => A): A =
wenzelm@66094
   152
  {
wenzelm@66094
   153
    require(dispatcher.check_thread)
wenzelm@66094
   154
    body
wenzelm@66094
   155
  }
wenzelm@66094
   156
wenzelm@66094
   157
  def send_dispatcher(body: => Unit): Unit =
wenzelm@66094
   158
  {
wenzelm@66094
   159
    if (dispatcher.check_thread) body
wenzelm@66094
   160
    else dispatcher.send(() => body)
wenzelm@66094
   161
  }
wenzelm@66094
   162
wenzelm@66094
   163
  def send_wait_dispatcher(body: => Unit): Unit =
wenzelm@66094
   164
  {
wenzelm@66094
   165
    if (dispatcher.check_thread) body
wenzelm@66094
   166
    else dispatcher.send_wait(() => body)
wenzelm@66094
   167
  }
wenzelm@66094
   168
wenzelm@66094
   169
wenzelm@66094
   170
  /* outlets */
wenzelm@66094
   171
wenzelm@56715
   172
  val statistics = new Session.Outlet[Session.Statistics](dispatcher)
wenzelm@56715
   173
  val global_options = new Session.Outlet[Session.Global_Options](dispatcher)
wenzelm@56715
   174
  val caret_focus = new Session.Outlet[Session.Caret_Focus.type](dispatcher)
wenzelm@56715
   175
  val raw_edits = new Session.Outlet[Session.Raw_Edits](dispatcher)
wenzelm@56715
   176
  val commands_changed = new Session.Outlet[Session.Commands_Changed](dispatcher)
wenzelm@56715
   177
  val phase_changed = new Session.Outlet[Session.Phase](dispatcher)
wenzelm@56715
   178
  val syslog_messages = new Session.Outlet[Prover.Output](dispatcher)
wenzelm@56715
   179
  val raw_output_messages = new Session.Outlet[Prover.Output](dispatcher)
wenzelm@56715
   180
  val trace_events = new Session.Outlet[Simplifier_Trace.Event.type](dispatcher)
wenzelm@60900
   181
  val debugger_updates = new Session.Outlet[Debugger.Update.type](dispatcher)
wenzelm@34809
   182
wenzelm@60749
   183
  val all_messages = new Session.Outlet[Prover.Message](dispatcher)  // potential bottle-neck!
wenzelm@34809
   184
wenzelm@56706
   185
wenzelm@56799
   186
  /** main protocol manager **/
wenzelm@45635
   187
wenzelm@56799
   188
  /* internal messages */
wenzelm@45635
   189
wenzelm@62556
   190
  private case class Start(start_prover: Prover.Receiver => Prover)
wenzelm@56799
   191
  private case object Stop
wenzelm@56799
   192
  private case class Cancel_Exec(exec_id: Document_ID.Exec)
wenzelm@56799
   193
  private case class Protocol_Command(name: String, args: List[String])
wenzelm@56799
   194
  private case class Update_Options(options: Options)
wenzelm@66382
   195
  private case object Consolidate_Execution
wenzelm@56799
   196
  private case object Prune_History
wenzelm@45635
   197
wenzelm@38841
   198
wenzelm@65208
   199
  /* phase */
wenzelm@65208
   200
wenzelm@65208
   201
  private def post_phase(new_phase: Session.Phase): Session.Phase =
wenzelm@65208
   202
  {
wenzelm@65208
   203
    phase_changed.post(new_phase)
wenzelm@65208
   204
    new_phase
wenzelm@65208
   205
  }
wenzelm@65208
   206
  private val _phase = Synchronized[Session.Phase](Session.Inactive)
wenzelm@65208
   207
  private def phase_=(new_phase: Session.Phase): Unit = _phase.change(_ => post_phase(new_phase))
wenzelm@65208
   208
wenzelm@65208
   209
  def phase = _phase.value
wenzelm@65208
   210
  def is_ready: Boolean = phase == Session.Ready
wenzelm@65208
   211
wenzelm@65208
   212
wenzelm@43651
   213
  /* global state */
wenzelm@43644
   214
wenzelm@56775
   215
  private val syslog = new Session.Syslog(syslog_limit)
wenzelm@56775
   216
  def syslog_content(): String = syslog.content
wenzelm@39626
   217
wenzelm@56690
   218
  private val global_state = Synchronized(Document.State.init)
wenzelm@56687
   219
  def current_state(): Document.State = global_state.value
wenzelm@46944
   220
wenzelm@63584
   221
  def recent_syntax(name: Document.Node.Name): Outer_Syntax =
wenzelm@60934
   222
    global_state.value.recent_finished.version.get_finished.nodes(name).syntax getOrElse
wenzelm@66720
   223
    resources.session_base.overall_syntax
wenzelm@34816
   224
wenzelm@43651
   225
wenzelm@56799
   226
  /* pipelined change parsing */
wenzelm@56799
   227
wenzelm@56799
   228
  private case class Text_Edits(
wenzelm@56799
   229
    previous: Future[Document.Version],
wenzelm@56799
   230
    doc_blobs: Document.Blobs,
wenzelm@56799
   231
    text_edits: List[Document.Edit_Text],
wenzelm@56799
   232
    version_result: Promise[Document.Version])
wenzelm@43651
   233
wenzelm@56799
   234
  private val change_parser = Consumer_Thread.fork[Text_Edits]("change_parser", daemon = true)
wenzelm@56799
   235
  {
wenzelm@56799
   236
    case Text_Edits(previous, doc_blobs, text_edits, version_result) =>
wenzelm@56799
   237
      val prev = previous.get_finished
wenzelm@56799
   238
      val change =
wenzelm@56799
   239
        Timing.timeit("parse_change", timing) {
wenzelm@56799
   240
          resources.parse_change(reparse_limit, prev, doc_blobs, text_edits)
wenzelm@56799
   241
        }
wenzelm@56799
   242
      version_result.fulfill(change.version)
wenzelm@56799
   243
      manager.send(change)
wenzelm@56799
   244
      true
wenzelm@56799
   245
  }
wenzelm@41534
   246
wenzelm@56706
   247
wenzelm@56719
   248
  /* buffered changes */
wenzelm@56719
   249
wenzelm@56719
   250
  private object change_buffer
wenzelm@56719
   251
  {
wenzelm@56719
   252
    private var assignment: Boolean = false
wenzelm@56719
   253
    private var nodes: Set[Document.Node.Name] = Set.empty
wenzelm@56719
   254
    private var commands: Set[Command] = Set.empty
wenzelm@56719
   255
wenzelm@56719
   256
    def flush(): Unit = synchronized {
wenzelm@59319
   257
      if (assignment || nodes.nonEmpty || commands.nonEmpty)
wenzelm@56719
   258
        commands_changed.post(Session.Commands_Changed(assignment, nodes, commands))
wenzelm@56719
   259
      assignment = false
wenzelm@56719
   260
      nodes = Set.empty
wenzelm@56719
   261
      commands = Set.empty
wenzelm@56719
   262
    }
wenzelm@61556
   263
    private val delay_flush = Standard_Thread.delay_first(output_delay) { flush() }
wenzelm@56719
   264
wenzelm@56719
   265
    def invoke(assign: Boolean, cmds: List[Command]): Unit = synchronized {
wenzelm@56719
   266
      assignment |= assign
wenzelm@56719
   267
      for (command <- cmds) {
wenzelm@56719
   268
        nodes += command.node_name
wenzelm@64803
   269
        command.blobs_names.foreach(nodes += _)
wenzelm@56719
   270
        commands += command
wenzelm@56719
   271
      }
wenzelm@56771
   272
      delay_flush.invoke()
wenzelm@56719
   273
    }
wenzelm@56719
   274
wenzelm@56719
   275
    def shutdown()
wenzelm@56719
   276
    {
wenzelm@56771
   277
      delay_flush.revoke()
wenzelm@56719
   278
      flush()
wenzelm@56719
   279
    }
wenzelm@56719
   280
  }
wenzelm@56719
   281
wenzelm@56719
   282
wenzelm@56706
   283
  /* postponed changes */
wenzelm@44733
   284
wenzelm@56706
   285
  private object postponed_changes
wenzelm@56706
   286
  {
wenzelm@56706
   287
    private var postponed: List[Session.Change] = Nil
wenzelm@56706
   288
wenzelm@56706
   289
    def store(change: Session.Change): Unit = synchronized { postponed ::= change }
wenzelm@56705
   290
wenzelm@57976
   291
    def flush(state: Document.State): List[Session.Change] = synchronized {
wenzelm@56706
   292
      val (assigned, unassigned) = postponed.partition(change => state.is_assigned(change.previous))
wenzelm@56706
   293
      postponed = unassigned
wenzelm@57976
   294
      assigned.reverse
wenzelm@56706
   295
    }
wenzelm@56706
   296
  }
wenzelm@44733
   297
wenzelm@56706
   298
wenzelm@56793
   299
  /* prover process */
wenzelm@56793
   300
wenzelm@56793
   301
  private object prover
wenzelm@56793
   302
  {
wenzelm@61590
   303
    private val variable = Synchronized[Option[Prover]](None)
wenzelm@56793
   304
wenzelm@56793
   305
    def defined: Boolean = variable.value.isDefined
wenzelm@56793
   306
    def get: Prover = variable.value.get
wenzelm@56793
   307
    def set(p: Prover) { variable.change(_ => Some(p)) }
wenzelm@56793
   308
    def reset { variable.change(_ => None) }
wenzelm@56793
   309
    def await_reset() { variable.guarded_access({ case None => Some((), None) case _ => None }) }
wenzelm@56793
   310
  }
wenzelm@56793
   311
wenzelm@56793
   312
wenzelm@59366
   313
  /* protocol handlers */
wenzelm@59366
   314
wenzelm@65215
   315
  private val protocol_handlers = Protocol_Handlers.init(session)
wenzelm@59366
   316
wenzelm@59366
   317
  def get_protocol_handler(name: String): Option[Session.Protocol_Handler] =
wenzelm@65214
   318
    protocol_handlers.get(name)
wenzelm@59366
   319
wenzelm@65315
   320
  def init_protocol_handler(handler: Session.Protocol_Handler): Unit =
wenzelm@65315
   321
    protocol_handlers.init(handler)
wenzelm@65214
   322
wenzelm@65315
   323
  def init_protocol_handler(name: String): Unit =
wenzelm@65315
   324
    protocol_handlers.init(name)
wenzelm@59366
   325
wenzelm@59366
   326
wenzelm@65222
   327
  /* debugger */
wenzelm@65222
   328
wenzelm@65222
   329
  private val debugger_handler = new Debugger.Handler(this)
wenzelm@65315
   330
  init_protocol_handler(debugger_handler)
wenzelm@65222
   331
wenzelm@65222
   332
  def debugger: Debugger = debugger_handler.debugger
wenzelm@65222
   333
wenzelm@65222
   334
wenzelm@56706
   335
  /* manager thread */
wenzelm@44733
   336
wenzelm@62115
   337
  private val delay_prune =
wenzelm@62115
   338
    Standard_Thread.delay_first(prune_delay) { manager.send(Prune_History) }
wenzelm@56771
   339
wenzelm@56706
   340
  private val manager: Consumer_Thread[Any] =
wenzelm@56706
   341
  {
wenzelm@52649
   342
    /* raw edits */
wenzelm@52649
   343
wenzelm@54521
   344
    def handle_raw_edits(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
wenzelm@52649
   345
    //{{{
wenzelm@52649
   346
    {
wenzelm@56793
   347
      require(prover.defined)
wenzelm@56712
   348
wenzelm@52649
   349
      prover.get.discontinue_execution()
wenzelm@52649
   350
wenzelm@56687
   351
      val previous = global_state.value.history.tip.version
wenzelm@52649
   352
      val version = Future.promise[Document.Version]
wenzelm@56711
   353
      global_state.change(_.continue_history(previous, edits, version))
wenzelm@52649
   354
wenzelm@56715
   355
      raw_edits.post(Session.Raw_Edits(doc_blobs, edits))
wenzelm@56704
   356
      change_parser.send(Text_Edits(previous, doc_blobs, edits, version))
wenzelm@52649
   357
    }
wenzelm@52649
   358
    //}}}
wenzelm@52649
   359
wenzelm@52649
   360
wenzelm@43716
   361
    /* resulting changes */
wenzelm@34809
   362
wenzelm@56315
   363
    def handle_change(change: Session.Change)
wenzelm@38221
   364
    //{{{
wenzelm@34809
   365
    {
wenzelm@56793
   366
      require(prover.defined)
wenzelm@56712
   367
wenzelm@44383
   368
      def id_command(command: Command)
wenzelm@43720
   369
      {
wenzelm@54519
   370
        for {
wenzelm@57842
   371
          (name, digest) <- command.blobs_defined
wenzelm@56687
   372
          if !global_state.value.defined_blob(digest)
wenzelm@54519
   373
        } {
wenzelm@57842
   374
          change.version.nodes(name).get_blob match {
wenzelm@54519
   375
            case Some(blob) =>
wenzelm@56687
   376
              global_state.change(_.define_blob(digest))
wenzelm@56335
   377
              prover.get.define_blob(digest, blob.bytes)
wenzelm@55783
   378
            case None =>
wenzelm@57842
   379
              Output.error_message("Missing blob " + quote(name.toString))
wenzelm@54519
   380
          }
wenzelm@54519
   381
        }
wenzelm@54519
   382
wenzelm@56687
   383
        if (!global_state.value.defined_command(command.id)) {
wenzelm@56687
   384
          global_state.change(_.define_command(command))
wenzelm@44644
   385
          prover.get.define_command(command)
wenzelm@43720
   386
        }
wenzelm@43720
   387
      }
wenzelm@56315
   388
      change.doc_edits foreach {
wenzelm@44383
   389
        case (_, edit) =>
wenzelm@44383
   390
          edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command }
wenzelm@44383
   391
      }
wenzelm@43722
   392
wenzelm@56687
   393
      val assignment = global_state.value.the_assignment(change.previous).check_finished
wenzelm@56687
   394
      global_state.change(_.define_version(change.version, assignment))
wenzelm@56315
   395
      prover.get.update(change.previous.id, change.version.id, change.doc_edits)
wenzelm@56316
   396
      resources.commit(change)
wenzelm@34809
   397
    }
wenzelm@38221
   398
    //}}}
wenzelm@34809
   399
wenzelm@34809
   400
wenzelm@46772
   401
    /* prover output */
wenzelm@34809
   402
wenzelm@56385
   403
    def handle_output(output: Prover.Output)
wenzelm@38221
   404
    //{{{
wenzelm@34809
   405
    {
wenzelm@51662
   406
      def bad_output()
wenzelm@43716
   407
      {
wenzelm@43716
   408
        if (verbose)
wenzelm@56782
   409
          Output.warning("Ignoring bad prover output: " + output.message.toString)
wenzelm@43716
   410
      }
wenzelm@43716
   411
wenzelm@52531
   412
      def accumulate(state_id: Document_ID.Generic, message: XML.Elem)
wenzelm@51662
   413
      {
wenzelm@51662
   414
        try {
wenzelm@67825
   415
          val st = global_state.change_result(_.accumulate(state_id, message, xml_cache))
wenzelm@56719
   416
          change_buffer.invoke(false, List(st.command))
wenzelm@51662
   417
        }
wenzelm@51662
   418
        catch {
wenzelm@51662
   419
          case _: Document.State.Fail => bad_output()
wenzelm@51662
   420
        }
wenzelm@51662
   421
      }
wenzelm@51662
   422
wenzelm@54442
   423
      output match {
wenzelm@56385
   424
        case msg: Prover.Protocol_Output =>
wenzelm@65214
   425
          val handled = protocol_handlers.invoke(msg)
wenzelm@54442
   426
          if (!handled) {
wenzelm@54442
   427
            msg.properties match {
wenzelm@56793
   428
              case Markup.Protocol_Handler(name) if prover.defined =>
wenzelm@65315
   429
                init_protocol_handler(name)
wenzelm@54442
   430
wenzelm@56793
   431
              case Protocol.Command_Timing(state_id, timing) if prover.defined =>
wenzelm@54442
   432
                val message = XML.elem(Markup.STATUS, List(XML.Elem(Markup.Timing(timing), Nil)))
wenzelm@65218
   433
                accumulate(state_id, xml_cache.elem(message))
wenzelm@46122
   434
wenzelm@66873
   435
              case Protocol.Theory_Timing(_, _) =>
wenzelm@66873
   436
                // FIXME
wenzelm@66873
   437
wenzelm@68090
   438
              case Markup.Export(_) =>
wenzelm@68090
   439
                // FIXME
wenzelm@68090
   440
wenzelm@54442
   441
              case Markup.Assign_Update =>
wenzelm@54442
   442
                msg.text match {
wenzelm@54442
   443
                  case Protocol.Assign_Update(id, update) =>
wenzelm@54442
   444
                    try {
wenzelm@56687
   445
                      val cmds = global_state.change_result(_.assign(id, update))
wenzelm@56719
   446
                      change_buffer.invoke(true, cmds)
wenzelm@57976
   447
                      manager.send(Session.Change_Flush)
wenzelm@54442
   448
                    }
wenzelm@54442
   449
                    catch { case _: Document.State.Fail => bad_output() }
wenzelm@54442
   450
                  case _ => bad_output()
wenzelm@54442
   451
                }
wenzelm@56771
   452
                delay_prune.invoke()
wenzelm@46122
   453
wenzelm@54442
   454
              case Markup.Removed_Versions =>
wenzelm@54442
   455
                msg.text match {
wenzelm@54442
   456
                  case Protocol.Removed(removed) =>
wenzelm@54442
   457
                    try {
wenzelm@56687
   458
                      global_state.change(_.removed_versions(removed))
wenzelm@57976
   459
                      manager.send(Session.Change_Flush)
wenzelm@54442
   460
                    }
wenzelm@54442
   461
                    catch { case _: Document.State.Fail => bad_output() }
wenzelm@54442
   462
                  case _ => bad_output()
wenzelm@54442
   463
                }
wenzelm@54442
   464
wenzelm@54442
   465
              case Markup.ML_Statistics(props) =>
wenzelm@56715
   466
                statistics.post(Session.Statistics(props))
wenzelm@54442
   467
wenzelm@54442
   468
              case Markup.Task_Statistics(props) =>
wenzelm@54442
   469
                // FIXME
wenzelm@52111
   470
wenzelm@54442
   471
              case _ => bad_output()
wenzelm@54442
   472
            }
wenzelm@54442
   473
          }
wenzelm@54442
   474
        case _ =>
wenzelm@54442
   475
          output.properties match {
wenzelm@54442
   476
            case Position.Id(state_id) =>
wenzelm@54442
   477
              accumulate(state_id, output.message)
wenzelm@56210
   478
wenzelm@54442
   479
            case _ if output.is_init =>
wenzelm@65264
   480
              prover.get.options(session_options)
wenzelm@65470
   481
              prover.get.session_base(resources)
wenzelm@54442
   482
              phase = Session.Ready
wenzelm@65223
   483
              debugger.ready()
wenzelm@56210
   484
wenzelm@65317
   485
            case Markup.Process_Result(result) if output.is_exit =>
wenzelm@65317
   486
              phase = Session.Terminated(result)
wenzelm@56793
   487
              prover.reset
wenzelm@56210
   488
wenzelm@61376
   489
            case _ =>
wenzelm@61376
   490
              raw_output_messages.post(output)
wenzelm@44661
   491
          }
wenzelm@52111
   492
        }
wenzelm@34809
   493
    }
wenzelm@38221
   494
    //}}}
wenzelm@34809
   495
wenzelm@34820
   496
wenzelm@56706
   497
    /* main thread */
wenzelm@56706
   498
wenzelm@56710
   499
    Consumer_Thread.fork[Any]("Session.manager", daemon = true)
wenzelm@56709
   500
    {
wenzelm@56709
   501
      case arg: Any =>
wenzelm@56709
   502
        //{{{
wenzelm@56709
   503
        arg match {
wenzelm@56733
   504
          case output: Prover.Output =>
wenzelm@56775
   505
            if (output.is_stdout || output.is_stderr)
wenzelm@56775
   506
              raw_output_messages.post(output)
wenzelm@56733
   507
            else handle_output(output)
wenzelm@56775
   508
wenzelm@56733
   509
            if (output.is_syslog) {
wenzelm@56775
   510
              syslog += output.message
wenzelm@56733
   511
              syslog_messages.post(output)
wenzelm@56733
   512
            }
wenzelm@56775
   513
wenzelm@56733
   514
            all_messages.post(output)
wenzelm@56733
   515
wenzelm@56733
   516
          case input: Prover.Input =>
wenzelm@56733
   517
            all_messages.post(input)
wenzelm@56733
   518
wenzelm@62556
   519
          case Start(start_prover) if !prover.defined =>
wenzelm@65207
   520
            prover.set(start_prover(manager.send(_)))
wenzelm@56706
   521
wenzelm@56709
   522
          case Stop =>
wenzelm@65208
   523
            delay_prune.revoke()
wenzelm@65208
   524
            if (prover.defined) {
wenzelm@65219
   525
              protocol_handlers.exit()
wenzelm@56772
   526
              global_state.change(_ => Document.State.init)
wenzelm@56709
   527
              prover.get.terminate
wenzelm@56709
   528
            }
wenzelm@56706
   529
wenzelm@66382
   530
          case Consolidate_Execution =>
wenzelm@66382
   531
            if (prover.defined) prover.get.consolidate_execution()
wenzelm@66382
   532
wenzelm@56771
   533
          case Prune_History =>
wenzelm@56793
   534
            if (prover.defined) {
wenzelm@57976
   535
              val old_versions = global_state.change_result(_.remove_versions(prune_size))
wenzelm@59319
   536
              if (old_versions.nonEmpty) prover.get.remove_versions(old_versions)
wenzelm@56771
   537
            }
wenzelm@56771
   538
wenzelm@56709
   539
          case Update_Options(options) =>
wenzelm@56793
   540
            if (prover.defined && is_ready) {
wenzelm@56709
   541
              prover.get.options(options)
wenzelm@56709
   542
              handle_raw_edits(Document.Blobs.empty, Nil)
wenzelm@56709
   543
            }
wenzelm@56715
   544
            global_options.post(Session.Global_Options(options))
wenzelm@34809
   545
wenzelm@56793
   546
          case Cancel_Exec(exec_id) if prover.defined =>
wenzelm@56709
   547
            prover.get.cancel_exec(exec_id)
wenzelm@56706
   548
wenzelm@56793
   549
          case Session.Raw_Edits(doc_blobs, edits) if prover.defined =>
wenzelm@56709
   550
            handle_raw_edits(doc_blobs, edits)
wenzelm@56709
   551
wenzelm@56793
   552
          case Session.Dialog_Result(id, serial, result) if prover.defined =>
wenzelm@56709
   553
            prover.get.dialog_result(serial, result)
wenzelm@56709
   554
            handle_output(new Prover.Output(Protocol.Dialog_Result(id, serial, result)))
wenzelm@56706
   555
wenzelm@56793
   556
          case Protocol_Command(name, args) if prover.defined =>
wenzelm@56709
   557
            prover.get.protocol_command(name, args:_*)
wenzelm@56706
   558
wenzelm@56793
   559
          case change: Session.Change if prover.defined =>
wenzelm@57976
   560
            val state = global_state.value
wenzelm@57976
   561
            if (!state.removing_versions && state.is_assigned(change.previous))
wenzelm@56709
   562
              handle_change(change)
wenzelm@56709
   563
            else postponed_changes.store(change)
wenzelm@57651
   564
wenzelm@57976
   565
          case Session.Change_Flush if prover.defined =>
wenzelm@57976
   566
            val state = global_state.value
wenzelm@57976
   567
            if (!state.removing_versions)
wenzelm@57976
   568
              postponed_changes.flush(state).foreach(handle_change(_))
wenzelm@57976
   569
wenzelm@57651
   570
          case bad =>
wenzelm@57651
   571
            if (verbose) Output.warning("Ignoring bad message: " + bad.toString)
wenzelm@56706
   572
        }
wenzelm@56709
   573
        true
wenzelm@56709
   574
        //}}}
wenzelm@56709
   575
    }
wenzelm@34777
   576
  }
wenzelm@34777
   577
wenzelm@66382
   578
  private val consolidator: Thread =
wenzelm@66382
   579
    Standard_Thread.fork("Session.consolidator", daemon = true) {
wenzelm@66382
   580
      try {
wenzelm@66382
   581
        while (true) {
wenzelm@66382
   582
          Thread.sleep(consolidate_delay.ms)
wenzelm@66382
   583
wenzelm@66382
   584
          val state = global_state.value
wenzelm@66382
   585
          state.stable_tip_version match {
wenzelm@66382
   586
            case None =>
wenzelm@66382
   587
            case Some(version) =>
wenzelm@66382
   588
              val consolidated =
wenzelm@66382
   589
                version.nodes.iterator.forall(
wenzelm@66382
   590
                  { case (name, _) =>
wenzelm@66382
   591
                      resources.session_base.loaded_theory(name) ||
wenzelm@66382
   592
                      state.node_consolidated(version, name) })
wenzelm@66382
   593
              if (!consolidated) manager.send(Consolidate_Execution)
wenzelm@66382
   594
          }
wenzelm@66382
   595
        }
wenzelm@66382
   596
      }
wenzelm@66382
   597
      catch { case Exn.Interrupt() => }
wenzelm@66382
   598
    }
wenzelm@66382
   599
wenzelm@34809
   600
wenzelm@56799
   601
  /* main operations */
wenzelm@56799
   602
wenzelm@56799
   603
  def snapshot(name: Document.Node.Name = Document.Node.Name.empty,
wenzelm@56799
   604
      pending_edits: List[Text.Edit] = Nil): Document.Snapshot =
wenzelm@56799
   605
    global_state.value.snapshot(name, pending_edits)
wenzelm@34809
   606
wenzelm@62556
   607
  def start(start_prover: Prover.Receiver => Prover)
wenzelm@65207
   608
  {
wenzelm@65207
   609
    _phase.change(
wenzelm@65207
   610
      {
wenzelm@65207
   611
        case Session.Inactive =>
wenzelm@65207
   612
          manager.send(Start(start_prover))
wenzelm@65208
   613
          post_phase(Session.Startup)
wenzelm@65207
   614
        case phase => error("Cannot start prover in phase " + quote(phase.print))
wenzelm@65207
   615
      })
wenzelm@65207
   616
  }
wenzelm@45076
   617
wenzelm@65311
   618
  def send_stop()
wenzelm@50117
   619
  {
wenzelm@65208
   620
    val was_ready =
wenzelm@65208
   621
      _phase.guarded_access(phase =>
wenzelm@65208
   622
        phase match {
wenzelm@65208
   623
          case Session.Startup | Session.Shutdown => None
wenzelm@65208
   624
          case Session.Terminated(_) => Some((false, phase))
wenzelm@65317
   625
          case Session.Inactive => Some((false, post_phase(Session.Terminated(Process_Result(0)))))
wenzelm@65208
   626
          case Session.Ready => Some((true, post_phase(Session.Shutdown)))
wenzelm@65208
   627
        })
wenzelm@65311
   628
    if (was_ready) manager.send(Stop)
wenzelm@65311
   629
  }
wenzelm@65311
   630
wenzelm@65317
   631
  def stop(): Process_Result =
wenzelm@65311
   632
  {
wenzelm@65311
   633
    send_stop()
wenzelm@56793
   634
    prover.await_reset()
wenzelm@65208
   635
wenzelm@56704
   636
    change_parser.shutdown()
wenzelm@56706
   637
    change_buffer.shutdown()
wenzelm@66382
   638
    consolidator.interrupt
wenzelm@66382
   639
    consolidator.join
wenzelm@56706
   640
    manager.shutdown()
wenzelm@56715
   641
    dispatcher.shutdown()
wenzelm@65209
   642
wenzelm@65209
   643
    phase match {
wenzelm@65317
   644
      case Session.Terminated(result) => result
wenzelm@65209
   645
      case phase => error("Bad session phase after shutdown: " + quote(phase.print))
wenzelm@65209
   646
    }
wenzelm@50117
   647
  }
wenzelm@38841
   648
wenzelm@53054
   649
  def protocol_command(name: String, args: String*)
wenzelm@56706
   650
  { manager.send(Protocol_Command(name, args.toList)) }
wenzelm@53054
   651
wenzelm@56706
   652
  def cancel_exec(exec_id: Document_ID.Exec)
wenzelm@56706
   653
  { manager.send(Cancel_Exec(exec_id)) }
wenzelm@52931
   654
wenzelm@54521
   655
  def update(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
wenzelm@59319
   656
  { if (edits.nonEmpty) manager.send_wait(Session.Raw_Edits(doc_blobs, edits)) }
wenzelm@50498
   657
wenzelm@52084
   658
  def update_options(options: Options)
wenzelm@56706
   659
  { manager.send_wait(Update_Options(options)) }
wenzelm@52084
   660
wenzelm@52531
   661
  def dialog_result(id: Document_ID.Generic, serial: Long, result: String)
wenzelm@56706
   662
  { manager.send(Session.Dialog_Result(id, serial, result)) }
wenzelm@34777
   663
}