src/Pure/System/isabelle_process.ML
author wenzelm
Tue Aug 12 16:10:59 2014 +0200 (2014-08-12 ago)
changeset 57913 8544ef75d1d8
parent 56895 f058120aaad4
child 57916 2c2c24dbf0a4
permissions -rw-r--r--
more frugal standard message properties;
     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 NONE cmd args handle exn =>
    52         error ("Isabelle protocol command failure: " ^ quote name ^ "\n" ^
    53           Runtime.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 val serial_props = Markup.serial_properties o serial;
    97 
    98 fun init_channels channel =
    99   let
   100     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
   101     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
   102 
   103     val msg_channel = Message_Channel.make channel;
   104 
   105     fun message name props body =
   106       Message_Channel.send msg_channel (Message_Channel.message name props body);
   107 
   108     fun standard_message props name body =
   109       if forall (fn s => s = "") body then ()
   110       else
   111         let
   112           val props' =
   113             (case (Properties.defined props Markup.idN, Position.get_id (Position.thread_data ())) of
   114               (false, SOME id') => props @ [(Markup.idN, id')]
   115             | _ => props);
   116         in message name props' body end;
   117   in
   118     Output.status_fn := standard_message [] Markup.statusN;
   119     Output.report_fn := standard_message [] Markup.reportN;
   120     Output.result_fn :=
   121       (fn props => fn s => standard_message (props @ serial_props ()) Markup.resultN s);
   122     Output.writeln_fn := (fn s => standard_message (serial_props ()) Markup.writelnN s);
   123     Output.tracing_fn :=
   124       (fn s => (update_tracing (); standard_message (serial_props ()) Markup.tracingN s));
   125     Output.warning_fn := (fn s => standard_message (serial_props ()) Markup.warningN s);
   126     Output.error_message_fn :=
   127       (fn (i, s) => standard_message (Markup.serial_properties i) Markup.errorN s);
   128     Output.protocol_message_fn := message Markup.protocolN;
   129     Output.urgent_message_fn := ! Output.writeln_fn;
   130     Output.prompt_fn := ignore;
   131     message Markup.initN [] [Session.welcome ()];
   132     msg_channel
   133   end;
   134 
   135 
   136 (* protocol loop -- uninterruptible *)
   137 
   138 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
   139 
   140 local
   141 
   142 fun recover crash =
   143   (Synchronized.change crashes (cons crash);
   144     warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
   145 
   146 fun read_chunk channel len =
   147   let
   148     val n =
   149       (case Int.fromString len of
   150         SOME n => n
   151       | NONE => error ("Isabelle process: malformed header " ^ quote len));
   152     val chunk = System_Channel.inputN channel n;
   153     val i = size chunk;
   154   in
   155     if i <> n then
   156       error ("Isabelle process: bad chunk (unexpected EOF after " ^
   157         string_of_int i ^ " of " ^ string_of_int n ^ " bytes)")
   158     else chunk
   159   end;
   160 
   161 fun read_command channel =
   162   (case System_Channel.input_line channel of
   163     NONE => raise Runtime.TERMINATE
   164   | SOME line => map (read_chunk channel) (space_explode "," line));
   165 
   166 fun task_context e =
   167   Future.task_context "Isabelle_Process.loop" (Future.new_group NONE) e ();
   168 
   169 in
   170 
   171 fun loop channel =
   172   let val continue =
   173     (case read_command channel of
   174       [] => (Output.error_message "Isabelle process: no input"; true)
   175     | name :: args => (task_context (fn () => run_command name args); true))
   176     handle Runtime.TERMINATE => false
   177       | exn => (Runtime.exn_error_message exn handle crash => recover crash; true);
   178   in
   179     if continue then loop channel
   180     else (Future.shutdown (); Execution.reset (); ())
   181   end;
   182 
   183 end;
   184 
   185 
   186 (* init *)
   187 
   188 val default_modes1 =
   189   [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN, Graph_Display.active_graphN];
   190 val default_modes2 = [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN];
   191 
   192 val init = uninterruptible (fn _ => fn rendezvous =>
   193   let
   194     val _ = SHA1_Samples.test ()
   195       handle exn as Fail msg => (Output.physical_stderr (msg ^ "\n"); reraise exn);
   196     val _ = Output.physical_stderr Symbol.STX;
   197 
   198     val _ = Printer.show_markup_default := true;
   199     val _ = Context.set_thread_data NONE;
   200     val _ =
   201       Unsynchronized.change print_mode
   202         (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
   203 
   204     val channel = rendezvous ();
   205     val msg_channel = init_channels channel;
   206     val _ = Session.init_protocol_handlers ();
   207     val _ = (loop |> Unsynchronized.setmp Toplevel.quiet true) channel;
   208   in Message_Channel.shutdown msg_channel end);
   209 
   210 fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
   211 fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
   212 
   213 end;
   214