src/Pure/ML-Systems/smlnj.ML
changeset 15702 2677db44c795
parent 14981 e73f8140af78
child 16266 7a6616be8712
     1.1 --- a/src/Pure/ML-Systems/smlnj.ML	Tue Apr 12 13:38:08 2005 +0200
     1.2 +++ b/src/Pure/ML-Systems/smlnj.ML	Wed Apr 13 09:48:41 2005 +0200
     1.3 @@ -127,6 +127,35 @@
     1.4  
     1.5  end;
     1.6  
     1.7 +(** Signal handling: emulation of the Poly/ML Signal structure. Note that types 
     1.8 +    Posix.Signal.signal and Signals.signal differ **)
     1.9 +
    1.10 +structure IsaSignal =
    1.11 +struct
    1.12 +
    1.13 +datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit;
    1.14 +
    1.15 +(*From the SML/NJ documentation:
    1.16 +"HANDLER(f) installs a handler for a signal. When signal is delivered to the process, the execution state of the current thread will be bundled up as a continuation k, then f(signal,n,k) will be called. The number n is the number of times signal has been signalled since the last time f was invoked for it."*)
    1.17 +
    1.18 +fun toAction SIG_DFL = Signals.DEFAULT
    1.19 +  | toAction SIG_IGN = Signals.IGNORE
    1.20 +  | toAction (SIG_HANDLE iu) = 
    1.21 +      Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
    1.22 +  
    1.23 +(*The types are correct, but I'm not sure about the semantics!*)
    1.24 +fun fromAction Signals.DEFAULT = SIG_DFL
    1.25 +  | fromAction Signals.IGNORE = SIG_IGN
    1.26 +  | fromAction (Signals.HANDLER f) = 
    1.27 +      SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
    1.28 +    
    1.29 +(*Poly/ML version has type  int * sig_handle -> sig_handle*)
    1.30 +fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
    1.31 +
    1.32 +val usr1 = UnixSignals.sigUSR1
    1.33 +val usr2 = UnixSignals.sigUSR2
    1.34 +
    1.35 +end;
    1.36  
    1.37  
    1.38  (** OS related **)