Skip to content

Commit

Permalink
Provide a quick fix of flushTBQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-volkov committed Oct 19, 2023
1 parent a630bae commit dce77da
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions Control/Concurrent/STM/TBQueue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,15 @@ tryReadTBQueue q = fmap Just (readTBQueue q) `orElse` pure Nothing
--
-- @since 2.4.5
flushTBQueue :: forall a. TBQueue a -> STM [a]
flushTBQueue (TBQueue _rindex windex elements cap) = do
w <- readTVar windex
go (decMod w cap) []
where
go :: Int -> [a] -> STM [a]
go i acc = do
ele <- unsafeRead elements i
case ele of
Nothing -> pure acc
Just a -> do
unsafeWrite elements i Nothing
go (decMod i cap) (a : acc)
flushTBQueue queue =
-- TODO: Optimize.
go []
where
go acc = do
tryReadResult <- tryReadTBQueue queue
case tryReadResult of
Just element -> go $ element : acc
Nothing -> return $ reverse acc

-- | Get the next value from the @TBQueue@ without removing it,
-- retrying if the queue is empty.
Expand Down

0 comments on commit dce77da

Please sign in to comment.