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