2 {-# OPTIONS_HADDOCK hide #-}
3 -----------------------------------------------------------------------------
5 -- Module : GHC.TopHandler
6 -- Copyright : (c) The University of Glasgow, 2001-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
13 -- Support for catching exceptions raised during top-level computations
14 -- (e.g. @Main.main@, 'Control.Concurrent.forkIO', and foreign exports)
16 -----------------------------------------------------------------------------
19 module GHC.TopHandler (
20 runMainIO, runIO, runIOFastExit, runNonIO,
21 topHandler, topHandlerFastExit,
22 reportStackOverflow, reportError,
25 #include "HsBaseConfig.h"
30 import Control.Exception
31 import Control.Concurrent.MVar
41 -- | 'runMainIO' is wrapped around 'Main.main' (or whatever main is
42 -- called in the program). It catches otherwise uncaught exceptions,
43 -- and also flushes stdout\/stderr before exiting.
44 runMainIO :: IO a -> IO a
47 main_thread_id <- myThreadId
48 weak_tid <- mkWeakThreadId main_thread_id
49 install_interrupt_handler $ do
50 m <- deRefWeak weak_tid
53 Just tid -> throwTo tid (AsyncException UserInterrupt)
60 install_interrupt_handler :: IO () -> IO ()
61 #ifdef mingw32_HOST_OS
62 install_interrupt_handler handler =
63 GHC.ConsoleHandler.installHandler $
72 -- specialised version of System.Posix.Signals.installHandler, which
73 -- isn't available here.
74 install_interrupt_handler handler = do
75 let sig = CONST_SIGINT :: CInt
76 withMVar signalHandlerLock $ \_ ->
78 sptr <- newStablePtr handler
80 stg_sig_install sig STG_SIG_RST p_sp nullPtr
83 foreign import ccall unsafe
86 -> CInt -- action code (STG_SIG_HAN etc.)
87 -> Ptr (StablePtr (IO ())) -- (in, out) Haskell handler
88 -> Ptr () -- (in, out) blocked
89 -> IO CInt -- (ret) action code
92 -- make a weak pointer to a ThreadId: holding the weak pointer doesn't
93 -- keep the thread alive and prevent it from being identified as
94 -- deadlocked. Vitally important for the main thread.
95 mkWeakThreadId :: ThreadId -> IO (Weak ThreadId)
96 mkWeakThreadId t@(ThreadId t#) = IO $ \s ->
97 case mkWeak# t# t (unsafeCoerce# 0#) s of
98 (# s1, w #) -> (# s1, Weak w #)
100 -- | 'runIO' is wrapped around every @foreign export@ and @foreign
101 -- import \"wrapper\"@ to mop up any uncaught exceptions. Thus, the
102 -- result of running 'System.Exit.exitWith' in a foreign-exported
103 -- function is the same as in the main thread: it terminates the
106 runIO :: IO a -> IO a
107 runIO main = catchException main topHandler
109 -- | Like 'runIO', but in the event of an exception that causes an exit,
110 -- we don't shut down the system cleanly, we just exit. This is
111 -- useful in some cases, because the safe exit version will give other
112 -- threads a chance to clean up first, which might shut down the
113 -- system in a different way. For example, try
115 -- main = forkIO (runIO (exitWith (ExitFailure 1))) >> threadDelay 10000
117 -- This will sometimes exit with "interrupted" and code 0, because the
118 -- main thread is given a chance to shut down when the child thread calls
119 -- safeExit. There is a race to shut down between the main and child threads.
121 runIOFastExit :: IO a -> IO a
122 runIOFastExit main = catchException main topHandlerFastExit
123 -- NB. this is used by the testsuite driver
125 -- | The same as 'runIO', but for non-IO computations. Used for
126 -- wrapping @foreign export@ and @foreign import \"wrapper\"@ when these
127 -- are used to export Haskell functions with non-IO types.
129 runNonIO :: a -> IO a
130 runNonIO a = catchException (a `seq` return a) topHandler
132 topHandler :: Exception -> IO a
133 topHandler err = catchException (real_handler safeExit err) topHandler
135 topHandlerFastExit :: Exception -> IO a
136 topHandlerFastExit err =
137 catchException (real_handler fastExit err) topHandlerFastExit
139 -- Make sure we handle errors while reporting the error!
140 -- (e.g. evaluating the string passed to 'error' might generate
141 -- another error, etc.)
143 real_handler :: (Int -> IO a) -> Exception -> IO a
144 real_handler exit exn =
147 AsyncException StackOverflow -> do
151 AsyncException UserInterrupt -> exitInterrupted
153 -- only the main thread gets ExitException exceptions
154 ExitException ExitSuccess -> exit 0
155 ExitException (ExitFailure n) -> exit n
162 reportStackOverflow :: IO a
163 reportStackOverflow = do callStackOverflowHook; return undefined
165 reportError :: Exception -> IO a
167 handler <- getUncaughtExceptionHandler
171 -- SUP: Are the hooks allowed to re-enter Haskell land? If so, remove
173 foreign import ccall unsafe "stackOverflow"
174 callStackOverflowHook :: IO ()
176 -- try to flush stdout/stderr, but don't worry if we fail
177 -- (these handles might have errors, and we don't want to go into
178 -- an infinite loop).
181 hFlush stdout `catchException` \_ -> return ()
182 hFlush stderr `catchException` \_ -> return ()
184 cleanUpAndExit :: Int -> IO a
185 cleanUpAndExit r = do cleanUp; safeExit r
187 -- we have to use unsafeCoerce# to get the 'IO a' result type, since the
188 -- compiler doesn't let us declare that as the result type of a foreign export.
189 safeExit :: Int -> IO a
190 safeExit r = unsafeCoerce# (shutdownHaskellAndExit $ fromIntegral r)
192 exitInterrupted :: IO a
194 #ifdef mingw32_HOST_OS
197 -- we must exit via the default action for SIGINT, so that the
198 -- parent of this process can take appropriate action (see #2301)
199 unsafeCoerce# (shutdownHaskellAndSignal CONST_SIGINT)
201 foreign import ccall "shutdownHaskellAndSignal"
202 shutdownHaskellAndSignal :: CInt -> IO ()
205 -- NOTE: shutdownHaskellAndExit must be called "safe", because it *can*
206 -- re-enter Haskell land through finalizers.
207 foreign import ccall "Rts.h shutdownHaskellAndExit"
208 shutdownHaskellAndExit :: CInt -> IO ()
210 fastExit :: Int -> IO a
211 fastExit r = unsafeCoerce# (stg_exit (fromIntegral r))
213 foreign import ccall "Rts.h stg_exit"
214 stg_exit :: CInt -> IO ()