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 . 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(pid): channels ready
14 ... message RAW(keywords)
15 ... message RAW(ready): main loop ready
18 signature ISABELLE_PROCESS =
20 val is_active: unit -> bool
21 val protocol_command: string -> (string list -> 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 (* message channels *)
70 fun chunk s = [string_of_int (size s), "\n", s];
72 fun message do_flush mbox ch raw_props body =
74 val robust_props = map (pairself YXML.embed_controls) raw_props;
75 val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
76 in Mailbox.send mbox (chunk header @ chunk body, do_flush) end;
78 fun standard_message mbox opt_serial ch body =
82 ((case opt_serial of SOME i => cons (Isabelle_Markup.serialN, string_of_int i) | _ => I)
83 (Position.properties_of (Position.thread_data ()))) body;
85 fun message_output mbox channel =
87 fun flush () = ignore (try System_Channel.flush channel);
90 SOME (msg, do_flush) =>
91 (List.app (fn s => System_Channel.output channel s) msg;
92 if do_flush then flush () else ();
93 loop (Mailbox.receive_timeout (seconds 0.02)))
94 | NONE => (flush (); loop (SOME o Mailbox.receive)));
95 in fn () => loop (SOME o Mailbox.receive) end;
99 fun setup_channels channel =
101 val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
102 val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
104 val mbox = Mailbox.create () : (string list * bool) Mailbox.T;
105 val _ = Simple_Thread.fork false (message_output mbox channel);
107 Output.Private_Hooks.status_fn := standard_message mbox NONE "B";
108 Output.Private_Hooks.report_fn := standard_message mbox NONE "C";
109 Output.Private_Hooks.writeln_fn := (fn s => standard_message mbox (SOME (serial ())) "D" s);
110 Output.Private_Hooks.tracing_fn := (fn s => standard_message mbox (SOME (serial ())) "E" s);
111 Output.Private_Hooks.warning_fn := (fn s => standard_message mbox (SOME (serial ())) "F" s);
112 Output.Private_Hooks.error_fn := (fn (i, s) => standard_message mbox (SOME i) "G" s);
113 Output.Private_Hooks.protocol_message_fn := message true mbox "H";
114 Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
115 Output.Private_Hooks.prompt_fn := ignore;
116 message true mbox "A" [] (Session.welcome ())
122 (* protocol loop -- uninterruptible *)
124 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
129 (Synchronized.change crashes (cons crash);
130 warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
132 fun read_chunk channel len =
135 (case Int.fromString len of
137 | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
138 val chunk = System_Channel.inputN channel n;
142 else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
145 fun read_command channel =
146 (case System_Channel.input_line channel of
147 NONE => raise Runtime.TERMINATE
148 | SOME line => map (read_chunk channel) (space_explode "," line));
154 (case read_command channel of
155 [] => (Output.error_msg "Isabelle process: no input"; true)
156 | name :: args => (run_command name args; true))
157 handle Runtime.TERMINATE => false
158 | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
159 in if continue then loop channel else () end;
166 fun init rendezvous = ignore (Simple_Thread.fork false (fn () =>
168 val _ = OS.Process.sleep (seconds 0.5); (*yield to raw ML toplevel*)
169 val _ = Output.physical_stderr Symbol.STX;
171 val _ = quick_and_dirty := false;
172 val _ = Goal.parallel_proofs := 0;
174 if Multithreading.max_threads_value () < 2
175 then Multithreading.max_threads := 2 else ();
176 val _ = Context.set_thread_data NONE;
178 Unsynchronized.change print_mode
180 (mode @ [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN])
181 |> fold (update op =)
182 [Symbol.xsymbolsN, isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
184 val channel = rendezvous ();
185 val _ = setup_channels channel;
187 val _ = Keyword.status ();
188 val _ = Thy_Info.status ();
189 val _ = Output.protocol_message Isabelle_Markup.ready "";
190 in loop channel end));
192 fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
193 fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);