primitive-0.9.1.0: Primitive memory-related operations

Safe Haskell | Unsafe
Language | Haskell2010

Data.Primitive.PrimVar

Contents

- Primitive References
- Atomic Operations

Description

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

newtype PrimVar s a Source #

A `PrimVar` behaves like a single-element mutable primitive array.

Constructors

PrimVar (MutablePrimArray s a) |

#### Instances

Instances details

Eq (PrimVar s a) Source # |
Instance details Defined in Data.Primitive.PrimVar Methods (==) :: PrimVar s a -> PrimVar s a -> Bool Source # (/=) :: PrimVar s a -> PrimVar s a -> Bool Source #

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.

atomicReadInt :: PrimMonad m => PrimVar (PrimState m) Int -> m Int Source #

Given a reference, atomically read an element.

atomicWriteInt :: PrimMonad m => PrimVar (PrimState m) Int -> Int -> m () Source #

Given a reference, atomically write an element.