Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flushTBQueue and provide a test #77

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 27 additions & 0 deletions testsuite/src/Issue76.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{-# LANGUAGE CPP #-}

-- See https://github.com/haskell/stm/pull/76.
--
-- Test-case contributed by Nikita Volkov <[email protected]>.
--
-- This bug is observable in version `stm-2.5.2.1`.

module Issue76 (main) where

import Control.Concurrent.STM
import Test.HUnit

main :: IO ()
#if MIN_VERSION_stm(2,4,5)
main = do
queue <- newTBQueueIO 100 :: IO (TBQueue Int)
lengthAfterFlush <- atomically $ do
writeTBQueue queue 1
writeTBQueue queue 2
_ <- flushTBQueue queue
lengthTBQueue queue
assertEqual "" 0 lengthAfterFlush
#else
-- test-case not applicable; `flushTBQueue` was only added in 2.4.5.0
main = return ()
#endif
2 changes: 2 additions & 0 deletions testsuite/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Test.Framework (defaultMain, testGroup)
import Test.Framework.Providers.HUnit

import qualified Issue9
import qualified Issue76
import qualified Stm052
import qualified Stm064
import qualified Stm065
Expand All @@ -18,6 +19,7 @@ main = do
tests = [
testGroup "regression"
[ testCase "issue #9" Issue9.main
, testCase "issue #76" Issue76.main
, testCase "stm052" Stm052.main
, testCase "stm064" Stm064.main
, testCase "stm065" Stm065.main
Expand Down
1 change: 1 addition & 0 deletions testsuite/testsuite.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test-suite stm
main-is: Main.hs
other-modules:
Issue9
Issue76
Stm052
Stm064
Stm065
Expand Down