BChan: add writeBChanNonBlocking, raise STM lower bound to 2.4.3

This commit is contained in:
Jonathan Daugherty 2019-09-13 10:20:12 -07:00
parent 5baad2bc63
commit e0fdfc7786
2 changed files with 12 additions and 1 deletions

View File

@ -101,7 +101,7 @@ library
config-ini,
vector,
contravariant,
stm >= 2.4,
stm >= 2.4.3,
text,
text-zipper >= 0.7.1,
template-haskell,

View File

@ -2,6 +2,7 @@ module Brick.BChan
( BChan
, newBChan
, writeBChan
, writeBChanNonBlocking
, readBChan
, readBChan2
)
@ -26,6 +27,16 @@ newBChan size = atomically $ BChan <$> newTBQueue (fromIntegral size)
writeBChan :: BChan a -> a -> IO ()
writeBChan (BChan q) a = atomically $ writeTBQueue q a
-- | Attempts to write a value to a @BChan@. If the channel has room,
-- the value is written and this returns 'True'. Otherwise this returns
-- 'False' and returns immediately.
writeBChanNonBlocking :: BChan a -> a -> IO Bool
writeBChanNonBlocking (BChan q) a = atomically $ do
f <- isFullTBQueue q
if f
then return False
else writeTBQueue q a >> return True
-- | Reads the next value from the @BChan@; blocks if necessary.
readBChan :: BChan a -> IO a
readBChan (BChan q) = atomically $ readTBQueue q