src/Pure/System/isabelle_process.ML
author wenzelm
Wed Jul 10 22:04:57 2013 +0200 (2013-07-10 ago)
changeset 52582 31467a4b1466
parent 52580 36aa39694ab4
child 52583 0a7240d88e09
permissions -rw-r--r--
tuned signature;
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@39528
     6
wenzelm@39528
     7
Startup phases:
wenzelm@48712
     8
  - raw Posix process startup with uncontrolled output on stdout/stderr
wenzelm@48712
     9
  - stderr \002: ML running
wenzelm@48712
    10
  - stdin/stdout/stderr freely available (raw ML loop)
wenzelm@48712
    11
  - protocol thread initialization
wenzelm@48712
    12
  - rendezvous on system channel
wenzelm@48712
    13
  - message INIT: channels ready
wenzelm@25528
    14
*)
wenzelm@25528
    15
wenzelm@25528
    16
signature ISABELLE_PROCESS =
wenzelm@25528
    17
sig
wenzelm@42897
    18
  val is_active: unit -> bool
wenzelm@46119
    19
  val protocol_command: string -> (string list -> unit) -> unit
wenzelm@50505
    20
  val reset_tracing: unit -> unit
wenzelm@43684
    21
  val crashes: exn list Synchronized.var
wenzelm@45029
    22
  val init_fifos: string -> string -> unit
wenzelm@45028
    23
  val init_socket: string -> unit
wenzelm@25528
    24
end;
wenzelm@25528
    25
wenzelm@31797
    26
structure Isabelle_Process: ISABELLE_PROCESS =
wenzelm@25528
    27
struct
wenzelm@25528
    28
wenzelm@42897
    29
(* print mode *)
wenzelm@25554
    30
wenzelm@25748
    31
val isabelle_processN = "isabelle_process";
wenzelm@25748
    32
wenzelm@42897
    33
fun is_active () = Print_Mode.print_mode_active isabelle_processN;
wenzelm@42897
    34
wenzelm@26550
    35
val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
wenzelm@28036
    36
val _ = Markup.add_mode isabelle_processN YXML.output_markup;
wenzelm@25841
    37
wenzelm@25841
    38
wenzelm@46119
    39
(* protocol commands *)
wenzelm@38270
    40
wenzelm@38270
    41
local
wenzelm@38270
    42
wenzelm@43684
    43
val commands =
wenzelm@43684
    44
  Synchronized.var "Isabelle_Process.commands" (Symtab.empty: (string list -> unit) Symtab.table);
wenzelm@38270
    45
wenzelm@38270
    46
in
wenzelm@38270
    47
wenzelm@46119
    48
fun protocol_command name cmd =
wenzelm@43684
    49
  Synchronized.change commands (fn cmds =>
wenzelm@38270
    50
   (if not (Symtab.defined cmds name) then ()
wenzelm@52582
    51
    else warning ("Redefining Isabelle protocol command " ^ quote name);
wenzelm@43684
    52
    Symtab.update (name, cmd) cmds));
wenzelm@38270
    53
wenzelm@46119
    54
fun run_command name args =
wenzelm@43684
    55
  (case Symtab.lookup (Synchronized.value commands) name of
wenzelm@52582
    56
    NONE => error ("Undefined Isabelle protocol command " ^ quote name)
wenzelm@46119
    57
  | SOME cmd =>
wenzelm@46119
    58
      (Runtime.debugging cmd args handle exn =>
wenzelm@52582
    59
        error ("Isabelle protocol command failure: " ^ quote name ^ "\n" ^
wenzelm@48056
    60
          ML_Compiler.exn_message exn)));
wenzelm@38270
    61
wenzelm@38270
    62
end;
wenzelm@38270
    63
wenzelm@38270
    64
wenzelm@50505
    65
(* restricted tracing messages *)
wenzelm@49647
    66
wenzelm@52105
    67
