src/Pure/System/isabelle_process.ML
author wenzelm
Tue, 25 Sep 2012 22:36:06 +0200
changeset 50581 66cbf8bb4693
parent 50123 61e222517d06
child 50662 21ae8500d261
permissions -rw-r--r--
basic integration of graphview into document model;
added Graph_Dockable;
updated Isabelle/jEdit authors and dependencies etc.;
     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 Startup phases:
     8   - raw Posix process startup with uncontrolled output on stdout/stderr
     9   - stderr \002: ML running
    10   - stdin/stdout/stderr freely available (raw ML loop)
    11   - protocol thread initialization
    12   - rendezvous on system channel
    13   - message INIT: channels ready
    14 *)
    15 
    16 signature ISABELLE_PROCESS =
    17 sig
    18   val is_active: unit -> bool
    19   val protocol_command: string -> (string list -> unit) -> unit
    20   val crashes: exn list Synchronized.var
    21   val init_fifos: string -> string -> unit
    22   val init_socket: string -> unit
    23 end;
    24 
    25 structure Isabelle_Process: ISABELLE_PROCESS =
    26 struct
    27 
    28 (* print mode *)
    29 
    30 val isabelle_processN = "isabelle_process";
    31 
    32 fun is_active () = Print_Mode.print_mode_active isabelle_processN;
    33 
    34 val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
    35 val _ = Markup.add_mode isabelle_processN YXML.output_markup;
    36 
    37 
    38 (* protocol commands *)
    39 
    40 local
    41 
    42 val commands =
    43   Synchronized.var "Isabelle_Process.commands" (Symtab.empty: (string list -> unit) Symtab.table);
    44 
    45 in
    46 
    47 fun protocol_command name cmd =
    48   Synchronized.change commands (fn cmds =>
    49    (if not (Symtab.defined cmds name) then ()
    50     else warning ("Redefining Isabelle process command " ^ quote name);
    51     Symtab.update (name, cmd) cmds));
    52 
    53 fun run_command name args =
    54   (case Symtab.lookup (Synchronized.value commands) name of
    55     NONE => error ("Undefined Isabelle process command " ^ quote name)
    56   | SOME cmd =>
    57       (Runtime.debugging cmd args handle exn =>
    58         error ("Isabelle process protocol failure: " ^ quote name ^ "\n" ^
    59           ML_Compiler.exn_message exn)));
    60 
    61 end;
    62 
    63 
    64 (* message channels *)
    65 
    66 local
    67 
    68 fun chunk s = [string_of_int (size s), "\n", s];
    69 
    70 fun message do_flush mbox ch raw_props body =
    71   let
    72     val robust_props = map (pairself YXML.embed_controls) raw_props;
    73     val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
    74   in Mailbox.send mbox (chunk header @ chunk body, do_flush) end;
    75 
    76 fun standard_message mbox opt_serial ch body =
    77   if body = "" then ()
    78   else
    79     message false mbox ch
    80       ((case opt_serial of SOME i => cons (Isabelle_Markup.serialN, string_of_int i) | _ => I)
    81         (Position.properties_of (Position.thread_data ()))) body;
    82 
    83 fun message_output mbox channel =
    84   let
    85     fun flush () = ignore (try System_Channel.flush channel);
    86     fun loop receive =
    87       (case receive mbox of
    88         SOME (msg, do_flush) =>
    89          (List.app (fn s => System_Channel.output channel s) msg;
    90           if do_flush then flush () else ();
    91           loop (Mailbox.receive_timeout (seconds 0.02)))
    92       | NONE => (flush (); loop (SOME o Mailbox.receive)));
    93   in fn () => loop (SOME o Mailbox.receive) end;
    94 
    95 in
    96 
    97 fun init_channels channel =
    98   let
    99     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
   100     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
   101 
   102     val mbox = Mailbox.create () : (string list * bool) Mailbox.T;
   103     val _ = Simple_Thread.fork false (message_output mbox channel);
   104   in
   105     Output.Private_Hooks.status_fn := standard_message mbox NONE "B";
   106     Output.Private_Hooks.report_fn := standard_message mbox NONE "C";
   107     Output.Private_Hooks.writeln_fn := (fn s => standard_message mbox (SOME (serial ())) "D" s);
   108     Output.Private_Hooks.tracing_fn := (fn s => standard_message mbox (SOME (serial ())) "E" s);
   109     Output.Private_Hooks.warning_fn := (fn s => standard_message mbox (SOME (serial ())) "F" s);
   110     Output.Private_Hooks.error_fn := (fn (i, s) => standard_message mbox (SOME i) "G" s);
   111     Output.Private_Hooks.protocol_message_fn := message true mbox "H";
   112     Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
   113     Output.Private_Hooks.prompt_fn := ignore;
   114     message true mbox "A" [] (Session.welcome ())
   115   end;
   116 
   117 end;
   118 
   119 
   120 (* protocol loop -- uninterruptible *)
   121 
   122 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
   123 
   124 local
   125 
   126 fun recover crash =
   127   (Synchronized.change crashes (cons crash);
   128     warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
   129 
   130 fun read_chunk channel len =
   131   let
   132     val n =
   133       (case Int.fromString len of
   134         SOME n => n
   135       | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
   136     val chunk = System_Channel.inputN channel n;
   137     val m = size chunk;
   138   in
   139     if m = n then chunk
   140     else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
   141   end;
   142 
   143 fun read_command channel =
   144   (case System_Channel.input_line channel of
   145     NONE => raise Runtime.TERMINATE
   146   | SOME line => map (read_chunk channel) (space_explode "," line));
   147 
   148 in
   149 
   150 fun loop channel =
   151   let val continue =
   152     (case read_command channel of
   153       [] => (Output.error_msg "Isabelle process: no input"; true)
   154     | name :: args => (run_command name args; true))
   155     handle Runtime.TERMINATE => false
   156       | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
   157   in if continue then loop channel else () end;
   158 
   159 end;
   160 
   161 
   162 (* init *)
   163 
   164 val default_modes1 = [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN];
   165 val default_modes2 =
   166   [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN, Graph_Display.graphview_reportN];
   167 
   168 fun init rendezvous = ignore (Simple_Thread.fork false (fn () =>
   169   let
   170     val _ = OS.Process.sleep (seconds 0.5);  (*yield to raw ML toplevel*)
   171     val _ = Output.physical_stderr Symbol.STX;
   172 
   173     val _ = quick_and_dirty := false;
   174     val _ = Goal.parallel_proofs := 4;
   175     val _ =
   176       if Multithreading.max_threads_value () < 2
   177       then Multithreading.max_threads := 2 else ();
   178     val _ = Context.set_thread_data NONE;
   179     val _ =
   180       Unsynchronized.change print_mode
   181         (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
   182 
   183     val channel = rendezvous ();
   184     val _ = init_channels channel;
   185   in loop channel end));
   186 
   187 fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
   188 fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
   189 
   190 end;
   191