{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# LANGUAGE CPP, DeriveDataTypeable #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
module Control.Concurrent.STM.TChan (
#ifdef __GLASGOW_HASKELL__
TChan,
newTChan,
newTChanIO,
newBroadcastTChan,
newBroadcastTChanIO,
dupTChan,
cloneTChan,
readTChan,
tryReadTChan,
peekTChan,
tryPeekTChan,
writeTChan,
unGetTChan,
isEmptyTChan
#endif
) where
#ifdef __GLASGOW_HASKELL__
import GHC.Conc
import Data.Typeable (Typeable)
#define _UPK_(x) {-# UNPACK #-} !(x)
data TChan a = TChan _UPK_(TVar (TVarList a))
_UPK_(TVar (TVarList a))
deriving (TChan a -> TChan a -> Bool
(TChan a -> TChan a -> Bool)
-> (TChan a -> TChan a -> Bool) -> Eq (TChan a)
forall a. TChan a -> TChan a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. TChan a -> TChan a -> Bool
== :: TChan a -> TChan a -> Bool
$c/= :: forall a. TChan a -> TChan a -> Bool
/= :: TChan a -> TChan a -> Bool
Eq, Typeable)
type TVarList a = TVar (TList a)
data TList a = TNil | TCons a _UPK_(TVarList a)
newTChan :: STM (TChan a)
newTChan :: forall a. STM (TChan a)
newTChan = do
hole <- TList a -> STM (TVar (TList a))
forall a. a -> STM (TVar a)
newTVar TList a
forall a. TList a
TNil
read <- newTVar hole
write <- newTVar hole
return (TChan read write)
newTChanIO :: IO (TChan a)
newTChanIO :: forall a. IO (TChan a)
newTChanIO = do
hole <- TList a -> IO (TVar (TList a))
forall a. a -> IO (TVar a)
newTVarIO TList a
forall a. TList a
TNil
read <- newTVarIO hole
write <- newTVarIO hole
return (TChan read write)
newBroadcastTChan :: STM (TChan a)
newBroadcastTChan :: forall a. STM (TChan a)
newBroadcastTChan = do
write_hole <- TList a -> STM (TVar (TList a))
forall a. a -> STM (TVar a)
newTVar TList a
forall a. TList a
TNil
read <- newTVar (error "reading from a TChan created by newBroadcastTChan; use dupTChan first")
write <- newTVar write_hole
return (TChan read write)
newBroadcastTChanIO :: IO (TChan a)
newBroadcastTChanIO :: forall a. IO (TChan a)
newBroadcastTChanIO = do
write_hole <- TList a -> IO (TVar (TList a))
forall a. a -> IO (TVar a)
newTVarIO TList a
forall a. TList a
TNil
read <- newTVarIO (error "reading from a TChan created by newBroadcastTChanIO; use dupTChan first")
write <- newTVarIO write_hole
return (TChan read write)
writeTChan :: TChan a -> a -> STM ()
writeTChan :: forall a. TChan a -> a -> STM ()
writeTChan (TChan TVar (TVarList a)
_read TVar (TVarList a)
write) a
a = do
listend <- TVar (TVarList a) -> STM (TVarList a)
forall a. TVar a -> STM a
readTVar TVar (TVarList a)
write
new_listend <- newTVar TNil
writeTVar listend (TCons a new_listend)
writeTVar write new_listend
readTChan :: TChan a -> STM a
readTChan :: forall a. TChan a -> STM a
readTChan (TChan TVar (TVarList a)
read TVar (TVarList a)
_write) = do
listhead <- TVar (TVarList a) -> STM (TVarList a)
forall a. TVar a -> STM a
readTVar TVar (TVarList a)
read
head <- readTVar listhead
case head of
TList a
TNil -> STM a
forall a. STM a
retry
TCons a
a TVarList a
tail -> do
TVar (TVarList a) -> TVarList a -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (TVarList a)
read TVarList a
tail
a -> STM a
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
tryReadTChan :: TChan a -> STM (Maybe a)
tryReadTChan :: forall a. TChan a -> STM (Maybe a)
tryReadTChan (TChan TVar (TVarList a)
read TVar (TVarList a)
_write) = do
listhead <- TVar (TVarList a) -> STM (TVarList a)
forall a. TVar a -> STM a
readTVar TVar (TVarList a)
read
head <- readTVar listhead
case head of
TList a
TNil -> 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
TCons a
a TVarList a
tl -> do
TVar (TVarList a) -> TVarList a -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (TVarList a)
read TVarList a
tl
Maybe a -> STM (Maybe a)
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
peekTChan :: TChan a -> STM a
peekTChan :: forall a. TChan a -> STM a
peekTChan (TChan TVar (TVarList a)
read TVar (TVarList a)
_write) = do
listhead <- TVar (TVarList a) -> STM (TVarList a)
forall a. TVar a -> STM a
readTVar TVar (TVarList a)
read
head <- readTVar listhead
case head of
TList a
TNil -> STM a
forall a. STM a
retry
TCons a
a TVarList a
_ -> a -> STM a
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
tryPeekTChan :: TChan a -> STM (Maybe a)
tryPeekTChan :: forall a. TChan a -> STM (Maybe a)
tryPeekTChan (TChan TVar (TVarList a)
read TVar (TVarList a)
_write) = do
listhead <- TVar (TVarList a) -> STM (TVarList a)
forall a. TVar a -> STM a
readTVar TVar (TVarList a)
read
head <- readTVar listhead
case head of
TList a
TNil -> 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
TCons a
a TVarList a
_ -> Maybe a -> STM (Maybe a)
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
dupTChan :: TChan a -> STM (TChan a)
dupTChan :: forall a. TChan a -> STM (TChan a)
dupTChan (TChan TVar (TVarList a)
_read TVar (TVarList a)
write) = do
hole <- TVar (TVarList a) -> STM (TVarList a)
forall a. TVar a -> STM a
readTVar TVar (TVarList a)
write
new_read <- newTVar hole
return (TChan new_read write)
unGetTChan :: TChan a -> a -> STM ()
unGetTChan :: forall a. TChan a -> a -> STM ()
unGetTChan (TChan TVar (TVarList a)
read TVar (TVarList a)
_write) a
a = do
listhead <- TVar (TVarList a) -> STM (TVarList a)
forall a. TVar a -> STM a
readTVar TVar (TVarList a)
read
newhead <- newTVar (TCons a listhead)
writeTVar read newhead
isEmptyTChan :: TChan a -> STM Bool
isEmptyTChan :: forall a. TChan a -> STM Bool
isEmptyTChan (TChan TVar (TVarList a)
read TVar (TVarList a)
_write) = do
listhead <- TVar (TVarList a) -> STM (TVarList a)
forall a. TVar a -> STM a
readTVar TVar (TVarList a)
read
head <- readTVar listhead
case head of
TList a
TNil -> Bool -> STM Bool
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
TCons a
_ TVarList a
_ -> Bool -> STM Bool
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
cloneTChan :: TChan a -> STM (TChan a)
cloneTChan :: forall a. TChan a -> STM (TChan a)
cloneTChan (TChan TVar (TVarList a)
read TVar (TVarList a)
write) = do
readpos <- TVar (TVarList a) -> STM (TVarList a)
forall a. TVar a -> STM a
readTVar TVar (TVarList a)
read
new_read <- newTVar readpos
return (TChan new_read write)
#endif