Skip to content

Commit a77aae7

Browse files
committed
flushTQueue: only perform writeTVar when necessary
To prevent unnecessarily invalidating other transactions writeTVar must only be called when its contents should actually be changed.
1 parent 5ea70d4 commit a77aae7

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Control/Concurrent/STM/TQueue.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module Control.Concurrent.STM.TQueue (
4747
) where
4848

4949
import GHC.Conc
50-
50+
import Control.Monad (unless)
5151
import Data.Typeable (Typeable)
5252

5353
-- | 'TQueue' is an abstract type representing an unbounded FIFO channel.
@@ -115,8 +115,8 @@ flushTQueue :: TQueue a -> STM [a]
115115
flushTQueue (TQueue read write) = do
116116
xs <- readTVar read
117117
ys <- readTVar write
118-
writeTVar read []
119-
writeTVar write []
118+
unless (null xs) $ writeTVar read []
119+
unless (null ys) $ writeTVar write []
120120
return (xs ++ reverse ys)
121121

122122
-- | Get the next value from the @TQueue@ without removing it,

0 commit comments

Comments
 (0)