[project @ 2002-05-09 13:16:29 by simonmar]
[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         ( IORef               -- abstract, instance of: Eq, Typeable
17         , newIORef            -- :: a -> IO (IORef a)
18         , readIORef           -- :: IORef a -> IO a
19         , writeIORef          -- :: IORef a -> a -> IO ()
20         , modifyIORef         -- :: IORef a -> (a -> a) -> IO ()
21
22 #if !defined(__PARALLEL_HASKELL__) && defined(__GLASGOW_HASKELL__)
23         , mkWeakIORef           -- :: IORef a -> IO () -> IO (Weak (IORef a))
24 #endif
25         ) where
26
27 import Prelude
28
29 #ifdef __GLASGOW_HASKELL__
30 import GHC.Base         ( mkWeak# )
31 import GHC.STRef
32 import GHC.IOBase
33 #if !defined(__PARALLEL_HASKELL__)
34 import GHC.Weak
35 #endif
36 #endif /* __GLASGOW_HASKELL__ */
37
38 import Data.Dynamic
39
40 #if defined(__GLASGOW_HASKELL__) && !defined(__PARALLEL_HASKELL__)
41 mkWeakIORef :: IORef a -> IO () -> IO (Weak (IORef a))
42 mkWeakIORef r@(IORef (STRef r#)) f = IO $ \s ->
43   case mkWeak# r# r f s of (# s1, w #) -> (# s1, Weak w #)
44 #endif
45
46 #if defined __HUGS__
47 data IORef a        -- mutable variables containing values of type a
48
49 primitive newIORef   "newRef" :: a -> IO (IORef a)
50 primitive readIORef  "getRef" :: IORef a -> IO a
51 primitive writeIORef "setRef" :: IORef a -> a -> IO ()
52 primitive eqIORef    "eqRef"  :: IORef a -> IORef a -> Bool
53
54 instance Eq (IORef a) where
55     (==) = eqIORef
56 #endif /* __HUGS__ */
57
58 modifyIORef :: IORef a -> (a -> a) -> IO ()
59 modifyIORef ref f = writeIORef ref . f =<< readIORef ref
60
61 #include "Dynamic.h"
62 INSTANCE_TYPEABLE1(IORef,ioRefTc,"IORef")