{-# LANGUAGE BangPatterns, MagicHash, UnboxedTuples #-}
{-# OPTIONS_GHC -O2 #-}
module GHC.Data.FastMutInt(
FastMutInt, newFastMutInt,
readFastMutInt, writeFastMutInt,
atomicFetchAddFastMut
) where
import GHC.Prelude.Basic
import GHC.Base
data FastMutInt = FastMutInt !(MutableByteArray# RealWorld)
newFastMutInt :: Int -> IO FastMutInt
newFastMutInt :: Int -> IO FastMutInt
newFastMutInt Int
n = do
FastMutInt
x <- IO FastMutInt
create
FastMutInt -> Int -> IO ()
writeFastMutInt FastMutInt
x Int
n
FastMutInt -> IO FastMutInt
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return FastMutInt
x
where
!(I# Int#
size) = Int -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize (Int
0 :: Int) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
3
create :: IO FastMutInt
create = (State# RealWorld -> (# State# RealWorld, FastMutInt #))
-> IO FastMutInt
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, FastMutInt #))
-> IO FastMutInt)
-> (State# RealWorld -> (# State# RealWorld, FastMutInt #))
-> IO FastMutInt
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case Int#
-> State# RealWorld
-> (# State# RealWorld, MutableByteArray# RealWorld #)
forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newByteArray# Int#
size State# RealWorld
s of
(# State# RealWorld
s, MutableByteArray# RealWorld
arr #) -> (# State# RealWorld
s, MutableByteArray# RealWorld -> FastMutInt
FastMutInt MutableByteArray# RealWorld
arr #)
readFastMutInt :: FastMutInt -> IO Int
readFastMutInt :: FastMutInt -> IO Int
readFastMutInt (FastMutInt MutableByteArray# RealWorld
arr) = (State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int)
-> (State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case MutableByteArray# RealWorld
-> Int# -> State# RealWorld -> (# State# RealWorld, Int# #)
forall d.
MutableByteArray# d -> Int# -> State# d -> (# State# d, Int# #)
readIntArray# MutableByteArray# RealWorld
arr Int#
0# State# RealWorld
s of
(# State# RealWorld
s, Int#
i #) -> (# State# RealWorld
s, Int# -> Int
I# Int#
i #)
writeFastMutInt :: FastMutInt -> Int -> IO ()
writeFastMutInt :: FastMutInt -> Int -> IO ()
writeFastMutInt (FastMutInt MutableByteArray# RealWorld
arr) (I# Int#
i) = (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case MutableByteArray# RealWorld
-> Int# -> Int# -> State# RealWorld -> State# RealWorld
forall d.
MutableByteArray# d -> Int# -> Int# -> State# d -> State# d
writeIntArray# MutableByteArray# RealWorld
arr Int#
0# Int#
i State# RealWorld
s of
State# RealWorld
s -> (# State# RealWorld
s, () #)
atomicFetchAddFastMut :: FastMutInt -> Int -> IO Int
atomicFetchAddFastMut :: FastMutInt -> Int -> IO Int
atomicFetchAddFastMut (FastMutInt MutableByteArray# RealWorld
arr) (I# Int#
i) = (State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int)
-> (State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case MutableByteArray# RealWorld
-> Int# -> Int# -> State# RealWorld -> (# State# RealWorld, Int# #)
forall d.
MutableByteArray# d
-> Int# -> Int# -> State# d -> (# State# d, Int# #)
fetchAddIntArray# MutableByteArray# RealWorld
arr Int#
0# Int#
i State# RealWorld
s of
(# State# RealWorld
s, Int#
n #) -> (# State# RealWorld
s, Int# -> Int
I# Int#
n #)