Remove -fglasgow-exts from pragmas and comments
[ghc-base.git] / System / Timeout.hs
1 -------------------------------------------------------------------------------
2 -- |
3 -- Module      :  System.Timeout
4 -- Copyright   :  (c) The University of Glasgow 2007
5 -- License     :  BSD-style (see the file libraries/base/LICENSE)
6 --
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  experimental
9 -- Portability :  non-portable
10 --
11 -- Attach a timeout event to arbitrary 'IO' computations.
12 --
13 -------------------------------------------------------------------------------
14
15 module System.Timeout ( timeout ) where
16
17 #if __NHC__
18 timeout :: Int -> IO a -> IO (Maybe a)
19 timeout n f = fmap Just f
20 #else
21
22 import Prelude             (IO, Ord((<)), Eq((==)), Int, (.), otherwise, fmap)
23 import Data.Maybe          (Maybe(..))
24 import Control.Monad       (Monad(..), guard)
25 import Control.Concurrent  (forkIO, threadDelay, myThreadId, killThread)
26 import Control.Exception   (handleJust, throwDynTo, dynExceptions, bracket)
27 import Data.Dynamic        (Typeable, fromDynamic)
28 import Data.Unique         (Unique, newUnique)
29
30 -- An internal type that is thrown as a dynamic exception to
31 -- interrupt the running IO computation when the timeout has
32 -- expired.
33
34 data Timeout = Timeout Unique deriving (Eq, Typeable)
35
36 -- |Wrap an 'IO' computation to time out and return @Nothing@ in case no result
37 -- is available within @n@ microseconds (@1\/10^6@ seconds). In case a result
38 -- is available before the timeout expires, @Just a@ is returned. A negative
39 -- timeout interval means \"wait indefinitely\". When specifying long timeouts,
40 -- be careful not to exceed @maxBound :: Int@.
41 --
42 -- The design of this combinator was guided by the objective that @timeout n f@
43 -- should behave exactly the same as @f@ as long as @f@ doesn't time out. This
44 -- means that @f@ has the same 'myThreadId' it would have without the timeout
45 -- wrapper. Any exceptions @f@ might throw cancel the timeout and propagate
46 -- further up. It also possible for @f@ to receive exceptions thrown to it by
47 -- another thread.
48 --
49 -- A tricky implementation detail is the question of how to abort an @IO@
50 -- computation. This combinator relies on asynchronous exceptions internally.
51 -- The technique works very well for computations executing inside of the
52 -- Haskell runtime system, but it doesn't work at all for non-Haskell code.
53 -- Foreign function calls, for example, cannot be timed out with this
54 -- combinator simply because an arbitrary C function cannot receive
55 -- asynchronous exceptions. When @timeout@ is used to wrap an FFI call that
56 -- blocks, no timeout event can be delivered until the FFI call returns, which
57 -- pretty much negates the purpose of the combinator. In practice, however,
58 -- this limitation is less severe than it may sound. Standard I\/O functions
59 -- like 'System.IO.hGetBuf', 'System.IO.hPutBuf', Network.Socket.accept, or
60 -- 'System.IO.hWaitForInput' appear to be blocking, but they really don't
61 -- because the runtime system uses scheduling mechanisms like @select(2)@ to
62 -- perform asynchronous I\/O, so it is possible to interrupt standard socket
63 -- I\/O or file I\/O using this combinator.
64
65 timeout :: Int -> IO a -> IO (Maybe a)
66 timeout n f
67     | n <  0    = fmap Just f
68     | n == 0    = return Nothing
69     | otherwise = do
70         pid <- myThreadId
71         ex  <- fmap Timeout newUnique
72         handleJust (\e -> dynExceptions e >>= fromDynamic >>= guard . (ex ==))
73                    (\_ -> return Nothing)
74                    (bracket (forkIO (threadDelay n >> throwDynTo pid ex))
75                             (killThread)
76                             (\_ -> fmap Just f))
77 #endif