val tracing_messages =
wenzelm@52105
    68
  Synchronized.var "tracing_messages" (Inttab.empty: int Inttab.table);
wenzelm@49647
    69
wenzelm@50505
    70
fun reset_tracing () =
wenzelm@52105
    71
  Synchronized.change tracing_messages (K Inttab.empty);
wenzelm@49647
    72
wenzelm@50505
    73
fun update_tracing () =
wenzelm@50911
    74
  (case Position.parse_id (Position.thread_data ()) of
wenzelm@49647
    75
    NONE => ()
wenzelm@49647
    76
  | SOME id =>
wenzelm@50505
    77
      let
wenzelm@52560
    78
        val ok =
wenzelm@52105
    79
          Synchronized.change_result tracing_messages (fn tab =>
wenzelm@50505
    80
            let
wenzelm@50911
    81
              val n = the_default 0 (Inttab.lookup tab id) + 1;
wenzelm@52105
    82
              val ok = n <= Options.default_int "editor_tracing_messages";
wenzelm@52560
    83
            in (ok, Inttab.update (id, n) tab) end);
wenzelm@50505
    84
      in
wenzelm@50505
    85
        if ok then ()
wenzelm@50505
    86
        else
wenzelm@50505
    87
          let
wenzelm@50505
    88
            val (text, promise) = Active.dialog_text ();
wenzelm@50505
    89
            val _ =
wenzelm@50505
    90
              writeln ("Tracing paused.  " ^ text "Stop" ^ ", or continue with next " ^
wenzelm@51044
    91
                text "100" ^ ", " ^ text "1000" ^ ", " ^ text "10000" ^ " messages?")
wenzelm@50505
    92
            val m = Markup.parse_int (Future.join promise)
wenzelm@50505
    93
              handle Fail _ => error "Stopped";
wenzelm@50505
    94
          in
wenzelm@52105
    95
            Synchronized.change tracing_messages
wenzelm@50911
    96
              (Inttab.map_default (id, 0) (fn k => k - m))
wenzelm@50505
    97
          end
wenzelm@50505
    98
      end);
wenzelm@49647
    99
wenzelm@49647
   100
wenzelm@40134
   101
(* message channels *)
wenzelm@25841
   102
wenzelm@25841
   103
local
wenzelm@25810
   104
wenzelm@40134
   105
fun chunk s = [string_of_int (size s), "\n", s];
wenzelm@52580
   106
fun flush channel = ignore (try System_Channel.flush channel);
wenzelm@26574
   107
wenzelm@52580
   108
datatype target =
wenzelm@52580
   109
  Channel of System_Channel.T |
wenzelm@52580
   110
  Mailbox of (string list * bool) Mailbox.T;
wenzelm@52580
   111
wenzelm@52580
   112
fun message do_flush target name raw_props body =
wenzelm@43771
   113
  let
wenzelm@43772
   114
    val robust_props = map (pairself YXML.embed_controls) raw_props;
wenzelm@49677
   115
    val header = YXML.string_of (XML.Elem ((name, robust_props), []));
wenzelm@52580
   116
    val msg = chunk header @ chunk body;
wenzelm@52580
   117
  in
wenzelm@52580
   118
    (case target of
wenzelm@52580
   119
      Channel channel => (List.app (fn s => System_Channel.output channel s) msg; flush channel)
wenzelm@52580
   120
    | Mailbox mbox => Mailbox.send mbox (msg, do_flush))
wenzelm@52580
   121
  end;
wenzelm@25554
   122
wenzelm@45029
   123
fun message_output mbox channel =
wenzelm@28188
   124
  let
wenzelm@40134
   125
    fun loop receive =
wenzelm@40134
   126
      (case receive mbox of
wenzelm@44731
   127
        SOME (msg, do_flush) =>
wenzelm@45029
   128
         (List.app (fn s => System_Channel.output channel s) msg;
wenzelm@52580
   129
          if do_flush then flush channel else ();
wenzelm@40301
   130
          loop (Mailbox.receive_timeout (seconds 0.02)))
wenzelm@52580
   131
      | NONE => (flush channel; loop (SOME o Mailbox.receive)));
