1 -----------------------------------------------------------------------------
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/core/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : portable
11 -- $Id: IORef.hs,v 1.4 2002/01/02 14:40:09 simonmar Exp $
13 -- Mutable references in the IO monad.
15 -----------------------------------------------------------------------------
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 ()
24 #if !defined(__PARALLEL_HASKELL__) && defined(__GLASGOW_HASKELL__)
25 , mkWeakIORef -- :: IORef a -> IO () -> IO (Weak (IORef a))
31 #ifdef __GLASGOW_HASKELL__
32 import GHC.Base ( mkWeak# )
35 #if !defined(__PARALLEL_HASKELL__)
38 #endif /* __GLASGOW_HASKELL__ */
42 #if defined(__GLASGOW_HASKELL__) && !defined(__PARALLEL_HASKELL__)
43 mkWeakIORef :: IORef a -> IO () -> IO (Weak (IORef a))
44 mkWeakIORef r@(IORef (STRef r#)) f = IO $ \s ->
45 case mkWeak# r# r f s of (# s1, w #) -> (# s1, Weak w #)
49 data IORef a -- mutable variables containing values of type a
51 primitive newIORef "newRef" :: a -> IO (IORef a)
52 primitive readIORef "getRef" :: IORef a -> IO a
53 primitive writeIORef "setRef" :: IORef a -> a -> IO ()
54 primitive eqIORef "eqRef" :: IORef a -> IORef a -> Bool
56 instance Eq (IORef a) where
60 modifyIORef :: IORef a -> (a -> a) -> IO ()
61 modifyIORef ref f = writeIORef ref . f =<< readIORef ref
64 INSTANCE_TYPEABLE1(IORef,ioRefTc,"IORef")