src/Pure/System/isabelle_process.ML
author wenzelm
Tue Jul 12 13:39:29 2011 +0200 (2011-07-12 ago)
changeset 43771 fc524449f511
parent 43746 a41f618c641d
child 43772 c825594fd0c1
permissions -rw-r--r--
allow empty body for raw_message -- important for Invoke_Scala;
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@38253
    24
  val init: string -> 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@38270
    40
(* 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@43684
    49
fun add_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@38270
    55
fun 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@38270
    58
  | SOME cmd => cmd args);
wenzelm@38270
    59
wenzelm@38270
    60
end;
wenzelm@38270
    61
wenzelm@38270
    62
wenzelm@40134
    63
(* message channels *)
wenzelm@25841
    64
wenzelm@25841
    65
local
wenzelm@25810
    66
wenzelm@40134
    67
fun chunk s = [string_of_int (size s), "\n", s];
wenzelm@26574
    68
wenzelm@43771
    69
fun message mbox ch raw_props body =
wenzelm@43771
    70
  let
wenzelm@43771
    71
    val robust_props = map (pairself YXML.escape_controls) raw_props;
wenzelm@43771
    72
    val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
wenzelm@43771
    73
  in Mailbox.send mbox (chunk header @ chunk body) end;
wenzelm@25554
    74
wenzelm@40134
    75
fun standard_message mbox with_serial ch body =
wenzelm@43771
    76
  if body = "" then ()
wenzelm@43771
    77
  else
wenzelm@43771
    78
    message mbox ch
wenzelm@43771
    79
      ((if with_serial then cons (Markup.serialN, serial_string ()) else I)
wenzelm@43771
    80
        (Position.properties_of (Position.thread_data ()))) body;
wenzelm@25554
    81
wenzelm@40134
    82
fun message_output mbox out_stream =
wenzelm@28188
    83
  let
wenzelm@40134
    84
    fun loop receive =
wenzelm@40134
    85
      (case receive mbox of
wenzelm@40134
    86
        SOME msg =>
wenzelm@40134
    87
         (List.app (fn s => TextIO.output (out_stream, s)) msg;
wenzelm@40301
    88
          loop (Mailbox.receive_timeout (seconds 0.02)))
wenzelm@40134
    89
      | NONE => (try TextIO.flushOut out_stream; loop (SOME o Mailbox.receive)));
wenzelm@40134
    90
  in fn () => loop (SOME o Mailbox.receive) end;
wenzelm@28188
    91
wenzelm@28188
    92
in
wenzelm@28188
    93
wenzelm@38253
    94
fun setup_channels in_fifo out_fifo =
wenzelm@28189
    95
  let
wenzelm@39530
    96
    (*rendezvous*)
wenzelm@39530
    97
    val in_stream = TextIO.openIn in_fifo;
wenzelm@39530
    98
    val out_stream = TextIO.openOut out_fifo;
wenzelm@39530
    99
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@40134
   103
    val mbox = Mailbox.create () : string list Mailbox.T;
wenzelm@40134
   104
    val _ = Simple_Thread.fork false (message_output mbox out_stream);
wenzelm@28044
   105
  in
wenzelm@40134
   106
    Output.Private_Hooks.status_fn := standard_message mbox false "B";
wenzelm@40134
   107
    Output.Private_Hooks.report_fn := standard_message mbox false "C";
wenzelm@40134
   108
    Output.Private_Hooks.writeln_fn := standard_message mbox true "D";
wenzelm@40134
   109
    Output.Private_Hooks.tracing_fn := standard_message mbox true "E";
wenzelm@40134
   110
    Output.Private_Hooks.warning_fn := standard_message mbox true "F";
wenzelm@40134
   111
    Output.Private_Hooks.error_fn := standard_message mbox true "G";
wenzelm@43746
   112
    Output.Private_Hooks.raw_message_fn := message 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@40134
   115
    message mbox "A" [] (Session.welcome ());
wenzelm@40134
   116
    in_stream
wenzelm@28044
   117
  end;
wenzelm@25841
   118
