[project @ 2001-06-28 14:15:04 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/core/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  experimental
9 -- Portability :  non-portable
10 --
11 -- $Id: IORef.hs,v 1.1 2001/06/28 14:15:02 simonmar Exp $
12 --
13 -- Mutable references in the IO monad.
14 --
15 -----------------------------------------------------------------------------
16
17 module Data.IORef
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 __GLASGOW_HASKELL__
32 import GHC.Prim         ( mkWeak# )
33 import GHC.STRef
34 import GHC.IOBase
35 #if !defined(__PARALLEL_HASKELL__)
36 import GHC.Weak
37 #endif
38 #endif /* __GLASGOW_HASKELL__ */
39
40 #ifdef __HUGS__
41 import IOExts           ( IORef, newIORef, writeIORef, readIORef )
42 import ST               ( stToIO, newSTRef, readSTRef, writeSTRef )
43 #endif
44
45 import Data.Dynamic
46
47 #ifndef __PARALLEL_HASKELL__
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 modifyIORef :: IORef a -> (a -> a) -> IO ()
54 modifyIORef ref f = writeIORef ref . f =<< readIORef ref
55
56 #include "Dynamic.h"
57 INSTANCE_TYPEABLE1(IORef,ioRefTc,"IORef")