src/Pure/ML-Systems/smlnj.ML
changeset 28488 18fea7e88ea1
parent 28443 de653f1ad78b
child 29564 f8b933a62151
equal deleted inserted replaced
28487:13e637e0c876 28488:18fea7e88ea1
   185   fun inputLine is = TextIO.inputLine is
   185   fun inputLine is = TextIO.inputLine is
   186     handle IO.Io _ => raise Interrupt;
   186     handle IO.Io _ => raise Interrupt;
   187 end;
   187 end;
   188 
   188 
   189 
   189 
   190 (** Signal handling: emulation of the Poly/ML Signal structure. Note that types
       
   191     Posix.Signal.signal and Signals.signal differ **)
       
   192 
       
   193 structure IsaSignal =
       
   194 struct
       
   195 
       
   196 datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit;
       
   197 
       
   198 (*From the SML/NJ documentation: "HANDLER(f) installs a handler for a
       
   199   signal. When signal is delivered to the process, the execution state
       
   200   of the current thread will be bundled up as a continuation k, then
       
   201   f(signal,n,k) will be called. The number n is the number of times
       
   202   signal has been signalled since the last time f was invoked for it."*)
       
   203 
       
   204 fun toAction SIG_DFL = Signals.DEFAULT
       
   205   | toAction SIG_IGN = Signals.IGNORE
       
   206   | toAction (SIG_HANDLE iu) =
       
   207       Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
       
   208 
       
   209 (*The types are correct, but I'm not sure about the semantics!*)
       
   210 fun fromAction Signals.DEFAULT = SIG_DFL
       
   211   | fromAction Signals.IGNORE = SIG_IGN
       
   212   | fromAction (Signals.HANDLER f) =
       
   213       SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
       
   214 
       
   215 (*Poly/ML version has type  int * sig_handle -> sig_handle*)
       
   216 fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
       
   217 
       
   218 val usr1 = UnixSignals.sigUSR1
       
   219 val usr2 = UnixSignals.sigUSR2
       
   220 val alrm = UnixSignals.sigALRM
       
   221 val chld = UnixSignals.sigCHLD
       
   222 val cont = UnixSignals.sigCONT
       
   223 val int = UnixSignals.sigINT
       
   224 val quit = UnixSignals.sigQUIT
       
   225 val term = UnixSignals.sigTERM
       
   226 
       
   227 end;
       
   228 
       
   229 
   190 
   230 (** OS related **)
   191 (** OS related **)
   231 
   192 
   232 (* current directory *)
   193 (* current directory *)
   233 
   194 
   239 
   200 
   240 val system_out = (fn (output, rc) => (output, mk_int rc)) o system_out;
   201 val system_out = (fn (output, rc) => (output, mk_int rc)) o system_out;
   241 
   202 
   242 
   203 
   243 (*Convert a process ID to a decimal string (chiefly for tracing)*)
   204 (*Convert a process ID to a decimal string (chiefly for tracing)*)
   244 fun string_of_pid pid =
   205 fun process_id pid =
   245   Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid));
   206   Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord (Posix.ProcEnv.getpid ())));
   246 
   207 
   247 
   208 
   248 (* getenv *)
   209 (* getenv *)
   249 
   210 
   250 fun getenv var =
   211 fun getenv var =