src/Pure/System/isabelle_process.ML
author wenzelm
Mon Aug 23 16:53:22 2010 +0200 (2010-08-23)
changeset 38639 f642faca303e
parent 38445 ba9ea6b9b75c
child 38871 28496da3bec2
permissions -rw-r--r--
main session actor as independent thread, to avoid starvation via regular worker pool;
tuned;
     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 ch body =
    66   message out_stream ch (Position.properties_of (Position.thread_data ())) body;
    67 
    68 fun init_message out_stream =
    69   message out_stream "A" [(Markup.pidN, process_id ())] (Session.welcome ());
    70 
    71 end;
    72 
    73 
    74 (* channels *)
    75 
    76 local
    77 
    78 fun auto_flush stream =
    79   let
    80     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream stream, IO.BLOCK_BUF);
    81     fun loop () =
    82       (OS.Process.sleep (Time.fromMilliseconds 20); try TextIO.flushOut stream; loop ());
    83   in loop end;
    84 
    85 fun rendezvous f fifo =
    86   let
    87     val path = File.platform_path (Path.explode fifo);
    88     val result = f fifo;  (*should block until peer is ready*)
    89     val _ =
    90       if String.isSuffix "cygwin" ml_platform then ()  (*Cygwin 1.7: no proper blocking on open*)
    91       else OS.FileSys.remove path;  (*prevent future access*)
    92   in result end;
    93 
    94 in
    95 
    96 fun setup_channels in_fifo out_fifo =
    97   let
    98     val in_stream = rendezvous TextIO.openIn in_fifo;
    99     val out_stream = rendezvous TextIO.openOut out_fifo;
   100     val _ = Simple_Thread.fork false (auto_flush out_stream);
   101     val _ = Simple_Thread.fork false (auto_flush TextIO.stdOut);
   102     val _ = Simple_Thread.fork false (auto_flush TextIO.stdErr);
   103   in
   104     Output.status_fn   := standard_message out_stream "B";
   105     Output.report_fn   := standard_message out_stream "C";
   106     Output.writeln_fn  := standard_message out_stream "D";
   107     Output.tracing_fn  := standard_message out_stream "E";
   108     Output.warning_fn  := standard_message out_stream "F";
   109     Output.error_fn    := standard_message out_stream "G";
   110     Output.debug_fn    := standard_message out_stream "H";
   111     Output.priority_fn := ! Output.writeln_fn;
   112     Output.prompt_fn   := ignore;
   113     (in_stream, out_stream)
   114   end;
   115 
   116 end;
   117 
   118 
   119 (* protocol loop *)
   120 
   121 val crashes = Unsynchronized.ref ([]: exn list);
   122 
   123 local
   124 
   125 fun recover crash =
   126   (CRITICAL (fn () => Unsynchronized.change crashes (cons crash));
   127     warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
   128 
   129 fun read_chunk stream len =
   130   let
   131     val n =
   132       (case Int.fromString len of
   133         SOME n => n
   134       | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
   135     val chunk = TextIO.inputN (stream, n);
   136     val m = size chunk;
   137   in
   138     if m = n then chunk
   139     else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
   140   end;
   141 
   142 fun read_command stream =
   143   (case TextIO.inputLine stream of
   144     NONE => raise Runtime.TERMINATE
   145   | SOME line => map (read_chunk stream) (space_explode "," line));
   146 
   147 fun run_command name args =
   148   Runtime.debugging (command name) args
   149     handle exn =>
   150       error ("Isabelle process command failure: " ^ name ^ "\n" ^ ML_Compiler.exn_message exn);
   151 
   152 in
   153 
   154 fun loop stream =
   155   let val continue =
   156     (case read_command stream of
   157       [] => (Output.error_msg "Isabelle process: no input"; true)
   158     | name :: args => (run_command name args; true))
   159     handle Runtime.TERMINATE => false
   160       | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
   161   in if continue then loop stream else () end;
   162 
   163 end;
   164 
   165 
   166 (* init *)
   167 
   168 fun init in_fifo out_fifo =
   169   let
   170     val _ = Unsynchronized.change print_mode
   171       (fold (update op =) [isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
   172     val (in_stream, out_stream) = setup_channels in_fifo out_fifo;
   173     val _ = init_message out_stream;
   174     val _ = quick_and_dirty := true;  (* FIXME !? *)
   175     val _ = Keyword.status ();
   176     val _ = Output.status (Markup.markup Markup.ready "");
   177     val _ = Context.set_thread_data NONE;
   178     val _ = Simple_Thread.fork false (fn () => (loop in_stream; quit ()));
   179   in () end;
   180 
   181 end;