src/Pure/System/isabelle_process.ML
author wenzelm
Sat Apr 07 19:59:09 2012 +0200 (2012-04-07 ago)
changeset 47396 c1d297ef7969
parent 46774 38f113b052b1
child 47410 33f2f968c0a1
permissions -rw-r--r--
enable parallel proofs (cf. e8552cba702d), only affects packages so far;
disable quick_and_dirty to make packages produce proofs -- NB: 'sorry' works via "int" mode of proof commands;
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@38445
     5
robustness and performance.
wenzelm@39528
     6
wenzelm@39528
     7
Startup phases:
wenzelm@39528
     8
  . raw Posix process startup with uncontrolled output on stdout/stderr
wenzelm@46548
     9
  . stderr \002: ML running
wenzelm@39528
    10
  .. stdin/stdout/stderr freely available (raw ML loop)
wenzelm@39528
    11
  .. protocol thread initialization
wenzelm@45029
    12
  ... rendezvous on system channel
wenzelm@45158
    13
  ... message INIT(pid): channels ready
wenzelm@46548
    14
  ... message RAW(keywords)
wenzelm@46548
    15
  ... message RAW(ready): main loop ready
wenzelm@25528
    16
*)
wenzelm@25528
    17
wenzelm@25528
    18
signature ISABELLE_PROCESS =
wenzelm@25528
    19
sig
wenzelm@42897
    20
  val is_active: unit -> bool
wenzelm@46119
    21
  val protocol_command: string -> (string list -> unit) -> unit
wenzelm@43684
    22
  val crashes: exn list Synchronized.var
wenzelm@45029
    23
  val init_fifos: string -> string -> unit
wenzelm@45028
    24
  val init_socket: string -> unit
wenzelm@25528
    25
end;
wenzelm@25528
    26
wenzelm@31797
    27
structure Isabelle_Process: ISABELLE_PROCESS =
wenzelm@25528
    28
struct
wenzelm@25528
    29
wenzelm@42897
    30
(* print mode *)
wenzelm@25554
    31
wenzelm@25748
    32
val isabelle_processN = "isabelle_process";
wenzelm@25748
    33
wenzelm@42897
    34
fun is_active () = Print_Mode.print_mode_active isabelle_processN;
wenzelm@42897
    35
wenzelm@26550
    36
val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
wenzelm@28036
    37
val _ = Markup.add_mode isabelle_processN YXML.output_markup;
wenzelm@25841
    38
wenzelm@25841
    39
wenzelm@46119
    40
(* protocol commands *)
wenzelm@38270
    41
wenzelm@38270
    42
local
wenzelm@38270
    43
wenzelm@43684
    44
val commands =
wenzelm@43684
    45
  Synchronized.var "Isabelle_Process.commands" (Symtab.empty: (string list -> unit) Symtab.table);
wenzelm@38270
    46
wenzelm@38270
    47
in
wenzelm@38270
    48
wenzelm@46119
    49
fun protocol_command name cmd =
wenzelm@43684
    50
  Synchronized.change commands (fn cmds =>
wenzelm@38270
    51
   (if not (Symtab.defined cmds name) then ()
wenzelm@38270
    52
    else warning ("Redefining Isabelle process command " ^ quote name);
wenzelm@43684
    53
    Symtab.update (name, cmd) cmds));
wenzelm@38270
    54
wenzelm@46119
    55
fun run_command name args =
wenzelm@43684
    56
  (case Symtab.lookup (Synchronized.value commands) name of
wenzelm@38270
    57
    NONE => error ("Undefined Isabelle process command " ^ quote name)
wenzelm@46119
    58
  | SOME cmd =>
wenzelm@46119
    59
      (Runtime.debugging cmd args handle exn =>
wenzelm@46119
    60
        error ("Isabelle process protocol failure: " ^ name ^ "\n" ^ ML_Compiler.exn_message exn)));
wenzelm@38270
    61
