From a77aae736a98a605dedc4669d8a4cc55fb2d160e Mon Sep 17 00:00:00 2001 From: Bas van Dijk Date: Tue, 20 Mar 2018 22:31:36 +0100 Subject: [PATCH] flushTQueue: only perform writeTVar when necessary To prevent unnecessarily invalidating other transactions writeTVar must only be called when its contents should actually be changed. --- Control/Concurrent/STM/TQueue.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Control/Concurrent/STM/TQueue.hs b/Control/Concurrent/STM/TQueue.hs index b49e310..483db15 100644 --- a/Control/Concurrent/STM/TQueue.hs +++ b/Control/Concurrent/STM/TQueue.hs @@ -47,7 +47,7 @@ module Control.Concurrent.STM.TQueue ( ) where import GHC.Conc - +import Control.Monad (unless) import Data.Typeable (Typeable) -- | 'TQueue' is an abstract type representing an unbounded FIFO channel. @@ -115,8 +115,8 @@ flushTQueue :: TQueue a -> STM [a] flushTQueue (TQueue read write) = do xs <- readTVar read ys <- readTVar write - writeTVar read [] - writeTVar write [] + unless (null xs) $ writeTVar read [] + unless (null ys) $ writeTVar write [] return (xs ++ reverse ys) -- | Get the next value from the @TQueue@ without removing it,