2 module Distribution
.Client
.Signal
3 ( installTerminationHandler
8 import qualified Control
.Exception
as Exception
10 #ifndef mingw32_HOST_OS
11 import Control
.Concurrent
(myThreadId
)
12 import Control
.Monad
(void
)
13 import qualified System
.Posix
.Signals
as Signals
16 -- | Terminated is an asynchronous exception, thrown when
17 -- SIGTERM is received. It's to 'kill' what 'UserInterrupt'
19 data Terminated
= Terminated
21 instance Exception
.Exception Terminated
where
22 toException
= Exception
.asyncExceptionToException
23 fromException
= Exception
.asyncExceptionFromException
25 instance Show Terminated
where
26 show Terminated
= "terminated"
28 -- | Install a signal handler that initiates a controlled shutdown on receiving
29 -- SIGTERM by throwing an asynchronous exception at the main thread. Must be
30 -- called from the main thread.
32 -- 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
)