src/Pure/System/isabelle_process.scala
author wenzelm
Wed, 11 Dec 2013 18:02:22 +0100
changeset 54717 42c209a6c225
parent 54443 9714b5474f39
child 55618 995162143ef4
permissions -rw-r--r--
support for polml-5.5.2; support Thread.numPhysicalProcessors of polyml-5.5.2 (according to SVN 1890); clarified max_threads: store plain value internally, reproduce result only on startup, and thus avoid potential system overhead;
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
27963
c9ea82444189 YXML.parse_failsafe;
wenzelm
parents: 27955
diff changeset
     3
    Options:    :folding=explicit:collapseFolds=1:
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     4
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     5
Isabelle process management -- always reactive due to multi-threaded I/O.
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
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     8
package isabelle
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     9
43520
cec9b95fa35d explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents: 43514
diff changeset
    10
import java.lang.System
27955
4c32c5e75eca use java.util.concurrent.LinkedBlockingQueue, which blocks as required;
wenzelm
parents: 27949
diff changeset
    11
import java.util.concurrent.LinkedBlockingQueue
28045
1a6f273108ae join stdout/stderr, eliminated Kind.STDERR;
wenzelm
parents: 27999
diff changeset
    12
import java.io.{BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter,
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
    13
  InputStream, OutputStream, BufferedOutputStream, IOException}
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
    14
32474
0818e6b1c8a6 Isabelle_Process: receiver as Actor, not EventBus;
wenzelm
parents: 32448
diff changeset
    15
import scala.actors.Actor
0818e6b1c8a6 Isabelle_Process: receiver as Actor, not EventBus;
wenzelm
parents: 32448
diff changeset
    16
import Actor._
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
    17
27973
18d02c0b90b6 moved class Result into static object, removed dynamic tree method;
wenzelm
parents: 27963
diff changeset
    18
32474
0818e6b1c8a6 Isabelle_Process: receiver as Actor, not EventBus;
wenzelm
parents: 32448
diff changeset
    19