wenzelm@38270
    62
end;
wenzelm@38270
    63
wenzelm@38270
    64
wenzelm@40134
    65
(* message channels *)
wenzelm@25841
    66
wenzelm@25841
    67
local
wenzelm@25810
    68
wenzelm@40134
    69
fun chunk s = [string_of_int (size s), "\n", s];
wenzelm@26574
    70
wenzelm@44731
    71
fun message do_flush mbox ch raw_props body =
wenzelm@43771
    72
  let
wenzelm@43772
    73
    val robust_props = map (pairself YXML.embed_controls) raw_props;
wenzelm@43771
    74
    val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
wenzelm@44731
    75
  in Mailbox.send mbox (chunk header @ chunk body, do_flush) end;
wenzelm@25554
    76
wenzelm@44270
    77
fun standard_message mbox opt_serial ch body =
wenzelm@43771
    78
  if body = "" then ()
wenzelm@43771
    79
  else
wenzelm@44731
    80
    message false mbox ch
wenzelm@45666
    81
      ((case opt_serial of SOME i => cons (Isabelle_Markup.serialN, string_of_int i) | _ => I)
wenzelm@43771
    82
        (Position.properties_of (Position.thread_data ()))) body;
wenzelm@25554
    83
wenzelm@45029
    84
fun message_output mbox channel =
wenzelm@28188
    85
  let
wenzelm@45029
    86
    fun flush () = ignore (try System_Channel.flush channel);
wenzelm@40134
    87
    fun loop receive =
wenzelm@40134
    88
      (case receive mbox of
wenzelm@44731
    89
        SOME (msg, do_flush) =>
wenzelm@45029
    90
         (List.app (fn s => System_Channel.output channel s) msg;
wenzelm@44731
    91
          if do_flush then flush () else ();
wenzelm@40301
    92
          loop (Mailbox.receive_timeout (seconds 0.02)))
wenzelm@44731
    93
      | NONE => (flush (); loop (SOME o Mailbox.receive)));
wenzelm@40134
    94
  in fn () => loop (SOME o Mailbox.receive) end;
wenzelm@28188
    95
wenzelm@28188
    96
in
wenzelm@28188
    97
wenzelm@45029
    98
fun setup_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@44731
   103
    val mbox = Mailbox.create () : (string list * bool) Mailbox.T;
wenzelm@45029
   104
    val _ = Simple_Thread.fork false (message_output mbox channel);
wenzelm@28044
   105
  in
wenzelm@44270
   106
    Output.Private_Hooks.status_fn := standard_message mbox NONE "B";
wenzelm@44270
   107
    Output.Private_Hooks.report_fn := standard_message mbox NONE "C";
wenzelm@44270
   108
    Output.Private_Hooks.writeln_fn := (fn s => standard_message mbox (SOME (serial ())) "D" s);
wenzelm@44270
   109
    Output.Private_Hooks.tracing_fn := (fn s => standard_message mbox (SOME (serial ())) "E" s);
wenzelm@44270
   110
    Output.Private_Hooks.warning_fn := (fn s => standard_message mbox (SOME (serial ())) "F" s);
wenzelm@44270
   111
    Output.Private_Hooks.error_fn := (fn (i, s) => standard_message mbox (SOME i) "G" s);
wenzelm@46774
   112
    Output.Private_Hooks.protocol_message_fn := message true mbox "H";
wenzelm@40133
   113
    Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
wenzelm@40133
   114
    Output.Private_Hooks.prompt_fn := ignore;
wenzelm@45028
   115
    message true mbox "A" [] (Session.welcome ())
wenzelm@28044
   116
  end;
wenzelm@25841
   117
wenzelm@28188
   118
end;
wenzelm@28188
   119
wenzelm@25841
   120
wenzelm@39234
   121
(* protocol loop -- uninterruptible *)
wenzelm@38270
   122
wenzelm@43684
   123
