[project @ 2002-07-16 16:08:58 by ross]
[ghc-base.git] / Data / IORef.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Data.IORef
4 -- Copyright   :  (c) The University of Glasgow 2001
5 -- License     :  BSD-style (see the file libraries/base/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  experimental
9 -- Portability :  portable
10 --
11 -- Mutable references in the IO monad.
12 --
13 -----------------------------------------------------------------------------
14
15 module Data.IORef
16   ( 
17         -- * IORefs
18         IORef,                -- abstract, instance of: Eq, Typeable
19         newIORef,             -- :: a -> IO (IORef a)
20         readIORef,            -- :: IORef a -> IO a
21         writeIORef,           -- :: IORef a -> a -> IO ()
22         modifyIORef,          -- :: IORef a -> (a -> a) -> IO ()
23
24 #if !defined(__PARALLEL_HASKELL__) && defined(__GLASGOW_HASKELL__)
25         mkWeakIORef,          -- :: IORef a -> IO () -> IO (Weak (IORef a))
26 #endif
27         ) where
28
29 import Prelude
30
31 #ifdef __HUGS__
32 import Hugs.IORef
33 #endif
34
35 #ifdef __GLASGOW_HASKELL__
36 import GHC.Base         ( mkWeak# )
37 import GHC.STRef
38 import GHC.IOBase
39 #if !defined(__PARALLEL_HASKELL__)
40 import GHC.Weak
41 #endif
42 #endif /* __GLASGOW_HASKELL__ */
43
44 import Data.Dynamic
45
46 #if defined(__GLASGOW_HASKELL__) && !defined(__PARALLEL_HASKELL__)
47 -- |Make a 'Weak' pointer to an 'IORef'
48 mkWeakIORef :: IORef a -> IO () -> IO (Weak (IORef a))
49 mkWeakIORef r@(IORef (STRef r#)) f = IO $ \s ->
50   case mkWeak# r# r f s of (# s1, w #) -> (# s1, Weak w #)
51 #endif
52
53 -- |Mutate the contents of an 'IORef'
54 modifyIORef :: IORef a -> (a -> a) -> IO ()
55 modifyIORef ref f = writeIORef ref . f =<< readIORef ref
56
57 #include "Dynamic.h"
58 INSTANCE_TYPEABLE1(IORef,ioRefTc,"IORef")