object Isabelle_Process
0818e6b1c8a6 Isabelle_Process: receiver as Actor, not EventBus;
wenzelm
parents: 32448
diff changeset
    20
{
46772
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
    21
  /* messages */
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
    22
44733
329320fc88df buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents: 44732
diff changeset
    23
  sealed abstract class Message
43721
fad8634cee62 echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents: 43695
diff changeset
    24
fad8634cee62 echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents: 43695
diff changeset
    25
  class Input(name: String, args: List[String]) extends Message
fad8634cee62 echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents: 43695
diff changeset
    26
  {
fad8634cee62 echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents: 43695
diff changeset
    27
    override def toString: String =
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 50117
diff changeset
    28
      XML.Elem(Markup(Markup.PROVER_COMMAND, List((Markup.NAME, name))),
43721
fad8634cee62 echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents: 43695
diff changeset
    29
        args.map(s =>
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 50117
diff changeset
    30
          List(XML.Text("\n"), XML.elem(Markup.PROVER_ARG, YXML.parse_body(s)))).flatten).toString
43721
fad8634cee62 echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents: 43695
diff changeset
    31
  }
fad8634cee62 echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents: 43695
diff changeset
    32
46772
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
    33
  class Output(val message: XML.Elem) extends Message
34100
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
    34
  {
43747
74a9e9c8d5e8 tuned signature;
wenzelm
parents: 43746
diff changeset
    35
    def kind: String = message.markup.name
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents: 43748
diff changeset
    36
    def properties: Properties.T = message.markup.properties
43747
74a9e9c8d5e8 tuned signature;
wenzelm
parents: 43746
diff changeset
    37
    def body: XML.Body = message.body
37689
628eabe2213a simplified Isabelle_Process.Result: use markup directly;
wenzelm
parents: 37132
diff changeset
    38
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 50117
diff changeset
    39
    def is_init = kind == Markup.INIT
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 50117
diff changeset
    40
    def is_exit = kind == Markup.EXIT
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 50117
diff changeset
    41
    def is_stdout = kind == Markup.STDOUT
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 50117
diff changeset
    42
    def is_stderr = kind == Markup.STDERR
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 50117
diff changeset
    43
    def is_system = kind == Markup.SYSTEM
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 50117
diff changeset
    44
    def is_status = kind == Markup.STATUS
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 50117
diff changeset
    45
    def is_report = kind == Markup.REPORT
46121
30a69cd8a9a0 prefer raw_message for protocol implementation;
wenzelm
parents: 45709
diff changeset
    46
    def is_syslog = is_init || is_exit || is_system || is_stderr
34100
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
    47
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
    48
    override def toString: String =
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
    49
    {
29522
793766d4c1b5 moved message markup into Scala layer -- reduced redundancy;
wenzelm
parents: 29506
diff changeset
    50
      val res =
54443
9714b5474f39 more distinctive Isabelle_Process.Output vs. Isabelle_Process.Protocol_Output;
wenzelm
parents: 54442
diff changeset
    51
        if (is_status || is_report) message.body.map(_.toString).mkString
37689
628eabe2213a simplified Isabelle_Process.Result: use markup directly;
wenzelm
parents: 37132
diff changeset
    52
        else Pretty.string_of(message.body)
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37712
diff changeset
    53
      if (properties.isEmpty)
29572
e3a99d957392 replaced java.util.Properties by plain association list;
wenzelm
parents: 29522
diff changeset
    54
        kind.toString + " [[" + res + "]]"
e3a99d957392 replaced java.util.Properties by plain association list;
wenzelm
parents: 29522
diff changeset
    55
      else
e3a99d957392 replaced java.util.Properties by plain association list;
wenzelm
parents: 29522
diff changeset
    56
        kind.toString + " " +
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37712
diff changeset
    57
          (for ((x, y) <- properties) yield x + "=" + y).mkString("{", ",", "}") + " [[" + res + "]]"
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
    58
    }
27973
18d02c0b90b6 moved class Result into static object, removed dynamic tree method;
wenzelm
parents: 27963
diff changeset
    59
  }
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    60
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    61
  class Protocol_Output(props: Properties.T, val bytes: Bytes)
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    62
    extends Output(XML.Elem(Markup(Markup.PROTOCOL, props), Nil))
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    63
  {
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    64
    lazy val text: String = bytes.toString
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    65
  }
27973
18d02c0b90b6 moved class Result into static object, removed dynamic tree method;
wenzelm
parents: 27963
diff changeset
    66
}
18d02c0b90b6 moved class Result into static object, removed dynamic tree method;
wenzelm
parents: 27963
diff changeset
    67
18d02c0b90b6 moved class Result into static object, removed dynamic tree method;
wenzelm
parents: 27963
diff changeset
    68
45055
55274f7e306b explicit option for socket vs. fifo communication;
wenzelm
parents: 45027
diff changeset
    69
class Isabelle_Process(
55274f7e306b explicit option for socket vs. fifo communication;
wenzelm
parents: 45027
diff changeset
    70
    receiver: Isabelle_Process.Message => Unit = Console.println(_),
54005
132640f4c453 tuned signature -- facilitate experimentation with other processes;
wenzelm
parents: 52799
diff changeset
    71
    arguments: List[String] = Nil)
