prim-ref (empty) → 0.1
raw patch · 5 files changed
+255/−0 lines, 5 filesdep +basedep +ghc-primdep +primitivesetup-changed
Dependencies added: base, ghc-prim, primitive, semigroups
Files
- LICENSE +30/−0
- README.md +1/−0
- Setup.hs +2/−0
- prim-ref.cabal +28/−0
- src/Data/Primitive/PrimRef.hs +194/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Andrew Martin (c) 2017++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Andrew Martin nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,1 @@+# prim-array
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ prim-ref.cabal view
@@ -0,0 +1,28 @@+name: prim-ref+version: 0.1+synopsis: Primitive byte array with type variable+homepage: https://github.com/andrewthad/prim-array#readme+license: BSD3+license-file: LICENSE+author: Andrew Martin+maintainer: andrew.thaddeus@gmail.com+copyright: 2018 Andrew Martin+category: Web+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules:+ Data.Primitive.PrimRef+ build-depends:+ base >= 4.5 && < 5+ , primitive >= 0.6 && < 0.7+ , ghc-prim >= 0.3 && < 0.6+ , semigroups+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/andrewthad/prim-array
+ src/Data/Primitive/PrimRef.hs view
@@ -0,0 +1,194 @@+{-# LANGUAGE CPP #-}+{-# LANGUAGE MagicHash #-}+{-# LANGUAGE RoleAnnotations #-}+{-# LANGUAGE UnboxedTuples #-}+{-# LANGUAGE Unsafe #-}++-- | Note: Edward Kmett wrote everything in this module. It was sitting+-- unpackaged on github, so I took it and published it.+module Data.Primitive.PrimRef+ ( + -- * Primitive References+ PrimRef(..)+ , newPrimRef+ , newPinnedPrimRef+ , newAlignedPinnedPrimRef+ , readPrimRef+ , writePrimRef+ , primRefContents+ -- * Frozen Primitive References+ , FrozenPrimRef(..)+ , newFrozenPrimRef+ , unsafeFreezePrimRef+ , unsafeThawPrimRef+ , indexFrozenPrimRef+ , frozenPrimRefContents+ -- * Atomic Operations+ , casInt+ , fetchAddInt+ , fetchSubInt+ , fetchAndInt+ , fetchNandInt+ , fetchOrInt+ , fetchXorInt+ , atomicReadInt+ , atomicWriteInt+ ) where++import Control.Monad.Primitive+import Control.Monad.ST+import Data.Data+import Data.Primitive+import GHC.Prim+import GHC.Types (Int(I#))++--------------------------------------------------------------------------------+-- * Primitive References+--------------------------------------------------------------------------------++newtype PrimRef s a = PrimRef (MutableByteArray s)++#ifndef HLINT+type role PrimRef nominal nominal+#endif++-- | Create a primitive reference.+newPrimRef :: (PrimMonad m, Prim a) => a -> m (PrimRef (PrimState m) a)+newPrimRef a = do+ m <- newByteArray (sizeOf a)+ writeByteArray m 0 a+ return (PrimRef m)+{-# INLINE newPrimRef #-}++-- | Create a pinned primitive reference.+newPinnedPrimRef :: (PrimMonad m, Prim a) => a -> m (PrimRef (PrimState m) a)+newPinnedPrimRef a = do+ m <- newPinnedByteArray (sizeOf a)+ writeByteArray m 0 a+ return (PrimRef m)+{-# INLINE newPinnedPrimRef #-}++-- | Create a pinned primitive reference with the appropriate alignment for its contents.+newAlignedPinnedPrimRef :: (PrimMonad m, Prim a) => a -> m (PrimRef (PrimState m) a)+newAlignedPinnedPrimRef a = do+ m <- newAlignedPinnedByteArray (sizeOf a) (alignment a)+ writeByteArray m 0 a+ return (PrimRef m)+{-# INLINE newAlignedPinnedPrimRef #-}++-- | Read a primitive value from the reference+readPrimRef :: (PrimMonad m, Prim a) => PrimRef (PrimState m) a -> m a+readPrimRef (PrimRef m) = readByteArray m 0+{-# INLINE readPrimRef #-}++-- | Write a primitive value to the reference+writePrimRef :: (PrimMonad m, Prim a) => PrimRef (PrimState m) a -> a -> m ()+writePrimRef (PrimRef m) a = writeByteArray m 0 a+{-# INLINE writePrimRef #-}++instance Eq (PrimRef s a) where+ PrimRef m == PrimRef n = sameMutableByteArray m n+ {-# INLINE (==) #-}++-- | Yield a pointer to the data of a 'PrimRef'. This operation is only safe on pinned byte arrays allocated by+-- 'newPinnedPrimRef' or 'newAlignedPinnedPrimRef'.+primRefContents :: PrimRef s a -> Addr+primRefContents (PrimRef m) = mutableByteArrayContents m+{-# INLINE primRefContents #-}++--------------------------------------------------------------------------------+-- * Frozen Primitive References+--------------------------------------------------------------------------------++-- | Convert a mutable 'PrimRef' to an immutable one without copying. The reference should not be modified after the conversion.+unsafeFreezePrimRef :: PrimMonad m => PrimRef (PrimState m) a -> m (FrozenPrimRef a)+unsafeFreezePrimRef (PrimRef m) = FrozenPrimRef <$> unsafeFreezeByteArray m+{-# INLINE unsafeFreezePrimRef #-}++newtype FrozenPrimRef a = FrozenPrimRef ByteArray++#ifndef HLINT+type role FrozenPrimRef nominal+#endif++newFrozenPrimRef :: Prim a => a -> FrozenPrimRef a+newFrozenPrimRef a = runST $ newPrimRef a >>= unsafeFreezePrimRef++-- | Read the stored primitive value from the frozen reference.+indexFrozenPrimRef :: Prim a => FrozenPrimRef a -> a+indexFrozenPrimRef (FrozenPrimRef ba) = indexByteArray ba 0+{-# INLINE indexFrozenPrimRef #-}++-- | Convert an immutable primitive reference to a mutable one without copying. The original reference should not be used after the conversion.+unsafeThawPrimRef :: PrimMonad m => FrozenPrimRef a -> m (PrimRef (PrimState m) a)+unsafeThawPrimRef (FrozenPrimRef m) = PrimRef <$> unsafeThawByteArray m+{-# INLINE unsafeThawPrimRef #-}++-- | Yield a pointer to the data of a 'FrozenPrimRef'. This operation is only safe on pinned byte arrays allocated by+-- 'newPinnedPrimRef' or 'newAlignedPinnedPrimRef' and then subsequently frozen.+frozenPrimRefContents :: FrozenPrimRef a -> Addr+frozenPrimRefContents (FrozenPrimRef m) = byteArrayContents m+{-# INLINE frozenPrimRefContents #-}++--------------------------------------------------------------------------------+-- * Atomic Operations+--------------------------------------------------------------------------------++-- | 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. Implies a full memory barrier.+casInt :: PrimMonad m => PrimRef (PrimState m) Int -> Int -> Int -> m Int+casInt (PrimRef (MutableByteArray m)) (I# old) (I# new) = primitive $ \s -> case casIntArray# m 0# old new s of+ (# s', result #) -> (# s', I# result #)++-- | Given a reference, and a value to add, atomically add the value to the element. Returns the value of the element before the operation. Implies a full memory barrier.+fetchAddInt :: PrimMonad m => PrimRef (PrimState m) Int -> Int -> m Int+fetchAddInt (PrimRef (MutableByteArray m)) (I# x) = primitive $ \s -> case fetchAddIntArray# m 0# x s of+ (# s', result #) -> (# s', I# result #)++-- | Given a reference, and a value to subtract, atomically subtract the value from the element. Returns the value of the element before the operation. Implies a full memory barrier.+fetchSubInt :: PrimMonad m => PrimRef (PrimState m) Int -> Int -> m Int+fetchSubInt (PrimRef (MutableByteArray m)) (I# x) = primitive $ \s -> case fetchSubIntArray# m 0# x s of+ (# s', result #) -> (# s', I# result #)++-- | 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. Implies a full memory barrier.+fetchAndInt :: PrimMonad m => PrimRef (PrimState m) Int -> Int -> m Int+fetchAndInt (PrimRef (MutableByteArray m)) (I# x) = primitive $ \s -> case fetchAndIntArray# m 0# x s of+ (# s', result #) -> (# s', I# result #)++-- | 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. Implies a full memory barrier.+fetchNandInt :: PrimMonad m => PrimRef (PrimState m) Int -> Int -> m Int+fetchNandInt (PrimRef (MutableByteArray m)) (I# x) = primitive $ \s -> case fetchNandIntArray# m 0# x s of+ (# s', result #) -> (# s', I# result #)++-- | 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. Implies a full memory barrier.+fetchOrInt :: PrimMonad m => PrimRef (PrimState m) Int -> Int -> m Int+fetchOrInt (PrimRef (MutableByteArray m)) (I# x) = primitive $ \s -> case fetchOrIntArray# m 0# x s of+ (# s', result #) -> (# s', I# result #)++-- | 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. Implies a full memory barrier.+fetchXorInt :: PrimMonad m => PrimRef (PrimState m) Int -> Int -> m Int+fetchXorInt (PrimRef (MutableByteArray m)) (I# x) = primitive $ \s -> case fetchXorIntArray# m 0# x s of+ (# s', result #) -> (# s', I# result #)++-- | Given a reference, read an element. Implies a full memory barrier.+atomicReadInt :: PrimMonad m => PrimRef (PrimState m) Int -> m Int+atomicReadInt (PrimRef (MutableByteArray m)) = primitive $ \s -> case atomicReadIntArray# m 0# s of+ (# s', result #) -> (# s', I# result #)++-- | Given a reference, write an element. Implies a full memory barrier.+atomicWriteInt :: PrimMonad m => PrimRef (PrimState m) Int -> Int -> m ()+atomicWriteInt (PrimRef (MutableByteArray m)) (I# x) = primitive_ $ \s -> atomicWriteIntArray# m 0# x s++instance (Prim a, Data a) => Data (FrozenPrimRef a) where+ gfoldl f z m = z newFrozenPrimRef `f` indexFrozenPrimRef m+ toConstr _ = newFrozenPrimRefConstr+ gunfold k z c = case constrIndex c of+ 1 -> k (z newFrozenPrimRef)+ _ -> error "gunfold"+ dataTypeOf _ = frozenPrimRefDataType++newFrozenPrimRefConstr :: Constr+newFrozenPrimRefConstr = mkConstr frozenPrimRefDataType "newFrozenPrimRef" [] Prefix++frozenPrimRefDataType :: DataType+frozenPrimRefDataType = mkDataType "Data.Transient.Primitive.FrozenPrimRef" [newFrozenPrimRefConstr]+