Copyright | (c) Dong Han 2017~2019 |
---|---|
License | BSD-style |
Maintainer | winterland1989@gmail.com |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
This package provide fast primitive references for primitive monad, such as ST or IO. Unboxed reference is implemented using single cell MutableByteArray/MutableUnliftedArray
s to eliminate indirection overhead which MutVar# s a carry, on the otherhand primitive reference only support limited type(instances of 'Prim/PrimUnlifted' class).
Synopsis
- newtype PrimRef s a = PrimRef (MutableByteArray s)
- type PrimIORef a = PrimRef RealWorld a
- newPrimRef :: (Prim a, PrimMonad m) => a -> m (PrimRef (PrimState m) a)
- readPrimRef :: (Prim a, PrimMonad m) => PrimRef (PrimState m) a -> m a
- writePrimRef :: (Prim a, PrimMonad m) => PrimRef (PrimState m) a -> a -> m ()
- modifyPrimRef :: (Prim a, PrimMonad m) => PrimRef (PrimState m) a -> (a -> a) -> m ()
- class Prim a where
- sizeOf# :: a -> Int#
- alignment# :: a -> Int#
- indexByteArray# :: ByteArray# -> Int# -> a
- readByteArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, a #)
- writeByteArray# :: MutableByteArray# s -> Int# -> a -> State# s -> State# s
- setByteArray# :: MutableByteArray# s -> Int# -> Int# -> a -> State# s -> State# s
- indexOffAddr# :: Addr# -> Int# -> a
- readOffAddr# :: Addr# -> Int# -> State# s -> (# State# s, a #)
- writeOffAddr# :: Addr# -> Int# -> a -> State# s -> State# s
- setOffAddr# :: Addr# -> Int# -> Int# -> a -> State# s -> State# s
- newtype UnliftedRef s a = UnliftedRef (MutableUnliftedArray s a)
- newUnliftedRef :: (PrimUnlifted a, PrimMonad m) => a -> m (UnliftedRef (PrimState m) a)
- readUnliftedRef :: (PrimUnlifted a, PrimMonad m) => UnliftedRef (PrimState m) a -> m a
- writeUnliftedRef :: (PrimUnlifted a, PrimMonad m) => UnliftedRef (PrimState m) a -> a -> m ()
- modifyUnliftedRef :: (PrimUnlifted a, PrimMonad m) => UnliftedRef (PrimState m) a -> (a -> a) -> m ()
- class PrimUnlifted a where
- writeUnliftedArray# :: MutableArrayArray# s -> Int# -> a -> State# s -> State# s
- readUnliftedArray# :: MutableArrayArray# s -> Int# -> State# s -> (# State# s, a #)
- indexUnliftedArray# :: ArrayArray# -> Int# -> a
- type Counter = PrimRef RealWorld Int
- newCounter :: Int -> IO Counter
- readCounter :: Counter -> IO Int
- writeCounter :: Counter -> Int -> IO ()
- modifyCounter :: Counter -> (Int -> Int) -> IO ()
- atomicAddCounter :: Counter -> Int -> IO Int
- atomicSubCounter :: Counter -> Int -> IO Int
- atomicAndCounter :: Counter -> Int -> IO Int
- atomicNandCounter :: Counter -> Int -> IO Int
- atomicOrCounter :: Counter -> Int -> IO Int
- atomicXorCounter :: Counter -> Int -> IO Int
- atomicAddCounter' :: Counter -> Int -> IO Int
- atomicSubCounter' :: Counter -> Int -> IO Int
- atomicAndCounter' :: Counter -> Int -> IO Int
- atomicNandCounter' :: Counter -> Int -> IO Int
- atomicOrCounter' :: Counter -> Int -> IO Int
- atomicXorCounter' :: Counter -> Int -> IO Int
- atomicAddCounter_ :: Counter -> Int -> IO ()
- atomicSubCounter_ :: Counter -> Int -> IO ()
- atomicAndCounter_ :: Counter -> Int -> IO ()
- atomicNandCounter_ :: Counter -> Int -> IO ()
- atomicOrCounter_ :: Counter -> Int -> IO ()
- atomicXorCounter_ :: Counter -> Int -> IO ()
Prim references
readPrimRef :: (Prim a, PrimMonad m) => PrimRef (PrimState m) a -> m a Source #
Read the value of an PrimRef
writePrimRef :: (Prim a, PrimMonad m) => PrimRef (PrimState m) a -> a -> m () Source #
Write a new value into an PrimRef
modifyPrimRef :: (Prim a, PrimMonad m) => PrimRef (PrimState m) a -> (a -> a) -> m () Source #
Mutate the contents of an PrimRef
.
Unboxed reference is always strict on the value it hold.
Class of types supporting primitive array operations. This includes
interfacing with GC-managed memory (functions suffixed with ByteArray#
)
and interfacing with unmanaged memory (functions suffixed with Addr#
).
Endianness is platform-dependent.
Size of values of type a
. The argument is not used.
alignment# :: a -> Int# #
Alignment of values of type a
. The argument is not used.
indexByteArray# :: ByteArray# -> Int# -> a #
Read a value from the array. The offset is in elements of type
a
rather than in bytes.
readByteArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, a #) #
Read a value from the mutable array. The offset is in elements of type
a
rather than in bytes.
writeByteArray# :: MutableByteArray# s -> Int# -> a -> State# s -> State# s #
Write a value to the mutable array. The offset is in elements of type
a
rather than in bytes.
setByteArray# :: MutableByteArray# s -> Int# -> Int# -> a -> State# s -> State# s #
Fill a slice of the mutable array with a value. The offset and length
of the chunk are in elements of type a
rather than in bytes.
indexOffAddr# :: Addr# -> Int# -> a #
Read a value from a memory position given by an address and an offset.
The memory block the address refers to must be immutable. The offset is in
elements of type a
rather than in bytes.
readOffAddr# :: Addr# -> Int# -> State# s -> (# State# s, a #) #
Read a value from a memory position given by an address and an offset.
The offset is in elements of type a
rather than in bytes.
writeOffAddr# :: Addr# -> Int# -> a -> State# s -> State# s #
Write a value to a memory position given by an address and an offset.
The offset is in elements of type a
rather than in bytes.
setOffAddr# :: Addr# -> Int# -> Int# -> a -> State# s -> State# s #
Fill a memory block given by an address, an offset and a length.
The offset and length are in elements of type a
rather than in bytes.
Instances
Unlifted references
newtype UnliftedRef s a Source #
A mutable variable in the PrimMonad
which can hold an instance of PrimUnlifted
.
newUnliftedRef :: (PrimUnlifted a, PrimMonad m) => a -> m (UnliftedRef (PrimState m) a) Source #
Build a new UnliftedRef
readUnliftedRef :: (PrimUnlifted a, PrimMonad m) => UnliftedRef (PrimState m) a -> m a Source #
Read the value of an UnliftedRef
writeUnliftedRef :: (PrimUnlifted a, PrimMonad m) => UnliftedRef (PrimState m) a -> a -> m () Source #
Write a new value into an UnliftedRef
modifyUnliftedRef :: (PrimUnlifted a, PrimMonad m) => UnliftedRef (PrimState m) a -> (a -> a) -> m () Source #
Mutate the contents of an UnliftedRef
.
Unlifted reference is always strict on the value it hold.
class PrimUnlifted a where Source #
Types with TYPE
UnliftedRep
, which can be stored / retrieved in ArrayArray#
.
writeUnliftedArray# :: MutableArrayArray# s -> Int# -> a -> State# s -> State# s Source #
readUnliftedArray# :: MutableArrayArray# s -> Int# -> State# s -> (# State# s, a #) Source #
indexUnliftedArray# :: ArrayArray# -> Int# -> a Source #
Instances
Atomic operations for PrimIORef Int
type Counter = PrimRef RealWorld Int Source #
Alias for 'PrimIORef Int' which support several atomic operations.
modifyCounter :: Counter -> (Int -> Int) -> IO () Source #
Mutate the contents of an Counter
(non-atomically).
return value BEFORE atomic operation
atomicAddCounter :: Counter -> Int -> IO Int Source #
Atomically add a Counter
, return the value BEFORE added.
atomicSubCounter :: Counter -> Int -> IO Int Source #
Atomically sub a Counter
, return the value BEFORE subbed.
atomicAndCounter :: Counter -> Int -> IO Int Source #
Atomically and a Counter
, return the value BEFORE anded.
atomicNandCounter :: Counter -> Int -> IO Int Source #
Atomically nand a Counter
, return the value BEFORE nanded.
atomicOrCounter :: Counter -> Int -> IO Int Source #
Atomically or a Counter
, return the value BEFORE ored.
atomicXorCounter :: Counter -> Int -> IO Int Source #
Atomically xor a Counter
, return the value BEFORE xored.
return value AFTER atomic operation
atomicAddCounter' :: Counter -> Int -> IO Int Source #
Atomically add a Counter
, return the value AFTER added.
atomicSubCounter' :: Counter -> Int -> IO Int Source #
Atomically sub a Counter
, return the value AFTER subbed.
atomicAndCounter' :: Counter -> Int -> IO Int Source #
Atomically and a Counter
, return the value AFTER anded.
atomicNandCounter' :: Counter -> Int -> IO Int Source #
Atomically nand a Counter
, return the value AFTER nanded.
atomicOrCounter' :: Counter -> Int -> IO Int Source #
Atomically or a Counter
, return the value AFTER ored.
atomicXorCounter' :: Counter -> Int -> IO Int Source #
Atomically xor a Counter
, return the value AFTER xored.