1 (* Title: Pure/System/isabelle_process.ML
4 Isabelle process wrapper, based on private fifos for maximum
5 robustness and performance, or local socket for maximum portability.
8 - raw Posix process startup with uncontrolled output on stdout/stderr
9 - stderr \002: ML running
10 - stdin/stdout/stderr freely available (raw ML loop)
11 - protocol thread initialization
12 - rendezvous on system channel
13 - message INIT: channels ready
16 signature ISABELLE_PROCESS =
18 val is_active: unit -> bool
19 val protocol_command: string -> (string list -> unit) -> unit
20 val tracing_messages: int Unsynchronized.ref;
21 val reset_tracing: unit -> unit
22 val crashes: exn list Synchronized.var
23 val init_fifos: string -> string -> unit
24 val init_socket: string -> unit
27 structure Isabelle_Process: ISABELLE_PROCESS =
32 val isabelle_processN = "isabelle_process";
34 fun is_active () = Print_Mode.print_mode_active isabelle_processN;
36 val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
37 val _ = Markup.add_mode isabelle_processN YXML.output_markup;
40 (* protocol commands *)
45 Synchronized.var "Isabelle_Process.commands" (Symtab.empty: (string list -> unit) Symtab.table);
49 fun protocol_command name cmd =
50 Synchronized.change commands (fn cmds =>
51 (if not (Symtab.defined cmds name) then ()
52 else warning ("Redefining Isabelle process command " ^ quote name);
53 Symtab.update (name, cmd) cmds));
55 fun run_command name args =
56 (case Symtab.lookup (Synchronized.value commands) name of
57 NONE => error ("Undefined Isabelle process command " ^ quote name)
59 (Runtime.debugging cmd args handle exn =>
60 error ("Isabelle process protocol failure: " ^ quote name ^ "\n" ^
61 ML_Compiler.exn_message exn)));
66 (* restricted tracing messages *)
68 val tracing_messages = Unsynchronized.ref 100;
70 val command_tracing_messages =
71 Synchronized.var "command_tracing_messages" (Inttab.empty: int Inttab.table);
73 fun reset_tracing () =
74 Synchronized.change command_tracing_messages (K Inttab.empty);
76 fun update_tracing () =
77 (case Position.parse_id (Position.thread_data ()) of
82 Synchronized.change_result command_tracing_messages (fn tab =>
84 val n = the_default 0 (Inttab.lookup tab id) + 1;
85 val ok = n <= ! tracing_messages;
86 in ((n, ok), Inttab.update (id, n) tab) end);
91 val (text, promise) = Active.dialog_text ();
93 writeln ("Tracing paused. " ^ text "Stop" ^ ", or continue with next " ^
94 text "100" ^ ", " ^ text "1000" ^ ", " ^ text "10000" ^ " messages?")
95 val m = Markup.parse_int (Future.join promise)
96 handle Fail _ => error "Stopped";
98 Synchronized.change command_tracing_messages
99 (Inttab.map_default (id, 0) (fn k => k - m))
104 (* message channels *)
108 fun chunk s = [string_of_int (size s), "\n", s];
110 fun message do_flush mbox name raw_props body =
112 val robust_props = map (pairself YXML.embed_controls) raw_props;
113 val header = YXML.string_of (XML.Elem ((name, robust_props), []));
114 in Mailbox.send mbox (chunk header @ chunk body, do_flush) end;
116 fun standard_message mbox opt_serial name body =
119 message false mbox name
120 ((case opt_serial of SOME i => cons (Markup.serialN, Markup.print_int i) | _ => I)
121 (Position.properties_of (Position.thread_data ()))) body;
123 fun message_output mbox channel =
125 fun flush () = ignore (try System_Channel.flush channel);
127 (case receive mbox of
128 SOME (msg, do_flush) =>
129 (List.app (fn s => System_Channel.output channel s) msg;
130 if do_flush then flush () else ();
131 loop (Mailbox.receive_timeout (seconds 0.02)))
132 | NONE => (flush (); loop (SOME o Mailbox.receive)));
133 in fn () => loop (SOME o Mailbox.receive) end;
137 fun init_channels channel =
139 val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
140 val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
142 val mbox = Mailbox.create () : (string list * bool) Mailbox.T;
143 val _ = Simple_Thread.fork false (message_output mbox channel);
145 Output.Private_Hooks.status_fn := standard_message mbox NONE Markup.statusN;
146 Output.Private_Hooks.report_fn := standard_message mbox NONE Markup.reportN;
147 Output.Private_Hooks.result_fn :=
148 (fn (i, s) => standard_message mbox (SOME i) Markup.resultN s);
149 Output.Private_Hooks.writeln_fn :=
150 (fn s => standard_message mbox (SOME (serial ())) Markup.writelnN s);
151 Output.Private_Hooks.tracing_fn :=
152 (fn s => (update_tracing (); standard_message mbox (SOME (serial ())) Markup.tracingN s));
153 Output.Private_Hooks.warning_fn :=
154 (fn s => standard_message mbox (SOME (serial ())) Markup.warningN s);
155 Output.Private_Hooks.error_fn :=
156 (fn (i, s) => standard_message mbox (SOME i) Markup.errorN s);
157 Output.Private_Hooks.protocol_message_fn := message true mbox Markup.protocolN;
158 Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
159 Output.Private_Hooks.prompt_fn := ignore;
160 message true mbox Markup.initN [] (Session.welcome ())
166 (* protocol loop -- uninterruptible *)
168 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
173 (Synchronized.change crashes (cons crash);
174 warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
176 fun read_chunk channel len =
179 (case Int.fromString len of
181 | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
182 val chunk = System_Channel.inputN channel n;
186 else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
189 fun read_command channel =
190 (case System_Channel.input_line channel of
191 NONE => raise Runtime.TERMINATE
192 | SOME line => map (read_chunk channel) (space_explode "," line));
198 (case read_command channel of
199 [] => (Output.error_msg "Isabelle process: no input"; true)
200 | name :: args => (run_command name args; true))
201 handle Runtime.TERMINATE => false
202 | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
203 in if continue then loop channel else () end;
211 [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN, Graph_Display.active_graphN];
212 val default_modes2 = [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN];
214 fun init rendezvous = ignore (Simple_Thread.fork false (fn () =>
216 val _ = OS.Process.sleep (seconds 0.5); (*yield to raw ML toplevel*)
217 val _ = Output.physical_stderr Symbol.STX;
219 val _ = Printer.show_markup_default := true;
220 val _ = Context.set_thread_data NONE;
222 Unsynchronized.change print_mode
223 (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
225 val channel = rendezvous ();
226 val _ = init_channels channel;
227 in loop channel end));
229 fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
230 fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
236 protocol_command "Isabelle_Process.options"
237 (fn [options_yxml] =>
238 let val options = Options.decode (YXML.parse_body options_yxml) in
239 Options.set_default options;
240 Future.ML_statistics := true;
241 Multithreading.trace := Options.int options "threads_trace";
242 Multithreading.max_threads := Options.int options "threads";
243 if Multithreading.max_threads_value () < 2
244 then Multithreading.max_threads := 2 else ();
245 Goal.skip_proofs := Options.bool options "editor_skip_proofs";
246 Goal.parallel_proofs := (if Options.int options "parallel_proofs" > 0 then 3 else 0);
247 Goal.parallel_subproofs_saturation := Options.int options "parallel_subproofs_saturation";
248 Goal.parallel_subproofs_threshold := Options.real options "parallel_subproofs_threshold";
249 tracing_messages := Options.int options "editor_tracing_messages"