Safe Haskell | Unsafe |
---|---|
Language | Haskell2010 |
Variant of MutVar
that has one less indirection for primitive types.
The difference is illustrated by comparing MutVar Int
and PrimVar Int
:
MutVar Int
:MutVar# --> I#
PrimVar Int
:MutableByteArray#
This module is adapted from a module in Edward Kmett's prim-ref
library.
Synopsis
- newtype PrimVar s a = PrimVar (MutablePrimArray s a)
- newPrimVar :: (PrimMonad m, Prim a) => a -> m (PrimVar (PrimState m) a)
- newPinnedPrimVar :: (PrimMonad m, Prim a) => a -> m (PrimVar (PrimState m) a)
- newAlignedPinnedPrimVar :: (PrimMonad m, Prim a) => a -> m (PrimVar (PrimState m) a)
- readPrimVar :: (PrimMonad m, Prim a) => PrimVar (PrimState m) a -> m a
- writePrimVar :: (PrimMonad m, Prim a) => PrimVar (PrimState m) a -> a -> m ()
- modifyPrimVar :: (PrimMonad m, Prim a) => PrimVar (PrimState m) a -> (a -> a) -> m ()
- primVarContents :: PrimVar s a -> Ptr a
- primVarToMutablePrimArray :: PrimVar s a -> MutablePrimArray s a
- casInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> Int -> m Int
- fetchAddInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m Int
- fetchSubInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m Int
- fetchAndInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m Int
- fetchNandInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m Int
- fetchOrInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m Int
- fetchXorInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m Int
- atomicReadInt :: PrimMonad m => PrimVar (PrimState m) Int -> m Int
- atomicWriteInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m ()
Primitive References
A PrimVar
behaves like a single-element mutable primitive array.
PrimVar (MutablePrimArray s a) |
newPrimVar :: (PrimMonad m, Prim a) => a -> m (PrimVar (PrimState m) a) Source #
Create a primitive reference.
newPinnedPrimVar :: (PrimMonad m, Prim a) => a -> m (PrimVar (PrimState m) a) Source #
Create a pinned primitive reference.
newAlignedPinnedPrimVar :: (PrimMonad m, Prim a) => a -> m (PrimVar (PrimState m) a) Source #
Create a pinned primitive reference with the appropriate alignment for its contents.
readPrimVar :: (PrimMonad m, Prim a) => PrimVar (PrimState m) a -> m a Source #
Read a value from the PrimVar
.
writePrimVar :: (PrimMonad m, Prim a) => PrimVar (PrimState m) a -> a -> m () Source #
Write a value to the PrimVar
.
modifyPrimVar :: (PrimMonad m, Prim a) => PrimVar (PrimState m) a -> (a -> a) -> m () Source #
Mutate the contents of a PrimVar
.
primVarContents :: PrimVar s a -> Ptr a Source #
Yield a pointer to the data of a PrimVar
. This operation is only safe on pinned byte arrays allocated by
newPinnedPrimVar
or newAlignedPinnedPrimVar
.
primVarToMutablePrimArray :: PrimVar s a -> MutablePrimArray s a Source #
Convert a PrimVar
to a one-elment MutablePrimArray
.
Atomic Operations
Atomic operations on `PrimVar s Int`. All atomic operations imply a full memory barrier.
casInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> Int -> m Int Source #
Given a primitive reference, the expected old value, and the new value, perform an atomic compare and swap i.e. write the new value if the current value matches the provided old value. Returns the value of the element before the operation.
fetchAddInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m Int Source #
Given a reference, and a value to add, atomically add the value to the element. Returns the value of the element before the operation.
fetchSubInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m Int Source #
Given a reference, and a value to subtract, atomically subtract the value from the element. Returns the value of the element before the operation.
fetchAndInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m Int Source #
Given a reference, and a value to bitwise and, atomically and the value with the element. Returns the value of the element before the operation.
fetchNandInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m Int Source #
Given a reference, and a value to bitwise nand, atomically nand the value with the element. Returns the value of the element before the operation.
fetchOrInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m Int Source #
Given a reference, and a value to bitwise or, atomically or the value with the element. Returns the value of the element before the operation.
fetchXorInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m Int Source #
Given a reference, and a value to bitwise xor, atomically xor the value with the element. Returns the value of the element before the operation.