module Concurrent.Capability.LocalArray
(
LocalArray
, newLocalArray
, readLocalArray
, writeLocalArray
, atomicModifyLocalArray
, atomicModifyLocalArray'
, modifyLocalArray
, modifyLocalArray'
, fetchModifyLocalArray
, fetchModifyLocalArray'
, SmallLocalArray
, newSmallLocalArray
, readSmallLocalArray
, writeSmallLocalArray
, atomicModifySmallLocalArray
, atomicModifySmallLocalArray'
, modifySmallLocalArray
, modifySmallLocalArray'
, fetchModifySmallLocalArray
, fetchModifySmallLocalArray'
) where
import Concurrent.Capability.Pinned
import Concurrent.Internal.Util
import Concurrent.Primitive
import Control.Concurrent
import Control.Monad.Primitive
import Data.Primitive
import Data.Tagged
newtype LocalArray a = LocalArray SmallArrayArray
newLocalArray :: MonadPrimIO m => Int -> a -> m (LocalArray a)
newLocalArray n a = primIO $ do
cs <- getNumCapabilities
r <- newSmallArrayArray cs
forN_ 0 cs $ \i -> do
m <- newArray n a
writeMutableArraySmallArray r i m
LocalArray <$> unsafeFreezeSmallArrayArray r
reflectLocalArray :: ReifiesCapability s => LocalArray a -> Tagged s (MutableArray RealWorld a)
reflectLocalArray (LocalArray m) = indexMutableArraySmallArray m <$> reflectCapability
withLocalArray :: forall m a r. ReifiesCapability m => LocalArray a -> (MutableArray RealWorld a -> m r) -> m r
withLocalArray l f = f $ untag (reflectLocalArray l :: Tagged m (MutableArray RealWorld a))
readLocalArray :: forall m a. (MonadPrimIO m, ReifiesCapability m) => LocalArray a -> Int -> m a
readLocalArray l i = withLocalArray l $ \arr -> readArray arr i
writeLocalArray :: (MonadPrimIO m, ReifiesCapability m) => LocalArray a -> Int -> a -> m ()
writeLocalArray l i a = withLocalArray l $ \arr -> writeArray arr i a
atomicModifyLocalArray :: (MonadPrimIO m, ReifiesCapability m) => LocalArray a -> Int -> (a -> (a, b)) -> m b
atomicModifyLocalArray l i f = withLocalArray l $ \arr -> localAtomicModifyArray arr i f
atomicModifyLocalArray' :: (MonadPrimIO m, ReifiesCapability m) => LocalArray a -> Int -> (a -> (a, b)) -> m b
atomicModifyLocalArray' l i f = withLocalArray l $ \arr -> localAtomicModifyArray' arr i f
modifyLocalArray :: (MonadPrimIO m, ReifiesCapability m) => LocalArray a -> Int -> (a -> a) -> m a
modifyLocalArray l i f = withLocalArray l $ \arr -> localModifyArray arr i f
modifyLocalArray' :: (MonadPrimIO m, ReifiesCapability m) => LocalArray a -> Int -> (a -> a) -> m a
modifyLocalArray' l i f = withLocalArray l $ \arr -> localModifyArray' arr i f
fetchModifyLocalArray :: (MonadPrimIO m, ReifiesCapability m) => LocalArray a -> Int -> (a -> a) -> m a
fetchModifyLocalArray l i f = withLocalArray l $ \arr -> localFetchModifyArray arr i f
fetchModifyLocalArray' :: (MonadPrimIO m, ReifiesCapability m) => LocalArray a -> Int -> (a -> a) -> m a
fetchModifyLocalArray' l i f = withLocalArray l $ \arr -> localFetchModifyArray' arr i f
newtype SmallLocalArray a = SmallLocalArray SmallArrayArray
newSmallLocalArray :: MonadPrimIO m => Int -> a -> m (SmallLocalArray a)
newSmallLocalArray n a = primIO $ do
cs <- getNumCapabilities
r <- newSmallArrayArray cs
forN_ 0 cs $ \i -> do
m <- newSmallArray n a
writeSmallMutableArraySmallArray r i m
SmallLocalArray <$> unsafeFreezeSmallArrayArray r
reflectSmallLocalArray :: ReifiesCapability s => SmallLocalArray a -> Tagged s (SmallMutableArray RealWorld a)
reflectSmallLocalArray (SmallLocalArray m) = indexSmallMutableArraySmallArray m <$> reflectCapability
withSmallLocalArray :: forall m a r. ReifiesCapability m => SmallLocalArray a -> (SmallMutableArray RealWorld a -> m r) -> m r
withSmallLocalArray l f = f $ untag (reflectSmallLocalArray l :: Tagged m (SmallMutableArray RealWorld a))
readSmallLocalArray :: forall m a. (MonadPrimIO m, ReifiesCapability m) => SmallLocalArray a -> Int -> m a
readSmallLocalArray l i = withSmallLocalArray l $ \arr -> readSmallArray arr i
writeSmallLocalArray :: (MonadPrimIO m, ReifiesCapability m) => SmallLocalArray a -> Int -> a -> m ()
writeSmallLocalArray l i a = withSmallLocalArray l $ \arr -> writeSmallArray arr i a
atomicModifySmallLocalArray :: (MonadPrimIO m, ReifiesCapability m) => SmallLocalArray a -> Int -> (a -> (a, b)) -> m b
atomicModifySmallLocalArray l i f = withSmallLocalArray l $ \arr -> localAtomicModifySmallArray arr i f
atomicModifySmallLocalArray' :: (MonadPrimIO m, ReifiesCapability m) => SmallLocalArray a -> Int -> (a -> (a, b)) -> m b
atomicModifySmallLocalArray' l i f = withSmallLocalArray l $ \arr -> localAtomicModifySmallArray' arr i f
modifySmallLocalArray :: (MonadPrimIO m, ReifiesCapability m) => SmallLocalArray a -> Int -> (a -> a) -> m a
modifySmallLocalArray l i f = withSmallLocalArray l $ \arr -> localModifySmallArray arr i f
modifySmallLocalArray' :: (MonadPrimIO m, ReifiesCapability m) => SmallLocalArray a -> Int -> (a -> a) -> m a
modifySmallLocalArray' l i f = withSmallLocalArray l $ \arr -> localModifySmallArray' arr i f
fetchModifySmallLocalArray :: (MonadPrimIO m, ReifiesCapability m) => SmallLocalArray a -> Int -> (a -> a) -> m a
fetchModifySmallLocalArray l i f = withSmallLocalArray l $ \arr -> localFetchModifySmallArray arr i f
fetchModifySmallLocalArray' :: (MonadPrimIO m, ReifiesCapability m) => SmallLocalArray a -> Int -> (a -> a) -> m a
fetchModifySmallLocalArray' l i f = withSmallLocalArray l $ \arr -> localFetchModifySmallArray' arr i f