Skip to content

Instantly share code, notes, and snippets.

@Wizek
Last active December 29, 2016 10:25
Show Gist options
  • Save Wizek/03609383bcf7054ac127a8e7f7bd2dc7 to your computer and use it in GitHub Desktop.
Save Wizek/03609383bcf7054ac127a8e7f7bd2dc7 to your computer and use it in GitHub Desktop.
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