src/Pure/System/isabelle_process.ML
author wenzelm
Tue Jul 12 13:39:29 2011 +0200 (2011-07-12 ago)
changeset 43771 fc524449f511
parent 43746 a41f618c641d
child 43772 c825594fd0c1
permissions -rw-r--r--
allow empty body for raw_message -- important for Invoke_Scala;
     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 mbox ch raw_props body =
    70   let
    71     val robust_props = map (pairself YXML.escape_controls) raw_props;
    72     val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
    73   in Mailbox.send mbox (chunk header @ chunk body) end;
    74 
    75 fun standard_message mbox with_serial ch body =
    76   if body = "" then ()
    77   else
    78     message mbox ch
    79       ((if with_serial then cons (Markup.serialN, serial_string ()) else I)
    80         (Position.properties_of (Position.thread_data ()))) body;
    81 
    82 fun message_output mbox out_stream =
    83   let
    84     fun loop receive =
    85       (case receive mbox of
    86         SOME msg =>
    87          (List.app (fn s => TextIO.output (out_stream, s)) msg;
    88           loop (Mailbox.receive_timeout (seconds 0.02)))
    89       | NONE => (try TextIO.flushOut out_stream; loop (SOME o Mailbox.receive)));
    90   in fn () => loop (SOME o Mailbox.receive) end;
    91 
    92 in
    93 
    94 fun setup_channels in_fifo out_fifo =
    95   let
    96     (*rendezvous*)
    97     val in_stream = TextIO.openIn in_fifo;
    98     val out_stream = TextIO.openOut out_fifo;
    99 
   100     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
   101     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
   102 
   103     val mbox = Mailbox.create () : string list Mailbox.T;
   104     val _ = Simple_Thread.fork false (message_output mbox out_stream);
   105   in
   106     Output.Private_Hooks.status_fn := standard_message mbox false "B";
   107     Output.Private_Hooks.report_fn := standard_message mbox false "C";
   108     Output.Private_Hooks.writeln_fn := standard_message mbox true "D";
   109     Output.Private_Hooks.tracing_fn := standard_message mbox true "E";
   110     Output.Private_Hooks.warning_fn := standard_message mbox true "F";
   111     Output.Private_Hooks.error_fn := standard_message mbox true "G";
   112     Output.Private_Hooks.raw_message_fn := message mbox "H";
   113     Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
   114     Output.Private_Hooks.prompt_fn := ignore;
   115     message mbox "A" [] (Session.welcome ());
   116     in_stream
   117   end;
   118 
   119 end;
   120 
   121 
   122 (* protocol loop -- uninterruptible *)
   123 
   124 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
   125 
   126 local
   127 
   128 fun recover crash =
   129   (Synchronized.change crashes (cons crash);
   130     warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
   131 
   132 fun read_chunk stream len =
   133   let
   134     val n =
   135       (case Int.fromString len of
   136         SOME n => n
   137       | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
   138     val chunk = TextIO.inputN (stream, n);
   139     val m = size chunk;
   140   in
   141     if m = n then chunk
   142     else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
   143   end;
   144 
   145 fun read_command stream =
   146   (case TextIO.inputLine stream of
   147     NONE => raise Runtime.TERMINATE
   148   | SOME line => map (read_chunk stream) (space_explode "," line));
   149 
   150 fun run_command name args =
   151   Runtime.debugging (command name) args
   152     handle exn =>
   153       error ("Isabelle process command failure: " ^ name ^ "\n" ^ ML_Compiler.exn_message exn);
   154 
   155 in
   156 
   157 fun loop stream =
   158   let val continue =
   159     (case read_command stream of
   160       [] => (Output.error_msg "Isabelle process: no input"; true)
   161     | name :: args => (run_command name args; true))
   162     handle Runtime.TERMINATE => false
   163       | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
   164   in if continue then loop stream else () end;
   165 
   166 end;
   167 
   168 
   169 (* init *)
   170 
   171 fun init in_fifo out_fifo = ignore (Simple_Thread.fork false (fn () =>
   172   let
   173     val _ = OS.Process.sleep (seconds 0.5);  (*yield to raw ML toplevel*)
   174     val _ = Output.raw_stdout Symbol.STX;
   175 
   176     val _ = quick_and_dirty := true;
   177     val _ = Goal.parallel_proofs := 0;
   178     val _ = Context.set_thread_data NONE;
   179     val _ =
   180       Unsynchronized.change print_mode
   181         (fold (update op =)
   182           [Symbol.xsymbolsN, isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
   183 
   184     val in_stream = setup_channels in_fifo out_fifo;
   185     val _ = Keyword.status ();
   186     val _ = Thy_Info.status ();
   187     val _ = Output.status (Markup.markup Markup.ready "process ready");
   188   in loop in_stream end));
   189 
   190 end;
   191