src/Pure/System/isabelle_process.ML
author wenzelm
Tue Sep 06 10:16:12 2011 +0200 (2011-09-06 ago)
changeset 44731 8f7b3a89fc15
parent 44389 a3b5fdfb04a3
child 44988 33aa6da101d8
permissions -rw-r--r--
flush after Output.raw_message (and init message) for reduced latency of important protocol events;
     1 (*  Title:      Pure/System/isabelle_process.ML
     2     Author:     Makarius
     3 
     4 Isabelle process wrapper, based on private fifos for maximum
     5 robustness and performance.
     6 
     7 Startup phases:
     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
    16 *)
    17 
    18 signature ISABELLE_PROCESS =
    19 sig
    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
    25 end;
    26 
    27 structure Isabelle_Process: ISABELLE_PROCESS =
    28 struct
    29 
    30 (* print mode *)
    31 
    32 val isabelle_processN = "isabelle_process";
    33 
    34 fun is_active () = Print_Mode.print_mode_active isabelle_processN;
    35 
    36 val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
    37 val _ = Markup.add_mode isabelle_processN YXML.output_markup;
    38 
    39 
    40 (* commands *)
    41 
    42 local
    43 
    44 val commands =
    45   Synchronized.var "Isabelle_Process.commands" (Symtab.empty: (string list -> unit) Symtab.table);
    46 
    47 in
    48 
    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));
    54 
    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);
    59 
    60 end;
    61 
    62 
    63 (* message channels *)
    64 
    65 local
    66 
    67 fun chunk s = [string_of_int (size s), "\n", s];
    68 
    69 fun message do_flush mbox ch raw_props body =
    70   let
    71     val robust_props = map (pairself YXML.embed_controls) raw_props;
    72     val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
    73   in Mailbox.send mbox (chunk header @ chunk body, do_flush) end;
    74 
    75 fun standard_message mbox opt_serial ch body =
    76   if body = "" then ()
    77   else
    78     message false mbox ch
    79       ((case opt_serial of SOME i => cons (Markup.serialN, string_of_int i) | _ => I)
    80         (Position.properties_of (Position.thread_data ()))) body;
    81 
    82 fun message_output mbox out_stream =
    83   let
    84     fun flush () = ignore (try TextIO.flushOut out_stream);
    85     fun loop receive =
    86       (case receive mbox of
    87         SOME (msg, do_flush) =>
    88          (List.app (fn s => TextIO.output (out_stream, s)) msg;
    89           if do_flush then flush () else ();
    90           loop (Mailbox.receive_timeout (seconds 0.02)))
    91       | NONE => (flush (); loop (SOME o Mailbox.receive)));
    92   in fn () => loop (SOME o Mailbox.receive) end;
    93 
    94 in
    95 
    96 fun setup_channels in_fifo out_fifo =
    97   let
    98     (*rendezvous*)
    99     val in_stream = TextIO.openIn in_fifo;
   100     val out_stream = TextIO.openOut out_fifo;
   101 
   102     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
   103     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
   104 
   105     val mbox = Mailbox.create () : (string list * bool) Mailbox.T;
   106     val _ = Simple_Thread.fork false (message_output mbox out_stream);
   107   in
   108     Output.Private_Hooks.status_fn := standard_message mbox NONE "B";
   109     Output.Private_Hooks.report_fn := standard_message mbox NONE "C";
   110     Output.Private_Hooks.writeln_fn := (fn s => standard_message mbox (SOME (serial ())) "D" s);
   111     Output.Private_Hooks.tracing_fn := (fn s => standard_message mbox (SOME (serial ())) "E" s);
   112     Output.Private_Hooks.warning_fn := (fn s => standard_message mbox (SOME (serial ())) "F" s);
   113     Output.Private_Hooks.error_fn := (fn (i, s) => standard_message mbox (SOME i) "G" s);
   114     Output.Private_Hooks.raw_message_fn := message true mbox "H";
   115     Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
   116     Output.Private_Hooks.prompt_fn := ignore;
   117     message true mbox "A" [] (Session.welcome ());
   118     in_stream
   119   end;
   120 
   121 end;
   122 
   123 
   124 (* protocol loop -- uninterruptible *)
   125 
   126 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
   127 
   128 local
   129 
   130 fun recover crash =
   131   (Synchronized.change crashes (cons crash);
   132     warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
   133 
   134 fun read_chunk stream len =
   135   let
   136     val n =
   137       (case Int.fromString len of
   138         SOME n => n
   139       | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
   140     val chunk = TextIO.inputN (stream, n);
   141     val m = size chunk;
   142   in
   143     if m = n then chunk
   144     else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
   145   end;
   146 
   147 fun read_command stream =
   148   (case TextIO.inputLine stream of
   149     NONE => raise Runtime.TERMINATE
   150   | SOME line => map (read_chunk stream) (space_explode "," line));
   151 
   152 fun run_command name args =
   153   Runtime.debugging (command name) args
   154     handle exn =>
   155       error ("Isabelle process command failure: " ^ name ^ "\n" ^ ML_Compiler.exn_message exn);
   156 
   157 in
   158 
   159 fun loop stream =
   160   let val continue =
   161     (case read_command stream of
   162       [] => (Output.error_msg "Isabelle process: no input"; true)
   163     | name :: args => (run_command name args; true))
   164     handle Runtime.TERMINATE => false
   165       | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
   166   in if continue then loop stream else () end;
   167 
   168 end;
   169 
   170 
   171 (* init *)
   172 
   173 fun init in_fifo out_fifo = ignore (Simple_Thread.fork false (fn () =>
   174   let
   175     val _ = OS.Process.sleep (seconds 0.5);  (*yield to raw ML toplevel*)
   176     val _ = Output.physical_stdout Symbol.STX;
   177 
   178     val _ = quick_and_dirty := true;
   179     val _ = Goal.parallel_proofs := 0;
   180     val _ = Context.set_thread_data NONE;
   181     val _ =
   182       Unsynchronized.change print_mode
   183         (fold (update op =)
   184           [Symbol.xsymbolsN, isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
   185 
   186     val in_stream = setup_channels in_fifo out_fifo;
   187     val _ = Keyword.status ();
   188     val _ = Thy_Info.status ();
   189     val _ = Output.status (Markup.markup Markup.ready "process ready");
   190   in loop in_stream end));
   191 
   192 end;
   193