29192
082ee2a01a6d explicit EventBus for results;
wenzelm
parents: 29178
diff changeset
    72
{
31797
203d5e61e3bc renamed IsabelleProcess to Isabelle_Process;
wenzelm
parents: 31498
diff changeset
    73
  import Isabelle_Process._
29194
wenzelm
parents: 29192
diff changeset
    74
27973
18d02c0b90b6 moved class Result into static object, removed dynamic tree method;
wenzelm
parents: 27963
diff changeset
    75
48705
dd32321d6eef tuned signature -- slightly more abstract text representation of prover process;
wenzelm
parents: 48355
diff changeset
    76
  /* text representation */
dd32321d6eef tuned signature -- slightly more abstract text representation of prover process;
wenzelm
parents: 48355
diff changeset
    77
dd32321d6eef tuned signature -- slightly more abstract text representation of prover process;
wenzelm
parents: 48355
diff changeset
    78
  def encode(s: String): String = Symbol.encode(s)
dd32321d6eef tuned signature -- slightly more abstract text representation of prover process;
wenzelm
parents: 48355
diff changeset
    79
  def decode(s: String): String = Symbol.decode(s)
dd32321d6eef tuned signature -- slightly more abstract text representation of prover process;
wenzelm
parents: 48355
diff changeset
    80
dd32321d6eef tuned signature -- slightly more abstract text representation of prover process;
wenzelm
parents: 48355
diff changeset
    81
  object Encode
dd32321d6eef tuned signature -- slightly more abstract text representation of prover process;
wenzelm
parents: 48355
diff changeset
    82
  {
dd32321d6eef tuned signature -- slightly more abstract text representation of prover process;
wenzelm
parents: 48355
diff changeset
    83
    val string: XML.Encode.T[String] = (s => XML.Encode.string(encode(s)))
dd32321d6eef tuned signature -- slightly more abstract text representation of prover process;
wenzelm
parents: 48355
diff changeset
    84
  }
dd32321d6eef tuned signature -- slightly more abstract text representation of prover process;
wenzelm
parents: 48355
diff changeset
    85
dd32321d6eef tuned signature -- slightly more abstract text representation of prover process;
wenzelm
parents: 48355
diff changeset
    86
46772
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
    87
  /* output */
39573
wenzelm
parents: 39572
diff changeset
    88
51664
080ef458f21a public Isabelle_Process.xml_cache (thread-safe);
wenzelm
parents: 51663
diff changeset
    89
  val xml_cache = new XML.Cache()
080ef458f21a public Isabelle_Process.xml_cache (thread-safe);
wenzelm
parents: 51663
diff changeset
    90
46772
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
    91
  private def system_output(text: String)
39573
wenzelm
parents: 39572
diff changeset
    92
  {
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 50117
diff changeset
    93
    receiver(new Output(XML.Elem(Markup(Markup.SYSTEM, Nil), List(XML.Text(text)))))
39573
wenzelm
parents: 39572
diff changeset
    94
  }
wenzelm
parents: 39572
diff changeset
    95
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    96
  private def protocol_output(props: Properties.T, bytes: Bytes)
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    97
  {
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    98
    receiver(new Protocol_Output(props, bytes))
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    99
  }
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   100
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   101
  private def output(kind: String, props: Properties.T, body: XML.Body)
39573
wenzelm
parents: 39572
diff changeset
   102
  {
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 50117
diff changeset
   103
    if (kind == Markup.INIT) system_channel.accepted()
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   104
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   105
    val main = XML.Elem(Markup(kind, props), Protocol.clean_message(body))
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   106
    val reports = Protocol.message_reports(props, body)
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   107
    for (msg <- main :: reports) receiver(new Output(xml_cache.elem(msg)))
39573
wenzelm
parents: 39572
diff changeset
   108
  }
wenzelm
parents: 39572
diff changeset
   109
48016
edbc8e8accd9 more explicit treatment of return code vs. session phase;
wenzelm
parents: 46774
diff changeset
   110
  private def exit_message(rc: Int)
39573
wenzelm
parents: 39572
diff changeset
   111
  {
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   112
    output(Markup.EXIT, Markup.Return_Code(rc), List(XML.Text("Return code: " + rc.toString)))
39573
wenzelm
parents: 39572
diff changeset
   113
  }
wenzelm
parents: 39572
diff changeset
   114
wenzelm
parents: 39572
diff changeset
   115
52581
99835e3abca4 no need for raw stdin;
wenzelm
parents: 51664
diff changeset
   116
  /* command input actor */
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   117
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54005
diff changeset
   118
  private case class Input_Chunks(chunks: List[Bytes])
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   119
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   120
  private case object Close
39585
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   121
  private def close(p: (Thread, Actor))
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   122
  {
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   123
    if (p != null && p._1.isAlive) {
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   124
      p._2 ! Close
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   125
      p._1.join
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   126
    }
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   127
  }
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   128
39585
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   129
  @volatile private var command_input: (Thread, Actor) = null
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   130
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   131
39585
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   132
  /** process manager **/
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   133
54005
132640f4c453 tuned signature -- facilitate experimentation with other processes;
wenzelm
parents: 52799
diff changeset
   134
  def command_line(channel: System_Channel, args: List[String]): List[String] =
132640f4c453 tuned signature -- facilitate experimentation with other processes;
wenzelm
parents: 52799
diff changeset
   135
    Isabelle_System.getenv_strict("ISABELLE_PROCESS") :: (channel.isabelle_args ::: args)
132640f4c453 tuned signature -- facilitate experimentation with other processes;
wenzelm
parents: 52799
diff changeset
   136
45158
db4bf4fb5492 always use sockets on Windows/Cygwin;
wenzelm
parents: 45075
diff changeset
   137
  private val system_channel = System_Channel()
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   138
39585
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   139
  private val process =
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   140
    try {
54005
132640f4c453 tuned signature -- facilitate experimentation with other processes;
wenzelm
parents: 52799
diff changeset
   141
      val cmdline = command_line(system_channel, arguments)
48353
bcce872202b3 support external processes with explicit environment;
wenzelm
parents: 48020
diff changeset
   142
      new Isabelle_System.Managed_Process(null, null, false, cmdline: _*)
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   143
    }
45027
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents: 44775
diff changeset
   144
    catch { case e: IOException => system_channel.accepted(); throw(e) }
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   145
48355
6b36da29a0bf support for detached Bash_Job with some control operations;
wenzelm
parents: 48353
diff changeset
   146
  val (_, process_result) =
39587
f84b70e3bb9c more reactive handling of Isabelle_Process startup errors;
wenzelm
parents: 39585
diff changeset
   147
    Simple_Thread.future("process_result") { process.join }
f84b70e3bb9c more reactive handling of Isabelle_Process startup errors;
wenzelm
parents: 39585
diff changeset
   148
39585
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   149
  private def terminate_process()
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   150
  {
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   151
    try { process.terminate }
46772
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
   152
    catch { case e: IOException => system_output("Failed to terminate Isabelle: " + e.getMessage) }
39585
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   153
  }
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   154
39585
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   155
  private val process_manager = Simple_Thread.fork("process_manager")
39572
bb3469024b6a added Isabelle_Process.syslog;
wenzelm
parents: 39530
diff changeset
   156
  {
46548
c54a4a22501c clarified initial process startup errors: recover image load failure message (cf. 2cb7e34f6096) and suppress accidental output from raw ML toplevel;
wenzelm
parents: 46121
diff changeset
   157
    val (startup_failed, startup_errors) =
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   158
    {
48020
a4f9957878ab clarified prover startup: no timeout, read stderr more carefully;
wenzelm
parents: 48019
diff changeset
   159
      var finished: Option[Boolean] = None
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   160
      val result = new StringBuilder(100)
48020
a4f9957878ab clarified prover startup: no timeout, read stderr more carefully;
wenzelm
parents: 48019
diff changeset
   161
      while (finished.isEmpty && (process.stderr.ready || !process_result.is_finished)) {
46548
c54a4a22501c clarified initial process startup errors: recover image load failure message (cf. 2cb7e34f6096) and suppress accidental output from raw ML toplevel;
wenzelm
parents: 46121
diff changeset
   162
        while (finished.isEmpty && process.stderr.ready) {
48020
a4f9957878ab clarified prover startup: no timeout, read stderr more carefully;
wenzelm
parents: 48019
diff changeset
   163
          try {
a4f9957878ab clarified prover startup: no timeout, read stderr more carefully;
wenzelm
parents: 48019
diff changeset
   164
            val c = process.stderr.read
a4f9957878ab clarified prover startup: no timeout, read stderr more carefully;
wenzelm
parents: 48019
diff changeset
   165
            if (c == 2) finished = Some(true)
a4f9957878ab clarified prover startup: no timeout, read stderr more carefully;
wenzelm
parents: 48019
diff changeset
   166
            else result += c.toChar
a4f9957878ab clarified prover startup: no timeout, read stderr more carefully;
wenzelm
parents: 48019
diff changeset
   167
          }
a4f9957878ab clarified prover startup: no timeout, read stderr more carefully;
wenzelm
parents: 48019
diff changeset
   168
          catch { case _: IOException => finished = Some(false) }
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   169
        }
48020
a4f9957878ab clarified prover startup: no timeout, read stderr more carefully;
wenzelm
parents: 48019
diff changeset
   170
        Thread.sleep(10)
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   171
      }
39623
6aae022fde9b tuned message;
wenzelm
parents: 39618
diff changeset
   172
      (finished.isEmpty || !finished.get, result.toString.trim)
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   173
    }
46772
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
   174
    if (startup_errors != "") system_output(startup_errors)
39572
bb3469024b6a added Isabelle_Process.syslog;
wenzelm
parents: 39530
diff changeset
   175
52581
99835e3abca4 no need for raw stdin;
wenzelm
parents: 51664
diff changeset
   176
    process.stdin.close
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   177
    if (startup_failed) {
48016
edbc8e8accd9 more explicit treatment of return code vs. session phase;
wenzelm
parents: 46774
diff changeset
   178
      exit_message(127)
39585
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   179
      Thread.sleep(300)
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   180
      terminate_process()
39587
f84b70e3bb9c more reactive handling of Isabelle_Process startup errors;
wenzelm
parents: 39585
diff changeset
   181
      process_result.join
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   182
    }
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   183
    else {
45027
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents: 44775
diff changeset
   184
      val (command_stream, message_stream) = system_channel.rendezvous()
39530
16adc476348f Isabelle_Process: more robust rendezvous, even without proper blocking on open (Cygwin);
wenzelm
parents: 39528
diff changeset
   185
46773
wenzelm
parents: 46772
diff changeset
   186
      val stdout = physical_output_actor(false)
wenzelm
parents: 46772
diff changeset
   187
      val stderr = physical_output_actor(true)
39585
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   188
      command_input = input_actor(command_stream)
39572
bb3469024b6a added Isabelle_Process.syslog;
wenzelm
parents: 39530
diff changeset
   189
      val message = message_actor(message_stream)
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   190
39587
f84b70e3bb9c more reactive handling of Isabelle_Process startup errors;
wenzelm
parents: 39585
diff changeset
   191
      val rc = process_result.join
46772
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
   192
      system_output("process terminated")
52581
99835e3abca4 no need for raw stdin;
wenzelm
parents: 51664
diff changeset
   193
      close(command_input)
99835e3abca4 no need for raw stdin;
wenzelm
parents: 51664
diff changeset
   194
      for ((thread, _) <- List(stdout, stderr, command_input, message))
45633
2cb7e34f6096 retain stderr and include it in syslog, which is buffered and thus increases the chance that users see remains from crashes etc.;
wenzelm
parents: 45158
diff changeset
   195
        thread.join
46772
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
   196
      system_output("process_manager terminated")
48016
edbc8e8accd9 more explicit treatment of return code vs. session phase;
wenzelm
parents: 46774
diff changeset
   197
      exit_message(rc)
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   198
    }
45027
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents: 44775
diff changeset
   199
    system_channel.accepted()
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   200
  }
27973
18d02c0b90b6 moved class Result into static object, removed dynamic tree method;
wenzelm
parents: 27963
diff changeset
   201
39572
bb3469024b6a added Isabelle_Process.syslog;
wenzelm
parents: 39530
diff changeset
   202
39585
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   203
  /* management methods */
27973
18d02c0b90b6 moved class Result into static object, removed dynamic tree method;
wenzelm
parents: 27963
diff changeset
   204
39585
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   205
  def join() { process_manager.join() }
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   206
39585
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   207
  def terminate()
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   208
  {
52581
99835e3abca4 no need for raw stdin;
wenzelm
parents: 51664
diff changeset
   209
    close(command_input)
46772
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
   210
    system_output("Terminating Isabelle process")
39585
00be8711082f main Isabelle_Process via Isabelle_System.Managed_Process;
wenzelm
parents: 39575
diff changeset
   211
    terminate_process()
27973
18d02c0b90b6 moved class Result into static object, removed dynamic tree method;
wenzelm
parents: 27963
diff changeset
   212
  }
18d02c0b90b6 moved class Result into static object, removed dynamic tree method;
wenzelm
parents: 27963
diff changeset
   213
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   214
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   215
38259
2b61c5e27399 distinguish proper Isabelle_Process INPUT vs. raw STDIN, tuned corresponding method names;
wenzelm
parents: 38253
diff changeset
   216
  /** stream actors **/
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   217
46773
wenzelm
parents: 46772
diff changeset
   218
  /* physical output */
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   219
46773
wenzelm
parents: 46772
diff changeset
   220
  private def physical_output_actor(err: Boolean): (Thread, Actor) =
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   221
  {
45633
2cb7e34f6096 retain stderr and include it in syslog, which is buffered and thus increases the chance that users see remains from crashes etc.;
wenzelm
parents: 45158
diff changeset
   222
    val (name, reader, markup) =
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 50117
diff changeset
   223
      if (err) ("standard_error", process.stderr, Markup.STDERR)
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 50117
diff changeset
   224
      else ("standard_output", process.stdout, Markup.STDOUT)
45633
2cb7e34f6096 retain stderr and include it in syslog, which is buffered and thus increases the chance that users see remains from crashes etc.;
wenzelm
parents: 45158
diff changeset
   225
38636
b7647ca7de5a module for simplified thread operations (Scala version);
wenzelm
parents: 38573
diff changeset
   226
    Simple_Thread.actor(name) {
46769
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   227
      try {
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   228
        var result = new StringBuilder(100)
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   229
        var finished = false
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   230
        while (!finished) {
28045
1a6f273108ae join stdout/stderr, eliminated Kind.STDERR;
wenzelm
parents: 27999
diff changeset
   231
          //{{{
1a6f273108ae join stdout/stderr, eliminated Kind.STDERR;
wenzelm
parents: 27999
diff changeset
   232
          var c = -1
1a6f273108ae join stdout/stderr, eliminated Kind.STDERR;
wenzelm
parents: 27999
diff changeset
   233
          var done = false
45633
2cb7e34f6096 retain stderr and include it in syslog, which is buffered and thus increases the chance that users see remains from crashes etc.;
wenzelm
parents: 45158
diff changeset
   234
          while (!done && (result.length == 0 || reader.ready)) {
2cb7e34f6096 retain stderr and include it in syslog, which is buffered and thus increases the chance that users see remains from crashes etc.;
wenzelm
parents: 45158
diff changeset
   235
            c = reader.read
28045
1a6f273108ae join stdout/stderr, eliminated Kind.STDERR;
wenzelm
parents: 27999
diff changeset
   236
            if (c >= 0) result.append(c.asInstanceOf[Char])
1a6f273108ae join stdout/stderr, eliminated Kind.STDERR;
wenzelm
parents: 27999
diff changeset
   237
            else done = true
1a6f273108ae join stdout/stderr, eliminated Kind.STDERR;
wenzelm
parents: 27999
diff changeset
   238
          }
1a6f273108ae join stdout/stderr, eliminated Kind.STDERR;
wenzelm
parents: 27999
diff changeset
   239
          if (result.length > 0) {
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   240
            output(markup, Nil, List(XML.Text(decode(result.toString))))
28045
1a6f273108ae join stdout/stderr, eliminated Kind.STDERR;
wenzelm
parents: 27999
diff changeset
   241
            result.length = 0
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   242
          }
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   243
          else {
45633
2cb7e34f6096 retain stderr and include it in syslog, which is buffered and thus increases the chance that users see remains from crashes etc.;
wenzelm
parents: 45158
diff changeset
   244
            reader.close
28045
1a6f273108ae join stdout/stderr, eliminated Kind.STDERR;
wenzelm
parents: 27999
diff changeset
   245
            finished = true
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   246
          }
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   247
          //}}}
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   248
        }
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   249
      }
46772
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
   250
      catch { case e: IOException => system_output(name + ": " + e.getMessage) }
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
   251
      system_output(name + " terminated")
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   252
    }
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   253
  }
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   254
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   255
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   256
  /* command input */
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   257
39572
bb3469024b6a added Isabelle_Process.syslog;
wenzelm
parents: 39530
diff changeset
   258
  private def input_actor(raw_stream: OutputStream): (Thread, Actor) =
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   259
  {
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   260
    val name = "command_input"
38636
b7647ca7de5a module for simplified thread operations (Scala version);
wenzelm
parents: 38573
diff changeset
   261
    Simple_Thread.actor(name) {
46769
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   262
      try {
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   263
        val stream = new BufferedOutputStream(raw_stream)
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   264
        var finished = false
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   265
        while (!finished) {
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   266
          //{{{
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   267
          receive {
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   268
            case Input_Chunks(chunks) =>
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54005
diff changeset
   269
              Bytes(chunks.map(_.length).mkString("", ",", "\n")).write(stream)
2c4940d2edf7 tuned signature;
wenzelm
parents: 54005
diff changeset
   270
              chunks.foreach(_.write(stream))
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   271
              stream.flush
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   272
            case Close =>
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   273
              stream.close
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   274
              finished = true
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38262
diff changeset
   275
            case bad => System.err.println(name + ": ignoring bad message " + bad)
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   276
          }
28045
1a6f273108ae join stdout/stderr, eliminated Kind.STDERR;
wenzelm
parents: 27999
diff changeset
   277
          //}}}
27963
c9ea82444189 YXML.parse_failsafe;
wenzelm
parents: 27955
diff changeset
   278
        }
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   279
      }
46772
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
   280
      catch { case e: IOException => system_output(name + ": " + e.getMessage) }
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
   281
      system_output(name + " terminated")
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   282
    }
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   283
  }
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   284
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   285
38259
2b61c5e27399 distinguish proper Isabelle_Process INPUT vs. raw STDIN, tuned corresponding method names;
wenzelm
parents: 38253
diff changeset
   286
  /* message output */
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   287
39572
bb3469024b6a added Isabelle_Process.syslog;
wenzelm
parents: 39530
diff changeset
   288
  private def message_actor(stream: InputStream): (Thread, Actor) =
39526
f1296795a8dc message_actor: more robust treatment of EOF;
wenzelm
parents: 39525
diff changeset
   289
  {
f1296795a8dc message_actor: more robust treatment of EOF;
wenzelm
parents: 39525
diff changeset
   290
    class EOF extends Exception
f1296795a8dc message_actor: more robust treatment of EOF;
wenzelm
parents: 39525
diff changeset
   291
    class Protocol_Error(msg: String) extends Exception(msg)
38259
2b61c5e27399 distinguish proper Isabelle_Process INPUT vs. raw STDIN, tuned corresponding method names;
wenzelm
parents: 38253
diff changeset
   292
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39526
diff changeset
   293
    val name = "message_output"
38636
b7647ca7de5a module for simplified thread operations (Scala version);
wenzelm
parents: 38573
diff changeset
   294
    Simple_Thread.actor(name) {
34100
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   295
      val default_buffer = new Array[Byte](65536)
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   296
      var c = -1
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   297
52799
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   298
      def read_int(): Int =
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   299
      //{{{
34100
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   300
      {
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   301
        var n = 0
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   302
        c = stream.read
39526
f1296795a8dc message_actor: more robust treatment of EOF;
wenzelm
parents: 39525
diff changeset
   303
        if (c == -1) throw new EOF
34100
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   304
        while (48 <= c && c <= 57) {
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   305
          n = 10 * n + (c - 48)
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   306
          c = stream.read
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   307
        }
52799
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   308
        if (c != 10)
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   309
          throw new Protocol_Error("malformed header: expected integer followed by newline")
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   310
        else n
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   311
      }
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   312
      //}}}
34100
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   313
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   314
      def read_chunk_bytes(): (Array[Byte], Int) =
52799
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   315
      //{{{
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   316
      {
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   317
        val n = read_int()
34100
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   318
        val buf =
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   319
          if (n <= default_buffer.size) default_buffer
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   320
          else new Array[Byte](n)
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   321
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   322
        var i = 0
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   323
        var m = 0
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   324
        do {
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   325
          m = stream.read(buf, i, n - i)
39731
5cb0d7b0d601 bulk read: observe EOF protocol more carefully -- 0 counts as successful read;
wenzelm
parents: 39632
diff changeset
   326
          if (m != -1) i += m
52799
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   327
        }
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   328
        while (m != -1 && n > i)
34100
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   329
52799
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   330
        if (i != n)
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   331
          throw new Protocol_Error("bad chunk (unexpected EOF after " + i + " of " + n + " bytes)")
34100
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   332
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   333
        (buf, n)
34100
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   334
      }
52799
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   335
      //}}}
34100
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   336
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   337
      def read_chunk(): XML.Body =
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   338
      {
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   339
        val (buf, n) = read_chunk_bytes()
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   340
        YXML.parse_body_failsafe(UTF8.decode_chars(decode, buf, 0, n))
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   341
      }
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   342
46769
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   343
      try {
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   344
        do {
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   345
          try {
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   346
            val header = read_chunk()
46769
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   347
            header match {
49677
c4e2762a265c more direct message header: eliminated historic encoding via single letter;
wenzelm
parents: 49445
diff changeset
   348
              case List(XML.Elem(Markup(name, props), Nil)) =>
c4e2762a265c more direct message header: eliminated historic encoding via single letter;
wenzelm
parents: 49445
diff changeset
   349
                val kind = name.intern
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   350
                if (kind == Markup.PROTOCOL) {
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   351
                  val (buf, n) = read_chunk_bytes()
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   352
                  protocol_output(props, Bytes(buf, 0, n))
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   353
                }
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   354
                else {
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   355
                  val body = read_chunk()
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   356
                  output(kind, props, body)
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   357
                }
46769
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   358
              case _ =>
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
   359
                read_chunk()
46769
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   360
                throw new Protocol_Error("bad header: " + header.toString)
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   361
            }
28063
3533485fc7b8 IsabelleSystem.mk_fifo, IsabelleSystem.rm_fifo;
wenzelm
parents: 28056
diff changeset
   362
          }
46769
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   363
          catch { case _: EOF => }
52799
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   364
        }
6a4498b048b7 tuned -- more uniform ML vs. Scala;
wenzelm
parents: 52582
diff changeset
   365
        while (c != -1)
46769
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   366
      }
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   367
      catch {
46772
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
   368
        case e: IOException => system_output("Cannot read message:\n" + e.getMessage)
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
   369
        case e: Protocol_Error => system_output("Malformed message:\n" + e.getMessage)
46769
0038386efd81 clarified scope of exception handlers;
wenzelm
parents: 46762
diff changeset
   370
      }
34100
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   371
      stream.close
ea24958c2af5 fifo: raw byte stream;
wenzelm
parents: 32474
diff changeset
   372
46772
be21f050eda4 tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm
parents: 46769
diff changeset
   373
      system_output(name + " terminated")
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   374
    }
39526
f1296795a8dc message_actor: more robust treatment of EOF;
wenzelm
parents: 39525
diff changeset
   375
  }
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   376
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   377
38259
2b61c5e27399 distinguish proper Isabelle_Process INPUT vs. raw STDIN, tuned corresponding method names;
wenzelm
parents: 38253
diff changeset
   378
  /** main methods **/
2b61c5e27399 distinguish proper Isabelle_Process INPUT vs. raw STDIN, tuned corresponding method names;
wenzelm
parents: 38253
diff changeset
   379
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54005
diff changeset
   380
  def protocol_command_raw(name: String, args: Bytes*): Unit =
2c4940d2edf7 tuned signature;
wenzelm
parents: 54005
diff changeset
   381
    command_input._2 ! Input_Chunks(Bytes(name) :: args.toList)
38372
e753f71b6b34 added Isabelle_Process.input_bytes, which avoids the somewhat slow Standard_System.string_bytes (just in case someone wants to stream raw data at 250MB/s);
wenzelm
parents: 38270
diff changeset
   382
52582
31467a4b1466 tuned signature;
wenzelm
parents: 52581
diff changeset
   383
  def protocol_command(name: String, args: String*)
43721
fad8634cee62 echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents: 43695
diff changeset
   384
  {
44732
c58b69d888ac more abstract receiver interface;
wenzelm
parents: 44705
diff changeset
   385
    receiver(new Input(name, args.toList))
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54005
diff changeset
   386
    protocol_command_raw(name, args.map(Bytes(_)): _*)
43721
fad8634cee62 echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents: 43695
diff changeset
   387
  }
38259
2b61c5e27399 distinguish proper Isabelle_Process INPUT vs. raw STDIN, tuned corresponding method names;
wenzelm
parents: 38253
diff changeset
   388
50117
32755e357a51 update options via protocol;
wenzelm
parents: 49677
diff changeset
   389
  def options(opts: Options): Unit =
52582
31467a4b1466 tuned signature;
wenzelm
parents: 52581
diff changeset
   390
    protocol_command("Isabelle_Process.options", YXML.string_of_body(opts.encode))
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
   391
}