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