val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
wenzelm@38270
   124
wenzelm@38270
   125
local
wenzelm@38270
   126
wenzelm@38270
   127
fun recover crash =
wenzelm@43684
   128
  (Synchronized.change crashes (cons crash);
wenzelm@38270
   129
    warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
wenzelm@38270
   130
wenzelm@45029
   131
fun read_chunk channel len =
wenzelm@38270
   132
  let
wenzelm@38270
   133
    val n =
wenzelm@38270
   134
      (case Int.fromString len of
wenzelm@38270
   135
        SOME n => n
wenzelm@38270
   136
      | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
wenzelm@45029
   137
    val chunk = System_Channel.inputN channel n;
wenzelm@38270
   138
    val m = size chunk;
wenzelm@38270
   139
  in
wenzelm@38270
   140
    if m = n then chunk
wenzelm@38270
   141
    else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
wenzelm@38270
   142
  end;
wenzelm@38270
   143
wenzelm@45029
   144
fun read_command channel =
wenzelm@45029
   145
  (case System_Channel.input_line channel of
wenzelm@38270
   146
    NONE => raise Runtime.TERMINATE
wenzelm@45029
   147
  | SOME line => map (read_chunk channel) (space_explode "," line));
wenzelm@38270
   148
wenzelm@38270
   149
in
wenzelm@38270
   150
wenzelm@45029
   151
fun loop channel =
wenzelm@38270
   152
  let val continue =
wenzelm@45029
   153
    (case read_command channel of
wenzelm@38270
   154
      [] => (Output.error_msg "Isabelle process: no input"; true)
wenzelm@38270
   155
    | name :: args => (run_command name args; true))
wenzelm@38270
   156
    handle Runtime.TERMINATE => false
wenzelm@38270
   157
      | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
wenzelm@45029
   158
  in if continue then loop channel else () end;
wenzelm@38270
   159
wenzelm@38270
   160
end;
wenzelm@38270
   161
wenzelm@38270
   162
wenzelm@25554
   163
(* init *)
wenzelm@25554
   164
wenzelm@45029
   165
fun init rendezvous = ignore (Simple_Thread.fork false (fn () =>
wenzelm@38253
   166
  let
wenzelm@40301
   167
    val _ = OS.Process.sleep (seconds 0.5);  (*yield to raw ML toplevel*)
wenzelm@46548
   168
    val _ = Output.physical_stderr Symbol.STX;
wenzelm@39528
   169
wenzelm@47396
   170
    val _ = quick_and_dirty := false;
wenzelm@47396
   171
    val _ = Goal.parallel_proofs := 1;
wenzelm@44988
   172
    val _ =
wenzelm@44988
   173
      if Multithreading.max_threads_value () < 2
wenzelm@44988
   174
      then Multithreading.max_threads := 2 else ();
wenzelm@39528
   175
    val _ = Context.set_thread_data NONE;
wenzelm@43671
   176
    val _ =
wenzelm@43671
   177
      Unsynchronized.change print_mode
wenzelm@45057
   178
        (fn mode =>
wenzelm@45057
   179
          (mode @ [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN])
wenzelm@45057
   180
          |> fold (update op =)
wenzelm@45057
   181
            [Symbol.xsymbolsN, isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
wenzelm@39528
   182
wenzelm@45029
   183
    val channel = rendezvous ();
wenzelm@45029
   184
    val _ = setup_channels channel;
wenzelm@45028
   185
wenzelm@38253
   186
    val _ = Keyword.status ();
wenzelm@43673
   187
    val _ = Thy_Info.status ();
wenzelm@46774
   188
    val _ = Output.protocol_message Isabelle_Markup.ready "";
wenzelm@45029
   189
  in loop channel end));
wenzelm@25528
   190
wenzelm@45029
   191
fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
wenzelm@45029
   192
fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
wenzelm@45028
   193
wenzelm@25528
   194
end;
wenzelm@39528
   195