module Brick.BChan
( BChan
, newBChan
, writeBChan
, readBChan
, readBChan2
)
where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>))
#endif
import Control.Concurrent.STM.TBQueue
import Control.Monad.STM (atomically, orElse)
data BChan a = BChan (TBQueue a)
newBChan :: Int
-> IO (BChan a)
newBChan size = atomically $ BChan <$> newTBQueue size
writeBChan :: BChan a -> a -> IO ()
writeBChan (BChan q) a = atomically $ writeTBQueue q a
readBChan :: BChan a -> IO a
readBChan (BChan q) = atomically $ readTBQueue q
readBChan2 :: BChan a -> BChan b -> IO (Either a b)
readBChan2 (BChan q1) (BChan q2) = atomically $
(Left <$> readTBQueue q1) `orElse` (Right <$> readTBQueue q2)