Start to actually use extensible exceptions
[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.IOBase
39 #if !defined(__PARALLEL_HASKELL__)
40 import GHC.Weak
41 #endif
42 #endif /* __GLASGOW_HASKELL__ */
43
44 #ifdef __NHC__
45 import NHC.IOExtras
46     ( IORef
47     , newIORef
48     , readIORef
49     , writeIORef
50     , excludeFinalisers
51     )
52 #endif
53
54 #if defined(__GLASGOW_HASKELL__) && !defined(__PARALLEL_HASKELL__)
55 -- |Make a 'Weak' pointer to an 'IORef'
56 mkWeakIORef :: IORef a -> IO () -> IO (Weak (IORef a))
57 mkWeakIORef r@(IORef (STRef r#)) f = IO $ \s ->
58   case mkWeak# r# r f s of (# s1, w #) -> (# s1, Weak w #)
59 #endif
60
61 -- |Mutate the contents of an 'IORef'
62 modifyIORef :: IORef a -> (a -> a) -> IO ()
63 modifyIORef ref f = readIORef ref >>= writeIORef ref . f
64
65
66 -- |Atomically modifies the contents of an 'IORef'.
67 --
68 -- This function is useful for using 'IORef' in a safe way in a multithreaded
69 -- program.  If you only have one 'IORef', then using 'atomicModifyIORef' to
70 -- access and modify it will prevent race conditions.
71 --
72 -- Extending the atomicity to multiple 'IORef's is problematic, so it
73 -- is recommended that if you need to do anything more complicated
74 -- then using 'Control.Concurrent.MVar.MVar' instead is a good idea.
75 --
76 atomicModifyIORef :: IORef a -> (a -> (a,b)) -> IO b
77 #if defined(__GLASGOW_HASKELL__)
78 atomicModifyIORef (IORef (STRef r#)) f = IO $ \s -> atomicModifyMutVar# r# f s
79
80 #elif defined(__HUGS__)
81 atomicModifyIORef = plainModifyIORef    -- Hugs has no preemption
82   where plainModifyIORef r f = do
83                 a <- readIORef r
84                 case f a of (a',b) -> writeIORef r a' >> return b
85 #elif defined(__NHC__)
86 atomicModifyIORef r f =
87   excludeFinalisers $ do
88     a <- readIORef r
89     let (a',b) = f a
90     writeIORef r a'
91     return b
92 #endif