[project @ 2002-06-05 14:08:24 by simonpj]
[ghc-base.git] / GHC / TopHandler.lhs
index bcad168..7750566 100644 (file)
@@ -1,25 +1,22 @@
--- -----------------------------------------------------------------------------
--- $Id: TopHandler.lhs,v 1.2 2001/07/31 12:51:37 simonmar Exp $
---
--- (c) The University of Glasgow, 2001
+\begin{code}
+-----------------------------------------------------------------------------
+-- |
+-- Module      :  GHC.TopHandler
+-- Copyright   :  (c) The University of Glasgow, 2001-2002
+-- License     :  see libraries/base/LICENSE
+-- 
+-- Maintainer  :  cvs-ghc@haskell.org
+-- Stability   :  internal
+-- Portability :  non-portable (GHC Extensions)
 --
--- GHC.TopHandler
+-- Top-level IO actions want to catch exceptions (e.g., 'forkIO' and 
+-- 'GHC.Main.mainIO') and report them - 'topHandler' is the exception
+-- handler they should use for this.
 --
--- 'Top-level' IO actions want to catch exceptions (e.g., forkIO and 
--- GHC.Main.mainIO) and report them - topHandler is the exception
--- handler they should use for this:
-
--- make sure we handle errors while reporting the error!
--- (e.g. evaluating the string passed to 'error' might generate
---  another error, etc.)
+-----------------------------------------------------------------------------
 
--- These functions can't go in GHC.Main, because GHC.Main isn't
--- included in HSstd.o (because GHC.Main depends on Main, which
--- doesn't exist yet...).
-
-\begin{code}
 module GHC.TopHandler (
-   topHandler, reportStackOverflow, reportError 
+   runMain, reportStackOverflow, reportError 
   ) where
 
 import Prelude
@@ -31,9 +28,17 @@ import Foreign.Ptr
 import GHC.IOBase
 import GHC.Exception
 
+-- runMain is applied to Main.main by TcModule
+runMain :: IO a -> IO ()
+runMain main = catchException (main >> return ()) topHandler
+  
 topHandler :: Exception -> IO ()
 topHandler err = catchException (real_handler err) topHandler
 
+-- Make sure we handle errors while reporting the error!
+-- (e.g. evaluating the string passed to 'error' might generate
+--  another error, etc.)
+--
 real_handler :: Exception -> IO ()
 real_handler ex =
   case ex of
@@ -43,6 +48,9 @@ real_handler ex =
        ExitException ExitSuccess     -> shutdownHaskellAndExit 0
        ExitException (ExitFailure n) -> shutdownHaskellAndExit n
 
+       Deadlock    -> reportError True 
+                       "no threads to run:  infinite loop or deadlock?"
+  
        ErrorCall s -> reportError True s
        other       -> reportError True (showsPrec 0 other "\n")
 
@@ -69,16 +77,20 @@ reportError bombOut str = do
        then stg_exit 1
         else return ()
 
-foreign label "ErrorHdrHook" errorHdrHook :: Ptr ()
+#ifndef ILX
+foreign import ccall "&ErrorHdrHook" errorHdrHook :: Ptr ()
+#else
+foreign import ccall "ErrorHdrHook" errorHdrHook :: Ptr ()
+#endif
 
-foreign import ccall "writeErrString__" unsafe
+foreign import ccall unsafe "writeErrString__"
        writeErrString :: Ptr () -> CString -> Int -> IO ()
 
 -- SUP: Are the hooks allowed to re-enter Haskell land?  If so, remove
 -- the unsafe below.
-foreign import ccall "stackOverflow" unsafe
+foreign import ccall unsafe "stackOverflow"
        callStackOverflowHook :: IO ()
 
-foreign import ccall "stg_exit" unsafe
+foreign import ccall unsafe "stg_exit"
        stg_exit :: Int -> IO ()
 \end{code}