src/Pure/System/isabelle_process.ML
author wenzelm
Fri Nov 12 21:37:01 2010 +0100 (2010-11-12 ago)
changeset 40518 035a27279705
parent 40301 bf39a257b3d3
child 40521 8896bd93488e
permissions -rw-r--r--
defensive defaults for more robust experience for new users;
     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 isabelle_processN: string
    21   val add_command: string -> (string list -> unit) -> unit
    22   val command: string -> string list -> unit
    23   val crashes: exn list Unsynchronized.ref
    24   val init: string -> string -> unit
    25 end;
    26 
    27 structure Isabelle_Process: ISABELLE_PROCESS =
    28 struct
    29 
    30 (* print modes *)
    31 
    32 val isabelle_processN = "isabelle_process";
    33 
    34 val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
    35 val _ = Markup.add_mode isabelle_processN YXML.output_markup;
    36 
    37 
    38 (* commands *)
    39 
    40 local
    41 
    42 val global_commands = Unsynchronized.ref (Symtab.empty: (string list -> unit) Symtab.table);
    43 
    44 in
    45 
    46 fun add_command name cmd = CRITICAL (fn () =>
    47   Unsynchronized.change global_commands (fn cmds =>
    48    (if not (Symtab.defined cmds name) then ()
    49     else warning ("Redefining Isabelle process command " ^ quote name);
    50     Symtab.update (name, cmd) cmds)));
    51 
    52 fun command name args =
    53   (case Symtab.lookup (! global_commands) name of
    54     NONE => error ("Undefined Isabelle process command " ^ quote name)
    55   | SOME cmd => cmd args);
    56 
    57 end;
    58 
    59 
    60 (* message channels *)
    61 
    62 local
    63 
    64 fun chunk s = [string_of_int (size s), "\n", s];
    65 
    66 fun message _ _ _ "" = ()
    67   | message mbox ch raw_props body =
    68       let
    69         val robust_props = map (pairself YXML.escape_controls) raw_props;
    70         val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
    71       in Mailbox.send mbox (chunk header @ chunk body) end;
    72 
    73 fun standard_message mbox with_serial ch body =
    74   message mbox ch
    75     ((if with_serial then cons (Markup.serialN, serial_string ()) else I)
    76       (Position.properties_of (Position.thread_data ()))) body;
    77 
    78 fun message_output mbox out_stream =
    79   let
    80     fun loop receive =
    81       (case receive mbox of
    82         SOME msg =>
    83          (List.app (fn s => TextIO.output (out_stream, s)) msg;
    84           loop (Mailbox.receive_timeout (seconds 0.02)))
    85       | NONE => (try TextIO.flushOut out_stream; loop (SOME o Mailbox.receive)));
    86   in fn () => loop (SOME o Mailbox.receive) end;
    87 
    88 in
    89 
    90 fun setup_channels in_fifo out_fifo =
    91   let
    92     (*rendezvous*)
    93     val in_stream = TextIO.openIn in_fifo;
    94     val out_stream = TextIO.openOut out_fifo;
    95 
    96     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
    97     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
    98 
    99     val mbox = Mailbox.create () : string list Mailbox.T;
   100     val _ = Simple_Thread.fork false (message_output mbox out_stream);
   101   in
   102     Output.Private_Hooks.status_fn := standard_message mbox false "B";
   103     Output.Private_Hooks.report_fn := standard_message mbox false "C";
   104     Output.Private_Hooks.writeln_fn := standard_message mbox true "D";
   105     Output.Private_Hooks.tracing_fn := standard_message mbox true "E";
   106     Output.Private_Hooks.warning_fn := standard_message mbox true "F";
   107     Output.Private_Hooks.error_fn := standard_message mbox true "G";
   108     Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
   109     Output.Private_Hooks.prompt_fn := ignore;
   110     message mbox "A" [] (Session.welcome ());
   111     in_stream
   112   end;
   113 
   114 end;
   115 
   116 
   117 (* protocol loop -- uninterruptible *)
   118 
   119 val crashes = Unsynchronized.ref ([]: exn list);
   120 
   121 local
   122 
   123 fun recover crash =
   124   (CRITICAL (fn () => Unsynchronized.change crashes (cons crash));
   125     warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
   126 
   127 fun read_chunk stream len =
   128   let
   129     val n =
   130       (case Int.fromString len of
   131         SOME n => n
   132       | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
   133     val chunk = TextIO.inputN (stream, n);
   134     val m = size chunk;
   135   in
   136     if m = n then chunk
   137     else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
   138   end;
   139 
   140 fun read_command stream =
   141   (case TextIO.inputLine stream of
   142     NONE => raise Runtime.TERMINATE
   143   | SOME line => map (read_chunk stream) (space_explode "," line));
   144 
   145 fun run_command name args =
   146   Runtime.debugging (command name) args
   147     handle exn =>
   148       error ("Isabelle process command failure: " ^ name ^ "\n" ^ ML_Compiler.exn_message exn);
   149 
   150 in
   151 
   152 fun loop stream =
   153   let val continue =
   154     (case read_command stream 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 stream else () end;
   160 
   161 end;
   162 
   163 
   164 (* init *)
   165 
   166 fun init in_fifo out_fifo = ignore (Simple_Thread.fork false (fn () =>
   167   let
   168     val _ = OS.Process.sleep (seconds 0.5);  (*yield to raw ML toplevel*)
   169     val _ = Output.raw_stdout Symbol.STX;
   170 
   171     val _ = quick_and_dirty := false;
   172     val _ = Goal.parallel_proofs := 0;
   173     val _ = Context.set_thread_data NONE;
   174     val _ = Unsynchronized.change print_mode
   175       (fold (update op =) [isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
   176 
   177     val in_stream = setup_channels in_fifo out_fifo;
   178     val _ = Keyword.status ();
   179     val _ = Output.status (Markup.markup Markup.ready "process ready");
   180   in loop in_stream end));
   181 
   182 end;
   183