ring-buffer 0.4 → 0.4.1
raw patch · 2 files changed
+15/−9 lines, 2 filesdep ~QuickCheckdep ~basedep ~exceptions
Dependency ranges changed: QuickCheck, base, exceptions, mtl, primitive, vector
Files
- ring-buffer.cabal +7/−7
- src/Data/RingBuffer.hs +8/−2
ring-buffer.cabal view
@@ -1,5 +1,5 @@ name: ring-buffer-version: 0.4+version: 0.4.1 synopsis: A concurrent, mutable ring-buffer description: A mutable ring-buffer implementation suitable for concurrent access. homepage: http://github.com/bgamari/ring-buffer@@ -18,11 +18,11 @@ library exposed-modules: Data.RingBuffer- build-depends: base >=4.7 && <4.11,- vector >=0.10 && <0.13,- mtl >=2.2 && <2.3,- primitive >=0.5 && <0.7,- exceptions >=0.8 && <0.9+ build-depends: base >=4.7 && <4.21,+ vector >=0.10 && <0.14,+ mtl >=2.2 && <2.4,+ primitive >=0.5 && <0.10,+ exceptions >=0.8 && <0.11 hs-source-dirs: src default-language: Haskell2010 @@ -31,7 +31,7 @@ main-is: Properties.hs default-language: Haskell2010 build-depends: base,- QuickCheck >= 2.7 && < 2.11,+ QuickCheck >= 2.7 && < 2.16, vector, ring-buffer
src/Data/RingBuffer.hs view
@@ -17,6 +17,7 @@ import qualified Data.Vector.Generic.Mutable as VGM import Control.Applicative import Control.Concurrent+import Control.Monad (when) import Control.Monad.Catch import Control.Monad.State import Control.Monad.Reader@@ -25,8 +26,8 @@ -- | A concurrent ring buffer. data RingBuffer v a- = RingBuffer { ringBuffer :: (VG.Mutable v) (PrimState IO) a- , ringState :: MVar RingState+ = RingBuffer { ringBuffer :: !(VG.Mutable v (PrimState IO) a)+ , ringState :: !(MVar RingState) } data RingState = RingState { ringFull :: !Bool -- ^ is the ring full?@@ -53,6 +54,7 @@ cap <- capacity' let (a, pos') = (pos + n) `divMod` cap put $ RingState (full || a > 0) pos'+{-# INLINEABLE advance #-} -- | Create a new ring of a given length --@@ -74,6 +76,7 @@ s <- get liftIO $ VGM.unsafeWrite (ringBuffer rb) (ringHead s) x advance 1+{-# INLINABLE append #-} -- | Add multiple items to the end of the ring -- This ignores any items above the length of the ring@@ -95,6 +98,7 @@ dest' = VGM.take (takeN - untilWrap) $ ringBuffer rb liftIO $ VGM.copy dest' src' advance takeN+{-# INLINABLE concat #-} -- | The maximum number of items the ring can contain capacity :: (VG.Vector v a) => RingBuffer v a -> Int@@ -133,6 +137,7 @@ let idx = (hd - n - 1) `mod` cap buf <- ask liftIO $ VGM.unsafeRead buf idx+{-# INLINABLE latest' #-} -- | Get the entire contents of the ring, with the most recently added element -- at the head. Note that this is rather inefficient.@@ -140,6 +145,7 @@ toList rb = withRing rb $ do len <- length' mapM latest' [0..len-1]+{-# INLINABLE toList #-} -- | Execute the given action with the items of the ring. -- Note that no references to the vector may leak out of the action as