src/Pure/System/isabelle_process.ML
author wenzelm
Fri, 17 Sep 2010 22:17:57 +0200
changeset 39782 fce2202892c4
parent 39778 cab2719398a7
child 39797 c01d89d18ff0
permissions -rw-r--r--
discontinued Output.debug, which belongs to early PGIP experiments (b6788dbd2ef9) and causes just too many problems (like spamming the message channel if it is used by more than one module);
     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.priority_fn := ! Output.writeln_fn;
   113     Output.prompt_fn   := ignore;
   114     (in_stream, out_stream)
   115   end;
   116 
   117 end;
   118 
   119 
   120 (* protocol loop -- uninterruptible *)
   121 
   122 val crashes = Unsynchronized.ref ([]: exn list);
   123 
   124 local
   125 
   126 fun recover crash =
   127   (CRITICAL (fn () => Unsynchronized.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 =
   170   let
   171     val _ = Unsynchronized.change print_mode
   172       (fold (update op =) [isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
   173     val (in_stream, out_stream) = setup_channels in_fifo out_fifo;
   174     val _ = init_message out_stream;
   175     val _ = quick_and_dirty := true;  (* FIXME !? *)
   176     val _ = Keyword.status ();
   177     val _ = Output.status (Markup.markup Markup.ready "");
   178     val _ = Context.set_thread_data NONE;
   179     val _ = Simple_Thread.fork false (fn () => (loop in_stream; quit ()));
   180   in () end;
   181 
   182 end;