44e5de192fa8bc6707f53d9ecc12e1ef7a0d63c4
[ghc-base.git] / Data / IORef.hs
1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Data.IORef
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  experimental
10 -- Portability :  portable
11 --
12 -- Mutable references in the IO monad.
13 --
14 -----------------------------------------------------------------------------
15
16 module Data.IORef
17   ( 
18         -- * IORefs
19         IORef,                -- abstract, instance of: Eq, Typeable
20         newIORef,             -- :: a -> IO (IORef a)
21         readIORef,            -- :: IORef a -> IO a
22         writeIORef,           -- :: IORef a -> a -> IO ()
23         modifyIORef,          -- :: IORef a -> (a -> a) -> IO ()
24         atomicModifyIORef,    -- :: IORef a -> (a -> (a,b)) -> IO b
25
26 #if !defined(__PARALLEL_HASKELL__) && defined(__GLASGOW_HASKELL__)
27         mkWeakIORef,          -- :: IORef a -> IO () -> IO (Weak (IORef a))
28 #endif
29         ) where
30
31 #ifdef __HUGS__
32 import Hugs.IORef
33 #endif
34
35 #ifdef __GLASGOW_HASKELL__
36 import GHC.Base
37 import GHC.STRef
38 import GHC.IO
39 import GHC.IORef hiding (atomicModifyIORef)
40 import qualified GHC.IORef
41 #if !defined(__PARALLEL_HASKELL__)
42 import GHC.Weak
43 #endif
44 #endif /* __GLASGOW_HASKELL__ */
45
46 #ifdef __NHC__
47 import NHC.IOExtras
48     ( IORef
49     , newIORef
50     , readIORef
51     , writeIORef
52     , excludeFinalisers
53     )
54 #endif
55
56 #if defined(__GLASGOW_HASKELL__) && !defined(__PARALLEL_HASKELL__)
57 -- |Make a 'Weak' pointer to an 'IORef'
58 mkWeakIORef :: IORef a -> IO () -> IO (Weak (IORef a))
59 mkWeakIORef r@(IORef (STRef r#)) f = IO $ \s ->
60   case mkWeak# r# r f s of (# s1, w #) -> (# s1, Weak w #)
61 #endif
62
63 -- |Mutate the contents of an 'IORef'
64 modifyIORef :: IORef a -> (a -> a) -> IO ()
65 modifyIORef ref f = readIORef ref >>= writeIORef ref . f
66
67
68 -- |Atomically modifies the contents of an 'IORef'.
69 --
70 -- This function is useful for using 'IORef' in a safe way in a multithreaded
71 -- program.  If you only have one 'IORef', then using 'atomicModifyIORef' to
72 -- access and modify it will prevent race conditions.
73 --
74 -- Extending the atomicity to multiple 'IORef's is problematic, so it
75 -- is recommended that if you need to do anything more complicated
76 -- then using 'Control.Concurrent.MVar.MVar' instead is a good idea.
77 --
78 atomicModifyIORef :: IORef a -> (a -> (a,b)) -> IO b
79 #if defined(__GLASGOW_HASKELL__)
80 atomicModifyIORef = GHC.IORef.atomicModifyIORef
81
82 #elif defined(__HUGS__)
83 atomicModifyIORef = plainModifyIORef    -- Hugs has no preemption
84   where plainModifyIORef r f = do
85                 a <- readIORef r
86                 case f a of (a',b) -> writeIORef r a' >> return b
87 #elif defined(__NHC__)
88 atomicModifyIORef r f =
89   excludeFinalisers $ do
90     a <- readIORef r
91     let (a',b) = f a
92     writeIORef r a'
93     return b
94 #endif