52ecb16cdd3a5d386e18af2f7c1ccd911e62f610
[ghc-hetmet.git] / ghc / lib / exts / IOExts.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1996
3 %
4
5 \section[IOExts]{Module @IOExts@}
6
7 \begin{code}
8 {-# OPTIONS -fno-implicit-prelude #-}
9
10 module IOExts
11         ( fixIO
12         , unsafePerformIO
13         , unsafeInterleaveIO
14
15         , IORef
16           -- instance Eq (IORef a)
17         , newIORef
18         , readIORef
19         , writeIORef
20
21         , IOArray
22           -- instance Eq (IOArray ix a)
23         , newIOArray
24         , boundsIOArray
25         , readIOArray
26         , writeIOArray
27         , freezeIOArray
28         
29         , openFileEx
30         , IOModeEx(..)
31
32         , trace
33         , performGC
34         
35         , reallyUnsafePtrEq
36         ) where
37 \end{code}
38
39 \begin{code}
40 import PrelBase
41 import PrelIOBase
42 import PrelHandle ( openFileEx, IOModeEx(..) )
43 import PrelST
44 import PrelUnsafe
45 import PrelArr
46 import PrelGHC
47 import Ix
48
49 reallyUnsafePtrEq a b =
50     case reallyUnsafePtrEquality# a b of
51          0# -> False
52          _  -> True
53 \end{code}
54
55 \begin{code}
56 newtype IORef a = IORef (MutableVar RealWorld a) 
57     deriving Eq
58
59 newIORef :: a -> IO (IORef a)
60 newIORef v = stToIO (newVar v) >>= \ var -> return (IORef var)
61
62 readIORef :: IORef a -> IO a
63 readIORef (IORef var) = stToIO (readVar var)
64
65 writeIORef :: IORef a -> a -> IO ()
66 writeIORef (IORef var) v = stToIO (writeVar var v)
67 \end{code}
68
69 \begin{code}
70 newtype IOArray ix elt = IOArray (MutableArray RealWorld ix elt)
71     deriving Eq
72
73 newIOArray          :: Ix ix => (ix,ix) -> elt -> IO (IOArray ix elt)
74 boundsIOArray       :: Ix ix => IOArray ix elt -> (ix, ix)
75 readIOArray         :: Ix ix => IOArray ix elt -> ix -> IO elt
76 writeIOArray        :: Ix ix => IOArray ix elt -> ix -> elt -> IO ()
77 freezeIOArray       :: Ix ix => IOArray ix elt -> IO (Array ix elt)
78
79 newIOArray ixs elt = 
80     stToIO (newArray ixs elt) >>= \arr -> 
81     return (IOArray arr)
82
83 boundsIOArray (IOArray arr) = boundsOfArray arr
84
85 readIOArray (IOArray arr) ix = stToIO (readArray arr ix)
86
87 writeIOArray (IOArray arr) ix elt = stToIO (writeArray arr ix elt)
88
89 freezeIOArray (IOArray arr) = stToIO (freezeArray arr)
90 \end{code}
91