src/Pure/ML-Systems/polyml_common.ML
author wenzelm
Thu Sep 04 16:03:47 2008 +0200 (2008-09-04)
changeset 28124 10a1f1f4c6ae
parent 26474 94735cff132c
child 28151 61f9c918b410
permissions -rw-r--r--
moved Multithreading.task/schedule to Concurrent/schedule.ML;
     1 (*  Title:      Pure/ML-Systems/polyml_common.ML
     2     ID:         $Id$
     3 
     4 Compatibility file for Poly/ML -- common part for 4.x and 5.x.
     5 *)
     6 
     7 use "ML-Systems/exn.ML";
     8 if List.exists (fn s => s = "Universal") (PolyML.Compiler.structureNames ()) then ()
     9 else use "ML-Systems/universal.ML";
    10 use "ML-Systems/multithreading.ML";
    11 use "ML-Systems/time_limit.ML";
    12 use "ML-Systems/system_shell.ML";
    13 
    14 
    15 
    16 (** ML system and platform related **)
    17 
    18 val forget_structure = PolyML.Compiler.forgetStructure;
    19 
    20 
    21 (* Compiler options *)
    22 
    23 val ml_system_fix_ints = false;
    24 
    25 PolyML.Compiler.printInAlphabeticalOrder := false;
    26 PolyML.Compiler.maxInlineSize := 80;
    27 
    28 
    29 (* old Poly/ML emulation *)
    30 
    31 local
    32   val orig_exit = exit;
    33 in
    34   open PolyML;
    35   val exit = orig_exit;
    36   fun quit () = exit 0;
    37 end;
    38 
    39 
    40 (* restore old-style character / string functions *)
    41 
    42 val ord = SML90.ord;
    43 val chr = SML90.chr;
    44 val explode = SML90.explode;
    45 val implode = SML90.implode;
    46 
    47 
    48 (* compiler-independent timing functions *)
    49 
    50 fun start_timing () =
    51   let val CPUtimer = Timer.startCPUTimer();
    52       val time = Timer.checkCPUTimer(CPUtimer)
    53   in  (CPUtimer,time)  end;
    54 
    55 fun end_timing (CPUtimer, {sys,usr}) =
    56   let open Time  (*...for Time.toString, Time.+ and Time.- *)
    57       val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
    58   in  "User " ^ toString (usr2-usr) ^
    59       "  All "^ toString (sys2-sys + usr2-usr) ^
    60       " secs"
    61       handle Time => ""
    62   end;
    63 
    64 fun check_timer timer =
    65   let
    66     val {sys, usr} = Timer.checkCPUTimer timer;
    67     val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
    68   in (sys, usr, gc) end;
    69 
    70 
    71 (* prompts *)
    72 
    73 fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
    74 
    75 
    76 (* toplevel pretty printing (see also Pure/pure_setup.ML) *)
    77 
    78 fun make_pp _ pprint (str, blk, brk, en) _ _ obj =
    79   pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
    80     fn () => brk (99999, 0), en);
    81 
    82 (*print depth*)
    83 local
    84   val depth = ref 10;
    85 in
    86   fun get_print_depth () = ! depth;
    87   fun print_depth n = (depth := n; PolyML.print_depth n);
    88 end;
    89 
    90 
    91 
    92 (** interrupts **)
    93 
    94 exception Interrupt = SML90.Interrupt;
    95 
    96 local
    97 
    98 val sig_int = 2;
    99 val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ());
   100 
   101 val _ = Signal.signal (sig_int, default_handler);
   102 val _ = PolyML.onEntry (fn () => (Signal.signal (sig_int, default_handler); ()));
   103 
   104 fun change_signal new_handler f x =
   105   let
   106     (*RACE wrt. other signals!*)
   107     val old_handler = Signal.signal (sig_int, new_handler);
   108     val result = Exn.capture (f old_handler) x;
   109     val _ = Signal.signal (sig_int, old_handler);
   110   in Exn.release result end;
   111 
   112 in
   113 
   114 fun interruptible f = change_signal default_handler (fn _ => f);
   115 
   116 fun uninterruptible f =
   117   change_signal Signal.SIG_IGN
   118     (fn old_handler => f (fn g => change_signal old_handler (fn _ => g)));
   119 
   120 end;
   121 
   122 
   123 
   124 (** OS related **)
   125 
   126 (* Posix patches *)
   127 
   128 (*This extension of the Poly/ML Signal structure is only necessary
   129   because in SML/NJ, types Posix.Signal.signal and Signals.signal differ.*)
   130 structure IsaSignal =
   131 struct
   132   open Signal
   133   val usr1 = Posix.Signal.usr1
   134   val usr2 = Posix.Signal.usr2
   135   val alrm = Posix.Signal.alrm
   136   val chld = Posix.Signal.chld
   137   val cont = Posix.Signal.cont
   138   val int  = Posix.Signal.int
   139   val quit = Posix.Signal.quit
   140 end;
   141 
   142 
   143 (* current directory *)
   144 
   145 val cd = OS.FileSys.chDir;
   146 val pwd = OS.FileSys.getDir;
   147 
   148 
   149 (*Convert a process ID to a decimal string (chiefly for tracing)*)
   150 fun string_of_pid pid =
   151   Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid));
   152 
   153 
   154 (* getenv *)
   155 
   156 fun getenv var =
   157   (case OS.Process.getEnv var of
   158     NONE => ""
   159   | SOME txt => txt);
   160 
   161 
   162 (* profile execution *)
   163 
   164 fun profile 0 f x = f x
   165   | profile n f x =
   166       let
   167         val _ = RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler n;
   168         val res = Exn.capture f x;
   169         val _ = RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler 0;
   170       in Exn.release res end;
   171