src/Pure/ML-Systems/smlnj.ML
changeset 5816 6f3cb53502fa
parent 5708 fb09ab6a447f
child 6227 3198f547f8af
equal deleted inserted replaced
5815:b4d4a97df438 5816:6f3cb53502fa
    32 fun print_depth n =
    32 fun print_depth n =
    33  (Compiler.Control.Print.printDepth := n div 2;
    33  (Compiler.Control.Print.printDepth := n div 2;
    34   Compiler.Control.Print.printLength := n);
    34   Compiler.Control.Print.printLength := n);
    35 
    35 
    36 
    36 
    37 
    37 (* compiler-independent timing functions *)
    38 (** Compiler-independent timing functions **)
       
    39 
    38 
    40 (*Note start point for timing*)
    39 (*Note start point for timing*)
    41 fun startTiming() = 
    40 fun startTiming() =
    42   let val CPUtimer = Timer.startCPUTimer();
    41   let val CPUtimer = Timer.startCPUTimer();
    43       val time = Timer.checkCPUTimer(CPUtimer)
    42       val time = Timer.checkCPUTimer(CPUtimer)
    44   in  (CPUtimer,time)  end;
    43   in  (CPUtimer,time)  end;
    45 
    44 
    46 (*Finish timing and return string*)
    45 (*Finish timing and return string*)
    97     if verbose then show_output () else ()
    96     if verbose then show_output () else ()
    98   end;
    97   end;
    99 
    98 
   100 
    99 
   101 
   100 
       
   101 (** interrupts **)
       
   102 
       
   103 local
       
   104 
       
   105 datatype 'a result =
       
   106   Result of 'a |
       
   107   Exn of exn;
       
   108 
       
   109 fun capture f x = Result (f x) handle exn => Exn exn;
       
   110 
       
   111 fun release (Result x) = x
       
   112   | release (Exn exn) = raise exn;
       
   113 
       
   114 
       
   115 val sig_int = Signals.sigINT;
       
   116 val sig_int_mask = Signals.MASK [Signals.sigINT];
       
   117 
       
   118 fun interruptible () =
       
   119   (case Signals.masked () of
       
   120     Signals.MASKALL => false
       
   121   | Signals.MASK sigs => List.all (fn s => s <> sig_int) sigs);
       
   122 
       
   123 val mask_signals = Signals.maskSignals;
       
   124 val unmask_signals = Signals.unmaskSignals;
       
   125 
       
   126 fun change_mask ok change unchange f x =
       
   127   if ok () then f x
       
   128   else
       
   129     let
       
   130       val _ = change sig_int_mask;
       
   131       val result = capture f x;
       
   132       val _ = unchange sig_int_mask;
       
   133     in release result end;
       
   134 
       
   135 in
       
   136 
       
   137 
       
   138 (* mask / unmask interrupt *)
       
   139 
       
   140 fun mask_interrupt f = change_mask (not o interruptible) mask_signals unmask_signals f;
       
   141 fun unmask_interrupt f = change_mask interruptible unmask_signals mask_signals f;
       
   142 
       
   143 
       
   144 (* exhibit interrupt (via exception) *)
       
   145 
       
   146 exception Interrupt;
       
   147 
       
   148 fun exhibit_interrupt f x =
       
   149   let
       
   150     val orig_handler = Signals.inqHandler sig_int;
       
   151     fun reset_handler () = (Signals.setHandler (sig_int, orig_handler); ());
       
   152 
       
   153     val interrupted = ref false;
       
   154 
       
   155     fun set_handler cont =
       
   156       Signals.setHandler (sig_int, Signals.HANDLER (fn _ => (interrupted := true; cont)));
       
   157 
       
   158     fun proceed cont =
       
   159       let
       
   160         val _ = set_handler cont;
       
   161         val result = unmask_interrupt (capture f) x;
       
   162         val _ = reset_handler ();
       
   163       in release result end;
       
   164   in
       
   165     SMLofNJ.Cont.callcc proceed;
       
   166     reset_handler ();
       
   167     if ! interrupted then raise Interrupt else ()
       
   168    end;
       
   169 
       
   170 end;
       
   171 
       
   172 
       
   173 
   102 (** OS related **)
   174 (** OS related **)
   103 
   175 
   104 (* system command execution *)
   176 (* system command execution *)
   105 
   177 
   106 (*execute Unix command which doesn't take any input from stdin and
   178 (*execute Unix command which doesn't take any input from stdin and
   119 
   191 
   120 
   192 
   121 (* file handling *)
   193 (* file handling *)
   122 
   194 
   123 (*get time of last modification; if file doesn't exist return an empty string*)
   195 (*get time of last modification; if file doesn't exist return an empty string*)
   124 fun file_info "" = ""		(* FIXME !? *)
   196 fun file_info "" = ""           (* FIXME !? *)
   125   | file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
   197   | file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
   126 
   198 
   127 
   199 
   128 (* getenv *)
   200 (* getenv *)
   129 
   201