src/Pure/System/isabelle_process.ML
author wenzelm
Sat Apr 09 16:16:05 2016 +0200 (2016-04-09)
changeset 62930 51ac6bc389e8
parent 62923 3a122e1e352a
child 63806 c54a53ef1873
permissions -rw-r--r--
shared output primitives of physical/virtual Pure;
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@62564
    13
  val init_protocol: string -> unit
wenzelm@62563
    14
  val init_options: unit -> unit
wenzelm@62599
    15
  val init_protocol_options: Options.T -> 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@59056
    51
      (Runtime.exn_trace_system (fn () => cmd args)
wenzelm@59055
    52
        handle _ (*sic!*) => error ("Isabelle protocol command failure: " ^ quote name)));
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@62930
   117
    Private_Output.status_fn := standard_message [] Markup.statusN;
wenzelm@62930
   118
    Private_Output.report_fn := standard_message [] Markup.reportN;
wenzelm@62930
   119
    Private_Output.result_fn :=
wenzelm@52854
   120
      (fn props => fn s => standard_message (props @ serial_props ()) Markup.resultN s);
wenzelm@62930
   121
    Private_Output.writeln_fn := (fn s => standard_message (serial_props ()) Markup.writelnN s);
wenzelm@62930
   122
    Private_Output.state_fn := (fn s => standard_message (serial_props ()) Markup.stateN s);
wenzelm@62930
   123
    Private_Output.information_fn :=
wenzelm@62930
   124
      (fn s => standard_message (serial_props ()) Markup.informationN s);
wenzelm@62930
   125
    Private_Output.tracing_fn :=
wenzelm@52854
   126
      (fn s => (update_tracing (); standard_message (serial_props ()) Markup.tracingN s));
wenzelm@62930
   127
    Private_Output.warning_fn := (fn s => standard_message (serial_props ()) Markup.warningN s);
wenzelm@62930
   128
    Private_Output.legacy_fn := (fn s => standard_message (serial_props ()) Markup.legacyN s);
wenzelm@62930
   129
    Private_Output.error_message_fn :=
wenzelm@52854
   130
      (fn (i, s) => standard_message (Markup.serial_properties i) Markup.errorN s);
wenzelm@62930
   131
    Private_Output.system_message_fn := message Markup.systemN [];
wenzelm@62930
   132
    Private_Output.protocol_message_fn := message Markup.protocolN;
wenzelm@56333
   133
    message Markup.initN [] [Session.welcome ()];
wenzelm@52584
   134
    msg_channel
wenzelm@28044
   135
  end;
wenzelm@25841
   136
wenzelm@25841
   137
wenzelm@39234
   138
(* protocol loop -- uninterruptible *)
wenzelm@38270
   139
wenzelm@43684
   140
val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
wenzelm@38270
   141
wenzelm@38270
   142
local
wenzelm@38270
   143
wenzelm@38270
   144
fun recover crash =
wenzelm@43684
   145
  (Synchronized.change crashes (cons crash);
wenzelm@57975
   146
    Output.physical_stderr
wenzelm@57975
   147
      "Recovered from Isabelle process crash -- see also Isabelle_Process.crashes\n");
wenzelm@38270
   148
wenzelm@45029
   149
fun read_chunk channel len =
wenzelm@38270
   150
  let
wenzelm@38270
   151
    val n =
wenzelm@38270
   152
      (case Int.fromString len of
wenzelm@38270
   153
        SOME n => n
wenzelm@52799
   154
      | NONE => error ("Isabelle process: malformed header " ^ quote len));
wenzelm@45029
   155
    val chunk = System_Channel.inputN channel n;
wenzelm@52799
   156
    val i = size chunk;
wenzelm@38270
   157
  in
wenzelm@52799
   158
    if i <> n then
wenzelm@52799
   159
      error ("Isabelle process: bad chunk (unexpected EOF after " ^
wenzelm@52799
   160
        string_of_int i ^ " of " ^ string_of_int n ^ " bytes)")