wenzelm@40134
   132
  in fn () => loop (SOME o Mailbox.receive) end;
wenzelm@28188
   133
wenzelm@28188
   134
in
wenzelm@28188
   135
wenzelm@48712
   136
fun init_channels channel =
wenzelm@28189
   137
  let
wenzelm@40134
   138
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
wenzelm@40134
   139
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
wenzelm@40134
   140
wenzelm@52580
   141
    val target =
wenzelm@52580
   142
      if Multithreading.available then
wenzelm@52580
   143
        let
wenzelm@52580
   144
          val mbox = Mailbox.create ();
wenzelm@52580
   145
          val _ = Simple_Thread.fork false (message_output mbox channel);
wenzelm@52580
   146
        in Mailbox mbox end
wenzelm@52580
   147
      else Channel channel;
wenzelm@52580
   148
wenzelm@52580
   149
    fun standard_message opt_serial name body =
wenzelm@52580
   150
      if body = "" then ()
wenzelm@52580
   151
      else
wenzelm@52580
   152
        message false target name
wenzelm@52580
   153
          ((case opt_serial of SOME i => cons (Markup.serialN, Markup.print_int i) | _ => I)
wenzelm@52580
   154
            (Position.properties_of (Position.thread_data ()))) body;
wenzelm@28044
   155
  in
wenzelm@52580
   156
    Output.Private_Hooks.status_fn := standard_message NONE Markup.statusN;
wenzelm@52580
   157
    Output.Private_Hooks.report_fn := standard_message NONE Markup.reportN;
wenzelm@52580
   158
    Output.Private_Hooks.result_fn := (fn (i, s) => standard_message (SOME i) Markup.resultN s);
wenzelm@49677
   159
    Output.Private_Hooks.writeln_fn :=
wenzelm@52580
   160
      (fn s => standard_message (SOME (serial ())) Markup.writelnN s);
wenzelm@49647
   161
    Output.Private_Hooks.tracing_fn :=
wenzelm@52580
   162
      (fn s => (update_tracing (); standard_message (SOME (serial ())) Markup.tracingN s));
wenzelm@49677
   163
    Output.Private_Hooks.warning_fn :=
wenzelm@52580
   164
      (fn s => standard_message (SOME (serial ())) Markup.warningN s);
wenzelm@49677
   165
    Output.Private_Hooks.error_fn :=
wenzelm@52580
   166
      (fn (i, s) => standard_message (SOME i) Markup.errorN s);
wenzelm@52580
   167
    Output.Private_Hooks.protocol_message_fn := message true target Markup.protocolN;
wenzelm@40133
   168
    Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
wenzelm@40133
   169
    Output.Private_Hooks.prompt_fn := ignore;
wenzelm@52580
   170
    message true target Markup.initN [] (Session.welcome ())
wenzelm@28044
   171
  end;
wenzelm@25841
   172
wenzelm@28188
   173
end;
wenzelm@28188
   174
wenzelm@25841
   175
wenzelm@39234
   176
(* protocol loop -- uninterruptible *)
wenzelm@38270
   177
wenzelm@43684
   178
val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
wenzelm@38270
   179
wenzelm@38270
   180
local
wenzelm@38270
   181
wenzelm@38270
   182
