concurrent-0

Copyright(c) Edward Kmett 2015
LicenseBSD-style
MaintainerEdward Kmett <ekmett@gmail.com>
Portabilitynon-portable
Safe HaskellUnsafe
LanguageHaskell2010

Concurrent.Primitive.Array

Contents

Description

Various sorts of primitive arrays

Synopsis

Array Primitives

sizeofArray :: Array a -> Int Source

sizeofMutableArray :: MutableArray s a -> Int Source

casArray :: PrimMonad m => MutableArray (PrimState m) a -> Int -> a -> a -> m (Int, a) Source

atomicModifyArray :: PrimMonad m => MutableArray (PrimState m) a -> Int -> (a -> (a, b)) -> m b Source

atomicModifyArray' :: PrimMonad m => MutableArray (PrimState m) a -> Int -> (a -> (a, b)) -> m b Source

modifyArray :: PrimMonad m => MutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position. Return the new result

modifyArray m i f = atomicModifyArray m i $ a -> let b = f a in (b, b)

modifyArray' :: PrimMonad m => MutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position strictly. Return the new result.

Can this be smarter? e.g. start it off already as a blackhole we appear to be evaluating, putting frames on the stack, etc. That would avoid anybody ever getting and seeing the unevaluated closure.

fetchModifyArray :: PrimMonad m => MutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position. Return the old result.

fetchModifyArray' :: PrimMonad m => MutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position strictly. Return the old result.

Capability-local array manipulation primitives

localAtomicModifyArray :: PrimMonad m => MutableArray (PrimState m) a -> Int -> (a -> (a, b)) -> m b Source

Modify the contents of a position in an array in a manner that at least can't be preempted another thread in the current capability.

localAtomicModifyArray' :: PrimMonad m => MutableArray (PrimState m) a -> Int -> (a -> (a, b)) -> m b Source

Modify the contents of a position in an array strictly in a manner that at least can't be preempted another thread in the current capability.

localModifyArray :: PrimMonad m => MutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position. Return the new result.

Logically,

localModifyArray m i f = localAtomicModifyArray m i $ a -> let b = f a in (b, b)

but it is a bit more efficient.

localModifyArray' :: PrimMonad m => MutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position strictly. Return the new result.

Can this be smarter? e.g. start it off already as a blackhole we appear to be evaluating, putting frames on the stack, etc. That would avoid anybody ever getting and seeing the unevaluated closure.

localFetchModifyArray :: PrimMonad m => MutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position. Return the old result.

localFetchModifyArray' :: PrimMonad m => MutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position strictly. Return the old result.

ByteArray Primitives

sizeOfByteArray :: ByteArray -> Int Source

sizeOfMutableByteArray :: MutableByteArray s -> Int Source

casIntArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Int -> Int -> m Int Source

atomicReadIntArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> m Int Source

atomicWriteIntArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Int -> m () Source

fetchAddIntArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Int -> m Int Source

fetchSubIntArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Int -> m Int Source

fetchAndIntArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Int -> m Int Source

fetchNandIntArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Int -> m Int Source

fetchOrIntArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Int -> m Int Source

fetchXorIntArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Int -> m Int Source

Prefetching

prefetchByteArray0 :: PrimMonad m => ByteArray -> Int -> m () Source

prefetchByteArray1 :: PrimMonad m => ByteArray -> Int -> m () Source

prefetchByteArray2 :: PrimMonad m => ByteArray -> Int -> m () Source

prefetchByteArray3 :: PrimMonad m => ByteArray -> Int -> m () Source

prefetchMutableByteArray0 :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> m () Source

prefetchMutableByteArray1 :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> m () Source

prefetchMutableByteArray2 :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> m () Source

prefetchMutableByteArray3 :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> m () Source

prefetchValue0 :: PrimMonad m => a -> m () Source

prefetchValue1 :: PrimMonad m => a -> m () Source

prefetchValue2 :: PrimMonad m => a -> m () Source

prefetchValue3 :: PrimMonad m => a -> m () Source

SmallArrays

data SmallMutableArray s a Source

Mutable boxed arrays associated with a primitive state token.

Instances

newSmallArray :: PrimMonad m => Int -> a -> m (SmallMutableArray (PrimState m) a) Source

Create a new mutable array of the specified size and initialise all elements with the given value.

readSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> m a Source

Read a value from the array at the given index.

writeSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> a -> m () Source

Write a value to the array at the given index.

indexSmallArray :: SmallArray a -> Int -> a Source

Read a value from the immutable array at the given index.

indexSmallArrayM :: Monad m => SmallArray a -> Int -> m a Source

Monadically read a value from the immutable array at the given index. This allows us to be strict in the array while remaining lazy in the read element which is very useful for collective operations. Suppose we want to copy an array. We could do something like this:

