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 crashes: exn list Synchronized.var
21 val init_fifos: string -> string -> unit
22 val init_socket: string -> unit
25 structure Isabelle_Process: ISABELLE_PROCESS =
30 val isabelle_processN = "isabelle_process";
32 fun is_active () = Print_Mode.print_mode_active isabelle_processN;
34 val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
35 val _ = Markup.add_mode isabelle_processN YXML.output_markup;
38 (* protocol commands *)
43 Synchronized.var "Isabelle_Process.commands" (Symtab.empty: (string list -> unit) Symtab.table);
47 fun protocol_command name cmd =
48 Synchronized.change commands (fn cmds =>
49 (if not (Symtab.defined cmds name) then ()
50 else warning ("Redefining Isabelle process command " ^ quote name);
51 Symtab.update (name, cmd) cmds));
53 fun run_command name args =
54 (case Symtab.lookup (Synchronized.value commands) name of
55 NONE => error ("Undefined Isabelle process command " ^ quote name)
57 (Runtime.debugging cmd args handle exn =>
58 error ("Isabelle process protocol failure: " ^ quote name ^ "\n" ^
59 ML_Compiler.exn_message exn)));
64 (* message channels *)
68 fun chunk s = [string_of_int (size s), "\n", s];
70 fun message do_flush mbox ch raw_props body =
72 val robust_props = map (pairself YXML.embed_controls) raw_props;
73 val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
74 in Mailbox.send mbox (chunk header @ chunk body, do_flush) end;
76 fun standard_message mbox opt_serial ch body =
80 ((case opt_serial of SOME i => cons (Isabelle_Markup.serialN, string_of_int i) | _ => I)
81 (Position.properties_of (Position.thread_data ()))) body;
83 fun message_output mbox channel =
85 fun flush () = ignore (try System_Channel.flush channel);
88 SOME (msg, do_flush) =>
89 (List.app (fn s => System_Channel.output channel s) msg;
90 if do_flush then flush () else ();
91 loop (Mailbox.receive_timeout (seconds 0.02)))
92 | NONE => (flush (); loop (SOME o Mailbox.receive)));
93 in fn () => loop (SOME o Mailbox.receive) end;
97 fun init_channels channel =
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 * bool) Mailbox.T;
103 val _ = Simple_Thread.fork false (message_output mbox channel);
105 Output.Private_Hooks.status_fn := standard_message mbox NONE "B";
106 Output.Private_Hooks.report_fn := standard_message mbox NONE "C";
107 Output.Private_Hooks.writeln_fn := (fn s => standard_message mbox (SOME (serial ())) "D" s);
108 Output.Private_Hooks.tracing_fn := (fn s => standard_message mbox (SOME (serial ())) "E" s);
109 Output.Private_Hooks.warning_fn := (fn s => standard_message mbox (SOME (serial ())) "F" s);
110 Output.Private_Hooks.error_fn := (fn (i, s) => standard_message mbox (SOME i) "G" s);
111 Output.Private_Hooks.protocol_message_fn := message true mbox "H";
112 Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
113 Output.Private_Hooks.prompt_fn := ignore;
114 message true 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 channel len =
133 (case Int.fromString len of
135 | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
136 val chunk = System_Channel.inputN channel n;
140 else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
143 fun read_command channel =
144 (case System_Channel.input_line channel of
145 NONE => raise Runtime.TERMINATE
146 | SOME line => map (read_chunk channel) (space_explode "," line));
152 (case read_command channel of
153 [] => (Output.error_msg "Isabelle process: no input"; true)
154 | name :: args => (run_command name args; true))
155 handle Runtime.TERMINATE => false
156 | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
157 in if continue then loop channel else () end;
164 val default_modes1 = [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN];
166 [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN, Graph_Display.graphview_reportN];
168 fun init rendezvous = ignore (Simple_Thread.fork false (fn () =>
170 val _ = OS.Process.sleep (seconds 0.5); (*yield to raw ML toplevel*)
171 val _ = Output.physical_stderr Symbol.STX;
173 val _ = quick_and_dirty := false;
174 val _ = Goal.parallel_proofs := 4;
176 if Multithreading.max_threads_value () < 2
177 then Multithreading.max_threads := 2 else ();
178 val _ = Context.set_thread_data NONE;
180 Unsynchronized.change print_mode
181 (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
183 val channel = rendezvous ();
184 val _ = init_channels channel;
185 in loop channel end));
187 fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
188 fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);