src/Pure/System/isabelle_process.ML
author wenzelm
Fri Aug 02 22:17:53 2013 +0200 (2013-08-02 ago)
changeset 52854 92932931bd82
parent 52852 08ecbffaf25c
child 53192 04df1d236e1c
permissions -rw-r--r--
more general Output.result: allow to update arbitrary properties;
clarified kind/instance of overlay GUI component, with separate output (subject to version as main Output);
wenzelm@30173
     1
(*  Title:      Pure/System/isabelle_process.ML
wenzelm@25528
     2
    Author:     Makarius
wenzelm@25528
     3
wenzelm@38445
     4
Isabelle process wrapper, based on private fifos for maximum
wenzelm@48712
     5
robustness and performance, or local socket for maximum portability.
wenzelm@25528
     6
*)
wenzelm@25528
     7
wenzelm@25528
     8
signature ISABELLE_PROCESS =
wenzelm@25528
     9
sig
wenzelm@42897
    10
  val is_active: unit -> bool
wenzelm@46119
    11
  val protocol_command: string -> (string list -> unit) -> unit
wenzelm@52655
    12
  val reset_tracing: Document_ID.exec -> unit
wenzelm@43684
    13
  val crashes: exn list Synchronized.var
wenzelm@45029
    14
  val init_fifos: string -> string -> unit
wenzelm@45028
    15
  val init_socket: string -> unit
wenzelm@25528
    16
end;
wenzelm@25528
    17
wenzelm@31797
    18
structure Isabelle_Process: ISABELLE_PROCESS =
wenzelm@25528
    19
struct
wenzelm@25528
    20
wenzelm@42897
    21
(* print mode *)
wenzelm@25554
    22
wenzelm@25748
    23
val isabelle_processN = "isabelle_process";
wenzelm@25748
    24
wenzelm@42897
    25
fun is_active () = Print_Mode.print_mode_active isabelle_processN;
wenzelm@42897
    26
wenzelm@26550
    27
val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
wenzelm@28036
    28
val _ = Markup.add_mode isabelle_processN YXML.output_markup;
wenzelm@25841
    29
wenzelm@25841
    30
wenzelm@46119
    31
(* protocol commands *)
wenzelm@38270
    32
wenzelm@38270
    33
local
wenzelm@38270
    34
wenzelm@43684
    35
val commands =
wenzelm@52800
    36
  Synchronized.var "Isabelle_Process.commands"
wenzelm@52800
    37
    (Symtab.empty: (string list -> unit) Symtab.table);
wenzelm@38270
    38
wenzelm@38270
    39
in
wenzelm@38270
    40
wenzelm@46119
    41
fun protocol_command name cmd =
wenzelm@43684
    42
  Synchronized.change commands (fn cmds =>
wenzelm@38270
    43
   (if not (Symtab.defined cmds name) then ()
wenzelm@52582
    44
    else warning ("Redefining Isabelle protocol command " ^ quote name);
wenzelm@43684
    45
    Symtab.update (name, cmd) cmds));
wenzelm@38270
    46
wenzelm@46119
    47
fun run_command name args =
wenzelm@43684
    48
  (case Symtab.lookup (Synchronized.value commands) name of
wenzelm@52582
    49
    NONE => error ("Undefined Isabelle protocol command " ^ quote name)
wenzelm@46119
    50
  | SOME cmd =>
wenzelm@46119
    51
      (Runtime.debugging cmd args handle exn =>
wenzelm@52582
    52
        error ("Isabelle protocol command failure: " ^ quote name ^ "\n" ^
wenzelm@48056
    53
          ML_Compiler.exn_message exn)));
wenzelm@38270
    54
wenzelm@38270
    55
end;
wenzelm@38270
    56
wenzelm@38270
    57
wenzelm@50505
    58
(* restricted tracing messages *)
wenzelm@49647
    59
wenzelm@52105
    60
val tracing_messages =
wenzelm@52105
    61
  Synchronized.var "tracing_messages" (Inttab.empty: int Inttab.table);
wenzelm@49647
    62
wenzelm@52655
    63
fun reset_tracing exec_id =
wenzelm@52655
    64
  Synchronized.change tracing_messages (Inttab.delete_safe exec_id);
wenzelm@49647
    65
wenzelm@50505
    66
