1
0
forked from GitHub/gf-core

Restore old signal handler after each computation.

This commit is contained in:
bringert
2005-11-07 21:27:13 +00:00
parent abf9823601
commit 71cd8efe27

View File

@@ -5,9 +5,9 @@
-- Stability : (stability)
-- Portability : (portability)
--
-- > CVS $Date: 2005/11/07 20:15:05 $
-- > CVS $Date: 2005/11/07 22:27:13 $
-- > CVS $Author: bringert $
-- > CVS $Revision: 1.1 $
-- > CVS $Revision: 1.2 $
--
-- Allows SIGINT (Ctrl-C) to interrupt computations.
-----------------------------------------------------------------------------
@@ -26,18 +26,19 @@ import System.Posix.Signals
-- normally.
-- NOTES:
-- * This will replace any existing SIGINT
-- handlers, and after the computation has completed
-- the default handler will be installed for SIGINT.
-- handler during the action. After the computation
-- has completed the existing handler will be restored.
-- * If the IO action is lazy (e.g. using readFile,
-- unsafeInterleaveIO etc.) the lazy computation will
-- not be interruptible, as it will be performed
-- after the signal handler has been removed.
runInterruptibly :: IO a -> IO (Either Exception a)
runInterruptibly a = do t <- myThreadId
installHandler sigINT (Catch (killThread t)) Nothing
x <- p `catch` h
installHandler sigINT Default Nothing
return x
runInterruptibly a =
do t <- myThreadId
oldH <- installHandler sigINT (Catch (killThread t)) Nothing
x <- p `catch` h
installHandler sigINT oldH Nothing
return x
where p = a >>= \x -> return $! Right $! x
h e = return $ Left e