X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=System%2FTimeout.hs;h=df33625c8d4cca3f8b51052c4c87492481d4cd5f;hb=4475dcabbc206d1cf0fc3fee88f600a4791d948c;hp=dbf92099c27db28fcba6ee5f047243cf5257425b;hpb=9998ccb135e022aee442d32bf1f9e723a2256887;p=ghc-base.git diff --git a/System/Timeout.hs b/System/Timeout.hs index dbf9209..df33625 100644 --- a/System/Timeout.hs +++ b/System/Timeout.hs @@ -1,4 +1,8 @@ -{-# OPTIONS -fglasgow-exts #-} +{-# LANGUAGE CPP #-} +#ifdef __GLASGOW_HASKELL__ +{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-} +#endif + ------------------------------------------------------------------------------- -- | -- Module : System.Timeout @@ -13,26 +17,34 @@ -- ------------------------------------------------------------------------------- -module System.Timeout ( timeout ) where +#ifdef __GLASGOW_HASKELL__ +#include "Typeable.h" +#endif -#if __NHC__ -timeout :: Int -> IO a -> IO (Maybe a) -timeout n f = fmap Just f -#else +module System.Timeout ( timeout ) where -import Prelude (IO, Ord((<)), Eq((==)), Int, (.), otherwise, fmap) +#ifdef __GLASGOW_HASKELL__ +import Prelude (Show(show), IO, Ord((<)), Eq((==)), Int, + otherwise, fmap) import Data.Maybe (Maybe(..)) -import Control.Monad (Monad(..), guard) +import Control.Monad (Monad(..)) import Control.Concurrent (forkIO, threadDelay, myThreadId, killThread) -import Control.Exception (handleJust, throwDynTo, dynExceptions, bracket) -import Data.Dynamic (Typeable, fromDynamic) +import Control.Exception (Exception, handleJust, throwTo, bracket) +import Data.Typeable import Data.Unique (Unique, newUnique) -- An internal type that is thrown as a dynamic exception to -- interrupt the running IO computation when the timeout has -- expired. -data Timeout = Timeout Unique deriving (Eq, Typeable) +newtype Timeout = Timeout Unique deriving Eq +INSTANCE_TYPEABLE0(Timeout,timeoutTc,"Timeout") + +instance Show Timeout where + show _ = "<>" + +instance Exception Timeout +#endif /* !__GLASGOW_HASKELL__ */ -- |Wrap an 'IO' computation to time out and return @Nothing@ in case no result -- is available within @n@ microseconds (@1\/10^6@ seconds). In case a result @@ -64,15 +76,18 @@ data Timeout = Timeout Unique deriving (Eq, Typeable) -- I\/O or file I\/O using this combinator. timeout :: Int -> IO a -> IO (Maybe a) +#ifdef __GLASGOW_HASKELL__ timeout n f | n < 0 = fmap Just f | n == 0 = return Nothing | otherwise = do pid <- myThreadId ex <- fmap Timeout newUnique - handleJust (\e -> dynExceptions e >>= fromDynamic >>= guard . (ex ==)) + handleJust (\e -> if e == ex then Just () else Nothing) (\_ -> return Nothing) - (bracket (forkIO (threadDelay n >> throwDynTo pid ex)) + (bracket (forkIO (threadDelay n >> throwTo pid ex)) (killThread) (\_ -> fmap Just f)) -#endif +#else +timeout n f = fmap Just f +#endif /* !__GLASGOW_HASKELL__ */