{-# OPTIONS_GHC -Wall -fwarn-tabs #-}
{-# LANGUAGE CPP, DeriveDataTypeable #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Safe #-}
#endif
module Control.Concurrent.STM.TMQueue
(
TMQueue()
, newTMQueue
, newTMQueueIO
, readTMQueue
, tryReadTMQueue
, peekTMQueue
, tryPeekTMQueue
, writeTMQueue
, unGetTMQueue
, closeTMQueue
, isClosedTMQueue
, isEmptyTMQueue
) 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.TQueue
data TMQueue a = TMQueue
{-# UNPACK #-} !(TVar Bool)
{-# UNPACK #-} !(TQueue a)
deriving Typeable
newTMQueue :: STM (TMQueue a)
newTMQueue :: forall a. STM (TMQueue a)
newTMQueue = do
TVar Bool
closed <- Bool -> STM (TVar Bool)
forall a. a -> STM (TVar a)
newTVar Bool
False
TQueue a
queue <- STM (TQueue a)
forall a. STM (TQueue a)
newTQueue
TMQueue a -> STM (TMQueue a)
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return (TVar Bool -> TQueue a -> TMQueue a
forall a. TVar Bool -> TQueue a -> TMQueue a
TMQueue TVar Bool
closed TQueue a
queue)
newTMQueueIO :: IO (TMQueue a)
newTMQueueIO :: forall a. IO (TMQueue a)
newTMQueueIO = do
TVar Bool
closed <- Bool -> IO (TVar Bool)
forall a. a -> IO (TVar a)
newTVarIO Bool
False
TQueue a
queue <- IO (TQueue a)
forall a. IO (TQueue a)
newTQueueIO
TMQueue a -> IO (TMQueue a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (TVar Bool -> TQueue a -> TMQueue a
forall a. TVar Bool -> TQueue a -> TMQueue a
TMQueue TVar Bool
closed TQueue a
queue)
readTMQueue :: TMQueue a -> STM (Maybe a)
readTMQueue :: forall a. TMQueue a -> STM (Maybe a)
readTMQueue (TMQueue TVar Bool
closed TQueue a
queue) = do
Bool
b <- TVar Bool -> STM Bool
forall a. TVar a -> STM a
readTVar TVar Bool
closed
if Bool
b
then TQueue a -> STM (Maybe a)
forall a. TQueue a -> STM (Maybe a)
tryReadTQueue TQueue a
queue
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
<$> TQueue a -> STM a
forall a. TQueue a -> STM a
readTQueue TQueue a
queue
tryReadTMQueue :: TMQueue a -> STM (Maybe (Maybe a))
tryReadTMQueue :: forall a. TMQueue a -> STM (Maybe (Maybe a))
tryReadTMQueue (TMQueue TVar Bool
closed TQueue a
queue) = 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 a b. (a -> b) -> Maybe a -> Maybe b
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
<$> TQueue a -> STM (Maybe a)
forall a. TQueue a -> STM (Maybe a)
tryReadTQueue TQueue a
queue
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
<$> TQueue a -> STM (Maybe a)
forall a. TQueue a -> STM (Maybe a)
tryReadTQueue TQueue a
queue
peekTMQueue :: TMQueue a -> STM (Maybe a)
peekTMQueue :: forall a. TMQueue a -> STM (Maybe a)
peekTMQueue (TMQueue TVar Bool
closed TQueue a
queue) = do
Bool
b <- TVar Bool -> STM Bool
forall a. TVar a -> STM a
readTVar TVar Bool
closed
if Bool
b
then do
Bool
b' <- TQueue a -> STM Bool
forall a. TQueue a -> STM Bool
isEmptyTQueue TQueue a
queue
if Bool
b'
then Maybe a -> STM (Maybe a)
forall a. a -> STM 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
<$> TQueue a -> STM a
forall a. TQueue a -> STM a
peekTQueue TQueue a
queue
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
<$> TQueue a -> STM a
forall a. TQueue a -> STM a
peekTQueue TQueue a
queue
tryPeekTMQueue :: TMQueue a -> STM (Maybe (Maybe a))
tryPeekTMQueue :: forall a. TMQueue a -> STM (Maybe (Maybe a))
tryPeekTMQueue (TMQueue TVar Bool
closed TQueue a
queue) = 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 a b. (a -> b) -> Maybe a -> Maybe b
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
<$> TQueue a -> STM (Maybe a)
forall a. TQueue a -> STM (Maybe a)
tryPeekTQueue TQueue a
queue
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
<$> TQueue a -> STM (Maybe a)
forall a. TQueue a -> STM (Maybe a)
tryPeekTQueue TQueue a
queue
writeTMQueue :: TMQueue a -> a -> STM ()
writeTMQueue :: forall a. TMQueue a -> a -> STM ()
writeTMQueue (TMQueue TVar Bool
closed TQueue a
queue) 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 a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
else TQueue a -> a -> STM ()
forall a. TQueue a -> a -> STM ()
writeTQueue TQueue a
queue a
x
unGetTMQueue :: TMQueue a -> a -> STM ()
unGetTMQueue :: forall a. TMQueue a -> a -> STM ()
unGetTMQueue (TMQueue TVar Bool
closed TQueue a
queue) 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 a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
else TQueue a -> a -> STM ()
forall a. TQueue a -> a -> STM ()
unGetTQueue TQueue a
queue a
x
closeTMQueue :: TMQueue a -> STM ()
closeTMQueue :: forall a. TMQueue a -> STM ()
closeTMQueue (TMQueue TVar Bool
closed TQueue a
_queue) =
TVar Bool -> Bool -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar Bool
closed Bool
True
isClosedTMQueue :: TMQueue a -> STM Bool
isClosedTMQueue :: forall a. TMQueue a -> STM Bool
isClosedTMQueue (TMQueue TVar Bool
closed TQueue a
_queue) =
TVar Bool -> STM Bool
forall a. TVar a -> STM a
readTVar TVar Bool
closed
isEmptyTMQueue :: TMQueue a -> STM Bool
isEmptyTMQueue :: forall a. TMQueue a -> STM Bool
isEmptyTMQueue (TMQueue TVar Bool
_closed TQueue a
queue) =
TQueue a -> STM Bool
forall a. TQueue a -> STM Bool
isEmptyTQueue TQueue a
queue