Add Haddocks for Eq (STRef a) and Eq (IORef a)
[ghc.git] / libraries / base / GHC / IORef.hs
1 {-# LANGUAGE Unsafe #-}
2 {-# LANGUAGE NoImplicitPrelude, MagicHash #-}
3 {-# OPTIONS_GHC -funbox-strict-fields #-}
4 {-# OPTIONS_HADDOCK hide #-}
5
6 -----------------------------------------------------------------------------
7 -- |
8 -- Module : GHC.IORef
9 -- Copyright : (c) The University of Glasgow 2008
10 -- License : see libraries/base/LICENSE
11 --
12 -- Maintainer : cvs-ghc@haskell.org
13 -- Stability : internal
14 -- Portability : non-portable (GHC Extensions)
15 --
16 -- The IORef type
17 --
18 -----------------------------------------------------------------------------
19
20 module GHC.IORef (
21 IORef(..),
22 newIORef, readIORef, writeIORef, atomicModifyIORef
23 ) where
24
25 import GHC.Base
26 import GHC.STRef
27 import GHC.IO
28
29 -- ---------------------------------------------------------------------------
30 -- IORefs
31
32 -- |A mutable variable in the 'IO' monad
33 newtype IORef a = IORef (STRef RealWorld a)
34 deriving Eq
35 -- ^ Pointer equality.
36 --
37 -- @since 4.1.0.0
38
39 -- |Build a new 'IORef'
40 newIORef :: a -> IO (IORef a)
41 newIORef v = stToIO (newSTRef v) >>= \ var -> return (IORef var)
42
43 -- |Read the value of an 'IORef'
44 readIORef :: IORef a -> IO a
45 readIORef (IORef var) = stToIO (readSTRef var)
46
47 -- |Write a new value into an 'IORef'
48 writeIORef :: IORef a -> a -> IO ()
49 writeIORef (IORef var) v = stToIO (writeSTRef var v)
50
51 atomicModifyIORef :: IORef a -> (a -> (a,b)) -> IO b
52 atomicModifyIORef (IORef (STRef r#)) f = IO $ \s -> atomicModifyMutVar# r# f s
53