module Network.HTTP2.Priority.Queue (
Precedence(..)
, TPriorityQueue
, new
, isEmpty
, enqueue
, dequeue
, delete
) where
import Control.Concurrent.STM
import Network.HTTP2.Priority.PSQ (PriorityQueue, Key, Precedence(..))
import qualified Network.HTTP2.Priority.PSQ as Q
newtype TPriorityQueue a = TPriorityQueue (TVar (PriorityQueue a))
new :: STM (TPriorityQueue a)
new :: forall a. STM (TPriorityQueue a)
new = forall a. TVar (PriorityQueue a) -> TPriorityQueue a
TPriorityQueue forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. a -> STM (TVar a)
newTVar forall a. PriorityQueue a
Q.empty
isEmpty :: TPriorityQueue a -> STM Bool
isEmpty :: forall a. TPriorityQueue a -> STM Bool
isEmpty (TPriorityQueue TVar (PriorityQueue a)
th) = forall a. PriorityQueue a -> Bool
Q.isEmpty forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. TVar a -> STM a
readTVar TVar (PriorityQueue a)
th
enqueue :: TPriorityQueue a -> Key -> Precedence -> a -> STM ()
enqueue :: forall a. TPriorityQueue a -> Key -> Precedence -> a -> STM ()
enqueue (TPriorityQueue TVar (PriorityQueue a)
th) Key
k Precedence
p a
v = forall a. TVar a -> (a -> a) -> STM ()
modifyTVar' TVar (PriorityQueue a)
th forall a b. (a -> b) -> a -> b
$ forall a.
Key -> Precedence -> a -> PriorityQueue a -> PriorityQueue a
Q.enqueue Key
k Precedence
p a
v
dequeue :: TPriorityQueue a -> STM (Key, Precedence, a)
dequeue :: forall a. TPriorityQueue a -> STM (Key, Precedence, a)
dequeue (TPriorityQueue TVar (PriorityQueue a)
th) = do
PriorityQueue a
h <- forall a. TVar a -> STM a
readTVar TVar (PriorityQueue a)
th
case forall a.
PriorityQueue a -> Maybe (Key, Precedence, a, PriorityQueue a)
Q.dequeue PriorityQueue a
h of
Maybe (Key, Precedence, a, PriorityQueue a)
Nothing -> forall a. STM a
retry
Just (Key
k, Precedence
p, a
v, PriorityQueue a
h') -> do
forall a. TVar a -> a -> STM ()
writeTVar TVar (PriorityQueue a)
th PriorityQueue a
h'
forall (m :: * -> *) a. Monad m => a -> m a
return (Key
k, Precedence
p, a
v)
delete :: Key -> TPriorityQueue a -> STM (Maybe a)
delete :: forall a. Key -> TPriorityQueue a -> STM (Maybe a)
delete Key
k (TPriorityQueue TVar (PriorityQueue a)
th) = do
PriorityQueue a
q <- forall a. TVar a -> STM a
readTVar TVar (PriorityQueue a)
th
let (Maybe a
mv, PriorityQueue a
q') = forall a. Key -> PriorityQueue a -> (Maybe a, PriorityQueue a)
Q.delete Key
k PriorityQueue a
q
forall a. TVar a -> a -> STM ()
writeTVar TVar (PriorityQueue a)
th PriorityQueue a
q'
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
mv