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