src/Pure/System/isabelle_process.ML
author wenzelm
Fri Aug 02 20:47:02 2013 +0200 (2013-08-02 ago)
changeset 52852 08ecbffaf25c
parent 52800 1baa5d19ac44
child 52854 92932931bd82
permissions -rw-r--r--
tuned signature;
     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, or local socket for maximum portability.
     6 *)
     7 
     8 signature ISABELLE_PROCESS =
     9 sig
    10   val is_active: unit -> bool
    11   val protocol_command: string -> (string list -> unit) -> unit
    12   val reset_tracing: Document_ID.exec -> unit
    13   val crashes: exn list Synchronized.var
    14   val init_fifos: string -> string -> unit
    15   val init_socket: string -> unit
    16 end;
    17 
    18 structure Isabelle_Process: ISABELLE_PROCESS =
    19 struct
    20 
    21 (* print mode *)
    22 
    23 val isabelle_processN = "isabelle_process";
    24 
    25 fun is_active () = Print_Mode.print_mode_active isabelle_processN;
    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 (* protocol commands *)
    32 
    33 local
    34 
    35 val commands =
    36   Synchronized.var "Isabelle_Process.commands"
    37     (Symtab.empty: (string list -> unit) Symtab.table);
    38 
    39 in
    40 
    41 fun protocol_command name cmd =
    42   Synchronized.change commands (fn cmds =>
    43    (if not (Symtab.defined cmds name) then ()
    44     else warning ("Redefining Isabelle protocol command " ^ quote name);
    45     Symtab.update (name, cmd) cmds));
    46 
    47 fun run_command name args =
    48   (case Symtab.lookup (Synchronized.value commands) name of
    49     NONE => error ("Undefined Isabelle protocol command " ^ quote name)
    50   | SOME cmd =>
    51       (Runtime.debugging cmd args handle exn =>
    52         error ("Isabelle protocol command failure: " ^ quote name ^ "\n" ^
    53           ML_Compiler.exn_message exn)));
    54 
    55 end;
    56 
    57 
    58 (* restricted tracing messages *)
    59 
    60 val tracing_messages =
    61   Synchronized.var "tracing_messages" (Inttab.empty: int Inttab.table);
    62 
    63 fun reset_tracing exec_id =
    64   Synchronized.change tracing_messages (Inttab.delete_safe exec_id);
    65 
    66 fun update_tracing () =
    67   (case Position.parse_id (Position.thread_data ()) of
    68     NONE => ()
    69   | SOME exec_id =>
    70       let
    71         val ok =
    72           Synchronized.change_result tracing_messages (fn tab =>
    73             let
    74               val n = the_default 0 (Inttab.lookup tab exec_id) + 1;
    75               val ok = n <= Options.default_int "editor_tracing_messages";
    76             in (ok, Inttab.update (exec_id, n) tab) end);
    77       in
    78         if ok then ()
    79         else
    80           let
    81             val (text, promise) = Active.dialog_text ();
    82             val _ =
    83               writeln ("Tracing paused.  " ^ text "Stop" ^ ", or continue with next " ^
    84                 text "100" ^ ", " ^ text "1000" ^ ", " ^ text "10000" ^ " messages?")
    85             val m = Markup.parse_int (Future.join promise)
    86               handle Fail _ => error "Stopped";
    87           in
    88             Synchronized.change tracing_messages
    89               (Inttab.map_default (exec_id, 0) (fn k => k - m))
    90           end
    91       end);
    92 
    93 
    94 (* output channels *)
    95 
    96 fun init_channels channel =
    97   let
    98     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
    99     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
   100 
   101     val msg_channel = Message_Channel.make channel;
   102 
   103     fun message name props body =
   104       Message_Channel.send msg_channel (Message_Channel.message name props body);
   105 
   106     fun standard_message opt_serial name body =
   107       if body = "" then ()
   108       else
   109         message name
   110           ((case opt_serial of SOME i => cons (Markup.serialN, Markup.print_int i) | _ => I)
   111             (Position.properties_of (Position.thread_data ()))) body;
   112   in
   113     Output.Internal.status_fn := standard_message NONE Markup.statusN;
   114     Output.Internal.report_fn := standard_message NONE Markup.reportN;
   115     Output.Internal.result_fn := (fn (i, s) => standard_message (SOME i) Markup.resultN s);
   116     Output.Internal.writeln_fn := (fn s => standard_message (SOME (serial ())) Markup.writelnN s);
   117     Output.Internal.tracing_fn :=
   118       (fn s => (update_tracing (); standard_message (SOME (serial ())) Markup.tracingN s));
   119     Output.Internal.warning_fn := (fn s => standard_message (SOME (serial ())) Markup.warningN s);
   120     Output.Internal.error_fn := (fn (i, s) => standard_message (SOME i) Markup.errorN s);
   121     Output.Internal.protocol_message_fn := message Markup.protocolN;
   122     Output.Internal.urgent_message_fn := ! Output.Internal.writeln_fn;
   123     Output.Internal.prompt_fn := ignore;
   124     message Markup.initN [] (Session.welcome ());
   125     msg_channel
   126   end;
   127 
   128 
   129 (* protocol loop -- uninterruptible *)
   130 
   131 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
   132 
   133 local
   134 
   135 fun recover crash =
   136   (Synchronized.change crashes (cons crash);
   137     warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
   138 
   139 fun read_chunk channel len =
   140   let
   141     val n =
   142       (case Int.fromString len of
   143         SOME n => n
   144       | NONE => error ("Isabelle process: malformed header " ^ quote len));
   145     val chunk = System_Channel.inputN channel n;
   146     val i = size chunk;
   147   in
   148     if i <> n then
   149       error ("Isabelle process: bad chunk (unexpected EOF after " ^
   150         string_of_int i ^ " of " ^ string_of_int n ^ " bytes)")
   151     else chunk
   152   end;
   153 
   154 fun read_command channel =
   155   (case System_Channel.input_line channel of
   156     NONE => raise Runtime.TERMINATE
   157   | SOME line => map (read_chunk channel) (space_explode "," line));
   158 
   159 fun worker_guest e =
   160   Future.worker_guest "Isabelle_Process.loop" (Future.new_group NONE) e ();
   161 
   162 in
   163 
   164 fun loop channel =
   165   let val continue =
   166     (case read_command channel of
   167       [] => (Output.error_msg "Isabelle process: no input"; true)
   168     | name :: args => (worker_guest (fn () => run_command name args); true))
   169     handle Runtime.TERMINATE => false
   170       | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
   171   in
   172     if continue then loop channel
   173     else (Future.shutdown (); Goal.reset_futures (); Execution.reset ())
   174   end;
   175 
   176 end;
   177 
   178 
   179 (* init *)
   180 
   181 val default_modes1 =
   182   [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN, Graph_Display.active_graphN];
   183 val default_modes2 = [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN];
   184 
   185 val init = uninterruptible (fn _ => fn rendezvous =>
   186   let
   187     val _ = Output.physical_stderr Symbol.STX;
   188 
   189     val _ = Printer.show_markup_default := true;
   190     val _ = Context.set_thread_data NONE;
   191     val _ =
   192       Unsynchronized.change print_mode
   193         (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
   194 
   195     val channel = rendezvous ();
   196     val msg_channel = init_channels channel;
   197     val _ = Session.init_protocol_handlers ();
   198     val _ = loop channel;
   199   in Message_Channel.shutdown msg_channel end);
   200 
   201 fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
   202 fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
   203 
   204 end;
   205