src/Pure/System/isabelle_process.ML
author wenzelm
Tue, 07 Aug 2012 19:16:58 +0200
changeset 48712 6b7a9bcc0bae
parent 48710 5b51ccdc8623
child 49108 61e222517d06
permissions -rw-r--r--
simplified process startup phases: INIT suffices for is_ready;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30173
eabece26b89b moved isabelle_process.ML, isabelle_process.scala, isar.ML, session.ML to Pure/System/ (together with associated Isar commands);
wenzelm
parents: 29522
diff changeset
     1
(*  Title:      Pure/System/isabelle_process.ML
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     3
38445
ba9ea6b9b75c simplified internal message format: dropped special Symbol.STX header;
wenzelm
parents: 38270
diff changeset
     4
Isabelle process wrapper, based on private fifos for maximum
48712
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
     5
robustness and performance, or local socket for maximum portability.
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: 39513
diff changeset
     6
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: 39513
diff changeset
     7
Startup phases:
48712
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
     8
  - raw Posix process startup with uncontrolled output on stdout/stderr
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
     9
  - stderr \002: ML running
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
    10
  - stdin/stdout/stderr freely available (raw ML loop)
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
    11
  - protocol thread initialization
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
    12
  - rendezvous on system channel
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
    13
  - message INIT: channels ready
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    14
*)
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    15
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    16
signature ISABELLE_PROCESS =
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    17
sig
42897
6bc8a6dcb3e0 added Isabelle_Process.is_active;
wenzelm
parents: 40521
diff changeset
    18
  val is_active: unit -> bool
46119
0d7172a7672c tuned signature -- emphasize special nature of protocol commands;
wenzelm
parents: 45666
diff changeset
    19
  val protocol_command: string -> (string list -> unit) -> unit
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
    20
  val crashes: exn list Synchronized.var
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
    21
  val init_fifos: string -> string -> unit
45028
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 44988
diff changeset
    22
  val init_socket: string -> unit
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    23
end;
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    24
31797
203d5e61e3bc renamed IsabelleProcess to Isabelle_Process;
wenzelm
parents: 31384
diff changeset
    25
structure Isabelle_Process: ISABELLE_PROCESS =
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    26
struct
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    27
42897
6bc8a6dcb3e0 added Isabelle_Process.is_active;
wenzelm
parents: 40521
diff changeset
    28
(* print mode *)
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    29
25748
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    30
val isabelle_processN = "isabelle_process";
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    31
42897
6bc8a6dcb3e0 added Isabelle_Process.is_active;
wenzelm
parents: 40521
diff changeset
    32
fun is_active () = Print_Mode.print_mode_active isabelle_processN;
6bc8a6dcb3e0 added Isabelle_Process.is_active;
wenzelm
parents: 40521
diff changeset
    33
26550
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    34
val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
28036
wenzelm
parents: 27986
diff changeset
    35
val _ = Markup.add_mode isabelle_processN YXML.output_markup;
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    36
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    37
46119
0d7172a7672c tuned signature -- emphasize special nature of protocol commands;
wenzelm
parents: 45666
diff changeset
    38
(* protocol commands *)
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    39
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    40
local
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    41
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
    42
val commands =
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
    43
  Synchronized.var "Isabelle_Process.commands" (Symtab.empty: (string list -> unit) Symtab.table);
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    44
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    45
in
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    46
46119
0d7172a7672c tuned signature -- emphasize special nature of protocol commands;
wenzelm
parents: 45666
diff changeset
    47
fun protocol_command name cmd =
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
    48
  Synchronized.change commands (fn cmds =>
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    49
   (if not (Symtab.defined cmds name) then ()
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    50
    else warning ("Redefining Isabelle process command " ^ quote name);
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
    51
    Symtab.update (name, cmd) cmds));
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    52
46119
0d7172a7672c tuned signature -- emphasize special nature of protocol commands;
wenzelm
parents: 45666
diff changeset
    53
fun run_command name args =
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
    54
  (case Symtab.lookup (Synchronized.value commands) name of
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    55
    NONE => error ("Undefined Isabelle process command " ^ quote name)
46119
0d7172a7672c tuned signature -- emphasize special nature of protocol commands;
wenzelm
parents: 45666
diff changeset
    56
  | SOME cmd =>
0d7172a7672c tuned signature -- emphasize special nature of protocol commands;
wenzelm
parents: 45666
diff changeset
    57
      (Runtime.debugging cmd args handle exn =>
48056
396749e9daaf tuned message;
wenzelm
parents: 47410
diff changeset
    58
        error ("Isabelle process protocol failure: " ^ quote name ^ "\n" ^
396749e9daaf tuned message;
wenzelm
parents: 47410
diff changeset
    59
          ML_Compiler.exn_message exn)));
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    60
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    61
end;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    62
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    63
40134
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
    64
