src/Pure/System/isabelle_process.ML
author wenzelm
Fri Sep 17 20:56:32 2010 +0200 (2010-09-17 ago)
changeset 39509 cab2719398a7
parent 39234 d76a2fd129b5
child 39513 fce2202892c4
permissions -rw-r--r--
Isabelle_Process: status/report do not require serial numbers;
     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 
     8 signature ISABELLE_PROCESS =
     9 sig
    10   val isabelle_processN: string
    11   val add_command: string -> (string list -> unit) -> unit
    12   val command: string -> string list -> unit
    13   val crashes: exn list Unsynchronized.ref
    14   val init: string -> string -> unit
    15 end;
    16 
    17 structure Isabelle_Process: ISABELLE_PROCESS =
    18 struct
    19 
    20 (* print modes *)
    21 
    22 val isabelle_processN = "isabelle_process";
    23 
    24 val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
    25 val _ = Markup.add_mode isabelle_processN YXML.output_markup;
    26 
    27 
    28 (* commands *)
    29 
    30 local
    31 
    32 val global_commands = Unsynchronized.ref (Symtab.empty: (string list -> unit) Symtab.table);
    33 
    34 in
    35 
    36 fun add_command name cmd = CRITICAL (fn () =>
    37   Unsynchronized.change global_commands (fn cmds =>
    38    (if not (Symtab.defined cmds name) then ()
    39     else warning ("Redefining Isabelle process command " ^ quote name);
    40     Symtab.update (name, cmd) cmds)));
    41 
    42 fun command name args =
    43   (case Symtab.lookup (! global_commands) name of
    44     NONE => error ("Undefined Isabelle process command " ^ quote name)
    45   | SOME cmd => cmd args);
    46 
    47 end;
    48 
    49 
    50 (* message markup *)
    51 
    52 local
    53 
    54 fun chunk s = string_of_int (size s) ^ "\n" ^ s;
    55 
    56 fun message _ _ _ "" = ()
    57   | message out_stream ch raw_props body =
    58       let
    59         val robust_props = map (pairself YXML.escape_controls) raw_props;
    60         val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
    61       in TextIO.output (out_stream, chunk header ^ chunk body) (*atomic output!*) end;
    62 
    63 in
    64 
    65 fun standard_message out_stream with_serial ch body =
    66   message out_stream ch
    67     ((if with_serial then cons (Markup.serialN, serial_string ()) else I)
    68       (Position.properties_of (Position.thread_data ()))) body;
    69 
    70 fun init_message out_stream =
    71   message out_stream "A" [(Markup.pidN, process_id ())] (Session.welcome ());
    72 
    73 end;
    74 
    75 
    76 (* channels *)
    77 
    78 local
    79 
    80 fun auto_flush stream =
    81   let
    82     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream stream, IO.BLOCK_BUF);
    83     fun loop () =
    84       (OS.Process.sleep (Time.fromMilliseconds 20); try TextIO.flushOut stream; loop ());
    85   in loop end;
    86 
    87 fun rendezvous f fifo =
    88   let
    89     val path = File.platform_path (Path.explode fifo);
    90     val result = f fifo;  (*should block until peer is ready*)
    91     val _ =
    92       if String.isSuffix "cygwin" ml_platform then ()  (*Cygwin 1.7: no proper blocking on open*)
    93       else OS.FileSys.remove path;  (*prevent future access*)
    94   in result end;
    95 
    96 in
    97 
    98 fun setup_channels in_fifo out_fifo =
    99   let
   100     val in_stream = rendezvous TextIO.openIn in_fifo;
   101     val out_stream = rendezvous TextIO.openOut out_fifo;
   102     val _ = Simple_Thread.fork false (auto_flush out_stream);
   103     val _ = Simple_Thread.fork false (auto_flush TextIO.stdOut);
   104     val _ = Simple_Thread.fork false (auto_flush TextIO.stdErr);
   105   in
   106     Output.status_fn   := standard_message out_stream false "B";
   107     Output.report_fn   := standard_message out_stream false "C";
   108     Output.writeln_fn  := standard_message out_stream true "D";
   109     Output.tracing_fn  := standard_message out_stream true "E";
   110     Output.warning_fn  := standard_message out_stream true "F";
   111     Output.error_fn    := standard_message out_stream true "G";
   112     Output.debug_fn    := standard_message out_stream true "H";
   113     Output.priority_fn := ! Output.writeln_fn;
   114     Output.prompt_fn   := ignore;
   115     (in_stream, out_stream)
   116   end;
   117 
   118 end;
   119 
   120 
   121 (* protocol loop -- uninterruptible *)
   122 
   123 val crashes = Unsynchronized.ref ([]: exn list);
   124 
   125 local
   126 
   127 fun recover crash =
   128   (CRITICAL (fn () => Unsynchronized.change crashes (cons crash));
   129     warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
   130 
   131 fun read_chunk stream len =
   132   let
   133     val n =
   134       (case Int.fromString len of
   135         SOME n => n
   136       | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
   137     val chunk = TextIO.inputN (stream, n);
   138     val m = size chunk;
   139   in
   140     if m = n then chunk
   141     else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
   142   end;
   143 
   144 fun read_command stream =
   145   (case TextIO.inputLine stream of
   146     NONE => raise Runtime.TERMINATE
   147   | SOME line => map (read_chunk stream) (space_explode "," line));
   148 
   149 fun run_command name args =
   150   Runtime.debugging (command name) args
   151     handle exn =>
   152       error ("Isabelle process command failure: " ^ name ^ "\n" ^ ML_Compiler.exn_message exn);
   153 
   154 in
   155 
   156 fun loop stream =
   157   let val continue =
   158     (case read_command stream of
   159       [] => (Output.error_msg "Isabelle process: no input"; true)
   160     | name :: args => (run_command name args; true))
   161     handle Runtime.TERMINATE => false
   162       | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
   163   in if continue then loop stream else () end;
   164 
   165 end;
   166 
   167 
   168 (* init *)
   169 
   170 fun init in_fifo out_fifo =
   171   let
   172     val _ = Unsynchronized.change print_mode
   173       (fold (update op =) [isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
   174     val (in_stream, out_stream) = setup_channels in_fifo out_fifo;
   175     val _ = init_message out_stream;
   176     val _ = quick_and_dirty := true;  (* FIXME !? *)
   177     val _ = Keyword.status ();
   178     val _ = Output.status (Markup.markup Markup.ready "");
   179     val _ = Context.set_thread_data NONE;
   180     val _ = Simple_Thread.fork false (fn () => (loop in_stream; quit ()));
   181   in () end;
   182 
   183 end;