Last active
December 29, 2016 10:25
-
-
Save Wizek/03609383bcf7054ac127a8e7f7bd2dc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.IORef | |
import System.Exit (exitSuccess, ExitCode(ExitSuccess)) | |
import Control.Concurrent | |
import System.Posix.Signals -- (installHandler, Handler(Catch), sigINT, sigTERM) | |
import Control.Concurrent.Lock ( Lock ) | |
import qualified Control.Concurrent.Lock as Lock | |
forkIOInterruptible io = do | |
ctid <- forkIO io | |
tid <- myThreadId | |
oldHandlerRef <- newIORef Nothing | |
let | |
handler info = do | |
f ctid | |
readIORef oldHandlerRef >>= \x -> case x of | |
Nothing -> noop | |
Just Default -> noop | |
Just Ignore -> noop | |
Just (Catch io) -> io | |
Just (CatchInfo iof) -> iof info | |
Just (CatchInfoOnce iof) -> iof info | |
f tid | |
oldHandler <- installHandler sigINT (CatchInfoOnce $ handler) Nothing | |
writeIORef oldHandlerRef (Just oldHandler) | |
return ctid | |
where | |
-- f tid = throwTo tid ExitSuccess | |
f = killThread | |
noop = return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment