src/Pure/System/isabelle_process.ML
changeset 38253 3d4e521014f7
parent 38236 d8c7be27e01d
child 38254 fb1b255d6e36
equal deleted inserted replaced
38252:175a5b4b2c94 38253:3d4e521014f7
     9 *)
     9 *)
    10 
    10 
    11 signature ISABELLE_PROCESS =
    11 signature ISABELLE_PROCESS =
    12 sig
    12 sig
    13   val isabelle_processN: string
    13   val isabelle_processN: string
    14   val init: string -> unit
    14   val init: string -> string -> unit
    15 end;
    15 end;
    16 
    16 
    17 structure Isabelle_Process: ISABELLE_PROCESS =
    17 structure Isabelle_Process: ISABELLE_PROCESS =
    18 struct
    18 struct
    19 
    19 
    58     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream stream, IO.BLOCK_BUF);
    58     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream stream, IO.BLOCK_BUF);
    59     fun loop () =
    59     fun loop () =
    60       (OS.Process.sleep (Time.fromMilliseconds 50); try TextIO.flushOut stream; loop ());
    60       (OS.Process.sleep (Time.fromMilliseconds 50); try TextIO.flushOut stream; loop ());
    61   in loop end;
    61   in loop end;
    62 
    62 
       
    63 fun rendezvous f fifo =
       
    64   let
       
    65     val path = File.platform_path (Path.explode fifo);
       
    66     val result = f fifo;  (*block until peer is ready*)
       
    67     val _ = OS.FileSys.remove path;  (*ready; prevent future access -- potential race condition*)
       
    68   in result end;
       
    69 
    63 in
    70 in
    64 
    71 
    65 fun setup_channels out =
    72 fun setup_channels in_fifo out_fifo =
    66   let
    73   let
    67     val path = File.platform_path (Path.explode out);
    74     val in_stream = rendezvous TextIO.openIn in_fifo;
    68     val out_stream = TextIO.openOut path;  (*fifo blocks until reader is ready*)
    75     val out_stream = rendezvous TextIO.openOut out_fifo;
    69     val _ = OS.FileSys.remove path;  (*prevent alien access, indicate writer is ready*)
       
    70     val _ = Simple_Thread.fork false (auto_flush out_stream);
    76     val _ = Simple_Thread.fork false (auto_flush out_stream);
    71     val _ = Simple_Thread.fork false (auto_flush TextIO.stdOut);
    77     val _ = Simple_Thread.fork false (auto_flush TextIO.stdOut);
    72     val _ = Simple_Thread.fork false (auto_flush TextIO.stdErr);
    78     val _ = Simple_Thread.fork false (auto_flush TextIO.stdErr);
    73   in
    79   in
    74     Output.status_fn   := standard_message out_stream "B";
    80     Output.status_fn   := standard_message out_stream "B";
    78     Output.warning_fn  := standard_message out_stream "F";
    84     Output.warning_fn  := standard_message out_stream "F";
    79     Output.error_fn    := standard_message out_stream "G";
    85     Output.error_fn    := standard_message out_stream "G";
    80     Output.debug_fn    := standard_message out_stream "H";
    86     Output.debug_fn    := standard_message out_stream "H";
    81     Output.priority_fn := ! Output.writeln_fn;
    87     Output.priority_fn := ! Output.writeln_fn;
    82     Output.prompt_fn   := ignore;
    88     Output.prompt_fn   := ignore;
    83     out_stream
    89     (in_stream, out_stream)
    84   end;
    90   end;
    85 
    91 
    86 end;
    92 end;
    87 
    93 
    88 
    94 
    89 (* init *)
    95 (* init *)
    90 
    96 
    91 fun init out =
    97 fun init in_fifo out_fifo =
    92  (Unsynchronized.change print_mode
    98   let
    93     (fold (update op =) [isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
    99     val _ = Unsynchronized.change print_mode
    94   setup_channels out |> init_message;
   100       (fold (update op =) [isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
    95   quick_and_dirty := true;  (* FIXME !? *)
   101     val (in_stream, out_stream) = setup_channels in_fifo out_fifo;
    96   Keyword.status ();
   102     val _ = init_message out_stream;
    97   Output.status (Markup.markup Markup.ready "");
   103     val _ = quick_and_dirty := true;  (* FIXME !? *)
    98   Isar.toplevel_loop {init = true, welcome = false, sync = true, secure = true});
   104     val _ = Keyword.status ();
       
   105     val _ = Output.status (Markup.markup Markup.ready "");
       
   106   in Isar.toplevel_loop in_stream {init = true, welcome = false, sync = true, secure = true} end;
    99 
   107 
   100 end;
   108 end;