copy marr arr ... = do ...
                       writeSmallArray marr i (indexSmallArray arr i) ...
                       ...

But since primitive arrays are lazy, the calls to indexSmallArray will not be evaluated. Rather, marr will be filled with thunks each of which would retain a reference to arr. This is definitely not what we want!

With indexSmallArrayM, we can instead write

copy marr arr ... = do ...
                       x <- indexSmallArrayM arr i
                       writeSmallArray marr i x
                       ...

Now, indexing is executed immediately although the returned element is still not evaluated.

unsafeFreezeSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> m (SmallArray a) Source

Convert a mutable array to an immutable one without copying. The array should not be modified after the conversion.

unsafeThawSmallArray :: PrimMonad m => SmallArray a -> m (SmallMutableArray (PrimState m) a) Source

Convert an immutable array to an mutable one without copying. The immutable array should not be used after the conversion.

sameSmallMutableArray :: SmallMutableArray s a -> SmallMutableArray s a -> Bool Source

Check whether the two arrays refer to the same memory block.

copySmallArray Source

Arguments

:: PrimMonad m 
=> SmallMutableArray (PrimState m) a

destination array

-> Int

offset into destination array

-> SmallArray a

source array

-> Int

offset into source array

-> Int

number of elements to copy

-> m () 

Copy a slice of an immutable array to a mutable array.

copySmallMutableArray Source

Arguments

:: PrimMonad m 
=> SmallMutableArray (PrimState m) a

destination array

-> Int

offset into destination array

-> SmallMutableArray (PrimState m) a

source array

-> Int

offset into source array

-> Int

number of elements to copy

-> m () 

Copy a slice of a mutable array to another array. The two arrays may not be the same.

cloneSmallArray Source

Arguments

:: SmallArray a

source array

-> Int

offset into destination array

-> Int

number of elements to copy

-> SmallArray a 

Return a newly allocated SmallArray with the specified subrange of the provided SmallArray. The provided SmallArray should contain the full subrange specified by the two Ints, but this is not checked.

cloneSmallMutableArray Source

Arguments

:: PrimMonad m 
=> SmallMutableArray (PrimState m) a

source array

-> Int

offset into destination array

-> Int

number of elements to copy

-> m (SmallMutableArray (PrimState m) a) 

Return a newly allocated SmallMutableArray. with the specified subrange of the provided SmallMutableArray. The provided SmallMutableArray should contain the full subrange specified by the two Ints, but this is not checked.

casSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> a -> a -> m (Int, a) Source

Perform an unsafe, machine-level atomic compare and swap on an element within an array.

Atomic modification

atomicModifySmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> (a -> (a, b)) -> m b Source

atomicModifySmallArray' :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> (a -> (a, b)) -> m b Source

modifySmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position. Return the new result

modifySmallArray m i f = atomicModifySmallArray m i $ a -> let b = f a in (b, b)

modifySmallArray' :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position strictly. Return the new result.

Can this be smarter? e.g. start it off already as a blackhole we appear to be evaluating, putting frames on the stack, etc. That would avoid anybody ever getting and seeing the unevaluated closure.

fetchModifySmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position. Return the old result.

fetchModifySmallArray' :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position strictly. Return the old result.

localAtomicModifySmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> (a -> (a, b)) -> m b Source

Modify the contents of a position in an array in a manner that at least can't be preempted another thread in the current capability.

localAtomicModifySmallArray' :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> (a -> (a, b)) -> m b Source

Modify the contents of a position in an array strictly in a manner that at least can't be preempted another thread in the current capability.

localModifySmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position. Return the new result.

Logically,

localModifySmallArray m i f = localAtomicModifySmallArray m i $ a -> let b = f a in (b, b)

but it is a bit more efficient.

localModifySmallArray' :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position strictly. Return the new result.

Can this be smarter? e.g. start it off already as a blackhole we appear to be evaluating, putting frames on the stack, etc. That would avoid anybody ever getting and seeing the unevaluated closure.

localFetchModifySmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position. Return the old result.

localFetchModifySmallArray' :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> (a -> a) -> m a Source

Modify the contents of an array at a given position strictly. Return the old result.

ArrayArrays

Indexing

indexMutableArrayArray :: ArrayArray -> Int -> MutableArray s a Source

indexMutableByteArrayArray :: ArrayArray -> Int -> MutableByteArray s Source

MutableArrayArrays

cloneArrayArray Source

Arguments

:: ArrayArray

source array

-> Int

offset into destination array

-> Int

number of elements to copy

-> ArrayArray 

Return a newly allocated ArrayArray with the specified subrange of the provided ArrayArray. The provided ArrayArray should contain the full subrange specified by the two Ints, but this is not checked.

cloneMutableArrayArray Source

Arguments