(* message channels *)
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    65
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    66
local
25810
bac99880fa99 output message properties: id or position;
wenzelm
parents: 25748
diff changeset
    67
40134
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
    68
fun chunk s = [string_of_int (size s), "\n", s];
26574
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    69
44731
8f7b3a89fc15 flush after Output.raw_message (and init message) for reduced latency of important protocol events;
wenzelm
parents: 44389
diff changeset
    70
fun message do_flush mbox ch raw_props body =
43771
fc524449f511 allow empty body for raw_message -- important for Invoke_Scala;
wenzelm
parents: 43746
diff changeset
    71
  let
43772
c825594fd0c1 clarified YXML.embed_controls -- this is idempotent and cannot be nested;
wenzelm
parents: 43771
diff changeset
    72
    val robust_props = map (pairself YXML.embed_controls) raw_props;
43771
fc524449f511 allow empty body for raw_message -- important for Invoke_Scala;
wenzelm
parents: 43746
diff changeset
    73
    val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
44731
8f7b3a89fc15 flush after Output.raw_message (and init message) for reduced latency of important protocol events;
wenzelm
parents: 44389
diff changeset
    74
  in Mailbox.send mbox (chunk header @ chunk body, do_flush) end;
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    75
44270
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 43772
diff changeset
    76
fun standard_message mbox opt_serial ch body =
43771
fc524449f511 allow empty body for raw_message -- important for Invoke_Scala;
wenzelm
parents: 43746
diff changeset
    77
  if body = "" then ()
fc524449f511 allow empty body for raw_message -- important for Invoke_Scala;
wenzelm
parents: 43746
diff changeset
    78
  else
44731
8f7b3a89fc15 flush after Output.raw_message (and init message) for reduced latency of important protocol events;
wenzelm
parents: 44389
diff changeset
    79
    message false mbox ch
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 45158
diff changeset
    80
      ((case opt_serial of SOME i => cons (Isabelle_Markup.serialN, string_of_int i) | _ => I)
43771
fc524449f511 allow empty body for raw_message -- important for Invoke_Scala;
wenzelm
parents: 43746
diff changeset
    81
        (Position.properties_of (Position.thread_data ()))) body;
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    82
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
    83
fun message_output mbox channel =
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    84
  let
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
    85
    fun flush () = ignore (try System_Channel.flush channel);
40134
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
    86
    fun loop receive =
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
    87
      (case receive mbox of
44731
8f7b3a89fc15 flush after Output.raw_message (and init message) for reduced latency of important protocol events;
wenzelm
parents: 44389
diff changeset
    88
        SOME (msg, do_flush) =>
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
    89
         (List.app (fn s => System_Channel.output channel s) msg;
44731
8f7b3a89fc15 flush after Output.raw_message (and init message) for reduced latency of important protocol events;
wenzelm
parents: 44389
diff changeset
    90
          if do_flush then flush () else ();
40301
bf39a257b3d3 simplified some time constants;
wenzelm
parents: 40134
diff changeset
    91
          loop (Mailbox.receive_timeout (seconds 0.02)))
44731
8f7b3a89fc15 flush after Output.raw_message (and init message) for reduced latency of important protocol events;
wenzelm
parents: 44389
diff changeset
    92
      | NONE => (flush (); loop (SOME o Mailbox.receive)));
40134
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
    93
  in fn () => loop (SOME o Mailbox.receive) end;
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    94
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    95
in
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    96
48712
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
    97
fun init_channels channel =
28189
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
    98
  let
40134
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
    99
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
   100
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
   101
44731
8f7b3a89fc15 flush after Output.raw_message (and init message) for reduced latency of important protocol events;
wenzelm
parents: 44389
diff changeset
   102
    val mbox = Mailbox.create () : (string list * bool) Mailbox.T;
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   103
    val _ = Simple_Thread.fork false (message_output mbox channel);
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
   104
  in
44270
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 43772
diff changeset
   105
    Output.Private_Hooks.status_fn := standard_message mbox NONE "B";
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 43772
diff changeset
   106
    Output.Private_Hooks.report_fn := standard_message mbox NONE "C";
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 43772
diff changeset
   107
    Output.Private_Hooks.writeln_fn := (fn s => standard_message mbox (SOME (serial ())) "D" s);
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 43772
diff changeset
   108
    Output.Private_Hooks.tracing_fn := (fn s => standard_message mbox (SOME (serial ())) "E" s);
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 43772
diff changeset
   109
    Output.Private_Hooks.warning_fn := (fn s => standard_message mbox (SOME (serial ())) "F" s);
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 43772
diff changeset
   110
    Output.Private_Hooks.error_fn := (fn (i, s) => standard_message mbox (SOME i) "G" s);
46774
38f113b052b1 clarified terminology of raw protocol messages;
wenzelm
parents: 46548
diff changeset
   111
    Output.Private_Hooks.protocol_message_fn := message true mbox "H";
