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