fun recover crash =
wenzelm@43684
   183
  (Synchronized.change crashes (cons crash);
wenzelm@38270
   184
    warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
wenzelm@38270
   185
wenzelm@45029
   186
fun read_chunk channel len =
wenzelm@38270
   187
  let
wenzelm@38270
   188
    val n =
wenzelm@38270
   189
      (case Int.fromString len of
wenzelm@38270
   190
        SOME n => n
wenzelm@38270
   191
      | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
wenzelm@45029
   192
    val chunk = System_Channel.inputN channel n;
wenzelm@38270
   193
    val m = size chunk;
wenzelm@38270
   194
  in
wenzelm@38270
   195
    if m = n then chunk
wenzelm@38270
   196
    else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
wenzelm@38270
   197
  end;
wenzelm@38270
   198
wenzelm@45029
   199
fun read_command channel =
wenzelm@45029
   200
  (case System_Channel.input_line channel of
wenzelm@38270
   201
    NONE => raise Runtime.TERMINATE
wenzelm@45029
   202
  | SOME line => map (read_chunk channel) (space_explode "," line));
wenzelm@38270
   203
wenzelm@52559
   204
fun worker_guest e =
wenzelm@52559
   205
  Future.worker_guest "Isabelle_Process.loop" (Future.new_group NONE) e ();
wenzelm@52559
   206
wenzelm@38270
   207
in
wenzelm@38270
   208
wenzelm@45029
   209
fun loop channel =
wenzelm@38270
   210
  let val continue =
wenzelm@45029
   211
    (case read_command channel of
wenzelm@38270
   212
      [] => (Output.error_msg "Isabelle process: no input"; true)
wenzelm@52559
   213
    | name :: args => (worker_guest (fn () => run_command name args); true))
wenzelm@38270
   214
    handle Runtime.TERMINATE => false
wenzelm@38270
   215
      | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
wenzelm@45029
   216
  in if continue then loop channel else () end;
wenzelm@38270
   217
wenzelm@38270
   218
end;
wenzelm@38270
   219
wenzelm@38270
   220
wenzelm@25554
   221
(* init *)
wenzelm@25554
   222
wenzelm@50715
   223
val default_modes1 =
wenzelm@50715
   224
  [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN, Graph_Display.active_graphN];
wenzelm@50715
   225
val default_modes2 = [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN];
wenzelm@49566
   226
wenzelm@52578
   227
val init = uninterruptible (fn _ => fn rendezvous =>
wenzelm@38253
   228
  let
wenzelm@46548
   229
    val _ = Output.physical_stderr Symbol.STX;
wenzelm@39528
   230
wenzelm@49661
   231
    val _ = Printer.show_markup_default := true;
wenzelm@39528
   232
    val _ = Context.set_thread_data NONE;
wenzelm@43671
   233
    val _ =
wenzelm@43671
   234
      Unsynchronized.change print_mode
wenzelm@49566
   235
        (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
wenzelm@39528
   236
wenzelm@45029
   237
    val channel = rendezvous ();
wenzelm@48712
   238
    val _ = init_channels channel;
wenzelm@52111
   239
    val _ = Session.init_protocol_handlers ();
wenzelm@52578
   240
  in loop channel end);
wenzelm@25528
   241
wenzelm@45029
   242
fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
wenzelm@45029
   243
fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
wenzelm@45028
   244
wenzelm@50117
   245
wenzelm@50117
   246
(* options *)
wenzelm@50117
   247
wenzelm@50117
   248
val _ =
wenzelm@50117
   249
  protocol_command "Isabelle_Process.options"
wenzelm@50117
   250
    (fn [options_yxml] =>
wenzelm@50117
   251
      let val options = Options.decode (YXML.parse_body options_yxml) in
wenzelm@51941
   252
        Options.set_default options;
wenzelm@50698
   253
        Future.ML_statistics := true;
wenzelm@50117
   254
        Multithreading.trace := Options.int options "threads_trace";
wenzelm@50117
   255
        Multithreading.max_threads := Options.int options "threads";
wenzelm@51554
   256
        Goal.skip_proofs := Options.bool options "editor_skip_proofs";
wenzelm@52105
   257
        Goal.parallel_proofs := (if Options.int options "parallel_proofs" > 0 then 3 else 0)
wenzelm@50117
   258
      end);
wenzelm@50117
   259
wenzelm@25528
   260
end;
wenzelm@39528
   261