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