add runIOFastExit :: IO a -> IO a
[ghc-base.git] / GHC / TopHandler.lhs
1 \begin{code}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  GHC.TopHandler
5 -- Copyright   :  (c) The University of Glasgow, 2001-2002
6 -- License     :  see libraries/base/LICENSE
7 -- 
8 -- Maintainer  :  cvs-ghc@haskell.org
9 -- Stability   :  internal
10 -- Portability :  non-portable (GHC Extensions)
11 --
12 -- Support for catching exceptions raised during top-level computations
13 -- (e.g. @Main.main@, 'Control.Concurrent.forkIO', and foreign exports)
14 --
15 -----------------------------------------------------------------------------
16
17 -- #hide
18 module GHC.TopHandler (
19    runMainIO, runIO, runIOFastExit, runNonIO, reportStackOverflow, reportError
20   ) where
21
22 import Prelude
23
24 import System.IO
25 import Control.Exception
26
27 import Foreign.C        ( CInt )
28 import GHC.IOBase
29 import GHC.Exception
30 import GHC.Prim (unsafeCoerce#)
31
32 -- | 'runMainIO' is wrapped around 'Main.main' (or whatever main is
33 -- called in the program).  It catches otherwise uncaught exceptions,
34 -- and also flushes stdout\/stderr before exiting.
35 runMainIO :: IO a -> IO a
36 runMainIO main = (do a <- main; cleanUp; return a) `catchException` topHandler
37
38 -- | 'runIO' is wrapped around every @foreign export@ and @foreign
39 -- import \"wrapper\"@ to mop up any uncaught exceptions.  Thus, the
40 -- result of running 'System.Exit.exitWith' in a foreign-exported
41 -- function is the same as in the main thread: it terminates the
42 -- program.
43 --
44 runIO :: IO a -> IO a
45 runIO main = catchException main topHandler
46
47 -- | Like 'runIO', but in the event of an exception that causes an exit,
48 -- we don't shut down the system cleanly, we just exit.  This is
49 -- useful in some cases, because the safe exit version will give other
50 -- threads a chance to clean up first, which might shut down the
51 -- system in a different way.  For example, try 
52 --
53 --   main = forkIO (runIO (exitWith (ExitFailure 1))) >> threadDelay 10000
54 --
55 -- This will sometimes exit with "interrupted" and code 0, because the
56 -- main thread is given a chance to shut down when the child thread calls
57 -- safeExit.  There is a race to shut down between the main and child threads.
58 --
59 runIOFastExit :: IO a -> IO a
60 runIOFastExit main = catchException main topHandlerFastExit
61         -- NB. this is used by the testsuite driver
62
63 -- | The same as 'runIO', but for non-IO computations.  Used for
64 -- wrapping @foreign export@ and @foreign import \"wrapper\"@ when these
65 -- are used to export Haskell functions with non-IO types.
66 --
67 runNonIO :: a -> IO a
68 runNonIO a = catchException (a `seq` return a) topHandler
69
70 topHandler :: Exception -> IO a
71 topHandler err = catchException (real_handler err) topHandler
72
73 -- Make sure we handle errors while reporting the error!
74 -- (e.g. evaluating the string passed to 'error' might generate
75 --  another error, etc.)
76 --
77 real_handler :: Exception -> IO a
78 real_handler ex =
79   cleanUp >>
80   case ex of
81         AsyncException StackOverflow -> do
82            reportStackOverflow
83            safeExit 2
84
85         -- only the main thread gets ExitException exceptions
86         ExitException ExitSuccess     -> safeExit 0
87         ExitException (ExitFailure n) -> safeExit n
88
89         other -> do
90            reportError other
91            safeExit 1
92            
93
94 reportStackOverflow :: IO a
95 reportStackOverflow = do callStackOverflowHook; return undefined
96
97 reportError :: Exception -> IO a
98 reportError ex = do
99    handler <- getUncaughtExceptionHandler
100    handler ex
101    return undefined
102
103 -- SUP: Are the hooks allowed to re-enter Haskell land?  If so, remove
104 -- the unsafe below.
105 foreign import ccall unsafe "stackOverflow"
106         callStackOverflowHook :: IO ()
107
108 -- try to flush stdout/stderr, but don't worry if we fail
109 -- (these handles might have errors, and we don't want to go into
110 -- an infinite loop).
111 cleanUp :: IO ()
112 cleanUp = do
113   hFlush stdout `catchException` \_ -> return ()
114   hFlush stderr `catchException` \_ -> return ()
115
116 cleanUpAndExit :: Int -> IO a
117 cleanUpAndExit r = do cleanUp; safeExit r
118
119 -- we have to use unsafeCoerce# to get the 'IO a' result type, since the
120 -- compiler doesn't let us declare that as the result type of a foreign export.
121 safeExit :: Int -> IO a
122 safeExit r = unsafeCoerce# (shutdownHaskellAndExit r)
123
124 -- NOTE: shutdownHaskellAndExit must be called "safe", because it *can*
125 -- re-enter Haskell land through finalizers.
126 foreign import ccall "shutdownHaskellAndExit" 
127   shutdownHaskellAndExit :: Int -> IO ()
128 \end{code}