Hello,
I am trying to implement bounded time execution (similar to SML/NJ's TimeLimit.timeLimit) in PolyML. Below is my current attempt. However, the code causes a SEGV when Interrupt'ed and run for a second time -- presumably because the signal handler is persistent? Any help would be appreciated.
structure TimeLimit : sig exception TimeOut val timeLimit : Time.time -> ('a -> 'b) -> 'a -> 'b end = struct
exception TimeOut
fun timeLimit t f x = let val result = Process.channel () fun workerThread () = Process.send (result, SOME (f x)) val interrupt = Process.console workerThread val old_handle = Signal.signal (Posix.Signal.alrm, Signal.SIG_HANDLE (fn _ => (interrupt (); Process.send (result, NONE)))) in Posix.Process.alarm t; case Process.receive result of SOME x => (Posix.Process.alarm Time.zeroTime; Signal.signal (Posix.Signal.alrm, old_handle); x) | NONE => (Signal.signal (Posix.Signal.alrm, old_handle); raise TimeOut) end end;
Regards, Tjark
On Monday, May 31, 2004, at 13:05 Europe/London, Tjark Weber wrote:
I am trying to implement bounded time execution (similar to SML/NJ's TimeLimit.timeLimit) in PolyML. Below is my current attempt. However, the code causes a SEGV when Interrupt'ed and run for a second time -- presumably because the signal handler is persistent? Any help would be appreciated.
I tried this on a number of platforms using PolyML 4.1.3 and haven't had any crash. Which version of Poly/ML are you running? Can you give me some more information. There was a bug in 4.1.2 on the PowerPC which could have caused a crash in the circumstances you describe.
Regards, David.
Hi David,
On Monday 31 May 2004 18:03, David Matthews wrote:
I tried this on a number of platforms using PolyML 4.1.3 and haven't had any crash. Which version of Poly/ML are you running?
thanks for your quick reply. You are right; the segmentation fault apparently was caused not by PolyML (version 4.1.3 on x86-linux), but by a wrapper script that I was using.
Below is a version of my code that passes on any exception raised in f, just like the SML/NJ function TimeLimit.timeLimit does. Maybe it's useful for someone.
structure TimeLimit : sig exception TimeOut val timeLimit : Time.time -> ('a -> 'b) -> 'a -> 'b end = struct
exception TimeOut
fun timeLimit t f x = let datatype ('a, 'b) union = INL of 'a | INR of 'b val result = Process.channel () fun workerThread () = Process.send (result, SOME (INL (f x) handle exn => INR exn)) val interrupt = Process.console workerThread val old_handle = Signal.signal (Posix.Signal.alrm, Signal.SIG_HANDLE (fn _ => (interrupt (); Process.send (result, NONE)))) in Posix.Process.alarm t; case Process.receive result of SOME (INL fx) => (Posix.Process.alarm Time.zeroTime; Signal.signal (Posix.Signal.alrm, old_handle); fx) | SOME (INR exn) => (Posix.Process.alarm Time.zeroTime; Signal.signal (Posix.Signal.alrm, old_handle); raise exn) | NONE => (Signal.signal (Posix.Signal.alrm, old_handle); raise TimeOut) end
end;
Regards, Tjark