src/Pure/System/isabelle_process.ML
author wenzelm
Thu, 28 Nov 2013 12:54:39 +0100
changeset 55298 99b9249b3e05
parent 55286 db3d3d99c69d
child 56013 d64a4ef26edb
permissions -rw-r--r--
more official task context via Task_Queue.enroll, which is required to participate in group cancellation (e.g. to terminate command exec);
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       (Toplevel.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 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 body = "" then ()
   110       else
   111         message name
   112           (fold_rev Properties.put props (Position.properties_of (Position.thread_data ()))) body;
   113   in
   114     Output.Internal.status_fn := standard_message [] Markup.statusN;
   115     Output.Internal.report_fn := standard_message [] Markup.reportN;
   116     Output.Internal.result_fn :=
   117       (fn props => fn s => standard_message (props @ serial_props ()) Markup.resultN s);
   118     Output.Internal.writeln_fn := (fn s => standard_message (serial_props ()) Markup.writelnN s);
   119     Output.Internal.tracing_fn :=
   120       (fn s => (update_tracing (); standard_message (serial_props ()) Markup.tracingN s));
   121     Output.Internal.warning_fn := (fn s => standard_message (serial_props ()) Markup.warningN s);
   122     Output.Internal.error_fn :=
   123       (fn (i, s) => standard_message (Markup.serial_properties i) Markup.errorN s);
   124     Output.Internal.protocol_message_fn := message Markup.protocolN;
   125     Output.Internal.urgent_message_fn := ! Output.Internal.writeln_fn;
   126     Output.Internal.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 task_context e =
   163   Future.task_context "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 => (task_context (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 (); 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 _ = SHA1_Samples.test ()
   191       handle exn as Fail msg => (Output.physical_stderr (msg ^ "\n"); reraise exn);
   192     val _ = Output.physical_stderr Symbol.STX;
   193 
   194     val _ = Printer.show_markup_default := true;
   195     val _ = Context.set_thread_data NONE;
   196     val _ =
   197       Unsynchronized.change print_mode
   198         (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
   199 
   200     val channel = rendezvous ();
   201     val msg_channel = init_channels channel;
   202     val _ = Session.init_protocol_handlers ();
   203     val _ = loop channel;
   204   in Message_Channel.shutdown msg_channel end);
   205 
   206 fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
   207 fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
   208 
   209 end;
   210