src/Pure/System/isabelle_process.ML
author wenzelm
Wed Sep 21 22:18:17 2011 +0200 (2011-09-21)
changeset 45028 d608dd8cd409
parent 44988 33aa6da101d8
child 45029 63144ea111f7
permissions -rw-r--r--
alternative Socket_Channel;
use BinIO for fifos uniformly;
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@39528
    12
  ... switch to in_fifo/out_fifo channels (rendezvous via open)
wenzelm@39528
    13
  ... out_fifo INIT(pid): channels ready
wenzelm@39528
    14
  ... out_fifo STATUS(keywords)
wenzelm@39528
    15
  ... out_fifo 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@45028
    24
  val init_socket: string -> unit
wenzelm@45028
    25
  val init_fifos: string -> 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@40134
    83
fun message_output mbox out_stream =
wenzelm@28188
    84
  let
wenzelm@45028
    85
    fun flush () = ignore (try BinIO.flushOut out_stream);
wenzelm@40134
    86
    fun loop receive =
wenzelm@40134
    87
      (case receive mbox of
wenzelm@44731
    88
        SOME (msg, do_flush) =>
wenzelm@45028
    89
         (List.app (fn s => BinIO.output (out_stream, Byte.stringToBytes 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@45028
    97
fun setup_channels out_stream =
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@40134
   103
    val _ = Simple_Thread.fork false (message_output mbox out_stream);
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@45028
   130
fun read_line stream =
wenzelm@45028
   131
  let
wenzelm@45028
   132
    val content = String.implode o rev;
wenzelm@45028
   133
    fun read cs =
wenzelm@45028
   134
      (case BinIO.input1 stream of
wenzelm@45028
   135
        NONE => (content cs, null cs)
wenzelm@45028
   136
      | SOME b =>
wenzelm@45028
   137
          (case Byte.byteToChar b of
wenzelm@45028
   138
            #"\n" => (content cs, false)
wenzelm@45028
   139
          | c => read (c :: cs)));
wenzelm@45028
   140
  in case read [] of ("", true) => NONE | (s, _) => SOME s end;
wenzelm@45028
   141
wenzelm@38270
   142
fun read_chunk stream len =
wenzelm@38270
   143
  let
wenzelm@38270
   144
    val n =
wenzelm@38270
   145
      (case Int.fromString len of
wenzelm@38270
   146
        SOME n => n
wenzelm@38270
   147
      | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
wenzelm@45028
   148
    val chunk = Byte.bytesToString (BinIO.inputN (stream, n));
wenzelm@38270
   149
    val m = size chunk;
wenzelm@38270
   150
  in
wenzelm@38270
   151
    if m = n then chunk
wenzelm@38270
   152
    else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
wenzelm@38270
   153
  end;
wenzelm@38270
   154
wenzelm@38270
   155
fun read_command stream =
wenzelm@45028
   156
  (case read_line stream of
wenzelm@38270
   157
    NONE => raise Runtime.TERMINATE
wenzelm@38270
   158
  | SOME line => map (read_chunk stream) (space_explode "," line));
wenzelm@38270
   159
wenzelm@38270
   160
fun run_command name args =
wenzelm@38270
   161
  Runtime.debugging (command name) args
wenzelm@38270
   162
    handle exn =>
wenzelm@38270
   163
      error ("Isabelle process command failure: " ^ name ^ "\n" ^ ML_Compiler.exn_message exn);
wenzelm@38270
   164
wenzelm@38270
   165
in
wenzelm@38270
   166
wenzelm@38270
   167
fun loop stream =
wenzelm@38270
   168
  let val continue =
wenzelm@38270
   169
    (case read_command stream of
wenzelm@38270
   170
      [] => (Output.error_msg "Isabelle process: no input"; true)
wenzelm@38270
   171
    | name :: args => (run_command name args; true))
wenzelm@38270
   172
    handle Runtime.TERMINATE => false
wenzelm@38270
   173
      | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
wenzelm@38270
   174
  in if continue then loop stream else () end;
wenzelm@38270
   175
wenzelm@38270
   176
end;
wenzelm@38270
   177
wenzelm@38270
   178
wenzelm@25554
   179
(* init *)
wenzelm@25554
   180
wenzelm@45028
   181
fun init make_streams = ignore (Simple_Thread.fork false (fn () =>
wenzelm@38253
   182
  let
wenzelm@40301
   183
    val _ = OS.Process.sleep (seconds 0.5);  (*yield to raw ML toplevel*)
wenzelm@44389
   184
    val _ = Output.physical_stdout Symbol.STX;
wenzelm@39528
   185
wenzelm@40521
   186
    val _ = quick_and_dirty := true;
wenzelm@40518
   187
    val _ = Goal.parallel_proofs := 0;
wenzelm@44988
   188
    val _ =
wenzelm@44988
   189
      if Multithreading.max_threads_value () < 2
wenzelm@44988
   190
      then Multithreading.max_threads := 2 else ();
wenzelm@39528
   191
    val _ = Context.set_thread_data NONE;
wenzelm@43671
   192
    val _ =
wenzelm@43671
   193
      Unsynchronized.change print_mode
wenzelm@43671
   194
        (fold (update op =)
wenzelm@43671
   195
          [Symbol.xsymbolsN, isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
wenzelm@39528
   196
wenzelm@45028
   197
    val (in_stream, out_stream) = make_streams ();
wenzelm@45028
   198
    val _ = setup_channels out_stream;
wenzelm@45028
   199
wenzelm@38253
   200
    val _ = Keyword.status ();
wenzelm@43673
   201
    val _ = Thy_Info.status ();
wenzelm@39626
   202
    val _ = Output.status (Markup.markup Markup.ready "process ready");
wenzelm@39528
   203
  in loop in_stream end));
wenzelm@25528
   204
wenzelm@45028
   205
fun rendezvous fifo1 fifo2 = (BinIO.openIn fifo1, BinIO.openOut fifo2);
wenzelm@45028
   206
fun init_fifos fifo1 fifo2 = init (fn () => rendezvous fifo1 fifo2);
wenzelm@45028
   207
wenzelm@45028
   208
fun init_socket socket_name = init (fn () => Socket_IO.open_streams socket_name);
wenzelm@45028
   209
wenzelm@25528
   210
end;
wenzelm@39528
   211