src/Pure/System/isabelle_process.ML
author wenzelm
Wed Aug 11 00:44:48 2010 +0200 (2010-08-11 ago)
changeset 38270 71bb3c273dd1
parent 38265 cc9fde54311f
child 38445 ba9ea6b9b75c
permissions -rw-r--r--
native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
misc clarification of proc/pid state, eliminated closing flag;
misc tuning and simplification;
     1 (*  Title:      Pure/System/isabelle_process.ML
     2     Author:     Makarius
     3 
     4 Isabelle process wrapper.
     5 
     6 General format of process output:
     7   (1) message = "\002"  header chunk  body chunk
     8   (2) chunk = size (ASCII digits)  \n  content (YXML)
     9 *)
    10 
    11 signature ISABELLE_PROCESS =
    12 sig
    13   val isabelle_processN: string
    14   val add_command: string -> (string list -> unit) -> unit
    15   val command: string -> string list -> unit
    16   val crashes: exn list Unsynchronized.ref
    17   val init: string -> string -> unit
    18 end;
    19 
    20 structure Isabelle_Process: ISABELLE_PROCESS =
    21 struct
    22 
    23 (* print modes *)
    24 
    25 val isabelle_processN = "isabelle_process";
    26 
    27 val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
    28 val _ = Markup.add_mode isabelle_processN YXML.output_markup;
    29 
    30 
    31 (* commands *)
    32 
    33 local
    34 
    35 val global_commands = Unsynchronized.ref (Symtab.empty: (string list -> unit) Symtab.table);
    36 
    37 in
    38 
    39 fun add_command name cmd = CRITICAL (fn () =>
    40   Unsynchronized.change global_commands (fn cmds =>
    41    (if not (Symtab.defined cmds name) then ()
    42     else warning ("Redefining Isabelle process command " ^ quote name);
    43     Symtab.update (name, cmd) cmds)));
    44 
    45 fun command name args =
    46   (case Symtab.lookup (! global_commands) name of
    47     NONE => error ("Undefined Isabelle process command " ^ quote name)
    48   | SOME cmd => cmd args);
    49 
    50 end;
    51 
    52 
    53 (* message markup *)
    54 
    55 local
    56 
    57 fun chunk s = string_of_int (size s) ^ "\n" ^ s;
    58 
    59 fun message _ _ _ "" = ()
    60   | message out_stream ch props body =
    61       let
    62         val header = YXML.string_of (XML.Elem ((ch, map (pairself YXML.escape_controls) props), []));
    63         val msg = Symbol.STX ^ chunk header ^ chunk body;
    64       in TextIO.output (out_stream, msg) (*atomic output*) end;
    65 
    66 in
    67 
    68 fun standard_message out_stream ch body =
    69   message out_stream ch (Position.properties_of (Position.thread_data ())) body;
    70 
    71 fun init_message out_stream =
    72   message out_stream "A" [(Markup.pidN, process_id ())] (Session.welcome ());
    73 
    74 end;
    75 
    76 
    77 (* channels *)
    78 
    79 local
    80 
    81 fun auto_flush stream =
    82   let
    83     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream stream, IO.BLOCK_BUF);
    84     fun loop () =
    85       (OS.Process.sleep (Time.fromMilliseconds 20); try TextIO.flushOut stream; loop ());
    86   in loop end;
    87 
    88 fun rendezvous f fifo =
    89   let
    90     val path = File.platform_path (Path.explode fifo);
    91     val result = f fifo;  (*should block until peer is ready*)
    92     val _ =
    93       if String.isSuffix "cygwin" ml_platform then ()  (*Cygwin 1.7: no proper blocking on open*)
    94       else OS.FileSys.remove path;  (*prevent future access*)
    95   in result end;
    96 
    97 in
    98 
    99 fun setup_channels in_fifo out_fifo =
   100   let
   101     val in_stream = rendezvous TextIO.openIn in_fifo;
   102     val out_stream = rendezvous TextIO.openOut out_fifo;
   103     val _ = Simple_Thread.fork false (auto_flush out_stream);
   104     val _ = Simple_Thread.fork false (auto_flush TextIO.stdOut);
   105     val _ = Simple_Thread.fork false (auto_flush TextIO.stdErr);
   106   in
   107     Output.status_fn   := standard_message out_stream "B";
   108     Output.report_fn   := standard_message out_stream "C";
   109     Output.writeln_fn  := standard_message out_stream "D";
   110     Output.tracing_fn  := standard_message out_stream "E";
   111     Output.warning_fn  := standard_message out_stream "F";
   112     Output.error_fn    := standard_message out_stream "G";
   113     Output.debug_fn    := standard_message out_stream "H";
   114     Output.priority_fn := ! Output.writeln_fn;
   115     Output.prompt_fn   := ignore;
   116     (in_stream, out_stream)
   117   end;
   118 
   119 end;
   120 
   121 
   122 (* protocol loop *)
   123 
   124 val crashes = Unsynchronized.ref ([]: exn list);
   125 
   126 local
   127 
   128 fun recover crash =
   129   (CRITICAL (fn () => Unsynchronized.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 =
   172   let
   173     val _ = Unsynchronized.change print_mode
   174       (fold (update op =) [isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
   175     val (in_stream, out_stream) = setup_channels in_fifo out_fifo;
   176     val _ = init_message out_stream;
   177     val _ = quick_and_dirty := true;  (* FIXME !? *)
   178     val _ = Keyword.status ();
   179     val _ = Output.status (Markup.markup Markup.ready "");
   180     val _ = Context.set_thread_data NONE;
   181     val _ = Simple_Thread.fork false (fn () => (loop in_stream; quit ()));
   182   in () end;
   183 
   184 end;