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 reset_tracing: unit -> unit
21 val crashes: exn list Synchronized.var
22 val init_fifos: string -> string -> unit
23 val init_socket: string -> unit
26 structure Isabelle_Process: ISABELLE_PROCESS =
31 val isabelle_processN = "isabelle_process";
33 fun is_active () = Print_Mode.print_mode_active isabelle_processN;
35 val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
36 val _ = Markup.add_mode isabelle_processN YXML.output_markup;
39 (* protocol commands *)
44 Synchronized.var "Isabelle_Process.commands" (Symtab.empty: (string list -> unit) Symtab.table);
48 fun protocol_command name cmd =
49 Synchronized.change commands (fn cmds =>
50 (if not (Symtab.defined cmds name) then ()
51 else warning ("Redefining Isabelle process command " ^ quote name);
52 Symtab.update (name, cmd) cmds));
54 fun run_command name args =
55 (case Symtab.lookup (Synchronized.value commands) name of
56 NONE => error ("Undefined Isabelle process command " ^ quote name)
58 (Runtime.debugging cmd args handle exn =>
59 error ("Isabelle process protocol failure: " ^ quote name ^ "\n" ^
60 ML_Compiler.exn_message exn)));
65 (* restricted tracing messages *)
67 val tracing_messages =
68 Synchronized.var "tracing_messages" (Inttab.empty: int Inttab.table);
70 fun reset_tracing () =
71 Synchronized.change tracing_messages (K Inttab.empty);
73 fun update_tracing () =
74 (case Position.parse_id (Position.thread_data ()) of
79 Synchronized.change_result tracing_messages (fn tab =>
81 val n = the_default 0 (Inttab.lookup tab id) + 1;
82 val ok = n <= Options.default_int "editor_tracing_messages";
83 in (ok, Inttab.update (id, n) tab) end);
88 val (text, promise) = Active.dialog_text ();
90 writeln ("Tracing paused. " ^ text "Stop" ^ ", or continue with next " ^
91 text "100" ^ ", " ^ text "1000" ^ ", " ^ text "10000" ^ " messages?")
92 val m = Markup.parse_int (Future.join promise)
93 handle Fail _ => error "Stopped";
95 Synchronized.change tracing_messages
96 (Inttab.map_default (id, 0) (fn k => k - m))
101 (* message channels *)
105 fun chunk s = [string_of_int (size s), "\n", s];
106 fun flush channel = ignore (try System_Channel.flush channel);
109 Channel of System_Channel.T |
110 Mailbox of (string list * bool) Mailbox.T;
112 fun message do_flush target name raw_props body =
114 val robust_props = map (pairself YXML.embed_controls) raw_props;
115 val header = YXML.string_of (XML.Elem ((name, robust_props), []));
116 val msg = chunk header @ chunk body;
119 Channel channel => (List.app (fn s => System_Channel.output channel s) msg; flush channel)
120 | Mailbox mbox => Mailbox.send mbox (msg, do_flush))
123 fun message_output mbox channel =
126 (case receive mbox of
127 SOME (msg, do_flush) =>
128 (List.app (fn s => System_Channel.output channel s) msg;
129 if do_flush then flush channel else ();
130 loop (Mailbox.receive_timeout (seconds 0.02)))
131 | NONE => (flush channel; loop (SOME o Mailbox.receive)));
132 in fn () => loop (SOME o Mailbox.receive) end;
136 fun init_channels channel =
138 val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
139 val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
142 if Multithreading.available then
144 val mbox = Mailbox.create ();
145 val _ = Simple_Thread.fork false (message_output mbox channel);
147 else Channel channel;
149 fun standard_message opt_serial name body =
152 message false target name
153 ((case opt_serial of SOME i => cons (Markup.serialN, Markup.print_int i) | _ => I)
154 (Position.properties_of (Position.thread_data ()))) body;
156 Output.Private_Hooks.status_fn := standard_message NONE Markup.statusN;
157 Output.Private_Hooks.report_fn := standard_message NONE Markup.reportN;
158 Output.Private_Hooks.result_fn := (fn (i, s) => standard_message (SOME i) Markup.resultN s);
159 Output.Private_Hooks.writeln_fn :=
160 (fn s => standard_message (SOME (serial ())) Markup.writelnN s);
161 Output.Private_Hooks.tracing_fn :=
162 (fn s => (update_tracing (); standard_message (SOME (serial ())) Markup.tracingN s));
163 Output.Private_Hooks.warning_fn :=
164 (fn s => standard_message (SOME (serial ())) Markup.warningN s);
165 Output.Private_Hooks.error_fn :=
166 (fn (i, s) => standard_message (SOME i) Markup.errorN s);
167 Output.Private_Hooks.protocol_message_fn := message true target Markup.protocolN;
168 Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
169 Output.Private_Hooks.prompt_fn := ignore;
170 message true target Markup.initN [] (Session.welcome ())
176 (* protocol loop -- uninterruptible *)
178 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
183 (Synchronized.change crashes (cons crash);
184 warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
186 fun read_chunk channel len =
189 (case Int.fromString len of
191 | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
192 val chunk = System_Channel.inputN channel n;
196 else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
199 fun read_command channel =
200 (case System_Channel.input_line channel of
201 NONE => raise Runtime.TERMINATE
202 | SOME line => map (read_chunk channel) (space_explode "," line));
205 Future.worker_guest "Isabelle_Process.loop" (Future.new_group NONE) e ();
211 (case read_command channel of
212 [] => (Output.error_msg "Isabelle process: no input"; true)
213 | name :: args => (worker_guest (fn () => run_command name args); true))
214 handle Runtime.TERMINATE => false
215 | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
216 in if continue then loop channel else () end;
224 [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN, Graph_Display.active_graphN];
225 val default_modes2 = [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN];
227 val init = uninterruptible (fn _ => fn rendezvous =>
229 val _ = Output.physical_stderr Symbol.STX;
231 val _ = Printer.show_markup_default := true;
232 val _ = Context.set_thread_data NONE;
234 Unsynchronized.change print_mode
235 (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
237 val channel = rendezvous ();
238 val _ = init_channels channel;
239 val _ = Session.init_protocol_handlers ();
240 in loop channel end);
242 fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
243 fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
249 protocol_command "Isabelle_Process.options"
250 (fn [options_yxml] =>
251 let val options = Options.decode (YXML.parse_body options_yxml) in
252 Options.set_default options;
253 Future.ML_statistics := true;
254 Multithreading.trace := Options.int options "threads_trace";
255 Multithreading.max_threads := Options.int options "threads";
256 Goal.skip_proofs := Options.bool options "editor_skip_proofs";
257 Goal.parallel_proofs := (if Options.int options "parallel_proofs" > 0 then 3 else 0)