src/Pure/System/isabelle_process.ML
author wenzelm
Tue Jul 30 21:22:37 2013 +0200 (2013-07-30 ago)
changeset 52800 1baa5d19ac44
parent 52799 6a4498b048b7
child 52852 08ecbffaf25c
permissions -rw-r--r--
less aggressive flushing: cope with massive amounts of protocol messages, e.g. from threads_trace;
propagate flush from ML to Scala via special protocol message;
more formal type Message_Channel.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, 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.Private_Hooks.status_fn := standard_message NONE Markup.statusN;
   114     Output.Private_Hooks.report_fn := standard_message NONE Markup.reportN;
   115     Output.Private_Hooks.result_fn := (fn (i, s) => standard_message (SOME i) Markup.resultN s);
   116     Output.Private_Hooks.writeln_fn :=
   117       (fn s => standard_message (SOME (serial ())) Markup.writelnN s);
   118     Output.Private_Hooks.tracing_fn :=
   119       (fn s => (update_tracing (); standard_message (SOME (serial ())) Markup.tracingN s));
   120     Output.Private_Hooks.warning_fn :=
   121       (fn s => standard_message (SOME (serial ())) Markup.warningN s);
   122     Output.Private_Hooks.error_fn :=
   123       (fn (i, s) => standard_message (SOME i) Markup.errorN s);
   124     Output.Private_Hooks.protocol_message_fn := message Markup.protocolN;
   125     Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
   126     Output.Private_Hooks.prompt_fn := ignore;
   127     message Markup.initN [] (Session.welcome ());
   128     msg_channel
   129   end;
   130 
   131 
   132 (* protocol loop -- uninterruptible *)
   133 
   134 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
   135 
   136 local
   137 
   138 fun recover crash =
   139   (Synchronized.change crashes (cons crash);
   140     warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
   141 
   142 fun read_chunk channel len =
   143   let
   144     val n =
   145       (case Int.fromString len of
   146         SOME n => n
   147       | NONE => error ("Isabelle process: malformed header " ^ quote len));
   148     val chunk = System_Channel.inputN channel n;
   149     val i = size chunk;
   150   in
   151     if i <> n then
   152       error ("Isabelle process: bad chunk (unexpected EOF after " ^
   153         string_of_int i ^ " of " ^ string_of_int n ^ " bytes)")
   154     else chunk
   155   end;
   156 
   157 fun read_command channel =
   158   (case System_Channel.input_line channel of
   159     NONE => raise Runtime.TERMINATE
   160   | SOME line => map (read_chunk channel) (space_explode "," line));
   161 
   162 fun worker_guest e =
   163   Future.worker_guest "Isabelle_Process.loop" (Future.new_group NONE) e ();
   164 
   165 in
   166 
   167 fun loop channel =
   168   let val continue =
   169     (case read_command channel of
   170       [] => (Output.error_msg "Isabelle process: no input"; true)
   171     | name :: args => (worker_guest (fn () => run_command name args); true))
   172     handle Runtime.TERMINATE => false
   173       | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
   174   in
   175     if continue then loop channel
   176     else (Future.shutdown (); Goal.reset_futures (); Execution.reset ())
   177   end;
   178 
   179 end;
   180 
   181 
   182 (* init *)
   183 
   184 val default_modes1 =
   185   [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN, Graph_Display.active_graphN];
   186 val default_modes2 = [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN];
   187 
   188 val init = uninterruptible (fn _ => fn rendezvous =>
   189   let
   190     val _ = Output.physical_stderr Symbol.STX;
   191 
   192     val _ = Printer.show_markup_default := true;
   193     val _ = Context.set_thread_data NONE;
   194     val _ =
   195       Unsynchronized.change print_mode
   196         (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
   197 
   198     val channel = rendezvous ();
   199     val msg_channel = init_channels channel;
   200     val _ = Session.init_protocol_handlers ();
   201     val _ = loop channel;
   202   in Message_Channel.shutdown msg_channel end);
   203 
   204 fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
   205 fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
   206 
   207 end;
   208