2580df2cb0ba94aa20d79c568944002f62732b6c
[packages/old-locale.git] / Data / IORef.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Data.IORef
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
6 --
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : portable
10 --
11 -- Mutable references in the IO monad.
12 --
13 -----------------------------------------------------------------------------
14
15 module Data.IORef
16 (
17 -- * IORefs
18 IORef, -- abstract, instance of: Eq, Typeable
19 newIORef, -- :: a -> IO (IORef a)
20 readIORef, -- :: IORef a -> IO a
21 writeIORef, -- :: IORef a -> a -> IO ()
22 modifyIORef, -- :: IORef a -> (a -> a) -> IO ()
23
24 #if !defined(__PARALLEL_HASKELL__) && defined(__GLASGOW_HASKELL__)
25 mkWeakIORef, -- :: IORef a -> IO () -> IO (Weak (IORef a))
26 #endif
27 ) where
28
29 import Prelude
30
31 #ifdef __GLASGOW_HASKELL__
32 import GHC.Base ( mkWeak# )
33 import GHC.STRef
34 import GHC.IOBase
35 #if !defined(__PARALLEL_HASKELL__)
36 import GHC.Weak
37 #endif
38 #endif /* __GLASGOW_HASKELL__ */
39
40 import Data.Dynamic
41
42 #if defined(__GLASGOW_HASKELL__) && !defined(__PARALLEL_HASKELL__)
43 -- |Make a 'Weak' pointer to an 'IORef'
44 mkWeakIORef :: IORef a -> IO () -> IO (Weak (IORef a))
45 mkWeakIORef r@(IORef (STRef r#)) f = IO $ \s ->
46 case mkWeak# r# r f s of (# s1, w #) -> (# s1, Weak w #)
47 #endif
48
49 #if defined __HUGS__
50 data IORef a -- mutable variables containing values of type a
51
52 primitive newIORef "newRef" :: a -> IO (IORef a)
53 primitive readIORef "getRef" :: IORef a -> IO a
54 primitive writeIORef "setRef" :: IORef a -> a -> IO ()
55 primitive eqIORef "eqRef" :: IORef a -> IORef a -> Bool
56
57 instance Eq (IORef a) where
58 (==) = eqIORef
59 #endif /* __HUGS__ */
60
61 -- |Mutate the contents of an 'IORef'
62 modifyIORef :: IORef a -> (a -> a) -> IO ()
63 modifyIORef ref f = writeIORef ref . f =<< readIORef ref
64
65 #include "Dynamic.h"
66 INSTANCE_TYPEABLE1(IORef,ioRefTc,"IORef")