src/Pure/System/isabelle_process.ML
author wenzelm
Fri Jun 01 13:02:09 2012 +0200 (2012-06-01 ago)
changeset 48056 396749e9daaf
parent 47410 33f2f968c0a1
child 48709 719f458cd89e
permissions -rw-r--r--
tuned message;
     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   . stderr \002: ML running
    10   .. stdin/stdout/stderr freely available (raw ML loop)
    11   .. protocol thread initialization
    12   ... rendezvous on system channel
    13   ... message INIT(pid): channels ready
    14   ... message RAW(keywords)
    15   ... message RAW(ready): main loop ready
    16 *)
    17 
    18 signature ISABELLE_PROCESS =
    19 sig
    20   val is_active: unit -> bool
    21   val protocol_command: string -> (string list -> unit) -> unit
    22   val crashes: exn list Synchronized.var
    23   val init_fifos: string -> string -> unit
    24   val init_socket: 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 (* protocol 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 protocol_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 run_command name args =
    56   (case Symtab.lookup (Synchronized.value commands) name of
    57     NONE => error ("Undefined Isabelle process command " ^ quote name)
    58   | SOME cmd =>
    59       (Runtime.debugging cmd args handle exn =>
    60         error ("Isabelle process protocol failure: " ^ quote name ^ "\n" ^
    61           ML_Compiler.exn_message exn)));
    62 
    63 end;
    64 
    65 
    66 (* message channels *)
    67 
    68 local
    69 
    70 fun chunk s = [string_of_int (size s), "\n", s];
    71 
    72 fun message do_flush mbox ch raw_props body =
    73   let
    74     val robust_props = map (pairself YXML.embed_controls) raw_props;
    75     val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
    76   in Mailbox.send mbox (chunk header @ chunk body, do_flush) end;
    77 
    78 fun standard_message mbox opt_serial ch body =
    79   if body = "" then ()
    80   else
    81     message false mbox ch
    82       ((case opt_serial of SOME i => cons (Isabelle_Markup.serialN, string_of_int i) | _ => I)
    83         (Position.properties_of (Position.thread_data ()))) body;
    84 
    85 fun message_output mbox channel =
    86   let
    87     fun flush () = ignore (try System_Channel.flush channel);
    88     fun loop receive =
    89       (case receive mbox of
    90         SOME (msg, do_flush) =>
    91          (List.app (fn s => System_Channel.output channel s) msg;
    92           if do_flush then flush () else ();
    93           loop (Mailbox.receive_timeout (seconds 0.02)))
    94       | NONE => (flush (); loop (SOME o Mailbox.receive)));
    95   in fn () => loop (SOME o Mailbox.receive) end;
    96 
    97 in
    98 
    99 fun setup_channels channel =
   100   let
   101     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
   102     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
   103 
   104     val mbox = Mailbox.create () : (string list * bool) Mailbox.T;
   105     val _ = Simple_Thread.fork false (message_output mbox channel);
   106   in
   107     Output.Private_Hooks.status_fn := standard_message mbox NONE "B";
   108     Output.Private_Hooks.report_fn := standard_message mbox NONE "C";
   109     Output.Private_Hooks.writeln_fn := (fn s => standard_message mbox (SOME (serial ())) "D" s);
   110     Output.Private_Hooks.tracing_fn := (fn s => standard_message mbox (SOME (serial ())) "E" s);
   111     Output.Private_Hooks.warning_fn := (fn s => standard_message mbox (SOME (serial ())) "F" s);
   112     Output.Private_Hooks.error_fn := (fn (i, s) => standard_message mbox (SOME i) "G" s);
   113     Output.Private_Hooks.protocol_message_fn := message true mbox "H";
   114     Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
   115     Output.Private_Hooks.prompt_fn := ignore;
   116     message true mbox "A" [] (Session.welcome ())
   117   end;
   118 
   119 end;
   120 
   121 
   122 (* protocol loop -- uninterruptible *)
   123 
   124 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
   125 
   126 local
   127 
   128 fun recover crash =
   129   (Synchronized.change crashes (cons crash);
   130     warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
   131 
   132 fun read_chunk channel 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 = System_Channel.inputN channel 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 channel =
   146   (case System_Channel.input_line channel of
   147     NONE => raise Runtime.TERMINATE
   148   | SOME line => map (read_chunk channel) (space_explode "," line));
   149 
   150 in
   151 
   152 fun loop channel =
   153   let val continue =
   154     (case read_command channel of
   155       [] => (Output.error_msg "Isabelle process: no input"; true)
   156     | name :: args => (run_command name args; true))
   157     handle Runtime.TERMINATE => false
   158       | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
   159   in if continue then loop channel else () end;
   160 
   161 end;
   162 
   163 
   164 (* init *)
   165 
   166 fun init rendezvous = ignore (Simple_Thread.fork false (fn () =>
   167   let
   168     val _ = OS.Process.sleep (seconds 0.5);  (*yield to raw ML toplevel*)
   169     val _ = Output.physical_stderr Symbol.STX;
   170 
   171     val _ = quick_and_dirty := false;
   172     val _ = Goal.parallel_proofs := 0;
   173     val _ =
   174       if Multithreading.max_threads_value () < 2
   175       then Multithreading.max_threads := 2 else ();
   176     val _ = Context.set_thread_data NONE;
   177     val _ =
   178       Unsynchronized.change print_mode
   179         (fn mode =>
   180           (mode @ [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN])
   181           |> fold (update op =)
   182             [Symbol.xsymbolsN, isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
   183 
   184     val channel = rendezvous ();
   185     val _ = setup_channels channel;
   186 
   187     val _ = Keyword.status ();
   188     val _ = Thy_Info.status ();
   189     val _ = Output.protocol_message Isabelle_Markup.ready "";
   190   in loop channel end));
   191 
   192 fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
   193 fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
   194 
   195 end;
   196