1 (* Title: Pure/System/isabelle_process.ML
4 Isabelle process wrapper, based on private fifos for maximum
5 robustness and performance.
8 . raw Posix process startup with uncontrolled output on stdout/stderr
9 . stdout \002: ML running
10 .. stdin/stdout/stderr freely available (raw ML loop)
11 .. protocol thread initialization
12 ... switch to in_fifo/out_fifo channels (rendezvous via open)
13 ... out_fifo INIT(pid): channels ready
14 ... out_fifo STATUS(keywords)
15 ... out_fifo READY: main loop ready
18 signature ISABELLE_PROCESS =
20 val is_active: unit -> bool
21 val add_command: string -> (string list -> unit) -> unit
22 val command: string -> string list -> unit
23 val crashes: exn list Synchronized.var
24 val init: string -> 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;
45 Synchronized.var "Isabelle_Process.commands" (Symtab.empty: (string list -> unit) Symtab.table);
49 fun add_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 command name args =
56 (case Symtab.lookup (Synchronized.value commands) name of
57 NONE => error ("Undefined Isabelle process command " ^ quote name)
58 | SOME cmd => cmd args);
63 (* message channels *)
67 fun chunk s = [string_of_int (size s), "\n", s];
69 fun message _ _ _ "" = ()
70 | message mbox ch raw_props body =
72 val robust_props = map (pairself YXML.escape_controls) raw_props;
73 val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
74 in Mailbox.send mbox (chunk header @ chunk body) end;
76 fun standard_message mbox with_serial ch body =
78 ((if with_serial then cons (Markup.serialN, serial_string ()) else I)
79 (Position.properties_of (Position.thread_data ()))) body;
81 fun message_output mbox out_stream =
86 (List.app (fn s => TextIO.output (out_stream, s)) msg;
87 loop (Mailbox.receive_timeout (seconds 0.02)))
88 | NONE => (try TextIO.flushOut out_stream; loop (SOME o Mailbox.receive)));
89 in fn () => loop (SOME o Mailbox.receive) end;
93 fun setup_channels in_fifo out_fifo =
96 val in_stream = TextIO.openIn in_fifo;
97 val out_stream = TextIO.openOut out_fifo;
99 val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
100 val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
102 val mbox = Mailbox.create () : string list Mailbox.T;
103 val _ = Simple_Thread.fork false (message_output mbox out_stream);
105 Output.Private_Hooks.status_fn := standard_message mbox false "B";
106 Output.Private_Hooks.report_fn := standard_message mbox false "C";
107 Output.Private_Hooks.writeln_fn := standard_message mbox true "D";
108 Output.Private_Hooks.tracing_fn := standard_message mbox true "E";
109 Output.Private_Hooks.warning_fn := standard_message mbox true "F";
110 Output.Private_Hooks.error_fn := standard_message mbox true "G";
111 Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
112 Output.Private_Hooks.prompt_fn := ignore;
113 message mbox "A" [] (Session.welcome ());
120 (* protocol loop -- uninterruptible *)
122 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
127 (Synchronized.change crashes (cons crash);
128 warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
130 fun read_chunk stream len =
133 (case Int.fromString len of
135 | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
136 val chunk = TextIO.inputN (stream, n);
140 else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
143 fun read_command stream =
144 (case TextIO.inputLine stream of
145 NONE => raise Runtime.TERMINATE
146 | SOME line => map (read_chunk stream) (space_explode "," line));
148 fun run_command name args =
149 Runtime.debugging (command name) args
151 error ("Isabelle process command failure: " ^ name ^ "\n" ^ ML_Compiler.exn_message exn);
157 (case read_command stream of
158 [] => (Output.error_msg "Isabelle process: no input"; true)
159 | name :: args => (run_command name args; true))
160 handle Runtime.TERMINATE => false
161 | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
162 in if continue then loop stream else () end;
169 fun init in_fifo out_fifo = ignore (Simple_Thread.fork false (fn () =>
171 val _ = OS.Process.sleep (seconds 0.5); (*yield to raw ML toplevel*)
172 val _ = Output.raw_stdout Symbol.STX;
174 val _ = quick_and_dirty := true;
175 val _ = Goal.parallel_proofs := 0;
176 val _ = Context.set_thread_data NONE;
178 Unsynchronized.change print_mode
180 [Symbol.xsymbolsN, isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
182 val in_stream = setup_channels in_fifo out_fifo;
183 val _ = Keyword.status ();
184 val _ = Thy_Info.status ();
185 val _ = Output.status (Markup.markup Markup.ready "process ready");
186 in loop in_stream end));