src/Pure/ML-Systems/smlnj.ML
changeset 15702 2677db44c795
parent 14981 e73f8140af78
child 16266 7a6616be8712
--- a/src/Pure/ML-Systems/smlnj.ML	Tue Apr 12 13:38:08 2005 +0200
+++ b/src/Pure/ML-Systems/smlnj.ML	Wed Apr 13 09:48:41 2005 +0200
@@ -127,6 +127,35 @@
 
 end;
 
+(** Signal handling: emulation of the Poly/ML Signal structure. Note that types 
+    Posix.Signal.signal and Signals.signal differ **)
+
+structure IsaSignal =
+struct
+
+datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit;
+
+(*From the SML/NJ documentation:
+"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."*)
+
+fun toAction SIG_DFL = Signals.DEFAULT
+  | toAction SIG_IGN = Signals.IGNORE
+  | toAction (SIG_HANDLE iu) = 
+      Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
+  
+(*The types are correct, but I'm not sure about the semantics!*)
+fun fromAction Signals.DEFAULT = SIG_DFL
+  | fromAction Signals.IGNORE = SIG_IGN
+  | fromAction (Signals.HANDLER f) = 
+      SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
+    
+(*Poly/ML version has type  int * sig_handle -> sig_handle*)
+fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
+
+val usr1 = UnixSignals.sigUSR1
+val usr2 = UnixSignals.sigUSR2
+
+end;
 
 
 (** OS related **)