hoare 0.1.0.0 → 0.1.1.0
raw patch · 4 files changed
+22/−13 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- hoare.cabal +1/−1
- lib/Control/Concurrent/Channel.hs +11/−8
- lib/Control/Concurrent/Channel/Endless.hs +8/−1
- lib/Control/Concurrent/TBCQueue.hs +2/−3
hoare.cabal view
@@ -11,7 +11,7 @@ `TBQueue` and `TMVar` as /channels/ between threads. - A /closeable/ @TBCQueue@ for programs where the input ends.-version: 0.1.0.0+version: 0.1.1.0 license: Zlib author: Matt Kline maintainer: matt@bitbashing.io
lib/Control/Concurrent/Channel.hs view
@@ -20,7 +20,6 @@ import Control.DeepSeq import Control.Monad --- | Any closeable STM-based pipe for inter-thread communication. class Channel c where -- | Returns @Just value@ until the channel is closed, blocking for the next value. readChannel :: c a -> STM (Maybe a)@@ -31,7 +30,7 @@ writeChannel :: c a -> a -> STM Bool -- | Closes the channel so that future writes are no-ops and readers get @Nothing@- -- once all previously-written values are read.+ -- after all values currently inside have been read. closeChannel :: c a -> STM () isClosedChannel :: c a -> STM Bool@@ -49,11 +48,12 @@ -- | Writes to the channel, asserting that it hasn't been closed. ----- Useful in situations where only the writer closes the channel.+-- Useful in situations where only the writer closes the channel (when finished). writeChannel' :: (Channel c) => c a -> a -> STM () writeChannel' c v = do o <- writeChannel c v unless o $ error "absurd: writeChannel' on closed channel"+{-# INLINE writeChannel' #-} -- | Force a value to normal form before writing it to the given channel. --@@ -64,21 +64,24 @@ evalWriteChannel c v = do v' <- evaluate $ force v atomically $ writeChannel c v'+{-# INLINE evalWriteChannel #-} -- | `writeChannel'` meets `evalWriteChannel` evalWriteChannel' :: (Channel c, NFData a) => c a -> a -> IO () evalWriteChannel' c v = do v' <- evaluate $ force v atomically $ writeChannel' c v'+{-# INLINE evalWriteChannel' #-} -- | Consume the given channel until it closes, -- passing values to the given action and collecting its results. consumeChannel :: (Channel c, Monoid m) => c a -> (a -> IO m) -> IO m-consumeChannel c f = atomically (readChannel c) >>= \case- Just v -> do- res <- f v- mappend res <$!> consumeChannel c f- Nothing -> pure mempty+consumeChannel c f = go mempty where+ go !acc = atomically (readChannel c) >>= \case+ Just v -> do+ res <- f v+ go $ acc <> res+ Nothing -> pure acc -- | Consume the given channel until it closes, -- with each action updating some state. Returns the final state.
lib/Control/Concurrent/Channel/Endless.hs view
@@ -2,7 +2,7 @@ -- with endless channels. -- -- Many useful programs have no end to their input—they run in a steady state until killed.--- This module provides the same utilities as "Control.Concurrent.Channel" (see those docs),+-- This module provides the same utilities as "Control.Concurrent.Channel", -- but for standard STM types we know and love. module Control.Concurrent.Channel.Endless( Channel (..),@@ -39,6 +39,7 @@ evalWriteChannel c v = do v' <- evaluate $ force v atomically $ writeChannel c v'+{-# INLINE evalWriteChannel #-} consumeChannel :: (Channel c) => c a -> (a -> IO ()) -> IO () consumeChannel c f = forever $ atomically (readChannel c) >>= f@@ -46,6 +47,9 @@ feedChannel :: (Channel c) => c a -> IO a -> IO () feedChannel c f = forever $ f >>= atomically . writeChannel c +-- | `feedChannel`, but forces produced values.+--+-- See `evalWriteChannel` for the motivation. evalFeedChannel :: (Channel c, NFData a) => c a -> IO a -> IO () evalFeedChannel c f = feedChannel c f' where f' = f >>= evaluate . force@@ -53,11 +57,14 @@ -- There's not much to these when you don't ever close, but for completeness: +-- | Create a channel with the given action,+-- then wire it to the given producer and consumer. pipeline :: IO c -> (c -> IO x) -> (c -> IO y) -> IO (x, y) pipeline new producer consumer = do c <- new concurrently (producer c) (consumer c) +-- | `pipeline`, but ignore the results. pipeline_ :: IO c -> (c -> IO x) -> (c -> IO y) -> IO () pipeline_ n p c = void $ pipeline n p c {-# INLINE pipeline_ #-}
lib/Control/Concurrent/TBCQueue.hs view
@@ -23,7 +23,6 @@ newTBCQueue :: Natural -> STM (TBCQueue a) newTBCQueue n = TBCQueue <$> newTBQueue n <*> newTVar True --- | @atomically . newTBCQueue@ newTBCQueueIO :: Natural -> IO (TBCQueue a) newTBCQueueIO = atomically . newTBCQueue @@ -49,7 +48,7 @@ pure stillOpen -- | Closes the queue so that future writes are no-ops and readers get @Nothing@--- once all previously-written values are read.+-- after all values currently inside have been read. closeTBCQueue :: TBCQueue a -> STM () closeTBCQueue c = writeTVar c.open False @@ -61,6 +60,6 @@ -- | The number of items currently in the queue. ----- It is obviously racy to use this value outside the current STM transaction.+-- Be careful using this outside the current STM transaction as it is obviously racy. lengthTBCQueue :: TBCQueue a -> STM Natural lengthTBCQueue c = lengthTBQueue c.q