wenzelm@52799
   161
    else chunk
wenzelm@38270
   162
  end;
wenzelm@38270
   163
wenzelm@45029
   164
fun read_command channel =
wenzelm@59055
   165
  System_Channel.input_line channel
wenzelm@59055
   166
  |> Option.map (fn line => map (read_chunk channel) (space_explode "," line));
wenzelm@38270
   167
wenzelm@38270
   168
in
wenzelm@38270
   169
wenzelm@45029
   170
fun loop channel =
wenzelm@59055
   171
  let
wenzelm@59055
   172
    val continue =
wenzelm@59055
   173
      (case read_command channel of
wenzelm@59055
   174
        NONE => false
wenzelm@59055
   175
      | SOME [] => (Output.system_message "Isabelle process: no input"; true)
wenzelm@59370
   176
      | SOME (name :: args) => (run_command name args; true))
wenzelm@59055
   177
      handle exn => (Runtime.exn_system_message exn handle crash => recover crash; true);
wenzelm@52770
   178
  in
wenzelm@52770
   179
    if continue then loop channel
wenzelm@53192
   180
    else (Future.shutdown (); Execution.reset (); ())
wenzelm@52770
   181
  end;
wenzelm@38270
   182
wenzelm@38270
   183
end;
wenzelm@38270
   184
wenzelm@38270
   185
wenzelm@62564
   186
(* init protocol *)
wenzelm@25554
   187
wenzelm@59211
   188
val default_modes1 = [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN];
wenzelm@50715
   189
val default_modes2 = [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN];
wenzelm@49566
   190
wenzelm@62923
   191
val init_protocol = Thread_Attributes.uninterruptible (fn _ => fn socket =>
wenzelm@38253
   192
  let
wenzelm@62666
   193
    val _ = SHA1.test_samples ()
wenzelm@62505
   194
      handle exn as Fail msg => (Output.physical_stderr (msg ^ "\n"); Exn.reraise exn);
wenzelm@46548
   195
    val _ = Output.physical_stderr Symbol.STX;
wenzelm@39528
   196
wenzelm@49661
   197
    val _ = Printer.show_markup_default := true;
wenzelm@62889
   198
    val _ = Context.put_generic_context NONE;
wenzelm@43671
   199
    val _ =
wenzelm@43671
   200
      Unsynchronized.change print_mode
wenzelm@49566
   201
        (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
wenzelm@39528
   202
wenzelm@59350
   203
    val channel = System_Channel.rendezvous socket;
wenzelm@52584
   204
    val msg_channel = init_channels channel;
wenzelm@52111
   205
    val _ = Session.init_protocol_handlers ();
wenzelm@58848
   206
    val _ = loop channel;
wenzelm@52584
   207
  in Message_Channel.shutdown msg_channel end);
wenzelm@25528
   208
wenzelm@62563
   209
wenzelm@62563
   210
(* init options *)
wenzelm@62563
   211
wenzelm@62563
   212
fun init_options () =
wenzelm@62878
   213
 (ML_Print_Depth.set_print_depth (Options.default_int "ML_print_depth");
wenzelm@62712
   214
  Future.ML_statistics := Options.default_bool "ML_statistics";
wenzelm@62563
   215
  Multithreading.trace := Options.default_int "threads_trace";
wenzelm@62563
   216
  Multithreading.max_threads_update (Options.default_int "threads");
wenzelm@62563
   217
  Goal.parallel_proofs := Options.default_int "parallel_proofs");
wenzelm@62563
   218
wenzelm@62599
   219
fun init_protocol_options options =
wenzelm@62599
   220
 (Options.set_default options;
wenzelm@62599
   221
  init_options ();
wenzelm@62599
   222
  Goal.parallel_proofs := (if Options.int options "parallel_proofs" > 0 then 3 else 0));
wenzelm@62599
   223
wenzelm@25528
   224
end;