:: PrimMonad m 
=> MutableArrayArray (PrimState m)

source array

-> Int

offset into destination array

-> Int

number of elements to copy

-> m (MutableArrayArray (PrimState m)) 

Return a newly allocated MutableArrayArray. with the specified subrange of the provided MutableArrayArray. The provided MutableArrayArray should contain the full subrange specified by the two Ints, but this is not checked.

Reading

readArrayArray :: MonadPrim s m => MutableArrayArray s -> Int -> m (Array a) Source

readMutableArrayArray :: MonadPrim s m => MutableArrayArray s -> Int -> m (MutableArray s a) Source

readMutableByteArrayArray :: MonadPrim s m => MutableArrayArray s -> Int -> m (MutableByteArray s) Source

Writing

writeArrayArray :: MonadPrim s m => MutableArrayArray s -> Int -> Array a -> m () Source

writeByteArrayArray :: MonadPrim s m => MutableArrayArray s -> Int -> ByteArray -> m () Source

writeMutableArrayArray :: MonadPrim s m => MutableArrayArray s -> Int -> MutableArray s a -> m () Source

writeMutableByteArrayArray :: MonadPrim s m => MutableArrayArray s -> Int -> MutableByteArray s -> m () Source

SmallArrayArrays

Indexing

SmallMutableMutableArrayArrays

newSmallArrayArray :: PrimMonad m => Int -> m (SmallMutableArrayArray (PrimState m)) Source

copySmallArrayArray Source

Arguments

:: PrimMonad m 
=> SmallMutableArrayArray (PrimState m)

destination array

-> Int

offset into destination array

-> SmallArrayArray

source array

-> Int

offset into source array

-> Int

number of elements to copy

-> m () 

Copy a slice of an immutable array to a mutable array.

copySmallMutableArrayArray Source

Arguments

:: PrimMonad m 
=> SmallMutableArrayArray (PrimState m)

destination array

-> Int

offset into destination array

-> SmallMutableArrayArray (PrimState m)

source array

-> Int

offset into source array

-> Int

number of elements to copy

-> m () 

Copy a slice of a mutable array to another array. The two arrays may not be the same.

cloneSmallArrayArray Source

Arguments

:: SmallArrayArray

source array

-> Int

offset into destination array

-> Int

number of elements to copy

-> SmallArrayArray 

Return a newly allocated SmallArray with the specified subrange of the provided SmallArray. The provided SmallArray should contain the full subrange specified by the two Ints, but this is not checked.

cloneSmallMutableArrayArray Source

Arguments

:: PrimMonad m 
=> SmallMutableArrayArray (PrimState m)

source array

-> Int

offset into destination array

-> Int

number of elements to copy

-> m (SmallMutableArrayArray (PrimState m)) 

Return a newly allocated SmallMutableArray. with the specified subrange of the provided SmallMutableArray. The provided SmallMutableArray should contain the full subrange specified by the two Ints, but this is not checked.

Reading

readArraySmallArray :: PrimMonad m => SmallMutableArrayArray (PrimState m) -> Int -> m (Array a) Source

readArrayArraySmallArray :: PrimMonad m => SmallMutableArrayArray (PrimState m) -> Int -> m ArrayArray Source

readByteArraySmallArray :: PrimMonad m => SmallMutableArrayArray (PrimState m) -> Int -> m ByteArray Source

readMutableArraySmallArray :: PrimMonad m => SmallMutableArrayArray (PrimState m) -> Int -> m (MutableArray s a) Source

readMutableByteArraySmallArray :: PrimMonad m => SmallMutableArrayArray (PrimState m) -> Int -> m (MutableByteArray s) Source

readSmallArraySmallArray :: PrimMonad m => SmallMutableArrayArray (PrimState m) -> Int -> m (SmallArray a) Source

Writing

writeArraySmallArray :: PrimMonad m => SmallMutableArrayArray (PrimState m) -> Int -> Array a -> m () Source

writeArrayArraySmallArray :: PrimMonad m => SmallMutableArrayArray (PrimState m) -> Int -> ArrayArray -> m () Source

writeByteArraySmallArray :: PrimMonad m => SmallMutableArrayArray (PrimState m) -> Int -> ByteArray -> m () Source

writeMutableArraySmallArray :: PrimMonad m => SmallMutableArrayArray (PrimState m) -> Int -> MutableArray s a -> m () Source

writeMutableByteArraySmallArray :: PrimMonad m => SmallMutableArrayArray (PrimState m) -> Int -> MutableByteArray s -> m () Source

writeSmallArraySmallArray :: PrimMonad m => SmallMutableArrayArray (PrimState m) -> Int -> SmallArray a -> m () Source

writeSmallMutableArraySmallArray :: PrimMonad m => SmallMutableArrayArray (PrimState m) -> Int -> SmallMutableArray s a -> m () Source