Prefer #if defined to #ifdef
[ghc.git] / libraries / ghci / GHCi / Signals.hs
1 {-# LANGUAGE CPP #-}
2 module GHCi.Signals (installSignalHandlers) where
3
4 import Control.Concurrent
5 import Control.Exception
6 import System.Mem.Weak ( deRefWeak )
7
8 #if !defined(mingw32_HOST_OS)
9 import System.Posix.Signals
10 #endif
11
12 #if defined(mingw32_HOST_OS)
13 import GHC.ConsoleHandler
14 #endif
15
16 -- | Install standard signal handlers for catching ^C, which just throw an
17 -- exception in the target thread. The current target thread is the
18 -- thread at the head of the list in the MVar passed to
19 -- installSignalHandlers.
20 installSignalHandlers :: IO ()
21 installSignalHandlers = do
22 main_thread <- myThreadId
23 wtid <- mkWeakThreadId main_thread
24
25 let interrupt = do
26 r <- deRefWeak wtid
27 case r of
28 Nothing -> return ()
29 Just t -> throwTo t UserInterrupt
30
31 #if !defined(mingw32_HOST_OS)
32 _ <- installHandler sigQUIT (Catch interrupt) Nothing
33 _ <- installHandler sigINT (Catch interrupt) Nothing
34 #else
35 -- GHC 6.3+ has support for console events on Windows
36 -- NOTE: running GHCi under a bash shell for some reason requires
37 -- you to press Ctrl-Break rather than Ctrl-C to provoke
38 -- an interrupt. Ctrl-C is getting blocked somewhere, I don't know
39 -- why --SDM 17/12/2004
40 let sig_handler ControlC = interrupt
41 sig_handler Break = interrupt
42 sig_handler _ = return ()
43
44 _ <- installHandler (Catch sig_handler)
45 #endif
46 return ()