40133
b61d52de66f0 more explicit indication of Output.Private_Hooks (still public to accomodate clones of Proof General for now);
wenzelm
parents: 40132
diff changeset
   112
    Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
b61d52de66f0 more explicit indication of Output.Private_Hooks (still public to accomodate clones of Proof General for now);
wenzelm
parents: 40132
diff changeset
   113
    Output.Private_Hooks.prompt_fn := ignore;
45028
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 44988
diff changeset
   114
    message true mbox "A" [] (Session.welcome ())
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
   115
  end;
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   116
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   117
end;
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   118
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   119
39234
d76a2fd129b5 main command loops are supposed to be uninterruptible -- no special treatment here;
wenzelm
parents: 38871
diff changeset
   120
(* protocol loop -- uninterruptible *)
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   121
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
   122
val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   123
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   124
local
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   125
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   126
fun recover crash =
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
   127
  (Synchronized.change crashes (cons crash);
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   128
    warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   129
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   130
fun read_chunk channel len =
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   131
  let
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   132
    val n =
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   133
      (case Int.fromString len of
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   134
        SOME n => n
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   135
      | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   136
    val chunk = System_Channel.inputN channel n;
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   137
    val m = size chunk;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   138
  in
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   139
    if m = n then chunk
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   140
    else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   141
  end;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   142
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   143
fun read_command channel =
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   144
  (case System_Channel.input_line channel of
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   145
    NONE => raise Runtime.TERMINATE
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   146
  | SOME line => map (read_chunk channel) (space_explode "," line));
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   147
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   148
in
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   149
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   150
fun loop channel =
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   151
  let val continue =
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   152
    (case read_command channel of
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   153
      [] => (Output.error_msg "Isabelle process: no input"; true)
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   154
    | name :: args => (run_command name args; true))
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   155
    handle Runtime.TERMINATE => false
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   156
      | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   157
  in if continue then loop channel else () end;
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   158
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   159
end;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   160
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   161
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
   162
(* init *)
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
   163
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   164
fun init rendezvous = ignore (Simple_Thread.fork false (fn () =>
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   165
  let
40301
bf39a257b3d3 simplified some time constants;
wenzelm
parents: 40134
diff changeset
   166
    val _ = OS.Process.sleep (seconds 0.5);  (*yield to raw ML toplevel*)
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
   167
    val _ = Output.physical_stderr Symbol.STX;
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: 39513
diff changeset
   168
47396
c1d297ef7969 enable parallel proofs (cf. e8552cba702d), only affects packages so far;
wenzelm
parents: 46774
diff changeset
   169
    val _ = quick_and_dirty := false;
47410
33f2f968c0a1 disable parallel proofs (again) -- still suffering from instabilites wrt. interrupts;
wenzelm
parents: 47396
diff changeset
   170
    val _ = Goal.parallel_proofs := 0;
44988
33aa6da101d8 at least 2 worker threads to ensure some degree of lifeness, notably for asynchronous Document.print_state;
wenzelm
parents: 44731
diff changeset
   171
    val _ =
33aa6da101d8 at least 2 worker threads to ensure some degree of lifeness, notably for asynchronous Document.print_state;
wenzelm
parents: 44731
diff changeset
   172
      if Multithreading.max_threads_value () < 2
33aa6da101d8 at least 2 worker threads to ensure some degree of lifeness, notably for asynchronous Document.print_state;
wenzelm
parents: 44731
diff changeset
   173
      then Multithreading.max_threads := 2 else ();
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: 39513
diff changeset
   174
    val _ = Context.set_thread_data NONE;
43671
a250b092ac66 hard-wired print mode "xsymbols" increases chance that "iff" in HOL will print symbolic arrow;
wenzelm
parents: 42897
diff changeset
   175
    val _ =
a250b092ac66 hard-wired print mode "xsymbols" increases chance that "iff" in HOL will print symbolic arrow;
wenzelm
parents: 42897
diff changeset
   176
      Unsynchronized.change print_mode
45057
86c9b73158a8 default print mode for Isabelle/Scala, not just Isabelle/jEdit;
wenzelm
parents: 45029
diff changeset
   177
        (fn mode =>
86c9b73158a8 default print mode for Isabelle/Scala, not just Isabelle/jEdit;
wenzelm
parents: 45029
diff changeset
   178
          (mode @ [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN])
48709
719f458cd89e prefer static Build.outer_syntax in Isabelle/Scala session -- discontinued incremental protocol;
wenzelm
parents: 48056
diff changeset
   179
          |> fold (update op =) [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN]);
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: 39513
diff changeset
   180
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   181
    val channel = rendezvous ();
48712
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
   182
    val _ = init_channels channel;
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   183
  in loop channel end));
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
   184
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   185
fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   186
fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
45028
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 44988
diff changeset
   187
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
   188
end;
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: 39513
diff changeset
   189