module Control.Concurrent.Classy.STM.TQueue
(
TQueue
, newTQueue
, readTQueue
, tryReadTQueue
, flushTQueue
, peekTQueue
, tryPeekTQueue
, writeTQueue
, unGetTQueue
, isEmptyTQueue
) where
import Control.Monad (unless)
import Control.Monad.STM.Class
data TQueue stm a = TQueue (TVar stm [a])
(TVar stm [a])
newTQueue :: MonadSTM stm => stm (TQueue stm a)
newTQueue :: forall (stm :: * -> *) a. MonadSTM stm => stm (TQueue stm a)
newTQueue = do
TVar stm [a]
readT <- forall (stm :: * -> *) a. MonadSTM stm => a -> stm (TVar stm a)
newTVar []
TVar stm [a]
writeT <- forall (stm :: * -> *) a. MonadSTM stm => a -> stm (TVar stm a)
newTVar []
forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall (stm :: * -> *) a.
TVar stm [a] -> TVar stm [a] -> TQueue stm a
TQueue TVar stm [a]
readT TVar stm [a]
writeT)
writeTQueue :: MonadSTM stm => TQueue stm a -> a -> stm ()
writeTQueue :: forall (stm :: * -> *) a.
MonadSTM stm =>
TQueue stm a -> a -> stm ()
writeTQueue (TQueue TVar stm [a]
_ TVar stm [a]
writeT) a
a = do
[a]
listend <- forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> stm a
readTVar TVar stm [a]
writeT
forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> a -> stm ()
writeTVar TVar stm [a]
writeT (a
aforall a. a -> [a] -> [a]
:[a]
listend)
readTQueue :: MonadSTM stm => TQueue stm a -> stm a
readTQueue :: forall (stm :: * -> *) a. MonadSTM stm => TQueue stm a -> stm a
readTQueue (TQueue TVar stm [a]
readT TVar stm [a]
writeT) = do
[a]
xs <- forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> stm a
readTVar TVar stm [a]
readT
case [a]
xs of
(a
x:[a]
xs') -> do
forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> a -> stm ()
writeTVar TVar stm [a]
readT [a]
xs'
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
[] -> do
[a]
ys <- forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> stm a
readTVar TVar stm [a]
writeT
case [a]
ys of
[] -> forall (stm :: * -> *) a. MonadSTM stm => stm a
retry
[a]
_ -> do
let (a
z:[a]
zs) = forall a. [a] -> [a]
reverse [a]
ys
forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> a -> stm ()
writeTVar TVar stm [a]
writeT []
forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> a -> stm ()
writeTVar TVar stm [a]
readT [a]
zs
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
z
tryReadTQueue :: MonadSTM stm => TQueue stm a -> stm (Maybe a)
tryReadTQueue :: forall (stm :: * -> *) a.
MonadSTM stm =>
TQueue stm a -> stm (Maybe a)
tryReadTQueue TQueue stm a
c = (forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (stm :: * -> *) a. MonadSTM stm => TQueue stm a -> stm a
readTQueue TQueue stm a
c) forall (stm :: * -> *) a. MonadSTM stm => stm a -> stm a -> stm a
`orElse` forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
flushTQueue :: MonadSTM stm => TQueue stm a -> stm [a]
flushTQueue :: forall (stm :: * -> *) a. MonadSTM stm => TQueue stm a -> stm [a]
flushTQueue (TQueue TVar stm [a]
r TVar stm [a]
w) = do
[a]
xs <- forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> stm a
readTVar TVar stm [a]
r
[a]
ys <- forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> stm a
readTVar TVar stm [a]
w
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs) forall a b. (a -> b) -> a -> b
$ forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> a -> stm ()
writeTVar TVar stm [a]
r []
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
ys) forall a b. (a -> b) -> a -> b
$ forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> a -> stm ()
writeTVar TVar stm [a]
w []
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([a]
xs forall a. [a] -> [a] -> [a]
++ forall a. [a] -> [a]
reverse [a]
ys)
peekTQueue :: MonadSTM stm => TQueue stm a -> stm a
peekTQueue :: forall (stm :: * -> *) a. MonadSTM stm => TQueue stm a -> stm a
peekTQueue (TQueue TVar stm [a]
readT TVar stm [a]
writeT) = do
[a]
xs <- forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> stm a
readTVar TVar stm [a]
readT
case [a]
xs of
(a
x:[a]
_) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
[] -> do
[a]
ys <- forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> stm a
readTVar TVar stm [a]
writeT
case [a]
ys of
[] -> forall (stm :: * -> *) a. MonadSTM stm => stm a
retry
[a]
_ -> do
let (a
z:[a]
zs) = forall a. [a] -> [a]
reverse [a]
ys
forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> a -> stm ()
writeTVar TVar stm [a]
writeT []
forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> a -> stm ()
writeTVar TVar stm [a]
readT (a
zforall a. a -> [a] -> [a]
:[a]
zs)
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
z
tryPeekTQueue :: MonadSTM stm => TQueue stm a -> stm (Maybe a)
tryPeekTQueue :: forall (stm :: * -> *) a.
MonadSTM stm =>
TQueue stm a -> stm (Maybe a)
tryPeekTQueue TQueue stm a
c = do
Maybe a
m <- forall (stm :: * -> *) a.
MonadSTM stm =>
TQueue stm a -> stm (Maybe a)
tryReadTQueue TQueue stm a
c
case Maybe a
m of
Maybe a
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
Just a
x -> do
forall (stm :: * -> *) a.
MonadSTM stm =>
TQueue stm a -> a -> stm ()
unGetTQueue TQueue stm a
c a
x
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
m
unGetTQueue :: MonadSTM stm => TQueue stm a -> a -> stm ()
unGetTQueue :: forall (stm :: * -> *) a.
MonadSTM stm =>
TQueue stm a -> a -> stm ()
unGetTQueue (TQueue TVar stm [a]
readT TVar stm [a]
_) a
a = do
[a]
xs <- forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> stm a
readTVar TVar stm [a]
readT
forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> a -> stm ()
writeTVar TVar stm [a]
readT (a
aforall a. a -> [a] -> [a]
:[a]
xs)
isEmptyTQueue :: MonadSTM stm => TQueue stm a -> stm Bool
isEmptyTQueue :: forall (stm :: * -> *) a. MonadSTM stm => TQueue stm a -> stm Bool
isEmptyTQueue (TQueue TVar stm [a]
readT TVar stm [a]
writeT) = do
[a]
xs <- forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> stm a
readTVar TVar stm [a]
readT
case [a]
xs of
(a
_:[a]
_) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
[] -> forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> stm a
readTVar TVar stm [a]
writeT