Warning police: Make some prototypes from the RTS known
[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 safeExit err) topHandler
72
73 topHandlerFastExit :: Exception -> IO a
74 topHandlerFastExit err = 
75   catchException (real_handler fastExit err) topHandlerFastExit
76
77 -- Make sure we handle errors while reporting the error!
78 -- (e.g. evaluating the string passed to 'error' might generate
79 --  another error, etc.)
80 --
81 real_handler :: (Int -> IO a) -> Exception -> IO a
82 real_handler exit exn =
83   cleanUp >>
84   case exn of
85         AsyncException StackOverflow -> do
86            reportStackOverflow
87            exit 2
88
89         -- only the main thread gets ExitException exceptions
90         ExitException ExitSuccess     -> exit 0
91         ExitException (ExitFailure n) -> exit n
92
93         other -> do
94            reportError other
95            exit 1
96            
97
98 reportStackOverflow :: IO a
99 reportStackOverflow = do callStackOverflowHook; return undefined
100
101 reportError :: Exception -> IO a
102 reportError ex = do
103    handler <- getUncaughtExceptionHandler
104    handler ex
105    return undefined
106
107 -- SUP: Are the hooks allowed to re-enter Haskell land?  If so, remove
108 -- the unsafe below.
109 foreign import ccall unsafe "stackOverflow"
110         callStackOverflowHook :: IO ()
111
112 -- try to flush stdout/stderr, but don't worry if we fail
113 -- (these handles might have errors, and we don't want to go into
114 -- an infinite loop).
115 cleanUp :: IO ()
116 cleanUp = do
117   hFlush stdout `catchException` \_ -> return ()
118   hFlush stderr `catchException` \_ -> return ()
119
120 cleanUpAndExit :: Int -> IO a
121 cleanUpAndExit r = do cleanUp; safeExit r
122
123 -- we have to use unsafeCoerce# to get the 'IO a' result type, since the
124 -- compiler doesn't let us declare that as the result type of a foreign export.
125 safeExit :: Int -> IO a
126 safeExit r = unsafeCoerce# (shutdownHaskellAndExit r)
127
128 -- NOTE: shutdownHaskellAndExit must be called "safe", because it *can*
129 -- re-enter Haskell land through finalizers.
130 foreign import ccall "Rts.h shutdownHaskellAndExit"
131   shutdownHaskellAndExit :: Int -> IO ()
132
133 fastExit :: Int -> IO a
134 fastExit r = unsafeCoerce# (stg_exit (fromIntegral r))
135
136 foreign import ccall "Rts.h stg_exit"
137   stg_exit :: CInt -> IO ()
138 \end{code}