Fix warnings in utils/FastMutInt
[ghc-hetmet.git] / compiler / utils / FastMutInt.lhs
1 {-# OPTIONS -cpp #-}
2 --
3 -- (c) The University of Glasgow 2002-2006
4 --
5 -- Unboxed mutable Ints
6
7 \begin{code}
8 module FastMutInt(
9         FastMutInt, newFastMutInt,
10         readFastMutInt, writeFastMutInt
11   ) where
12
13 #ifdef __GLASGOW_HASKELL__
14
15 #include "MachDeps.h"
16 #ifndef SIZEOF_HSINT
17 #define SIZEOF_HSINT  INT_SIZE_IN_BYTES
18 #endif
19
20 import GHC.Base
21 import GHC.IOBase
22
23 #else /* ! __GLASGOW_HASKELL__ */
24
25 import Data.IORef
26
27 #endif
28
29 newFastMutInt :: IO FastMutInt
30 readFastMutInt :: FastMutInt -> IO Int
31 writeFastMutInt :: FastMutInt -> Int -> IO ()
32 \end{code}
33
34 \begin{code}
35 #ifdef __GLASGOW_HASKELL__
36 data FastMutInt = FastMutInt (MutableByteArray# RealWorld)
37
38 newFastMutInt = IO $ \s ->
39   case newByteArray# size s of { (# s, arr #) ->
40   (# s, FastMutInt arr #) }
41   where I# size = SIZEOF_HSINT
42
43 readFastMutInt (FastMutInt arr) = IO $ \s ->
44   case readIntArray# arr 0# s of { (# s, i #) ->
45   (# s, I# i #) }
46
47 writeFastMutInt (FastMutInt arr) (I# i) = IO $ \s ->
48   case writeIntArray# arr 0# i s of { s ->
49   (# s, () #) }
50 #else /* ! __GLASGOW_HASKELL__ */
51 --maybe someday we could use
52 --http://haskell.org/haskellwiki/Library/ArrayRef
53 --which has an implementation of IOURefs
54 --that is unboxed in GHC and just strict in all other compilers...
55 newtype FastMutInt = FastMutInt (IORef Int)
56
57 -- If any default value was chosen, it surely would be 0,
58 -- so we will use that since IORef requires a default value.
59 -- Or maybe it would be more interesting to package an error,
60 -- assuming nothing relies on being able to read a bogus Int?
61 -- That could interfere with its strictness for smart optimizers
62 -- (are they allowed to optimize a 'newtype' that way?) ...
63 -- Well, maybe that can be added (in DEBUG?) later.
64 newFastMutInt = fmap FastMutInt (newIORef 0)
65
66 readFastMutInt (FastMutInt ioRefInt) = readIORef ioRefInt
67
68 -- FastMutInt is strict in the value it contains.
69 writeFastMutInt (FastMutInt ioRefInt) i = i `seq` writeIORef ioRefInt i
70 #endif
71 \end{code}
72