3 -- (c) The University of Glasgow 2002-2006
5 -- Unboxed mutable Ints
9 FastMutInt, newFastMutInt,
10 readFastMutInt, writeFastMutInt
13 #ifdef __GLASGOW_HASKELL__
17 #define SIZEOF_HSINT INT_SIZE_IN_BYTES
23 #else /* ! __GLASGOW_HASKELL__ */
29 newFastMutInt :: IO FastMutInt
30 readFastMutInt :: FastMutInt -> IO Int
31 writeFastMutInt :: FastMutInt -> Int -> IO ()
35 #ifdef __GLASGOW_HASKELL__
36 data FastMutInt = FastMutInt (MutableByteArray# RealWorld)
38 newFastMutInt = IO $ \s ->
39 case newByteArray# size s of { (# s, arr #) ->
40 (# s, FastMutInt arr #) }
41 where I# size = SIZEOF_HSINT
43 readFastMutInt (FastMutInt arr) = IO $ \s ->
44 case readIntArray# arr 0# s of { (# s, i #) ->
47 writeFastMutInt (FastMutInt arr) (I# i) = IO $ \s ->
48 case writeIntArray# arr 0# i s of { 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)
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)
66 readFastMutInt (FastMutInt ioRefInt) = readIORef ioRefInt
68 -- FastMutInt is strict in the value it contains.
69 writeFastMutInt (FastMutInt ioRefInt) i = i `seq` writeIORef ioRefInt i