Skip to content

Add snapshotTBQueue #56

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions Control/Concurrent/STM/TBQueue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module Control.Concurrent.STM.TBQueue (
newTBQueueIO,
readTBQueue,
tryReadTBQueue,
snapshotTBQueue,
flushTBQueue,
peekTBQueue,
tryPeekTBQueue,
Expand Down Expand Up @@ -146,6 +147,14 @@ readTBQueue (TBQueue rsize read _wsize write _size) = do
tryReadTBQueue :: TBQueue a -> STM (Maybe a)
tryReadTBQueue c = fmap Just (readTBQueue c) `orElse` return Nothing

-- | Efficiently read the entire contents of a 'TBQueue' into a list without changing queue contents.
-- This function never retries.
snapshotTBQueue :: TBQueue a -> STM [a]
snapshotTBQueue (TBQueue _ read _ write _) = do
xs <- readTVar read
ys <- readTVar write
return (xs ++ reverse ys)

-- | Efficiently read the entire contents of a 'TBQueue' into a list. This
-- function never retries.
--
Expand Down
9 changes: 9 additions & 0 deletions Control/Concurrent/STM/TQueue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module Control.Concurrent.STM.TQueue (
newTQueueIO,
readTQueue,
tryReadTQueue,
snapshotTQueue,
flushTQueue,
peekTQueue,
tryPeekTQueue,
Expand Down Expand Up @@ -108,6 +109,14 @@ readTQueue (TQueue read write) = do
tryReadTQueue :: TQueue a -> STM (Maybe a)
tryReadTQueue c = fmap Just (readTQueue c) `orElse` return Nothing

-- | Efficiently read the entire contents of a 'TQueue' into a list without changing queue contents.
-- This function never retries.
snapshotTQueue :: TQueue a -> STM [a]
snapshotTQueue (TQueue read write) = do
xs <- readTVar read
ys <- readTVar write
return (xs ++ reverse ys)

-- | Efficiently read the entire contents of a 'TQueue' into a list. This
-- function never retries.
--
Expand Down
3 changes: 3 additions & 0 deletions testsuite/src/Issue17.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ assertEmptyTBQueue queue = do
atomically (tryPeekTBQueue queue) >>=
assertEqual "Expected empty: tryPeekTBQueue should return Nothing" Nothing

atomically (snapshotTBQueue queue) >>=
assertEqual "Expected empty: snapshotTBQueue should return []" []

atomically (flushTBQueue queue) >>=
assertEqual "Expected empty: flushTBQueue should return []" []

Expand Down
3 changes: 3 additions & 0 deletions testsuite/src/Issue9.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ main = do
-- Read 1
1 <- atomically (readTBQueue queue)

-- Snapshot [2..5]
[2,3,4,5] <- atomically (snapshotTBQueue queue)

-- Flush [2..5]
[2,3,4,5] <- atomically (flushTBQueue queue)

Expand Down