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