{-# OPTIONS_GHC -Wall -fwarn-tabs #-}
{-# LANGUAGE CPP, DeriveDataTypeable #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Safe #-}
#endif
module Control.Concurrent.STM.TMChan
(
TMChan()
, newTMChan
, newTMChanIO
, dupTMChan
, newBroadcastTMChan
, newBroadcastTMChanIO
, readTMChan
, tryReadTMChan
, peekTMChan
, tryPeekTMChan
, writeTMChan
, unGetTMChan
, closeTMChan
, isClosedTMChan
, isEmptyTMChan
) where
import Data.Typeable (Typeable)
#if __GLASGOW_HASKELL__ < 710
import Control.Applicative ((<$>))
#endif
import Control.Monad.STM (STM)
import Control.Concurrent.STM.TVar
import Control.Concurrent.STM.TChan
data TMChan a = TMChan
{-# UNPACK #-} !(TVar Bool)
{-# UNPACK #-} !(TChan a)
deriving Typeable
newTMChan :: STM (TMChan a)
newTMChan :: STM (TMChan a)
newTMChan = do
TVar Bool
closed <- Bool -> STM (TVar Bool)
forall a. a -> STM (TVar a)
newTVar Bool
False
TChan a
chan <- STM (TChan a)
forall a. STM (TChan a)
newTChan
TMChan a -> STM (TMChan a)
forall (m :: * -> *) a. Monad m => a -> m a
return (TVar Bool -> TChan a -> TMChan a
forall a. TVar Bool -> TChan a -> TMChan a
TMChan TVar Bool
closed TChan a
chan)
newTMChanIO :: IO (TMChan a)
newTMChanIO :: IO (TMChan a)
newTMChanIO = do
TVar Bool
closed <- Bool -> IO (TVar Bool)
forall a. a -> IO (TVar a)
newTVarIO Bool
False
TChan a
chan <- IO (TChan a)
forall a. IO (TChan a)
newTChanIO
TMChan a -> IO (TMChan a)
forall (m :: * -> *) a. Monad m => a -> m a
return (TVar Bool -> TChan a -> TMChan a
forall a. TVar Bool -> TChan a -> TMChan a
TMChan TVar Bool
closed TChan a
chan)
newBroadcastTMChan :: STM (TMChan a)
newBroadcastTMChan :: STM (TMChan a)
newBroadcastTMChan = do
TVar Bool
closed <- Bool -> STM (TVar Bool)
forall a. a -> STM (TVar a)
newTVar Bool
False
TChan a
chan <- STM (TChan a)
forall a. STM (TChan a)
newBroadcastTChan
TMChan a -> STM (TMChan a)
forall (m :: * -> *) a. Monad m => a -> m a
return (TVar Bool -> TChan a -> TMChan a
forall a. TVar Bool -> TChan a -> TMChan a
TMChan TVar Bool
closed TChan a
chan)
newBroadcastTMChanIO :: IO (TMChan a)
newBroadcastTMChanIO :: IO (TMChan a)
newBroadcastTMChanIO = do
TVar Bool
closed <- Bool -> IO (TVar Bool)
forall a. a -> IO (TVar a)
newTVarIO Bool
False
TChan a
chan <- IO (TChan a)
forall a. IO (TChan a)
newBroadcastTChanIO
TMChan a -> IO (TMChan a)
forall (m :: * -> *) a. Monad m => a -> m a
return (TVar Bool -> TChan a -> TMChan a
forall a. TVar Bool -> TChan a -> TMChan a
TMChan TVar Bool
closed TChan a
chan)
dupTMChan :: TMChan a -> STM (TMChan a)
dupTMChan :: TMChan a -> STM (TMChan a)
dupTMChan (TMChan TVar Bool
closed TChan a
chan) = do
TChan a
new_chan <- TChan a -> STM (TChan a)
forall a. TChan a -> STM (TChan a)
dupTChan TChan a
chan
TMChan a -> STM (TMChan a)
forall (m :: * -> *) a. Monad m => a -> m a
return (TVar Bool -> TChan a -> TMChan a
forall a. TVar Bool -> TChan a -> TMChan a
TMChan TVar Bool
closed TChan a
new_chan)
readTMChan :: TMChan a -> STM (Maybe a)
readTMChan :: TMChan a -> STM (Maybe a)
readTMChan (TMChan TVar Bool
closed TChan a
chan) = do
Bool
b <- TVar Bool -> STM Bool
forall a. TVar a -> STM a
readTVar TVar Bool
closed
if Bool
b
then TChan a -> STM (Maybe a)
forall a. TChan a -> STM (Maybe a)
tryReadTChan TChan a
chan
else a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> STM a -> STM (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TChan a -> STM a
forall a. TChan a -> STM a
readTChan TChan a
chan
tryReadTMChan :: TMChan a -> STM (Maybe (Maybe a))
tryReadTMChan :: TMChan a -> STM (Maybe (Maybe a))
tryReadTMChan (TMChan TVar Bool
closed TChan a
chan) = do
Bool
b <- TVar Bool -> STM Bool
forall a. TVar a -> STM a
readTVar TVar Bool
closed
if Bool
b
then (a -> Maybe a) -> Maybe a -> Maybe (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Maybe a
forall a. a -> Maybe a
Just (Maybe a -> Maybe (Maybe a))
-> STM (Maybe a) -> STM (Maybe (Maybe a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TChan a -> STM (Maybe a)
forall a. TChan a -> STM (Maybe a)
tryReadTChan TChan a
chan
else Maybe a -> Maybe (Maybe a)
forall a. a -> Maybe a
Just (Maybe a -> Maybe (Maybe a))
-> STM (Maybe a) -> STM (Maybe (Maybe a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TChan a -> STM (Maybe a)
forall a. TChan a -> STM (Maybe a)
tryReadTChan TChan a
chan
peekTMChan :: TMChan a -> STM (Maybe a)
peekTMChan :: TMChan a -> STM (Maybe a)
peekTMChan (TMChan TVar Bool
closed TChan a
chan) = do
Bool
b <- TVar Bool -> STM Bool
forall a. TVar a -> STM a
readTVar TVar Bool
closed
if Bool
b
then do
Bool
b' <- TChan a -> STM Bool
forall a. TChan a -> STM Bool
isEmptyTChan TChan a
chan
if Bool
b'
then Maybe a -> STM (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
else a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> STM a -> STM (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TChan a -> STM a
forall a. TChan a -> STM a
peekTChan TChan a
chan
else a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> STM a -> STM (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TChan a -> STM a
forall a. TChan a -> STM a
peekTChan TChan a
chan
tryPeekTMChan :: TMChan a -> STM (Maybe (Maybe a))
tryPeekTMChan :: TMChan a -> STM (Maybe (Maybe a))
tryPeekTMChan (TMChan TVar Bool
closed TChan a
chan) = do
Bool
b <- TVar Bool -> STM Bool
forall a. TVar a -> STM a
readTVar TVar Bool
closed
if Bool
b
then (a -> Maybe a) -> Maybe a -> Maybe (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Maybe a
forall a. a -> Maybe a
Just (Maybe a -> Maybe (Maybe a))
-> STM (Maybe a) -> STM (Maybe (Maybe a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TChan a -> STM (Maybe a)
forall a. TChan a -> STM (Maybe a)
tryPeekTChan TChan a
chan
else Maybe a -> Maybe (Maybe a)
forall a. a -> Maybe a
Just (Maybe a -> Maybe (Maybe a))
-> STM (Maybe a) -> STM (Maybe (Maybe a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TChan a -> STM (Maybe a)
forall a. TChan a -> STM (Maybe a)
tryPeekTChan TChan a
chan
writeTMChan :: TMChan a -> a -> STM ()
writeTMChan :: TMChan a -> a -> STM ()
writeTMChan (TMChan TVar Bool
closed TChan a
chan) a
x = do
Bool
b <- TVar Bool -> STM Bool
forall a. TVar a -> STM a
readTVar TVar Bool
closed
if Bool
b
then () -> STM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
else TChan a -> a -> STM ()
forall a. TChan a -> a -> STM ()
writeTChan TChan a
chan a
x
unGetTMChan :: TMChan a -> a -> STM ()
unGetTMChan :: TMChan a -> a -> STM ()
unGetTMChan (TMChan TVar Bool
closed TChan a
chan) a
x = do
Bool
b <- TVar Bool -> STM Bool
forall a. TVar a -> STM a
readTVar TVar Bool
closed
if Bool
b
then () -> STM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
else TChan a -> a -> STM ()
forall a. TChan a -> a -> STM ()
unGetTChan TChan a
chan a
x
closeTMChan :: TMChan a -> STM ()
closeTMChan :: TMChan a -> STM ()
closeTMChan (TMChan TVar Bool
closed TChan a
_chan) =
TVar Bool -> Bool -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar Bool
closed Bool
True
isClosedTMChan :: TMChan a -> STM Bool
isClosedTMChan :: TMChan a -> STM Bool
isClosedTMChan (TMChan TVar Bool
closed TChan a
_chan) =
TVar Bool -> STM Bool
forall a. TVar a -> STM a
readTVar TVar Bool
closed
isEmptyTMChan :: TMChan a -> STM Bool
isEmptyTMChan :: TMChan a -> STM Bool
isEmptyTMChan (TMChan TVar Bool
_closed TChan a
chan) =
TChan a -> STM Bool
forall a. TChan a -> STM Bool
isEmptyTChan TChan a
chan