src/Pure/System/isabelle_process.ML
author wenzelm
Mon Aug 30 11:35:17 2010 +0200 (2010-08-30 ago)
changeset 38871 28496da3bec2
parent 38445 ba9ea6b9b75c
child 39234 d76a2fd129b5
permissions -rw-r--r--
message serial number indicates physical order;
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@25528
     6
*)
wenzelm@25528
     7
wenzelm@25528
     8
signature ISABELLE_PROCESS =
wenzelm@25528
     9
sig
wenzelm@26574
    10
  val isabelle_processN: string
wenzelm@38270
    11
  val add_command: string -> (string list -> unit) -> unit
wenzelm@38270
    12
  val command: string -> string list -> unit
wenzelm@38270
    13
  val crashes: exn list Unsynchronized.ref
wenzelm@38253
    14
  val init: string -> string -> unit
wenzelm@25528
    15
end;
wenzelm@25528
    16
wenzelm@31797
    17
structure Isabelle_Process: ISABELLE_PROCESS =
wenzelm@25528
    18
struct
wenzelm@25528
    19
wenzelm@26550
    20
(* print modes *)
wenzelm@25554
    21
wenzelm@25748
    22
val isabelle_processN = "isabelle_process";
wenzelm@25748
    23
wenzelm@26550
    24
val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
wenzelm@28036
    25
val _ = Markup.add_mode isabelle_processN YXML.output_markup;
wenzelm@25841
    26
wenzelm@25841
    27
wenzelm@38270
    28
(* commands *)
wenzelm@38270
    29
wenzelm@38270
    30
local
wenzelm@38270
    31
wenzelm@38270
    32
val global_commands = Unsynchronized.ref (Symtab.empty: (string list -> unit) Symtab.table);
wenzelm@38270
    33
wenzelm@38270
    34
in
wenzelm@38270
    35
wenzelm@38270
    36
fun add_command name cmd = CRITICAL (fn () =>
wenzelm@38270
    37
  Unsynchronized.change global_commands (fn cmds =>
wenzelm@38270
    38
   (if not (Symtab.defined cmds name) then ()
wenzelm@38270
    39
    else warning ("Redefining Isabelle process command " ^ quote name);
wenzelm@38270
    40
    Symtab.update (name, cmd) cmds)));
wenzelm@38270
    41
wenzelm@38270
    42
fun command name args =
wenzelm@38270
    43
  (case Symtab.lookup (! global_commands) name of
wenzelm@38270
    44
    NONE => error ("Undefined Isabelle process command " ^ quote name)
wenzelm@38270
    45
  | SOME cmd => cmd args);
wenzelm@38270
    46
wenzelm@38270
    47
end;
wenzelm@38270
    48
wenzelm@38270
    49
wenzelm@25841
    50
(* message markup *)
wenzelm@25841
    51
wenzelm@25841
    52
local
wenzelm@25810
    53
wenzelm@34096
    54
fun chunk s = string_of_int (size s) ^ "\n" ^ s;
wenzelm@26574
    55
wenzelm@34096
    56
fun message _ _ _ "" = ()
wenzelm@38445
    57
  | message out_stream ch raw_props body =
wenzelm@34096
    58
      let
wenzelm@38445
    59
        val robust_props = map (pairself YXML.escape_controls) raw_props;
wenzelm@38445
    60
        val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
wenzelm@38445
    61
      in TextIO.output (out_stream, chunk header ^ chunk body) (*atomic output!*) end;
wenzelm@25554
    62
wenzelm@25554
    63
in
wenzelm@25554
    64
wenzelm@34096
    65
fun standard_message out_stream ch body =
wenzelm@38871
    66
  message out_stream ch
wenzelm@38871
    67
    ((Markup.serialN, serial_string ()) :: Position.properties_of (Position.thread_data ())) body;
wenzelm@25554
    68
wenzelm@28044
    69
fun init_message out_stream =
wenzelm@34214
    70
  message out_stream "A" [(Markup.pidN, process_id ())] (Session.welcome ());
wenzelm@25748
    71
wenzelm@25554
    72
end;
wenzelm@25554
    73
wenzelm@25554
    74
wenzelm@25841
    75
(* channels *)
wenzelm@25841
    76
wenzelm@28188
    77
local
wenzelm@28188
    78
wenzelm@28188
    79
fun auto_flush stream =
wenzelm@28188
    80
  let
wenzelm@28189
    81
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream stream, IO.BLOCK_BUF);
wenzelm@28188
    82
    fun loop () =
wenzelm@38256
    83
      (OS.Process.sleep (Time.fromMilliseconds 20); try TextIO.flushOut stream; loop ());
wenzelm@28188
    84
  in loop end;
