[project @ 2002-10-09 17:08:18 by malcolm]
[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 #ifdef __NHC__
45 import NHC.IOExtras
46     ( IORef
47     , newIORef
48     , readIORef
49     , writeIORef
50     )
51 #endif
52
53 #ifndef __NHC__
54 import Data.Dynamic
55 #endif
56
57 #if defined(__GLASGOW_HASKELL__) && !defined(__PARALLEL_HASKELL__)
58 -- |Make a 'Weak' pointer to an 'IORef'
59 mkWeakIORef :: IORef a -> IO () -> IO (Weak (IORef a))
60 mkWeakIORef r@(IORef (STRef r#)) f = IO $ \s ->
61   case mkWeak# r# r f s of (# s1, w #) -> (# s1, Weak w #)
62 #endif
63
64 -- |Mutate the contents of an 'IORef'
65 modifyIORef :: IORef a -> (a -> a) -> IO ()
66 modifyIORef ref f = writeIORef ref . f =<< readIORef ref
67
68 #ifndef __NHC__
69 #include "Dynamic.h"
70 INSTANCE_TYPEABLE1(IORef,ioRefTc,"IORef")
71 #endif