1 {-# LANGUAGE NoImplicitPrelude, MagicHash #-}
2 {-# OPTIONS_GHC -funbox-strict-fields #-}
3 {-# OPTIONS_HADDOCK hide #-}
5 -----------------------------------------------------------------------------
8 -- Copyright : (c) The University of Glasgow 2008
9 -- License : see libraries/base/LICENSE
11 -- Maintainer : cvs-ghc@haskell.org
12 -- Stability : internal
13 -- Portability : non-portable (GHC Extensions)
17 -----------------------------------------------------------------------------
20 newIORef, readIORef, writeIORef, atomicModifyIORef
27 -- ---------------------------------------------------------------------------
30 -- |A mutable variable in the 'IO' monad
31 newtype IORef a = IORef (STRef RealWorld a)
33 -- explicit instance because Haddock can't figure out a derived one
34 instance Eq (IORef a) where
35 IORef x == IORef y = x == y
37 -- |Build a new 'IORef'
38 newIORef :: a -> IO (IORef a)
39 newIORef v = stToIO (newSTRef v) >>= \ var -> return (IORef var)
41 -- |Read the value of an 'IORef'
42 readIORef :: IORef a -> IO a
43 readIORef (IORef var) = stToIO (readSTRef var)
45 -- |Write a new value into an 'IORef'
46 writeIORef :: IORef a -> a -> IO ()
47 writeIORef (IORef var) v = stToIO (writeSTRef var v)
49 atomicModifyIORef :: IORef a -> (a -> (a,b)) -> IO b
50 atomicModifyIORef (IORef (STRef r#)) f = IO $ \s -> atomicModifyMutVar# r# f s