wenzelm@28188
   119
end;
wenzelm@28188
   120
wenzelm@25841
   121
wenzelm@39234
   122
(* protocol loop -- uninterruptible *)
wenzelm@38270
   123
wenzelm@43684
   124
val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
wenzelm@38270
   125
wenzelm@38270
   126
local
wenzelm@38270
   127
wenzelm@38270
   128
fun recover crash =
wenzelm@43684
   129
  (Synchronized.change crashes (cons crash);
wenzelm@38270
   130
    warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
wenzelm@38270
   131
wenzelm@38270
   132
fun read_chunk stream len =
wenzelm@38270
   133
  let
wenzelm@38270
   134
    val n =
wenzelm@38270
   135
      (case Int.fromString len of
wenzelm@38270
   136
        SOME n => n
wenzelm@38270
   137
      | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
wenzelm@38270
   138
    val chunk = TextIO.inputN (stream, n);
wenzelm@38270
   139
    val m = size chunk;
wenzelm@38270
   140
  in
wenzelm@38270
   141
    if m = n then chunk
wenzelm@38270
   142
    else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
wenzelm@38270
   143
  end;
wenzelm@38270
   144
wenzelm@38270
   145
fun read_command stream =
wenzelm@38270
   146
  (case TextIO.inputLine stream of
wenzelm@38270
   147
    NONE => raise Runtime.TERMINATE
wenzelm@38270
   148
  | SOME line => map (read_chunk stream) (space_explode "," line));
wenzelm@38270
   149
wenzelm@38270
   150
fun run_command name args =
wenzelm@38270
   151
  Runtime.debugging (command name) args
wenzelm@38270
   152
    handle exn =>
wenzelm@38270
   153
      error ("Isabelle process command failure: " ^ name ^ "\n" ^ ML_Compiler.exn_message exn);
wenzelm@38270
   154
wenzelm@38270
   155
in
wenzelm@38270
   156
wenzelm@38270
   157
fun loop stream =
wenzelm@38270
   158
  let val continue =
wenzelm@38270
   159
    (case read_command stream of
wenzelm@38270
   160
      [] => (Output.error_msg "Isabelle process: no input"; true)
wenzelm@38270
   161
    | name :: args => (run_command name args; true))
wenzelm@38270
   162
    handle Runtime.TERMINATE => false
wenzelm@38270
   163
      | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
wenzelm@38270
   164
  in if continue then loop stream else () end;
wenzelm@38270
   165
wenzelm@38270
   166
end;
wenzelm@38270
   167
wenzelm@38270
   168
wenzelm@25554
   169
(* init *)
wenzelm@25554
   170
wenzelm@39528
   171
fun init in_fifo out_fifo = ignore (Simple_Thread.fork false (fn () =>
wenzelm@38253
   172
  let
wenzelm@40301
   173
    val _ = OS.Process.sleep (seconds 0.5);  (*yield to raw ML toplevel*)
wenzelm@39733
   174
    val _ = Output.raw_stdout Symbol.STX;
wenzelm@39528
   175
wenzelm@40521
   176
    val _ = quick_and_dirty := true;
wenzelm@40518
   177
    val _ = Goal.parallel_proofs := 0;
wenzelm@39528
   178
    val _ = Context.set_thread_data NONE;
wenzelm@43671
   179
    val _ =
wenzelm@43671
   180
      Unsynchronized.change print_mode
wenzelm@43671
   181
        (fold (update op =)
wenzelm@43671
   182
          [Symbol.xsymbolsN, isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
wenzelm@39528
   183
wenzelm@40134
   184
    val in_stream = setup_channels in_fifo out_fifo;
wenzelm@38253
   185
    val _ = Keyword.status ();
wenzelm@43673
   186
    val _ = Thy_Info.status ();
wenzelm@39626
   187
    val _ = Output.status (Markup.markup Markup.ready "process ready");
wenzelm@39528
   188
  in loop in_stream end));
wenzelm@25528
   189
wenzelm@25528
   190
end;
wenzelm@39528
   191