src/Pure/System/isabelle_process.scala
author wenzelm
Sat, 28 Mar 2020 14:01:45 +0100
changeset 71607 d97f504c8145
parent 71605 f7a652732f4e
child 71639 ec84f542e411
permissions -rw-r--r--
clarified Isabelle_Process phases;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43283
446e6621762d updated headers;
wenzelm
parents: 40848
diff changeset
     1
/*  Title:      Pure/System/isabelle_process.scala
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     3
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
     4
Isabelle process wrapper.
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     5
*/
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     6
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     7
package isabelle
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     8
55618
995162143ef4 tuned imports;
wenzelm
parents: 54443
diff changeset
     9
65310
da9f1ef8ef7c more process arguments;
wenzelm
parents: 65225
diff changeset
    10
import java.io.{File => JFile}
da9f1ef8ef7c more process arguments;
wenzelm
parents: 65225
diff changeset
    11
da9f1ef8ef7c more process arguments;
wenzelm
parents: 65225
diff changeset
    12
57917
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    13
object Isabelle_Process
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    14
{
71597
d025735a4090 clarified signature;
wenzelm
parents: 71594
diff changeset
    15
  def apply(
71594
8a298184f3f9 clarified signature;
wenzelm
parents: 69572
diff changeset
    16
    session: Session,
65216
060a8a1f2dec tuned signature;
wenzelm
parents: 62754
diff changeset
    17
    options: Options,
71594
8a298184f3f9 clarified signature;
wenzelm
parents: 69572
diff changeset
    18
    sessions_structure: Sessions.Structure,
71598
269dc4bf1f40 clarified signature;
wenzelm
parents: 71597
diff changeset
    19
    store: Sessions.Store,
65216
060a8a1f2dec tuned signature;
wenzelm
parents: 62754
diff changeset
    20
    logic: String = "",
060a8a1f2dec tuned signature;
wenzelm
parents: 62754
diff changeset
    21
    args: List[String] = Nil,
060a8a1f2dec tuned signature;
wenzelm
parents: 62754
diff changeset
    22
    modes: List[String] = Nil,
65310
da9f1ef8ef7c more process arguments;
wenzelm
parents: 65225
diff changeset
    23
    cwd: JFile = null,
71604
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    24
    env: Map[String, String] = Isabelle_System.settings()): Isabelle_Process =
65216
060a8a1f2dec tuned signature;
wenzelm
parents: 62754
diff changeset
    25
  {
62556
c115e69f457f more abstract Session.start, without prover command-line;
wenzelm
parents: 62555
diff changeset
    26
    val channel = System_Channel()
c115e69f457f more abstract Session.start, without prover command-line;
wenzelm
parents: 62555
diff changeset
    27
    val process =
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    28
      try {
69572
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 68209
diff changeset
    29
        val channel_options =
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 68209
diff changeset
    30
          options.string.update("system_channel_address", channel.address).
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 68209
diff changeset
    31
            string.update("system_channel_password", channel.password)
71598
269dc4bf1f40 clarified signature;
wenzelm
parents: 71597
diff changeset
    32
        ML_Process(channel_options, sessions_structure, store,
269dc4bf1f40 clarified signature;
wenzelm
parents: 71597
diff changeset
    33
          logic = logic, args = args, modes = modes, cwd = cwd, env = env)
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    34
      }
69572
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 68209
diff changeset
    35
      catch { case exn @ ERROR(_) => channel.shutdown(); throw exn }
62556
c115e69f457f more abstract Session.start, without prover command-line;
wenzelm
parents: 62555
diff changeset
    36
    process.stdin.close
57917
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    37
71604
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    38
    new Isabelle_Process(session, channel, process)
57917
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    39
  }
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    40
}
71604
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    41
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    42
class Isabelle_Process private(session: Session, channel: System_Channel, process: Bash.Process)
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    43
{
71607
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    44
  private val startup = Future.promise[String]
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    45
  private val terminated = Future.promise[Process_Result]
71604
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    46
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    47
  session.phase_changed +=
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    48
    Session.Consumer(getClass.getName) {
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    49
      case Session.Ready =>
71607
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    50
        startup.fulfill("")
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    51
      case Session.Terminated(result) =>
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    52
        if (!result.ok && !startup.is_finished) {
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    53
          val syslog = session.syslog_content()
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    54
          val err = "Session startup failed" + (if (syslog.isEmpty) "" else ":\n" + syslog)
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    55
          startup.fulfill(err)
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    56
        }
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    57
        terminated.fulfill(result)
71604
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    58
      case _ =>
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    59
    }
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    60
71607
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    61
  session.start(receiver => new Prover(receiver, session.xml_cache, channel, process))
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    62
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    63
  def await_startup(): Isabelle_Process =
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    64
    startup.join match {
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    65
      case "" => this
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    66
      case err => session.stop(); error(err)
71604
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    67
    }
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    68
71607
d97f504c8145 clarified Isabelle_Process phases;
wenzelm
parents: 71605
diff changeset
    69
  def join(): Process_Result = terminated.join
71604
c6fa217c9d5e clarified signature: more robust startup_join;
wenzelm
parents: 71598
diff changeset
    70
}