fun update_tracing () =
wenzelm@50911
    67
  (case Position.parse_id (Position.thread_data ()) of
wenzelm@49647
    68
    NONE => ()
wenzelm@52655
    69
  | SOME exec_id =>
wenzelm@50505
    70
      let
wenzelm@52560
    71
        val ok =
wenzelm@52105
    72
          Synchronized.change_result tracing_messages (fn tab =>
wenzelm@50505
    73
            let
wenzelm@52655
    74
              val n = the_default 0 (Inttab.lookup tab exec_id) + 1;
wenzelm@52105
    75
              val ok = n <= Options.default_int "editor_tracing_messages";
wenzelm@52655
    76
            in (ok, Inttab.update (exec_id, n) tab) end);
wenzelm@50505
    77
      in
wenzelm@50505
    78
        if ok then ()
wenzelm@50505
    79
        else
wenzelm@50505
    80
          let
wenzelm@50505
    81
            val (text, promise) = Active.dialog_text ();
wenzelm@50505
    82
            val _ =
wenzelm@50505
    83
              writeln ("Tracing paused.  " ^ text "Stop" ^ ", or continue with next " ^
wenzelm@51044
    84
                text "100" ^ ", " ^ text "1000" ^ ", " ^ text "10000" ^ " messages?")
wenzelm@50505
    85
            val m = Markup.parse_int (Future.join promise)
wenzelm@50505
    86
              handle Fail _ => error "Stopped";
wenzelm@50505
    87
          in
wenzelm@52105
    88
            Synchronized.change tracing_messages
wenzelm@52655
    89
              (Inttab.map_default (exec_id, 0) (fn k => k - m))
wenzelm@50505
    90
          end
wenzelm@50505
    91
      end);
wenzelm@49647
    92
wenzelm@49647
    93
wenzelm@52800
    94
(* output channels *)
wenzelm@28188
    95
wenzelm@52854
    96
val serial_props = Markup.serial_properties o serial;
wenzelm@52854
    97
wenzelm@48712
    98
fun init_channels channel =
wenzelm@28189
    99
  let
wenzelm@40134
   100
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
wenzelm@40134
   101
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
wenzelm@40134
   102
wenzelm@52584
   103
    val msg_channel = Message_Channel.make channel;
wenzelm@52580
   104
wenzelm@52800
   105
    fun message name props body =
wenzelm@52800
   106
      Message_Channel.send msg_channel (Message_Channel.message name props body);
wenzelm@52800
   107
wenzelm@52854
   108
    fun standard_message props name body =
wenzelm@52580
   109
      if body = "" then ()
wenzelm@52580
   110
      else
wenzelm@52800
   111
        message name
wenzelm@52854
   112
          (fold_rev Properties.put props (Position.properties_of (Position.thread_data ()))) body;
wenzelm@28044
   113
  in
wenzelm@52854
   114
    Output.Internal.status_fn := standard_message [] Markup.statusN;
wenzelm@52854
   115
    Output.Internal.report_fn := standard_message [] Markup.reportN;
wenzelm@52854
   116
    Output.Internal.result_fn :=
wenzelm@52854
   117
      (fn props => fn s => standard_message (props @ serial_props ()) Markup.resultN s);
wenzelm@52854
   118
    Output.Internal.writeln_fn := (fn s => standard_message (serial_props ()) Markup.writelnN s);
wenzelm@52852
   119
    Output.Internal.tracing_fn :=
wenzelm@52854
   120
      (fn s => (update_tracing (); standard_message (serial_props ()) Markup.tracingN s));
wenzelm@52854
   121
    Output.Internal.warning_fn := (fn s => standard_message (serial_props ()) Markup.warningN s);
wenzelm@52854
   122
    Output.Internal.error_fn :=
wenzelm@52854
   123
      (fn (i, s) => standard_message (Markup.serial_properties i) Markup.errorN s);
wenzelm@52852
   124
    Output.Internal.protocol_message_fn := message Markup.protocolN;
wenzelm@52852
   125
    Output.Internal.urgent_message_fn := ! Output.Internal.writeln_fn;
wenzelm@52852
   126
    Output.Internal.prompt_fn := ignore;
wenzelm@52800
   127
    message Markup.initN [] (Session.welcome ());
wenzelm@52584
   128
    msg_channel
wenzelm@28044
   129
  end;
