hoare 0.1.1.0 → 0.2.0.0
raw patch · 11 files changed
+802/−38 lines, 11 filesdep +clockdep ~basePVP ok
version bump matches the API change (PVP)
Dependencies added: clock
Dependency ranges changed: base
API changes (from Hackage documentation)
+ Control.Concurrent.Channel: Closed :: OverwriteResult a
+ Control.Concurrent.Channel: Overwrote :: a -> OverwriteResult a
+ Control.Concurrent.Channel: Wrote :: OverwriteResult a
+ Control.Concurrent.Channel: consumeChannelBatches :: Channel c => c a -> ([a] -> IO ()) -> IO ()
+ Control.Concurrent.Channel: consumeChannelBatches' :: Channel c => c a -> (a -> IO ()) -> IO ()
+ Control.Concurrent.Channel: data OverwriteResult a
+ Control.Concurrent.Channel: drainChannel :: Channel c => c a -> STM [a]
+ Control.Concurrent.Channel: evalOverwriteChannel :: (Channel c, NFData a) => c a -> a -> IO (OverwriteResult a)
+ Control.Concurrent.Channel: evalOverwriteChannel' :: (Channel c, NFData a) => c a -> a -> IO (Maybe a)
+ Control.Concurrent.Channel: instance Control.Concurrent.Channel.Channel Control.Concurrent.TCSlot.TCSlot
+ Control.Concurrent.Channel: isEmptyChannel :: Channel c => c a -> STM Bool
+ Control.Concurrent.Channel: isFullChannel :: Channel c => c a -> STM Bool
+ Control.Concurrent.Channel: overwriteChannel :: Channel c => c a -> a -> STM (OverwriteResult a)
+ Control.Concurrent.Channel: overwriteChannel' :: Channel c => c a -> a -> STM (Maybe a)
+ Control.Concurrent.Channel: stateConsumeChannelBatches :: Channel c => c a -> s -> (s -> [a] -> IO s) -> IO s
+ Control.Concurrent.Channel: stateConsumeChannelBatches' :: Channel c => c a -> s -> (s -> a -> IO s) -> IO s
+ Control.Concurrent.Channel.Endless: consumeChannelBatches :: Channel c => c a -> ([a] -> IO ()) -> IO ()
+ Control.Concurrent.Channel.Endless: consumeChannelBatches' :: Channel c => c a -> (a -> IO ()) -> IO ()
+ Control.Concurrent.Channel.Endless: drainChannel :: Channel c => c a -> STM [a]
+ Control.Concurrent.Channel.Endless: evalOverwriteChannel :: (Channel c, NFData a) => c a -> a -> IO (Maybe a)
+ Control.Concurrent.Channel.Endless: overwriteChannel :: Channel c => c a -> a -> STM (Maybe a)
+ Control.Concurrent.Channel.Endless: stateConsumeChannel :: Channel c => c a -> s -> (s -> a -> IO s) -> IO ()
+ Control.Concurrent.Channel.Endless: stateConsumeChannelBatches :: Channel c => c a -> s -> (s -> [a] -> IO s) -> IO ()
+ Control.Concurrent.Channel.Endless: stateConsumeChannelBatches' :: Channel c => c a -> s -> (s -> a -> IO s) -> IO ()
+ Control.Concurrent.Channel.Endless.Split: Reader :: c a -> Reader (c :: k -> Type) (a :: k)
+ Control.Concurrent.Channel.Endless.Split: Writer :: c a -> Writer (c :: k -> Type) (a :: k)
+ Control.Concurrent.Channel.Endless.Split: [unReader] :: Reader (c :: k -> Type) (a :: k) -> c a
+ Control.Concurrent.Channel.Endless.Split: [unWriter] :: Writer (c :: k -> Type) (a :: k) -> c a
+ Control.Concurrent.Channel.Endless.Split: consumeChannel :: forall (c :: Type -> Type) a. Channel c => Reader c a -> (a -> IO ()) -> IO ()
+ Control.Concurrent.Channel.Endless.Split: consumeChannelBatches :: forall (c :: Type -> Type) a. Channel c => Reader c a -> ([a] -> IO ()) -> IO ()
+ Control.Concurrent.Channel.Endless.Split: consumeChannelBatches' :: forall (c :: Type -> Type) a. Channel c => Reader c a -> (a -> IO ()) -> IO ()
+ Control.Concurrent.Channel.Endless.Split: drainChannel :: forall (c :: Type -> Type) a. Channel c => Reader c a -> STM [a]
+ Control.Concurrent.Channel.Endless.Split: evalFeedChannel :: forall (c :: Type -> Type) a. (Channel c, NFData a) => Writer c a -> IO a -> IO ()
+ Control.Concurrent.Channel.Endless.Split: evalOverwriteChannel :: forall (c :: Type -> Type) a. (Channel c, NFData a) => Writer c a -> a -> IO (Maybe a)
+ Control.Concurrent.Channel.Endless.Split: evalWriteChannel :: forall (c :: Type -> Type) a. (Channel c, NFData a) => Writer c a -> a -> IO ()
+ Control.Concurrent.Channel.Endless.Split: feedChannel :: forall (c :: Type -> Type) a. Channel c => Writer c a -> IO a -> IO ()
+ Control.Concurrent.Channel.Endless.Split: newtype Reader (c :: k -> Type) (a :: k)
+ Control.Concurrent.Channel.Endless.Split: newtype Writer (c :: k -> Type) (a :: k)
+ Control.Concurrent.Channel.Endless.Split: overwriteChannel :: forall (c :: Type -> Type) a. Channel c => Writer c a -> a -> STM (Maybe a)
+ Control.Concurrent.Channel.Endless.Split: pipeline :: forall {k} c (a :: k) x y. IO (c a) -> (Writer c a -> IO x) -> (Reader c a -> IO y) -> IO (x, y)
+ Control.Concurrent.Channel.Endless.Split: pipeline_ :: forall {k} c (a :: k) x y. IO (c a) -> (Writer c a -> IO x) -> (Reader c a -> IO y) -> IO ()
+ Control.Concurrent.Channel.Endless.Split: readChannel :: forall (c :: Type -> Type) a. Channel c => Reader c a -> STM a
+ Control.Concurrent.Channel.Endless.Split: split :: forall {k} c (a :: k). c a -> (Writer c a, Reader c a)
+ Control.Concurrent.Channel.Endless.Split: stateConsumeChannel :: forall (c :: Type -> Type) a s. Channel c => Reader c a -> s -> (s -> a -> IO s) -> IO ()
+ Control.Concurrent.Channel.Endless.Split: stateConsumeChannelBatches :: forall (c :: Type -> Type) a s. Channel c => Reader c a -> s -> (s -> [a] -> IO s) -> IO ()
+ Control.Concurrent.Channel.Endless.Split: stateConsumeChannelBatches' :: forall (c :: Type -> Type) a s. Channel c => Reader c a -> s -> (s -> a -> IO s) -> IO ()
+ Control.Concurrent.Channel.Endless.Split: type TBQReader = Reader TBQueue
+ Control.Concurrent.Channel.Endless.Split: type TBQWriter = Writer TBQueue
+ Control.Concurrent.Channel.Endless.Split: type TMVReader = Reader TMVar
+ Control.Concurrent.Channel.Endless.Split: type TMVWriter = Writer TMVar
+ Control.Concurrent.Channel.Endless.Split: writeChannel :: forall (c :: Type -> Type) a. Channel c => Writer c a -> a -> STM ()
+ Control.Concurrent.Channel.Metrics: ReaderMetrics :: !Seconds -> !Word64 -> !Word64 -> ReaderMetrics
+ Control.Concurrent.Channel.Metrics: WriterMetrics :: !Seconds -> !Word64 -> !Word64 -> !Word64 -> WriterMetrics
+ Control.Concurrent.Channel.Metrics: [elapsedBlocked] :: WriterMetrics -> !Seconds
+ Control.Concurrent.Channel.Metrics: [elapsedWait] :: ReaderMetrics -> !Seconds
+ Control.Concurrent.Channel.Metrics: [totalOverwritten] :: WriterMetrics -> !Word64
+ Control.Concurrent.Channel.Metrics: [totalRead] :: ReaderMetrics -> !Word64
+ Control.Concurrent.Channel.Metrics: [totalWritten] :: WriterMetrics -> !Word64
+ Control.Concurrent.Channel.Metrics: [waitedFor] :: ReaderMetrics -> !Word64
+ Control.Concurrent.Channel.Metrics: [wereBlocked] :: WriterMetrics -> !Word64
+ Control.Concurrent.Channel.Metrics: consumeChannel :: forall (c :: Type -> Type) a. Channel c => Reader c a -> (a -> IO ()) -> IO ReaderMetrics
+ Control.Concurrent.Channel.Metrics: consumeChannelBatches :: forall (c :: Type -> Type) a. Channel c => Reader c a -> ([a] -> IO ()) -> IO ReaderMetrics
+ Control.Concurrent.Channel.Metrics: consumeChannelBatches' :: forall (c :: Type -> Type) a. Channel c => Reader c a -> (a -> IO ()) -> IO ReaderMetrics
+ Control.Concurrent.Channel.Metrics: data ReaderMetrics
+ Control.Concurrent.Channel.Metrics: data WriterMetrics
+ Control.Concurrent.Channel.Metrics: drainChannel :: forall (c :: Type -> Type) a. Channel c => Reader c a -> ReaderMetrics -> IO ([a], ReaderMetrics)
+ Control.Concurrent.Channel.Metrics: evalFeedChannel :: forall (c :: Type -> Type) a. (Channel c, NFData a) => Writer c a -> IO (Maybe a) -> IO WriterMetrics
+ Control.Concurrent.Channel.Metrics: feedChannel :: forall (c :: Type -> Type) a. Channel c => Writer c a -> IO (Maybe a) -> IO WriterMetrics
+ Control.Concurrent.Channel.Metrics: newReaderMetrics :: ReaderMetrics
+ Control.Concurrent.Channel.Metrics: newWriterMetrics :: WriterMetrics
+ Control.Concurrent.Channel.Metrics: overwriteChannel :: forall (c :: Type -> Type) a. Channel c => Writer c a -> a -> WriterMetrics -> STM (OverwriteResult a, WriterMetrics)
+ Control.Concurrent.Channel.Metrics: readChannel :: forall (c :: Type -> Type) a. Channel c => Reader c a -> ReaderMetrics -> IO (Maybe a, ReaderMetrics)
+ Control.Concurrent.Channel.Metrics: stateConsumeChannel :: forall a (c :: Type -> Type) s. Channel c => Reader c a -> s -> (s -> a -> IO s) -> IO (s, ReaderMetrics)
+ Control.Concurrent.Channel.Metrics: stateConsumeChannelBatches :: forall a (c :: Type -> Type) s. Channel c => Reader c a -> s -> (s -> [a] -> IO s) -> IO (s, ReaderMetrics)
+ Control.Concurrent.Channel.Metrics: stateConsumeChannelBatches' :: forall (c :: Type -> Type) a s. Channel c => Reader c a -> s -> (s -> a -> IO s) -> IO (s, ReaderMetrics)
+ Control.Concurrent.Channel.Metrics: writeChannel :: forall (c :: Type -> Type) a. Channel c => Writer c a -> a -> WriterMetrics -> IO (Bool, WriterMetrics)
+ Control.Concurrent.Channel.Overwrite: Closed :: OverwriteResult a
+ Control.Concurrent.Channel.Overwrite: Overwrote :: a -> OverwriteResult a
+ Control.Concurrent.Channel.Overwrite: Wrote :: OverwriteResult a
+ Control.Concurrent.Channel.Overwrite: data OverwriteResult a
+ Control.Concurrent.Channel.Overwrite: instance GHC.Classes.Eq a => GHC.Classes.Eq (Control.Concurrent.Channel.Overwrite.OverwriteResult a)
+ Control.Concurrent.Channel.Overwrite: instance GHC.Internal.Base.Functor Control.Concurrent.Channel.Overwrite.OverwriteResult
+ Control.Concurrent.Channel.Overwrite: instance GHC.Internal.Data.Foldable.Foldable Control.Concurrent.Channel.Overwrite.OverwriteResult
+ Control.Concurrent.Channel.Overwrite: instance GHC.Internal.Data.Traversable.Traversable Control.Concurrent.Channel.Overwrite.OverwriteResult
+ Control.Concurrent.Channel.Overwrite: instance GHC.Internal.Show.Show a => GHC.Internal.Show.Show (Control.Concurrent.Channel.Overwrite.OverwriteResult a)
+ Control.Concurrent.Channel.Split: Closed :: OverwriteResult a
+ Control.Concurrent.Channel.Split: Overwrote :: a -> OverwriteResult a
+ Control.Concurrent.Channel.Split: Reader :: c a -> Reader (c :: k -> Type) (a :: k)
+ Control.Concurrent.Channel.Split: Writer :: c a -> Writer (c :: k -> Type) (a :: k)
+ Control.Concurrent.Channel.Split: Wrote :: OverwriteResult a
+ Control.Concurrent.Channel.Split: [unReader] :: Reader (c :: k -> Type) (a :: k) -> c a
+ Control.Concurrent.Channel.Split: [unWriter] :: Writer (c :: k -> Type) (a :: k) -> c a
+ Control.Concurrent.Channel.Split: class End (e :: k -> Type -> k -> Type)
+ Control.Concurrent.Channel.Split: closeChannel :: forall e (c :: Type -> Type) a. (End e, Channel c) => e c a -> STM ()
+ Control.Concurrent.Channel.Split: consumeChannel :: forall (c :: Type -> Type) a. Channel c => Reader c a -> (a -> IO ()) -> IO ()
+ Control.Concurrent.Channel.Split: consumeChannelBatches :: forall (c :: Type -> Type) a. Channel c => Reader c a -> ([a] -> IO ()) -> IO ()
+ Control.Concurrent.Channel.Split: consumeChannelBatches' :: forall (c :: Type -> Type) a. Channel c => Reader c a -> (a -> IO ()) -> IO ()
+ Control.Concurrent.Channel.Split: data OverwriteResult a
+ Control.Concurrent.Channel.Split: drainChannel :: forall (c :: Type -> Type) a. Channel c => Reader c a -> STM [a]
+ Control.Concurrent.Channel.Split: evalFeedChannel :: forall (c :: Type -> Type) a. (Channel c, NFData a) => Writer c a -> IO (Maybe a) -> IO ()
+ Control.Concurrent.Channel.Split: evalOverwriteChannel :: forall (c :: Type -> Type) a. (Channel c, NFData a) => Writer c a -> a -> IO (OverwriteResult a)
+ Control.Concurrent.Channel.Split: evalOverwriteChannel' :: forall (c :: Type -> Type) a. (Channel c, NFData a) => Writer c a -> a -> IO (Maybe a)
+ Control.Concurrent.Channel.Split: evalWriteChannel :: forall (c :: Type -> Type) a. (Channel c, NFData a) => Writer c a -> a -> IO Bool
+ Control.Concurrent.Channel.Split: evalWriteChannel' :: forall (c :: Type -> Type) a. (Channel c, NFData a) => Writer c a -> a -> IO ()
+ Control.Concurrent.Channel.Split: feedChannel :: forall (c :: Type -> Type) a. Channel c => Writer c a -> IO (Maybe a) -> IO ()
+ Control.Concurrent.Channel.Split: instance Control.Concurrent.Channel.Split.End Control.Concurrent.Channel.Split.Reader
+ Control.Concurrent.Channel.Split: instance Control.Concurrent.Channel.Split.End Control.Concurrent.Channel.Split.Writer
+ Control.Concurrent.Channel.Split: isClosedChannel :: forall e (c :: Type -> Type) a. (End e, Channel c) => e c a -> STM Bool
+ Control.Concurrent.Channel.Split: isEmptyChannel :: forall e (c :: Type -> Type) a. (End e, Channel c) => e c a -> STM Bool
+ Control.Concurrent.Channel.Split: isFullChannel :: forall e (c :: Type -> Type) a. (End e, Channel c) => e c a -> STM Bool
+ Control.Concurrent.Channel.Split: newtype Reader (c :: k -> Type) (a :: k)
+ Control.Concurrent.Channel.Split: newtype Writer (c :: k -> Type) (a :: k)
+ Control.Concurrent.Channel.Split: overwriteChannel :: forall (c :: Type -> Type) a. Channel c => Writer c a -> a -> STM (OverwriteResult a)
+ Control.Concurrent.Channel.Split: overwriteChannel' :: forall (c :: Type -> Type) a. Channel c => Writer c a -> a -> STM (Maybe a)
+ Control.Concurrent.Channel.Split: pipeline :: Channel c => IO (c a) -> (Writer c a -> IO x) -> (Reader c a -> IO y) -> IO (x, y)
+ Control.Concurrent.Channel.Split: pipeline_ :: Channel c => IO (c a) -> (Writer c a -> IO x) -> (Reader c a -> IO y) -> IO ()
+ Control.Concurrent.Channel.Split: readChannel :: forall (c :: Type -> Type) a. Channel c => Reader c a -> STM (Maybe a)
+ Control.Concurrent.Channel.Split: split :: forall {k} c (a :: k). c a -> (Writer c a, Reader c a)
+ Control.Concurrent.Channel.Split: stateConsumeChannel :: forall (c :: Type -> Type) a s. Channel c => Reader c a -> s -> (s -> a -> IO s) -> IO s
+ Control.Concurrent.Channel.Split: stateConsumeChannelBatches :: forall (c :: Type -> Type) a s. Channel c => Reader c a -> s -> (s -> [a] -> IO s) -> IO s
+ Control.Concurrent.Channel.Split: stateConsumeChannelBatches' :: forall (c :: Type -> Type) a s. Channel c => Reader c a -> s -> (s -> a -> IO s) -> IO s
+ Control.Concurrent.Channel.Split: type TBCQReader = Reader TBCQueue
+ Control.Concurrent.Channel.Split: type TBCQWriter = Writer TBCQueue
+ Control.Concurrent.Channel.Split: type TCSReader = Reader TCSlot
+ Control.Concurrent.Channel.Split: type TCSWriter = Writer TCSlot
+ Control.Concurrent.Channel.Split: unEnd :: forall c (a :: k). End e => e c a -> c a
+ Control.Concurrent.Channel.Split: writeChannel :: forall (c :: Type -> Type) a. Channel c => Writer c a -> a -> STM Bool
+ Control.Concurrent.Channel.Split: writeChannel' :: forall (c :: Type -> Type) a. Channel c => Writer c a -> a -> STM ()
+ Control.Concurrent.Channel.Try: Blocked :: Tried a
+ Control.Concurrent.Channel.Try: Done :: a -> Tried a
+ Control.Concurrent.Channel.Try: data Tried a
+ Control.Concurrent.Channel.Try: instance GHC.Classes.Eq a => GHC.Classes.Eq (Control.Concurrent.Channel.Try.Tried a)
+ Control.Concurrent.Channel.Try: instance GHC.Internal.Base.Functor Control.Concurrent.Channel.Try.Tried
+ Control.Concurrent.Channel.Try: instance GHC.Internal.Data.Foldable.Foldable Control.Concurrent.Channel.Try.Tried
+ Control.Concurrent.Channel.Try: instance GHC.Internal.Data.Traversable.Traversable Control.Concurrent.Channel.Try.Tried
+ Control.Concurrent.Channel.Try: instance GHC.Internal.Show.Show a => GHC.Internal.Show.Show (Control.Concurrent.Channel.Try.Tried a)
+ Control.Concurrent.Channel.Try: timedAtomically :: STM a -> IO (a, Maybe Seconds)
+ Control.Concurrent.Channel.Try: try :: STM a -> STM (Tried a)
+ Control.Concurrent.TBCQueue: drainTBCQueue :: TBCQueue a -> STM [a]
+ Control.Concurrent.TBCQueue: isEmptyTBCQueue :: TBCQueue a -> STM Bool
+ Control.Concurrent.TBCQueue: isFullTBCQueue :: TBCQueue a -> STM Bool
+ Control.Concurrent.TBCQueue: overwriteTBCQueue :: TBCQueue a -> a -> STM (OverwriteResult a)
+ Control.Concurrent.TCSlot: closeTCSlot :: TCSlot a -> STM ()
+ Control.Concurrent.TCSlot: data TCSlot a
+ Control.Concurrent.TCSlot: isClosedTCSlot :: TCSlot a -> STM Bool
+ Control.Concurrent.TCSlot: isEmptyTCSlot :: TCSlot a -> STM Bool
+ Control.Concurrent.TCSlot: isFullTCSlot :: TCSlot a -> STM Bool
+ Control.Concurrent.TCSlot: isOpenTCSlot :: TCSlot a -> STM Bool
+ Control.Concurrent.TCSlot: newTCSlot :: STM (TCSlot a)
+ Control.Concurrent.TCSlot: newTCSlotIO :: IO (TCSlot a)
+ Control.Concurrent.TCSlot: overwriteTCSlot :: TCSlot a -> a -> STM (OverwriteResult a)
+ Control.Concurrent.TCSlot: readTCSlot :: TCSlot a -> STM (Maybe a)
+ Control.Concurrent.TCSlot: writeTCSlot :: TCSlot a -> a -> STM Bool
- Control.Concurrent.Channel: consumeChannel :: (Channel c, Monoid m) => c a -> (a -> IO m) -> IO m
+ Control.Concurrent.Channel: consumeChannel :: Channel c => c a -> (a -> IO ()) -> IO ()
Files
- LICENSE.txt +19/−0
- hoare.cabal +22/−12
- lib/Control/Concurrent/Channel.hs +111/−17
- lib/Control/Concurrent/Channel/Endless.hs +82/−1
- lib/Control/Concurrent/Channel/Endless/Split.hs +96/−0
- lib/Control/Concurrent/Channel/Metrics.hs +172/−0
- lib/Control/Concurrent/Channel/Overwrite.hs +4/−0
- lib/Control/Concurrent/Channel/Split.hs +142/−0
- lib/Control/Concurrent/Channel/Try.hs +25/−0
- lib/Control/Concurrent/TBCQueue.hs +45/−8
- lib/Control/Concurrent/TCSlot.hs +84/−0
+ LICENSE.txt view
@@ -0,0 +1,19 @@+Copyright © 2026 Matt Kline++This software is provided 'as-is', without any express or implied+warranty. In no event will the authors be held liable for any damages+arising from the use of this software.++Permission is granted to anyone to use this software for any purpose,+including commercial applications, and to alter it and redistribute it+freely, subject to the following restrictions:++1. The origin of this software must not be misrepresented; you must not+ claim that you wrote the original software. If you use this software+ in a product, an acknowledgment in the product documentation would be+ appreciated but is not required.++2. Altered source versions must be plainly marked as such, and must not be+ misrepresented as being the original software.++3. This notice may not be removed or altered from any source distribution.
hoare.cabal view
@@ -10,14 +10,21 @@ - Functions for using bounded STM types like `TBQueue` and `TMVar` as /channels/ between threads. - - A /closeable/ @TBCQueue@ for programs where the input ends.-version: 0.1.1.0+ - A /closeable/ @TBCQueue@ and @TCSlot@ for programs where the input ends.++ - A "split" API on top of the above, using newtypes to distinguish+ the producer side from the consumer side.++ - Some wrappers to gather metrics (items produced/consumed, wait times).+ This API is experimental and unstable.+version: 0.2.0.0 license: Zlib+license-file: LICENSE.txt author: Matt Kline maintainer: matt@bitbashing.io category: Concurrency build-type: Simple-tested-with: GHC==9.8.4+tested-with: GHC==9.12.4 source-repository head type: git@@ -32,29 +39,32 @@ -Wno-missing-import-lists -Wno-missing-kind-signatures -Wno-missing-local-signatures+ -Wno-missing-poly-kind-signatures -Wno-missing-role-annotations -Wno-missing-safe-haskell-mode- -Wno-monomorphism-restriction -Wno-safe -Wno-type-defaults -Wno-unsafe -threaded -fno-ignore-asserts- default-language: Haskell2010- default-extensions: BangPatterns,- DerivingStrategies,- ImportQualifiedPost,- LambdaCase,- OverloadedRecordDot,- RecordWildCards,+ default-language: GHC2024+ default-extensions: OverloadedRecordDot library import: shared exposed-modules: Control.Concurrent.Channel Control.Concurrent.Channel.Endless+ Control.Concurrent.Channel.Endless.Split+ Control.Concurrent.Channel.Metrics+ Control.Concurrent.Channel.Overwrite+ Control.Concurrent.Channel.Split+ Control.Concurrent.Channel.Try Control.Concurrent.TBCQueue+ Control.Concurrent.TCSlot+ hs-source-dirs: lib- build-depends: base >=4.4 && < 5,+ build-depends: base >=4.20 && < 5, async >= 2.2 && < 3,+ clock >= 0.8.4 && < 0.9, deepseq >= 1.5 && < 2, stm >= 2.5 && < 3,
lib/Control/Concurrent/Channel.hs view
@@ -2,11 +2,19 @@ -- with closeable channels. module Control.Concurrent.Channel( Channel (..),+ OverwriteResult (..), writeChannel', evalWriteChannel, evalWriteChannel',+ overwriteChannel',+ evalOverwriteChannel,+ evalOverwriteChannel', consumeChannel,+ consumeChannelBatches,+ consumeChannelBatches', stateConsumeChannel,+ stateConsumeChannelBatches,+ stateConsumeChannelBatches', feedChannel, evalFeedChannel, pipeline,@@ -14,37 +22,67 @@ ) where import Control.Concurrent.Async+import Control.Concurrent.Channel.Overwrite import Control.Concurrent.STM import Control.Concurrent.TBCQueue-import Control.Exception+import Control.Concurrent.TCSlot+import Control.Exception (evaluate, finally) import Control.DeepSeq import Control.Monad+import Data.Function (fix)+import Data.Maybe (maybeToList) class Channel c where- -- | Returns @Just value@ until the channel is closed, blocking for the next value.+ -- | Reads from the channel, blocking if empty,+ -- and returning @Nothing@ once it is empty and closed. readChannel :: c a -> STM (Maybe a) - -- | Writes the given value to the channel if it's still open.+ -- | Drains the channel, blocking if it is empty.+ -- Returns @[]@ if the channel is empty and closed. --+ -- Useful for accumulating values to pass in bulk to an expensive operation,+ -- like a @send()@ syscall.+ drainChannel :: c a -> STM [a]++ -- | Writes the given value to the channel if it's still open, blocking if full.+ -- -- Returns whether the channel is still open so producers know when consumers no longer care. writeChannel :: c a -> a -> STM Bool + -- | Writes the given value to the channel if it's still open.+ --+ -- When the channel is full, removes and returns the oldest value.+ overwriteChannel :: c a -> a -> STM (OverwriteResult a)+ -- | Closes the channel so that future writes are no-ops and readers get @Nothing@ -- after all values currently inside have been read. closeChannel :: c a -> STM () + isEmptyChannel :: c a -> STM Bool+ isClosedChannel :: c a -> STM Bool + isFullChannel :: c a -> STM Bool+ instance Channel TBCQueue where readChannel = readTBCQueue-+ drainChannel = drainTBCQueue writeChannel = writeTBCQueue-+ overwriteChannel = overwriteTBCQueue closeChannel = closeTBCQueue-+ isEmptyChannel = isEmptyTBCQueue isClosedChannel = isClosedTBCQueue+ isFullChannel = isFullTBCQueue --- More to follow? A TMVar-like closeable slot?+instance Channel TCSlot where+ readChannel = readTCSlot+ drainChannel = fmap maybeToList <$> readTCSlot+ writeChannel = writeTCSlot+ overwriteChannel = overwriteTCSlot+ closeChannel = closeTCSlot+ isEmptyChannel = isEmptyTCSlot+ isClosedChannel = isClosedTCSlot+ isFullChannel = isFullTCSlot -- | Writes to the channel, asserting that it hasn't been closed. --@@ -73,16 +111,55 @@ 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 = go mempty where- go !acc = atomically (readChannel c) >>= \case- Just v -> do- res <- f v- go $ acc <> res- Nothing -> pure acc+-- | Overwrites the channel, asserting that it hasn't been closed+-- and returning any overwritten value.+--+-- Useful in situations where only the writer closes the channel (when finished).+overwriteChannel' :: (Channel c) => c a -> a -> STM (Maybe a)+overwriteChannel' c v = overwriteChannel c v >>= \case+ Wrote -> pure Nothing+ Overwrote o -> pure $ Just o+ Closed -> error "absurd: overwriteChannel' on closed channel" +-- | Force a value to normal form before overwriting the given channel with it.+evalOverwriteChannel :: (Channel c, NFData a) => c a -> a -> IO (OverwriteResult a)+evalOverwriteChannel c v = do+ v' <- evaluate $ force v+ atomically $ overwriteChannel c v'+{-# INLINE evalOverwriteChannel #-}++-- | `overwriteChannel'` meets `evalOverwriteChannel`+evalOverwriteChannel' :: (Channel c, NFData a) => c a -> a -> IO (Maybe a)+evalOverwriteChannel' c v = do+ v' <- evaluate $ force v+ atomically $ overwriteChannel' c v'+{-# INLINE evalOverwriteChannel' #-}++-- | Consume the given channel until it closes, passing values to the given action.+--+-- See 'stateConsumeChannel' to accumulate state at each step.+consumeChannel :: (Channel c) => c a -> (a -> IO ()) -> IO ()+consumeChannel c f = fix $ \loop -> atomically (readChannel c) >>= \case+ Just v -> f v >> loop+ Nothing -> pure ()++-- | Consume the given channel until it closes, passing batches of values to the given action.+--+-- Each batch is all the values in the channel at the time of the read (see `drainChannel`).+-- When the consumer falls behind, this uses fewer STM transactions than `consumeChannel`+-- at the cost of weaker backpressure.+consumeChannelBatches :: (Channel c) => c a -> ([a] -> IO ()) -> IO ()+consumeChannelBatches c f = fix $ \loop -> atomically (drainChannel c) >>= \case+ [] -> pure ()+ vs -> f vs >> loop++-- | `consumeChannelBatches`, but with an action for each value instead of each batch.+--+-- This has the same interface as `consumeChannel`,+-- but uses fewer STM transactions when the consumer falls behind.+consumeChannelBatches' :: (Channel c) => c a -> (a -> IO ()) -> IO ()+consumeChannelBatches' c f = consumeChannelBatches c $ mapM_ f+ -- | Consume the given channel until it closes, -- with each action updating some state. Returns the final state. stateConsumeChannel :: (Channel c) => c a -> s -> (s -> a -> IO s) -> IO s@@ -92,6 +169,22 @@ stateConsumeChannel c next f Nothing -> pure state +-- | `consumeChannelBatches`, but with each action updating some state.+-- Returns the final state.+stateConsumeChannelBatches :: (Channel c) => c a -> s -> (s -> [a] -> IO s) -> IO s+stateConsumeChannelBatches c !state f = atomically (drainChannel c) >>= \case+ [] -> pure state+ vs -> do+ next <- f state vs+ stateConsumeChannelBatches c next f++-- | `stateConsumeChannelBatches`, but with an action for each value instead of each batch.+--+-- This has the same interface as `stateConsumeChannel`,+-- but uses fewer STM transactions when the consumer falls behind.+stateConsumeChannelBatches' :: (Channel c) => c a -> s -> (s -> a -> IO s) -> IO s+stateConsumeChannelBatches' c state f = stateConsumeChannelBatches c state $ foldM (\ !s v -> f s v)+ feedChannel' :: (Channel c) => c a -> IO (Maybe a) -> IO () feedChannel' c f = f >>= \case Just v -> do@@ -122,7 +215,8 @@ pipeline new producer consumer = do c <- new let producer' = producer c `finally` atomically (closeChannel c)- concurrently producer' (consumer c)+ let consumer' = consumer c `finally` atomically (closeChannel c)+ concurrently producer' consumer' -- | `pipeline`, but ignore the results. pipeline_ :: (Channel c) => IO (c a) -> (c a -> IO x) -> (c a -> IO y) -> IO ()
lib/Control/Concurrent/Channel/Endless.hs view
@@ -7,7 +7,13 @@ module Control.Concurrent.Channel.Endless( Channel (..), evalWriteChannel,+ evalOverwriteChannel, consumeChannel,+ consumeChannelBatches,+ consumeChannelBatches',+ stateConsumeChannel,+ stateConsumeChannelBatches,+ stateConsumeChannelBatches', feedChannel, evalFeedChannel, pipeline,@@ -21,28 +27,103 @@ import Control.Monad class Channel c where+ -- | Reads the next value, blocking if empty. readChannel :: c a -> STM a + -- | Drains the channel, blocking if it is empty.+ --+ -- Useful for accumulating values to pass in bulk to an expensive operation,+ -- like a @send()@ syscall.+ drainChannel :: c a -> STM [a]++ -- | Writes the next value, blocking if full. writeChannel :: c a -> a -> STM () + -- | Writes the next value, returning the oldest if the channel was full.+ overwriteChannel :: c a -> a -> STM (Maybe a)+ instance Channel TBQueue where readChannel = readTBQueue + drainChannel c = do+ vs <- flushTBQueue c+ when (null vs) retry+ pure vs+ writeChannel = writeTBQueue + overwriteChannel c v = do+ wasFull <- isFullTBQueue c+ if wasFull+ then do+ o <- readTBQueue c+ writeTBQueue c v+ pure $ Just o+ else do+ writeTBQueue c v+ pure Nothing+ instance Channel TMVar where readChannel = takeTMVar - writeChannel = writeTMVar+ drainChannel = fmap (: []) <$> takeTMVar + writeChannel = putTMVar++ overwriteChannel c v = do+ o <- tryTakeTMVar c+ putTMVar c v+ pure o+ evalWriteChannel :: (Channel c, NFData a) => c a -> a -> IO () evalWriteChannel c v = do v' <- evaluate $ force v atomically $ writeChannel c v' {-# INLINE evalWriteChannel #-} +evalOverwriteChannel :: (Channel c, NFData a) => c a -> a -> IO (Maybe a)+evalOverwriteChannel c v = do+ v' <- evaluate $ force v+ atomically $ overwriteChannel c v'+{-# INLINE evalOverwriteChannel #-}+ consumeChannel :: (Channel c) => c a -> (a -> IO ()) -> IO () consumeChannel c f = forever $ atomically (readChannel c) >>= f++-- | Consume the given channel, passing batches of values to the given action.+--+-- Each batch is all the values in the channel at the time of the read (see `drainChannel`).+-- When the consumer falls behind, this uses fewer STM transactions than `consumeChannel`+-- at the cost of weaker backpressure.+consumeChannelBatches :: (Channel c) => c a -> ([a] -> IO ()) -> IO ()+consumeChannelBatches c f = forever $ atomically (drainChannel c) >>= f++-- | `consumeChannelBatches`, but with an action for each value instead of each batch.+--+-- This has the same interface as `consumeChannel`,+-- but uses fewer STM transactions when the consumer falls behind.+consumeChannelBatches' :: (Channel c) => c a -> (a -> IO ()) -> IO ()+consumeChannelBatches' c f = consumeChannelBatches c $ mapM_ f++stateConsumeChannel :: (Channel c) => c a -> s -> (s -> a -> IO s) -> IO ()+stateConsumeChannel c !state f = do+ v <- atomically (readChannel c)+ next <- f state v+ stateConsumeChannel c next f++-- | `consumeChannelBatches`, but with each action updating some state.+stateConsumeChannelBatches :: (Channel c) => c a -> s -> (s -> [a] -> IO s) -> IO ()+stateConsumeChannelBatches c !state f = do+ vs <- atomically (drainChannel c)+ next <- f state vs+ stateConsumeChannelBatches c next f++-- | `stateConsumeChannelBatches`, but with an action for each value instead of each batch.+--+-- This has the same interface as `stateConsumeChannel`,+-- but uses fewer STM transactions when the consumer falls behind.+stateConsumeChannelBatches' :: (Channel c) => c a -> s -> (s -> a -> IO s) -> IO ()+stateConsumeChannelBatches' c state f = stateConsumeChannelBatches c state $ foldM (\ !s v -> f s v) feedChannel :: (Channel c) => c a -> IO a -> IO () feedChannel c f = forever $ f >>= atomically . writeChannel c
+ lib/Control/Concurrent/Channel/Endless/Split.hs view
@@ -0,0 +1,96 @@+-- | "Control.Concurrent.Channel.Endless" with newtypes for producer and consumer sides.+module Control.Concurrent.Channel.Endless.Split(+ Reader (..),+ Writer (..),+ TBQReader,+ TBQWriter,+ TMVReader,+ TMVWriter,++ split,+ readChannel,+ drainChannel,+ writeChannel,+ overwriteChannel,+ evalWriteChannel,+ evalOverwriteChannel,+ consumeChannel,+ consumeChannelBatches,+ consumeChannelBatches',+ stateConsumeChannel,+ stateConsumeChannelBatches,+ stateConsumeChannelBatches',+ feedChannel,+ evalFeedChannel,+ pipeline,+ pipeline_+) where++import Control.Concurrent.STM+import Control.Concurrent.Channel.Endless qualified as E+import Control.DeepSeq++-- | The consumer side of a channel @c@.+newtype Reader c a = Reader { unReader :: c a }++-- | The producer side of a channel @c@.+newtype Writer c a = Writer { unWriter :: c a }++-- | Give the producer and consumer sides of the given channel.+split :: c a -> (Writer c a, Reader c a)+split c = (Writer c, Reader c)++type TBQReader = Reader TBQueue+type TBQWriter = Writer TBQueue+type TMVReader = Reader TMVar+type TMVWriter = Writer TMVar++readChannel :: (E.Channel c) => Reader c a -> STM a+readChannel = E.readChannel . unReader++drainChannel :: (E.Channel c) => Reader c a -> STM [a]+drainChannel = E.drainChannel . unReader++writeChannel :: (E.Channel c) => Writer c a -> a -> STM ()+writeChannel = E.writeChannel . unWriter++overwriteChannel :: (E.Channel c) => Writer c a -> a -> STM (Maybe a)+overwriteChannel = E.overwriteChannel . unWriter++evalWriteChannel :: (E.Channel c, NFData a) => Writer c a -> a -> IO ()+evalWriteChannel = E.evalWriteChannel . unWriter++evalOverwriteChannel :: (E.Channel c, NFData a) => Writer c a -> a -> IO (Maybe a)+evalOverwriteChannel = E.evalOverwriteChannel . unWriter++consumeChannel :: (E.Channel c) => Reader c a -> (a -> IO ()) -> IO ()+consumeChannel = E.consumeChannel . unReader++consumeChannelBatches :: (E.Channel c) => Reader c a -> ([a] -> IO ()) -> IO ()+consumeChannelBatches = E.consumeChannelBatches . unReader++consumeChannelBatches' :: (E.Channel c) => Reader c a -> (a -> IO ()) -> IO ()+consumeChannelBatches' = E.consumeChannelBatches' . unReader++stateConsumeChannel :: (E.Channel c) => Reader c a -> s -> (s -> a -> IO s) -> IO ()+stateConsumeChannel = E.stateConsumeChannel . unReader++stateConsumeChannelBatches :: (E.Channel c) => Reader c a -> s -> (s -> [a] -> IO s) -> IO ()+stateConsumeChannelBatches = E.stateConsumeChannelBatches . unReader++stateConsumeChannelBatches' :: (E.Channel c) => Reader c a -> s -> (s -> a -> IO s) -> IO ()+stateConsumeChannelBatches' = E.stateConsumeChannelBatches' . unReader++feedChannel :: (E.Channel c) => Writer c a -> IO a -> IO ()+feedChannel = E.feedChannel . unWriter++evalFeedChannel :: (E.Channel c, NFData a) => Writer c a -> IO a -> IO ()+evalFeedChannel = E.evalFeedChannel . unWriter++-- | Create a channel with the given action,+-- then give its writer to the producer and its reader to the consumer.+pipeline :: IO (c a) -> (Writer c a -> IO x) -> (Reader c a -> IO y) -> IO (x, y)+pipeline new producer consumer = E.pipeline new (producer . Writer) (consumer . Reader)++pipeline_ :: IO (c a) -> (Writer c a -> IO x) -> (Reader c a -> IO y) -> IO ()+pipeline_ new producer consumer = E.pipeline_ new (producer . Writer) (consumer . Reader)
+ lib/Control/Concurrent/Channel/Metrics.hs view
@@ -0,0 +1,172 @@+-- |+-- Stability : experimental+--+-- Wrappers around "Control.Concurrent.Channel.Split" that gather metrics+-- at handoffs between threads.+module Control.Concurrent.Channel.Metrics (+ ReaderMetrics(..),+ newReaderMetrics,+ readChannel,+ drainChannel,+ WriterMetrics(..),+ newWriterMetrics,+ writeChannel,+ overwriteChannel,+ consumeChannel,+ consumeChannelBatches,+ consumeChannelBatches',+ stateConsumeChannel,+ stateConsumeChannelBatches,+ stateConsumeChannelBatches',+ feedChannel,+ evalFeedChannel+) where++import Control.Concurrent.Async+import Control.Concurrent.STM+import Control.DeepSeq+import Control.Exception (evaluate)+import Control.Monad+import Data.Functor ((<&>))+import Data.IORef+import Data.Maybe+import Data.Word+import System.Clock.Seconds++import Control.Concurrent.Channel qualified as C+import Control.Concurrent.Channel.Overwrite+import Control.Concurrent.Channel.Split (Reader, Writer)+import Control.Concurrent.Channel.Split qualified as CS+import Control.Concurrent.Channel.Try++data ReaderMetrics = ReaderMetrics {+ elapsedWait :: !Seconds,+ totalRead :: !Word64,+ waitedFor :: !Word64+}++newReaderMetrics :: ReaderMetrics+newReaderMetrics = ReaderMetrics 0 0 0++mlen :: Maybe a -> Word64+mlen j = if isJust j then 1 else 0++delayedReader :: Word64 -> Seconds -> ReaderMetrics -> ReaderMetrics+delayedReader n dt m = if wasFirst then m1 else mrest where+ wasFirst = m.totalRead == 0+ -- Don't count blocking for the first item against our wait.+ m1 = ReaderMetrics{elapsedWait = 0, totalRead = n, waitedFor = 0}+ mrest = m{+ elapsedWait = m.elapsedWait + dt,+ totalRead = m.totalRead + n,+ waitedFor = m.waitedFor + n+ }++readChannel :: (C.Channel c) => Reader c a -> ReaderMetrics -> IO (Maybe a, ReaderMetrics)+readChannel r !m = timedAtomically (CS.readChannel r) <&> \case+ (v, Nothing) -> (v, m{totalRead = m.totalRead + mlen v})+ (v, Just dt) -> (v, delayedReader (mlen v) dt m)++wlen :: [a] -> Word64+wlen vs = fromIntegral $ length vs++drainChannel :: (C.Channel c) => Reader c a -> ReaderMetrics -> IO ([a], ReaderMetrics)+drainChannel r !m = timedAtomically (CS.drainChannel r) <&> \case+ (vs, Nothing) -> (vs, m{totalRead = m.totalRead + wlen vs})+ (vs, Just dt) -> (vs, delayedReader (wlen vs) dt m)++data WriterMetrics = WriterMetrics {+ elapsedBlocked :: !Seconds,+ totalWritten :: !Word64,+ wereBlocked :: !Word64,+ totalOverwritten :: !Word64+}++newWriterMetrics :: WriterMetrics+newWriterMetrics = WriterMetrics 0 0 0 0++-- Substantially simpler than `delayedReader` since a channel presumably never starts blocked,+-- so we don't have to ignore the first guy.+delayedWriter :: Seconds -> WriterMetrics -> WriterMetrics+delayedWriter dt m = m{+ elapsedBlocked = m.elapsedBlocked + dt,+ totalWritten = m.totalWritten + 1,+ wereBlocked = m.wereBlocked + 1+ }++writeChannel :: (C.Channel c) => Writer c a -> a -> WriterMetrics -> IO (Bool, WriterMetrics)+writeChannel w v !m = timedAtomically (CS.writeChannel w v) <&> \case+ (True, Nothing) -> (True, m{totalWritten = m.totalWritten + 1})+ (True, Just dt) -> (True, delayedWriter dt m)+ (False, Nothing) -> (False, m)+ (False, Just dt) -> (False, m{elapsedBlocked = m.elapsedBlocked + dt})++overwriteChannel :: (C.Channel c) => Writer c a -> a -> WriterMetrics -> STM (OverwriteResult a, WriterMetrics)+overwriteChannel w v !m = CS.overwriteChannel w v <&> \case+ Closed -> (Closed, m)+ Wrote -> (Wrote, m{totalWritten = m.totalWritten + 1})+ o@(Overwrote _) -> (o, m{+ totalWritten = m.totalWritten + 1,+ totalOverwritten = m.totalOverwritten + 1+ })++consumeChannel :: (C.Channel c) => Reader c a -> (a -> IO ()) -> IO ReaderMetrics+consumeChannel r f = go newReaderMetrics where+ go :: ReaderMetrics -> IO ReaderMetrics+ go !m = readChannel r m >>= \case+ (Just v, m') -> f v >> go m'+ (Nothing, m') -> pure m'++consumeChannelBatches :: (C.Channel c) => Reader c a -> ([a] -> IO ()) -> IO ReaderMetrics+consumeChannelBatches r f = go newReaderMetrics where+ go :: ReaderMetrics -> IO ReaderMetrics+ go !m = drainChannel r m >>= \case+ ([], m') -> pure m'+ (vs, m') -> f vs >> go m'++consumeChannelBatches' :: (C.Channel c) => Reader c a -> (a -> IO ()) -> IO ReaderMetrics+consumeChannelBatches' r f = consumeChannelBatches r $ mapM_ f++stateConsumeChannel :: forall a c s. (C.Channel c) => Reader c a -> s -> (s -> a -> IO s) -> IO (s, ReaderMetrics)+stateConsumeChannel r initialState f = go initialState newReaderMetrics where+ go :: s -> ReaderMetrics -> IO (s, ReaderMetrics)+ go !state !m = readChannel r m >>= \case+ (Just v, m') -> do+ next <- f state v+ go next m'+ (Nothing, m') -> pure (state, m')++stateConsumeChannelBatches :: forall a c s. (C.Channel c) => Reader c a -> s -> (s -> [a] -> IO s) -> IO (s, ReaderMetrics)+stateConsumeChannelBatches r initialState f = go initialState newReaderMetrics where+ go :: s -> ReaderMetrics -> IO (s, ReaderMetrics)+ go !state !m = drainChannel r m >>= \case+ ([], m') -> pure (state, m')+ (vs, m') -> do+ next <- f state vs+ go next m'++stateConsumeChannelBatches' :: (C.Channel c) => Reader c a -> s -> (s -> a -> IO s) -> IO (s, ReaderMetrics)+stateConsumeChannelBatches' r initialState f = stateConsumeChannelBatches r initialState $ foldM (\ !s v -> f s v)++feedChannel' :: (C.Channel c) => Writer c a -> IO (Maybe a) -> IORef WriterMetrics -> IO ()+feedChannel' w f mr = f >>= \case+ Just v -> do+ m <- readIORef mr+ (stillOpen, m') <- writeChannel w v m+ writeIORef mr m'+ when stillOpen $ feedChannel' w f mr+ Nothing -> atomically $ CS.closeChannel w++feedChannel :: (C.Channel c) => Writer c a -> IO (Maybe a) -> IO WriterMetrics+feedChannel w f = do+ -- Stop when the consumer closes the channel, even if f blocks.+ -- For example, f can block for a long time on a network socket.+ let chanClosed = atomically $ CS.isClosedChannel w >>= check+ mr <- newIORef newWriterMetrics+ race_ (feedChannel' w f mr) chanClosed+ readIORef mr++evalFeedChannel :: (C.Channel c, NFData a) => Writer c a -> IO (Maybe a) -> IO WriterMetrics+evalFeedChannel w f = feedChannel w f' where+ f' = f >>= evaluate . force+{-# INLINE evalFeedChannel #-}
+ lib/Control/Concurrent/Channel/Overwrite.hs view
@@ -0,0 +1,4 @@+module Control.Concurrent.Channel.Overwrite where++data OverwriteResult a = Wrote | Overwrote a | Closed+ deriving stock (Show, Eq, Foldable, Functor, Traversable)
+ lib/Control/Concurrent/Channel/Split.hs view
@@ -0,0 +1,142 @@+-- | "Control.Concurrent.Channel" with newtypes for producer and consumer sides.+module Control.Concurrent.Channel.Split(+ Reader (..),+ Writer (..),+ TBCQReader,+ TBCQWriter,+ TCSReader,+ TCSWriter,+ End (..),+ C.OverwriteResult (..),++ split,+ closeChannel,+ isClosedChannel,+ isEmptyChannel,+ isFullChannel,+ readChannel,+ drainChannel,+ writeChannel,+ overwriteChannel,+ writeChannel',+ evalWriteChannel,+ evalWriteChannel',+ overwriteChannel',+ evalOverwriteChannel,+ evalOverwriteChannel',+ consumeChannel,+ consumeChannelBatches,+ consumeChannelBatches',+ stateConsumeChannel,+ stateConsumeChannelBatches,+ stateConsumeChannelBatches',+ feedChannel,+ evalFeedChannel,+ pipeline,+ pipeline_+) where++import Control.Concurrent.STM+import Control.Concurrent.Channel qualified as C+import Control.Concurrent.TBCQueue+import Control.Concurrent.TCSlot+import Control.DeepSeq++-- | The consumer side of a channel @c@.+newtype Reader c a = Reader { unReader :: c a }++-- | The producer side of a channel @c@.+newtype Writer c a = Writer { unWriter :: c a }++-- | Give the producer and consumer sides of the given channel.+split :: c a -> (Writer c a, Reader c a)+split c = (Writer c, Reader c)++type TBCQReader = Reader TBCQueue+type TBCQWriter = Writer TBCQueue+type TCSReader = Reader TCSlot+type TCSWriter = Writer TCSlot++-- | Operations that either side of a channel can do.+class End e where+ unEnd :: e c a -> c a++instance End Reader where+ unEnd = unReader++instance End Writer where+ unEnd = unWriter++closeChannel :: (End e, C.Channel c) => e c a -> STM ()+closeChannel = C.closeChannel . unEnd++isClosedChannel :: (End e, C.Channel c) => e c a -> STM Bool+isClosedChannel = C.isClosedChannel . unEnd++isEmptyChannel :: (End e, C.Channel c) => e c a -> STM Bool+isEmptyChannel = C.isEmptyChannel . unEnd++isFullChannel :: (End e, C.Channel c) => e c a -> STM Bool+isFullChannel = C.isFullChannel . unEnd++readChannel :: (C.Channel c) => Reader c a -> STM (Maybe a)+readChannel = C.readChannel . unReader++drainChannel :: (C.Channel c) => Reader c a -> STM [a]+drainChannel = C.drainChannel . unReader++writeChannel :: (C.Channel c) => Writer c a -> a -> STM Bool+writeChannel = C.writeChannel . unWriter++overwriteChannel :: (C.Channel c) => Writer c a -> a -> STM (C.OverwriteResult a)+overwriteChannel = C.overwriteChannel . unWriter++writeChannel' :: (C.Channel c) => Writer c a -> a -> STM ()+writeChannel' = C.writeChannel' . unWriter++evalWriteChannel :: (C.Channel c, NFData a) => Writer c a -> a -> IO Bool+evalWriteChannel = C.evalWriteChannel . unWriter++evalWriteChannel' :: (C.Channel c, NFData a) => Writer c a -> a -> IO ()+evalWriteChannel' = C.evalWriteChannel' . unWriter++overwriteChannel' :: (C.Channel c) => Writer c a -> a -> STM (Maybe a)+overwriteChannel' = C.overwriteChannel' . unWriter++evalOverwriteChannel :: (C.Channel c, NFData a) => Writer c a -> a -> IO (C.OverwriteResult a)+evalOverwriteChannel = C.evalOverwriteChannel . unWriter++evalOverwriteChannel' :: (C.Channel c, NFData a) => Writer c a -> a -> IO (Maybe a)+evalOverwriteChannel' = C.evalOverwriteChannel' . unWriter++consumeChannel :: (C.Channel c) => Reader c a -> (a -> IO ()) -> IO ()+consumeChannel = C.consumeChannel . unReader++consumeChannelBatches :: (C.Channel c) => Reader c a -> ([a] -> IO ()) -> IO ()+consumeChannelBatches = C.consumeChannelBatches . unReader++consumeChannelBatches' :: (C.Channel c) => Reader c a -> (a -> IO ()) -> IO ()+consumeChannelBatches' = C.consumeChannelBatches' . unReader++stateConsumeChannel :: (C.Channel c) => Reader c a -> s -> (s -> a -> IO s) -> IO s+stateConsumeChannel = C.stateConsumeChannel . unReader++stateConsumeChannelBatches :: (C.Channel c) => Reader c a -> s -> (s -> [a] -> IO s) -> IO s+stateConsumeChannelBatches = C.stateConsumeChannelBatches . unReader++stateConsumeChannelBatches' :: (C.Channel c) => Reader c a -> s -> (s -> a -> IO s) -> IO s+stateConsumeChannelBatches' = C.stateConsumeChannelBatches' . unReader++feedChannel :: (C.Channel c) => Writer c a -> IO (Maybe a) -> IO ()+feedChannel = C.feedChannel . unWriter++evalFeedChannel :: (C.Channel c, NFData a) => Writer c a -> IO (Maybe a) -> IO ()+evalFeedChannel = C.evalFeedChannel . unWriter++-- | Create a channel with the given action,+-- then give its writer to the producer and its reader to the consumer.+pipeline :: (C.Channel c) => IO (c a) -> (Writer c a -> IO x) -> (Reader c a -> IO y) -> IO (x, y)+pipeline new producer consumer = C.pipeline new (producer . Writer) (consumer . Reader)++pipeline_ :: (C.Channel c) => IO (c a) -> (Writer c a -> IO x) -> (Reader c a -> IO y) -> IO ()+pipeline_ new producer consumer = C.pipeline_ new (producer . Writer) (consumer . Reader)
+ lib/Control/Concurrent/Channel/Try.hs view
@@ -0,0 +1,25 @@+module Control.Concurrent.Channel.Try where++import Control.Concurrent.STM+import System.Clock.Seconds++data Tried a = Done a | Blocked+ deriving stock (Show, Eq, Foldable, Functor, Traversable)++-- | Attempt any STM action, returning immediately with @Blocked@ if it retries.+try :: STM a -> STM (Tried a)+try s = fmap Done s `orElse` pure Blocked+{-# INLINE try #-}++-- | Run the STM action, and give the time that it blocked, if it blocked.+--+-- Transactions can't detect their own retry without unsafeIOToSTM horrors,+-- so blocked transactions need to run again after getting the time.+timedAtomically :: STM a -> IO (a, Maybe Seconds)+timedAtomically s = atomically (try s) >>= \case+ Done v -> pure (v, Nothing)+ Blocked -> do+ t1 <- getTime Monotonic+ v <- atomically s+ t2 <- getTime Monotonic+ pure (v, Just (t2 - t1))
lib/Control/Concurrent/TBCQueue.hs view
@@ -4,13 +4,18 @@ newTBCQueue, newTBCQueueIO, readTBCQueue,+ drainTBCQueue, writeTBCQueue,+ overwriteTBCQueue, closeTBCQueue, isOpenTBCQueue, isClosedTBCQueue,+ isEmptyTBCQueue,+ isFullTBCQueue, lengthTBCQueue ) where +import Control.Concurrent.Channel.Overwrite import Control.Concurrent.STM import Control.Monad import Numeric.Natural@@ -26,19 +31,29 @@ newTBCQueueIO :: Natural -> IO (TBCQueue a) newTBCQueueIO = atomically . newTBCQueue --- | Returns @Just value@ until the queue is closed, blocking for the next value.+-- | Reads from the queue, blocking if empty,+-- and returning @Nothing@ once it is empty and closed. readTBCQueue :: TBCQueue a -> STM (Maybe a)-readTBCQueue c = do- mv <- tryReadTBQueue c.q- case mv of- Just v -> pure $ Just v- Nothing -> do+readTBCQueue c = tryReadTBQueue c.q >>= \case+ Just v -> pure $ Just v+ Nothing -> do+ stillOpen <- readTVar c.open+ if stillOpen+ then retry+ else pure Nothing++drainTBCQueue :: TBCQueue a -> STM [a]+drainTBCQueue c = do+ f <- flushTBQueue c.q+ if null f+ then do stillOpen <- readTVar c.open if stillOpen then retry- else pure Nothing+ else pure []+ else pure f --- | Writes the given value to the queue if it's still open.+-- | Writes the given value to the queue if it's still open, blocking if full. -- -- Returns whether the queue is still open so producers know when consumers no longer care. writeTBCQueue :: TBCQueue a -> a -> STM Bool@@ -47,6 +62,22 @@ when stillOpen $ writeTBQueue c.q v pure stillOpen +overwriteTBCQueue :: TBCQueue a -> a -> STM (OverwriteResult a)+overwriteTBCQueue c !v = do+ stillOpen <- readTVar c.open+ if stillOpen+ then do+ wasFull <- isFullTBQueue c.q+ if wasFull+ then do+ o <- readTBQueue c.q+ writeTBQueue c.q v+ pure $ Overwrote o+ else do+ writeTBQueue c.q v+ pure Wrote+ else pure Closed+ -- | Closes the queue so that future writes are no-ops and readers get @Nothing@ -- after all values currently inside have been read. closeTBCQueue :: TBCQueue a -> STM ()@@ -57,6 +88,12 @@ isClosedTBCQueue :: TBCQueue a -> STM Bool isClosedTBCQueue = fmap not . isOpenTBCQueue++isEmptyTBCQueue :: TBCQueue a -> STM Bool+isEmptyTBCQueue c = isEmptyTBQueue c.q++isFullTBCQueue :: TBCQueue a -> STM Bool+isFullTBCQueue c = isFullTBQueue c.q -- | The number of items currently in the queue. --
+ lib/Control/Concurrent/TCSlot.hs view
@@ -0,0 +1,84 @@+-- | A /closeable/, single-element STM queue.+--+-- Identical to @TBCQueue@ but has a max length of 1.+-- Helpful when you want to minimize latency,+-- and has simpler ops since values are just held in a @TVar (Maybe a)@.+module Control.Concurrent.TCSlot(+ TCSlot,+ newTCSlot,+ newTCSlotIO,+ readTCSlot,+ writeTCSlot,+ overwriteTCSlot,+ closeTCSlot,+ isOpenTCSlot,+ isClosedTCSlot,+ isEmptyTCSlot,+ isFullTCSlot,+) where++import Control.Concurrent.Channel.Overwrite+import Control.Concurrent.STM+import Control.Monad+import Data.Maybe (isNothing)+++data TCSlot a = TCSlot {+ slot :: TVar (Maybe a),+ open :: TVar Bool+ }++newTCSlot :: STM (TCSlot a)+newTCSlot = TCSlot <$> newTVar Nothing <*> newTVar True++newTCSlotIO :: IO (TCSlot a)+newTCSlotIO = atomically newTCSlot++readTCSlot :: TCSlot a -> STM (Maybe a)+readTCSlot c = readTVar c.slot >>= \case+ Just v -> do+ writeTVar c.slot Nothing+ pure $ Just v+ Nothing -> do+ stillOpen <- readTVar c.open+ if stillOpen+ then retry+ else pure Nothing++writeTCSlot :: TCSlot a -> a -> STM Bool+writeTCSlot c !v = do+ stillOpen <- readTVar c.open+ when stillOpen $ do+ check . isNothing =<< readTVar c.slot+ writeTVar c.slot $ Just v+ pure stillOpen++overwriteTCSlot :: TCSlot a -> a -> STM (OverwriteResult a)+overwriteTCSlot c !v = do+ stillOpen <- readTVar c.open+ if stillOpen+ then readTVar c.slot >>= \case+ Just o -> do+ writeTVar c.slot $ Just v+ pure $ Overwrote o+ Nothing -> do+ writeTVar c.slot $ Just v+ pure Wrote+ else pure Closed++closeTCSlot :: TCSlot a -> STM ()+closeTCSlot c = writeTVar c.open False++isOpenTCSlot :: TCSlot a -> STM Bool+isOpenTCSlot c = readTVar c.open++isClosedTCSlot :: TCSlot a -> STM Bool+isClosedTCSlot = fmap not . isOpenTCSlot++isEmptyTCSlot :: TCSlot a -> STM Bool+isEmptyTCSlot = fmap not . isFullTCSlot++isFullTCSlot :: TCSlot a -> STM Bool+isFullTCSlot c = readTVar c.slot >>= \case+ Just _ -> pure True+ Nothing -> pure False