3 module Distribution
.Client
.Signal
4 ( installTerminationHandler
9 import qualified Control
.Exception
as Exception
11 #ifndef mingw32_HOST_OS
12 import Control
.Concurrent
(myThreadId
)
13 import Control
.Monad
(void
)
14 import qualified System
.Posix
.Signals
as Signals
17 -- | Terminated is an asynchronous exception, thrown when
18 -- SIGTERM is received. It's to 'kill' what 'UserInterrupt'
20 data Terminated
= Terminated
22 instance Exception
.Exception Terminated
where
23 toException
= Exception
.asyncExceptionToException
24 fromException
= Exception
.asyncExceptionFromException
26 instance Show Terminated
where
27 show Terminated
= "terminated"
29 -- | Install a signal handler that initiates a controlled shutdown on receiving
30 -- SIGTERM by throwing an asynchronous exception at the main thread. Must be
31 -- called from the main thread.
33 -- It is a noop on Windows.
34 installTerminationHandler
:: IO ()
36 #ifdef mingw32_HOST_OS
38 installTerminationHandler
= return ()
42 installTerminationHandler
= do
43 mainThreadId
<- myThreadId
44 void
$ Signals
.installHandler
46 (Signals
.CatchOnce
$ Exception
.throwTo mainThreadId Terminated
)