{-# LANGUAGE CPP #-}
module Distribution.Client.Signal
( installTerminationHandler
, Terminated (..)
)
where
import qualified Control.Exception as Exception
#ifndef mingw32_HOST_OS
import Control.Concurrent (myThreadId)
import Control.Monad (void)
import qualified System.Posix.Signals as Signals
#endif
data Terminated = Terminated
instance Exception.Exception Terminated where
toException :: Terminated -> SomeException
toException = Terminated -> SomeException
forall e. Exception e => e -> SomeException
Exception.asyncExceptionToException
fromException :: SomeException -> Maybe Terminated
fromException = SomeException -> Maybe Terminated
forall e. Exception e => SomeException -> Maybe e
Exception.asyncExceptionFromException
instance Show Terminated where
show :: Terminated -> String
show Terminated
Terminated = String
"terminated"
installTerminationHandler :: IO ()
#ifdef mingw32_HOST_OS
installTerminationHandler = return ()
#else
installTerminationHandler :: IO ()
installTerminationHandler = do
mainThreadId <- IO ThreadId
myThreadId
void $ Signals.installHandler
Signals.sigTERM
(Signals.CatchOnce $ Exception.throwTo mainThreadId Terminated)
Nothing
#endif