wenzelm@28188
    85
wenzelm@38253
    86
fun rendezvous f fifo =
wenzelm@38253
    87
  let
wenzelm@38253
    88
    val path = File.platform_path (Path.explode fifo);
wenzelm@38254
    89
    val result = f fifo;  (*should block until peer is ready*)
wenzelm@38254
    90
    val _ =
wenzelm@38254
    91
      if String.isSuffix "cygwin" ml_platform then ()  (*Cygwin 1.7: no proper blocking on open*)
wenzelm@38254
    92
      else OS.FileSys.remove path;  (*prevent future access*)
wenzelm@38253
    93
  in result end;
wenzelm@38253
    94
wenzelm@28188
    95
in
wenzelm@28188
    96
wenzelm@38253
    97
fun setup_channels in_fifo out_fifo =
wenzelm@28189
    98
  let
wenzelm@38253
    99
    val in_stream = rendezvous TextIO.openIn in_fifo;
wenzelm@38253
   100
    val out_stream = rendezvous TextIO.openOut out_fifo;
wenzelm@37216
   101
    val _ = Simple_Thread.fork false (auto_flush out_stream);
wenzelm@37216
   102
    val _ = Simple_Thread.fork false (auto_flush TextIO.stdOut);
wenzelm@37216
   103
    val _ = Simple_Thread.fork false (auto_flush TextIO.stdErr);
wenzelm@28044
   104
  in
wenzelm@34096
   105
    Output.status_fn   := standard_message out_stream "B";
wenzelm@38236
   106
    Output.report_fn   := standard_message out_stream "C";
wenzelm@38236
   107
    Output.writeln_fn  := standard_message out_stream "D";
wenzelm@38236
   108
    Output.tracing_fn  := standard_message out_stream "E";
wenzelm@38236
   109
    Output.warning_fn  := standard_message out_stream "F";
wenzelm@38236
   110
    Output.error_fn    := standard_message out_stream "G";
wenzelm@38236
   111
    Output.debug_fn    := standard_message out_stream "H";
wenzelm@37121
   112
    Output.priority_fn := ! Output.writeln_fn;
wenzelm@28498
   113
    Output.prompt_fn   := ignore;
wenzelm@38253
   114
    (in_stream, out_stream)
wenzelm@28044
   115
  end;
wenzelm@25841
   116
wenzelm@28188
   117
end;
wenzelm@28188
   118
wenzelm@25841
   119
wenzelm@38270
   120
(* protocol loop *)
wenzelm@38270
   121
wenzelm@38270
   122
val crashes = Unsynchronized.ref ([]: exn list);
wenzelm@38270
   123
wenzelm@38270
   124
local
wenzelm@38270
   125
wenzelm@38270
   126
fun recover crash =
wenzelm@38270
   127
  (CRITICAL (fn () => Unsynchronized.change crashes (cons crash));
wenzelm@38270
   128
    warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
wenzelm@38270
   129
wenzelm@38270
   130
fun read_chunk stream 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@38270
   136
    val chunk = TextIO.inputN (stream, 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@38270
   143
fun read_command stream =
wenzelm@38270
   144
  (case TextIO.inputLine stream of
wenzelm@38270
   145
    NONE => raise Runtime.TERMINATE
wenzelm@38270
   146
  | SOME line => map (read_chunk stream) (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@38270
   155
fun loop stream =
wenzelm@38270
   156
  let val continue =
wenzelm@38270
   157
    (case read_command stream 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@38270
   162
  in if continue then loop stream else () end;
wenzelm@38270
   163
wenzelm@38270
   164
end;
wenzelm@38270
   165
wenzelm@38270
   166
wenzelm@25554
   167
(* init *)
wenzelm@25554
   168
wenzelm@38253
   169
fun init in_fifo out_fifo =
wenzelm@38253
   170
  let
wenzelm@38253
   171
    val _ = Unsynchronized.change print_mode
wenzelm@38253
   172
      (fold (update op =) [isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
wenzelm@38253
   173
    val (in_stream, out_stream) = setup_channels in_fifo out_fifo;
wenzelm@38253
   174
    val _ = init_message out_stream;
wenzelm@38253
   175
    val _ = quick_and_dirty := true;  (* FIXME !? *)
wenzelm@38253
   176
    val _ = Keyword.status ();
wenzelm@38253
   177
    val _ = Output.status (Markup.markup Markup.ready "");
wenzelm@38270
   178
    val _ = Context.set_thread_data NONE;
wenzelm@38270
   179
    val _ = Simple_Thread.fork false (fn () => (loop in_stream; quit ()));
wenzelm@38259
   180
  in () end;
wenzelm@25528
   181
wenzelm@25528
   182
end;