Update base for new Safe Haskell design
[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 module GHC.IORef (
20 IORef(..),
21 newIORef, readIORef, writeIORef, atomicModifyIORef
22 ) where
23
24 import GHC.Base
25 import GHC.STRef
26 import GHC.IO
27
28 -- ---------------------------------------------------------------------------
29 -- IORefs
30
31 -- |A mutable variable in the 'IO' monad
32 newtype IORef a = IORef (STRef RealWorld a)
33
34 -- explicit instance because Haddock can't figure out a derived one
35 instance Eq (IORef a) where
36 IORef x == IORef y = x == y
37
38 -- |Build a new 'IORef'
39 newIORef :: a -> IO (IORef a)
40 newIORef v = stToIO (newSTRef v) >>= \ var -> return (IORef var)
41
42 -- |Read the value of an 'IORef'
43 readIORef :: IORef a -> IO a
44 readIORef (IORef var) = stToIO (readSTRef var)
45
46 -- |Write a new value into an 'IORef'
47 writeIORef :: IORef a -> a -> IO ()
48 writeIORef (IORef var) v = stToIO (writeSTRef var v)
49
50 atomicModifyIORef :: IORef a -> (a -> (a,b)) -> IO b
51 atomicModifyIORef (IORef (STRef r#)) f = IO $ \s -> atomicModifyMutVar# r# f s
52