wenzelm@25841
   130
wenzelm@25841
   131
wenzelm@39234
   132
(* protocol loop -- uninterruptible *)
wenzelm@38270
   133
wenzelm@43684
   134
val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
wenzelm@38270
   135
wenzelm@38270
   136
local
wenzelm@38270
   137
wenzelm@38270
   138
fun recover crash =
wenzelm@43684
   139
  (Synchronized.change crashes (cons crash);
wenzelm@38270
   140
    warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
wenzelm@38270
   141
wenzelm@45029
   142
fun read_chunk channel len =
wenzelm@38270
   143
  let
wenzelm@38270
   144
    val n =
wenzelm@38270
   145
      (case Int.fromString len of
wenzelm@38270
   146
        SOME n => n
wenzelm@52799
   147
      | NONE => error ("Isabelle process: malformed header " ^ quote len));
wenzelm@45029
   148
    val chunk = System_Channel.inputN channel n;
wenzelm@52799
   149
    val i = size chunk;
wenzelm@38270
   150
  in
wenzelm@52799
   151
    if i <> n then
wenzelm@52799
   152
      error ("Isabelle process: bad chunk (unexpected EOF after " ^
wenzelm@52799
   153
        string_of_int i ^ " of " ^ string_of_int n ^ " bytes)")
wenzelm@52799
   154
    else chunk
wenzelm@38270
   155
  end;
wenzelm@38270
   156
wenzelm@45029
   157
fun read_command channel =
wenzelm@45029
   158
  (case System_Channel.input_line channel of
wenzelm@38270
   159
    NONE => raise Runtime.TERMINATE
wenzelm@45029
   160
  | SOME line => map (read_chunk channel) (space_explode "," line));
wenzelm@38270
   161
wenzelm@52559
   162
fun worker_guest e =
wenzelm@52559
   163
  Future.worker_guest "Isabelle_Process.loop" (Future.new_group NONE) e ();
wenzelm@52559
   164
wenzelm@38270
   165
in
wenzelm@38270
   166
wenzelm@45029
   167
fun loop channel =
wenzelm@38270
   168
  let val continue =
wenzelm@45029
   169
    (case read_command channel of
wenzelm@38270
   170
      [] => (Output.error_msg "Isabelle process: no input"; true)
wenzelm@52559
   171
    | name :: args => (worker_guest (fn () => run_command name args); true))
wenzelm@38270
   172
    handle Runtime.TERMINATE => false
wenzelm@38270
   173
      | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
wenzelm@52770
   174
  in
wenzelm@52770
   175
    if continue then loop channel
wenzelm@52787
   176
    else (Future.shutdown (); Goal.reset_futures (); Execution.reset ())
wenzelm@52770
   177
  end;
wenzelm@38270
   178
wenzelm@38270
   179
end;
wenzelm@38270
   180
wenzelm@38270
   181
wenzelm@25554
   182
(* init *)
wenzelm@25554
   183
wenzelm@50715
   184
val default_modes1 =
wenzelm@50715
   185
  [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN, Graph_Display.active_graphN];
wenzelm@50715
   186
val default_modes2 = [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN];
wenzelm@49566
   187
wenzelm@52578
   188
val init = uninterruptible (fn _ => fn rendezvous =>
wenzelm@38253
   189
  let
wenzelm@46548
   190
    val _ = Output.physical_stderr Symbol.STX;
wenzelm@39528
   191
wenzelm@49661
   192
    val _ = Printer.show_markup_default := true;
wenzelm@39528
   193
    val _ = Context.set_thread_data NONE;
wenzelm@43671
   194
    val _ =
wenzelm@43671
   195
      Unsynchronized.change print_mode
wenzelm@49566
   196
        (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
wenzelm@39528
   197
wenzelm@45029
   198
    val channel = rendezvous ();
wenzelm@52584
   199
    val msg_channel = init_channels channel;
wenzelm@52111
   200
    val _ = Session.init_protocol_handlers ();
wenzelm@52583
   201
    val _ = loop channel;
wenzelm@52584
   202
  in Message_Channel.shutdown msg_channel end);
wenzelm@25528
   203
wenzelm@45029
   204
fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
wenzelm@45029
   205
fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
wenzelm@45028
   206
wenzelm@25528
   207
end;
wenzelm@39528
   208