module Simulation.Aivika.Queue.Base
(
FCFSQueue,
LCFSQueue,
SIROQueue,
PriorityQueue,
Queue,
newFCFSQueue,
newLCFSQueue,
newSIROQueue,
newPriorityQueue,
newQueue,
enqueueStrategy,
enqueueStoringStrategy,
dequeueStrategy,
queueNull,
queueFull,
queueMaxCount,
queueCount,
dequeue,
dequeueWithOutputPriority,
tryDequeue,
enqueue,
enqueueWithInputPriority,
enqueueWithStoringPriority,
enqueueWithInputStoringPriorities,
tryEnqueue,
tryEnqueueWithStoringPriority,
queueDelete,
queueDelete_,
queueDeleteBy,
queueDeleteBy_,
queueContains,
queueContainsBy,
clearQueue) where
import Data.IORef
import Data.Monoid
import Data.Maybe
import Control.Monad
import Control.Monad.Trans
import Simulation.Aivika.Internal.Specs
import Simulation.Aivika.Internal.Simulation
import Simulation.Aivika.Internal.Dynamics
import Simulation.Aivika.Internal.Event
import Simulation.Aivika.Internal.Process
import Simulation.Aivika.Resource.Base
import Simulation.Aivika.QueueStrategy
import qualified Simulation.Aivika.DoubleLinkedList as DLL
import qualified Simulation.Aivika.Vector as V
import qualified Simulation.Aivika.PriorityQueue as PQ
type FCFSQueue a = Queue FCFS FCFS FCFS a
type LCFSQueue a = Queue FCFS LCFS FCFS a
type SIROQueue a = Queue FCFS SIRO FCFS a
type PriorityQueue a = Queue FCFS StaticPriorities FCFS a
data Queue si sm so a =
Queue { forall si sm so a. Queue si sm so a -> Int
queueMaxCount :: Int,
forall si sm so a. Queue si sm so a -> si
enqueueStrategy :: si,
forall si sm so a. Queue si sm so a -> sm
enqueueStoringStrategy :: sm,
forall si sm so a. Queue si sm so a -> so
dequeueStrategy :: so,
forall si sm so a. Queue si sm so a -> Resource si
enqueueRes :: Resource si,
forall si sm so a. Queue si sm so a -> StrategyQueue sm a
queueStore :: StrategyQueue sm a,
forall si sm so a. Queue si sm so a -> Resource so
dequeueRes :: Resource so,
forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef :: IORef Int
}
newFCFSQueue :: Int -> Simulation (FCFSQueue a)
newFCFSQueue :: forall a. Int -> Simulation (FCFSQueue a)
newFCFSQueue = forall si sm so a.
(QueueStrategy si, QueueStrategy sm, QueueStrategy so) =>
si -> sm -> so -> Int -> Simulation (Queue si sm so a)
newQueue FCFS
FCFS FCFS
FCFS FCFS
FCFS
newLCFSQueue :: Int -> Simulation (LCFSQueue a)
newLCFSQueue :: forall a. Int -> Simulation (LCFSQueue a)
newLCFSQueue = forall si sm so a.
(QueueStrategy si, QueueStrategy sm, QueueStrategy so) =>
si -> sm -> so -> Int -> Simulation (Queue si sm so a)
newQueue FCFS
FCFS LCFS
LCFS FCFS
FCFS
newSIROQueue :: Int -> Simulation (SIROQueue a)
newSIROQueue :: forall a. Int -> Simulation (SIROQueue a)
newSIROQueue = forall si sm so a.
(QueueStrategy si, QueueStrategy sm, QueueStrategy so) =>
si -> sm -> so -> Int -> Simulation (Queue si sm so a)
newQueue FCFS
FCFS SIRO
SIRO FCFS
FCFS
newPriorityQueue :: Int -> Simulation (PriorityQueue a)
newPriorityQueue :: forall a. Int -> Simulation (PriorityQueue a)
newPriorityQueue = forall si sm so a.
(QueueStrategy si, QueueStrategy sm, QueueStrategy so) =>
si -> sm -> so -> Int -> Simulation (Queue si sm so a)
newQueue FCFS
FCFS StaticPriorities
StaticPriorities FCFS
FCFS
newQueue :: (QueueStrategy si,
QueueStrategy sm,
QueueStrategy so) =>
si
-> sm
-> so
-> Int
-> Simulation (Queue si sm so a)
newQueue :: forall si sm so a.
(QueueStrategy si, QueueStrategy sm, QueueStrategy so) =>
si -> sm -> so -> Int -> Simulation (Queue si sm so a)
newQueue si
si sm
sm so
so Int
count =
do IORef Int
i <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. a -> IO (IORef a)
newIORef Int
0
Resource si
ri <- forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Simulation (Resource s)
newResourceWithMaxCount si
si Int
count (forall a. a -> Maybe a
Just Int
count)
StrategyQueue sm a
qm <- forall s i. QueueStrategy s => s -> Simulation (StrategyQueue s i)
newStrategyQueue sm
sm
Resource so
ro <- forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Simulation (Resource s)
newResourceWithMaxCount so
so Int
0 (forall a. a -> Maybe a
Just Int
count)
forall (m :: * -> *) a. Monad m => a -> m a
return Queue { queueMaxCount :: Int
queueMaxCount = Int
count,
enqueueStrategy :: si
enqueueStrategy = si
si,
enqueueStoringStrategy :: sm
enqueueStoringStrategy = sm
sm,
dequeueStrategy :: so
dequeueStrategy = so
so,
enqueueRes :: Resource si
enqueueRes = Resource si
ri,
queueStore :: StrategyQueue sm a
queueStore = StrategyQueue sm a
qm,
dequeueRes :: Resource so
dequeueRes = Resource so
ro,
queueCountRef :: IORef Int
queueCountRef = IORef Int
i }
queueNull :: Queue si sm so a -> Event Bool
queueNull :: forall si sm so a. Queue si sm so a -> Event Bool
queueNull Queue si sm so a
q =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
n <- forall a. IORef a -> IO a
readIORef (forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n forall a. Eq a => a -> a -> Bool
== Int
0)
queueFull :: Queue si sm so a -> Event Bool
queueFull :: forall si sm so a. Queue si sm so a -> Event Bool
queueFull Queue si sm so a
q =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
n <- forall a. IORef a -> IO a
readIORef (forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n forall a. Eq a => a -> a -> Bool
== forall si sm so a. Queue si sm so a -> Int
queueMaxCount Queue si sm so a
q)
queueCount :: Queue si sm so a -> Event Int
queueCount :: forall si sm so a. Queue si sm so a -> Event Int
queueCount Queue si sm so a
q =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p -> forall a. IORef a -> IO a
readIORef (forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)
dequeue :: (DequeueStrategy si,
DequeueStrategy sm,
EnqueueStrategy so)
=> Queue si sm so a
-> Process a
dequeue :: forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm, EnqueueStrategy so) =>
Queue si sm so a -> Process a
dequeue Queue si sm so a
q =
do forall s. EnqueueStrategy s => Resource s -> Process ()
requestResource (forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q)
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent forall a b. (a -> b) -> a -> b
$ forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Event a
dequeueExtract Queue si sm so a
q
dequeueWithOutputPriority :: (DequeueStrategy si,
DequeueStrategy sm,
PriorityQueueStrategy so po)
=> Queue si sm so a
-> po
-> Process a
dequeueWithOutputPriority :: forall si sm so po a.
(DequeueStrategy si, DequeueStrategy sm,
PriorityQueueStrategy so po) =>
Queue si sm so a -> po -> Process a
dequeueWithOutputPriority Queue si sm so a
q po
po =
do forall s p.
PriorityQueueStrategy s p =>
Resource s -> p -> Process ()
requestResourceWithPriority (forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q) po
po
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent forall a b. (a -> b) -> a -> b
$ forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Event a
dequeueExtract Queue si sm so a
q
tryDequeue :: (DequeueStrategy si,
DequeueStrategy sm)
=> Queue si sm so a
-> Event (Maybe a)
tryDequeue :: forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Event (Maybe a)
tryDequeue Queue si sm so a
q =
do Bool
x <- forall s. Resource s -> Event Bool
tryRequestResourceWithinEvent (forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q)
if Bool
x
then forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Event a
dequeueExtract Queue si sm so a
q
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
queueDelete :: (Eq a,
DequeueStrategy si,
DeletingQueueStrategy sm,
DequeueStrategy so)
=> Queue si sm so a
-> a
-> Event Bool
queueDelete :: forall a si sm so.
(Eq a, DequeueStrategy si, DeletingQueueStrategy sm,
DequeueStrategy so) =>
Queue si sm so a -> a -> Event Bool
queueDelete Queue si sm so a
q a
a = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Maybe a -> Bool
isJust forall a b. (a -> b) -> a -> b
$ forall si sm so a.
(DequeueStrategy si, DeletingQueueStrategy sm,
DequeueStrategy so) =>
Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
queueDeleteBy Queue si sm so a
q (forall a. Eq a => a -> a -> Bool
== a
a)
queueDelete_ :: (Eq a,
DequeueStrategy si,
DeletingQueueStrategy sm,
DequeueStrategy so)
=> Queue si sm so a
-> a
-> Event ()
queueDelete_ :: forall a si sm so.
(Eq a, DequeueStrategy si, DeletingQueueStrategy sm,
DequeueStrategy so) =>
Queue si sm so a -> a -> Event ()
queueDelete_ Queue si sm so a
q a
a = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall si sm so a.
(DequeueStrategy si, DeletingQueueStrategy sm,
DequeueStrategy so) =>
Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
queueDeleteBy Queue si sm so a
q (forall a. Eq a => a -> a -> Bool
== a
a)
queueDeleteBy :: (DequeueStrategy si,
DeletingQueueStrategy sm,
DequeueStrategy so)
=> Queue si sm so a
-> (a -> Bool)
-> Event (Maybe a)
queueDeleteBy :: forall si sm so a.
(DequeueStrategy si, DeletingQueueStrategy sm,
DequeueStrategy so) =>
Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
queueDeleteBy Queue si sm so a
q a -> Bool
pred =
do Bool
x <- forall s. Resource s -> Event Bool
tryRequestResourceWithinEvent (forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q)
if Bool
x
then do Maybe a
i <- forall s i.
DeletingQueueStrategy s =>
StrategyQueue s i -> (i -> Bool) -> Event (Maybe i)
strategyQueueDeleteBy (forall si sm so a. Queue si sm so a -> StrategyQueue sm a
queueStore Queue si sm so a
q) a -> Bool
pred
case Maybe a
i of
Maybe a
Nothing ->
do forall s. DequeueStrategy s => Resource s -> Event ()
releaseResourceWithinEvent (forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
Just a
i ->
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> a -> Event a
dequeuePostExtract Queue si sm so a
q a
i
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
queueDeleteBy_ :: (DequeueStrategy si,
DeletingQueueStrategy sm,
DequeueStrategy so)
=> Queue si sm so a
-> (a -> Bool)
-> Event ()
queueDeleteBy_ :: forall si sm so a.
(DequeueStrategy si, DeletingQueueStrategy sm,
DequeueStrategy so) =>
Queue si sm so a -> (a -> Bool) -> Event ()
queueDeleteBy_ Queue si sm so a
q a -> Bool
pred = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall si sm so a.
(DequeueStrategy si, DeletingQueueStrategy sm,
DequeueStrategy so) =>
Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
queueDeleteBy Queue si sm so a
q a -> Bool
pred
queueContains :: (Eq a,
DeletingQueueStrategy sm)
=> Queue si sm so a
-> a
-> Event Bool
queueContains :: forall a sm si so.
(Eq a, DeletingQueueStrategy sm) =>
Queue si sm so a -> a -> Event Bool
queueContains Queue si sm so a
q a
a = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Maybe a -> Bool
isJust forall a b. (a -> b) -> a -> b
$ forall sm si so a.
DeletingQueueStrategy sm =>
Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
queueContainsBy Queue si sm so a
q (forall a. Eq a => a -> a -> Bool
== a
a)
queueContainsBy :: DeletingQueueStrategy sm
=> Queue si sm so a
-> (a -> Bool)
-> Event (Maybe a)
queueContainsBy :: forall sm si so a.
DeletingQueueStrategy sm =>
Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
queueContainsBy Queue si sm so a
q a -> Bool
pred =
forall s i.
DeletingQueueStrategy s =>
StrategyQueue s i -> (i -> Bool) -> Event (Maybe i)
strategyQueueContainsBy (forall si sm so a. Queue si sm so a -> StrategyQueue sm a
queueStore Queue si sm so a
q) a -> Bool
pred
clearQueue :: (DequeueStrategy si,
DequeueStrategy sm)
=> Queue si sm so a
-> Event ()
clearQueue :: forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Event ()
clearQueue Queue si sm so a
q =
do Maybe a
x <- forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Event (Maybe a)
tryDequeue Queue si sm so a
q
case Maybe a
x of
Maybe a
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just a
a -> forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Event ()
clearQueue Queue si sm so a
q
enqueue :: (EnqueueStrategy si,
EnqueueStrategy sm,
DequeueStrategy so)
=> Queue si sm so a
-> a
-> Process ()
enqueue :: forall si sm so a.
(EnqueueStrategy si, EnqueueStrategy sm, DequeueStrategy so) =>
Queue si sm so a -> a -> Process ()
enqueue Queue si sm so a
q a
a =
do forall s. EnqueueStrategy s => Resource s -> Process ()
requestResource (forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q)
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent forall a b. (a -> b) -> a -> b
$ forall sm so si a.
(EnqueueStrategy sm, DequeueStrategy so) =>
Queue si sm so a -> a -> Event ()
enqueueStore Queue si sm so a
q a
a
enqueueWithInputPriority :: (PriorityQueueStrategy si pi,
EnqueueStrategy sm,
DequeueStrategy so)
=> Queue si sm so a
-> pi
-> a
-> Process ()
enqueueWithInputPriority :: forall si pi sm so a.
(PriorityQueueStrategy si pi, EnqueueStrategy sm,
DequeueStrategy so) =>
Queue si sm so a -> pi -> a -> Process ()
enqueueWithInputPriority Queue si sm so a
q pi
pi a
a =
do forall s p.
PriorityQueueStrategy s p =>
Resource s -> p -> Process ()
requestResourceWithPriority (forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q) pi
pi
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent forall a b. (a -> b) -> a -> b
$ forall sm so si a.
(EnqueueStrategy sm, DequeueStrategy so) =>
Queue si sm so a -> a -> Event ()
enqueueStore Queue si sm so a
q a
a
enqueueWithStoringPriority :: (EnqueueStrategy si,
PriorityQueueStrategy sm pm,
DequeueStrategy so)
=> Queue si sm so a
-> pm
-> a
-> Process ()
enqueueWithStoringPriority :: forall si sm pm so a.
(EnqueueStrategy si, PriorityQueueStrategy sm pm,
DequeueStrategy so) =>
Queue si sm so a -> pm -> a -> Process ()
enqueueWithStoringPriority Queue si sm so a
q pm
pm a
a =
do forall s. EnqueueStrategy s => Resource s -> Process ()
requestResource (forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q)
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent forall a b. (a -> b) -> a -> b
$ forall sm pm so si a.
(PriorityQueueStrategy sm pm, DequeueStrategy so) =>
Queue si sm so a -> pm -> a -> Event ()
enqueueStoreWithPriority Queue si sm so a
q pm
pm a
a
enqueueWithInputStoringPriorities :: (PriorityQueueStrategy si pi,
PriorityQueueStrategy sm pm,
DequeueStrategy so)
=> Queue si sm so a
-> pi
-> pm
-> a
-> Process ()
enqueueWithInputStoringPriorities :: forall si pi sm pm so a.
(PriorityQueueStrategy si pi, PriorityQueueStrategy sm pm,
DequeueStrategy so) =>
Queue si sm so a -> pi -> pm -> a -> Process ()
enqueueWithInputStoringPriorities Queue si sm so a
q pi
pi pm
pm a
a =
do forall s p.
PriorityQueueStrategy s p =>
Resource s -> p -> Process ()
requestResourceWithPriority (forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q) pi
pi
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent forall a b. (a -> b) -> a -> b
$ forall sm pm so si a.
(PriorityQueueStrategy sm pm, DequeueStrategy so) =>
Queue si sm so a -> pm -> a -> Event ()
enqueueStoreWithPriority Queue si sm so a
q pm
pm a
a
tryEnqueue :: (EnqueueStrategy sm,
DequeueStrategy so)
=> Queue si sm so a
-> a
-> Event Bool
tryEnqueue :: forall sm so si a.
(EnqueueStrategy sm, DequeueStrategy so) =>
Queue si sm so a -> a -> Event Bool
tryEnqueue Queue si sm so a
q a
a =
do Bool
x <- forall s. Resource s -> Event Bool
tryRequestResourceWithinEvent (forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q)
if Bool
x
then do forall sm so si a.
(EnqueueStrategy sm, DequeueStrategy so) =>
Queue si sm so a -> a -> Event ()
enqueueStore Queue si sm so a
q a
a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
tryEnqueueWithStoringPriority :: (PriorityQueueStrategy sm pm,
DequeueStrategy so)
=> Queue si sm so a
-> pm
-> a
-> Event Bool
tryEnqueueWithStoringPriority :: forall sm pm so si a.
(PriorityQueueStrategy sm pm, DequeueStrategy so) =>
Queue si sm so a -> pm -> a -> Event Bool
tryEnqueueWithStoringPriority Queue si sm so a
q pm
pm a
a =
do Bool
x <- forall s. Resource s -> Event Bool
tryRequestResourceWithinEvent (forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q)
if Bool
x
then do forall sm pm so si a.
(PriorityQueueStrategy sm pm, DequeueStrategy so) =>
Queue si sm so a -> pm -> a -> Event ()
enqueueStoreWithPriority Queue si sm so a
q pm
pm a
a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
enqueueStore :: (EnqueueStrategy sm,
DequeueStrategy so)
=> Queue si sm so a
-> a
-> Event ()
enqueueStore :: forall sm so si a.
(EnqueueStrategy sm, DequeueStrategy so) =>
Queue si sm so a -> a -> Event ()
enqueueStore Queue si sm so a
q a
a =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall s i. EnqueueStrategy s => StrategyQueue s i -> i -> Event ()
strategyEnqueue (forall si sm so a. Queue si sm so a -> StrategyQueue sm a
queueStore Queue si sm so a
q) a
a
Int
c <- forall a. IORef a -> IO a
readIORef (forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)
let c' :: Int
c' = Int
c forall a. Num a => a -> a -> a
+ Int
1
Int
c' seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef (forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q) Int
c'
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall s. DequeueStrategy s => Resource s -> Event ()
releaseResourceWithinEvent (forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q)
enqueueStoreWithPriority :: (PriorityQueueStrategy sm pm,
DequeueStrategy so)
=> Queue si sm so a
-> pm
-> a
-> Event ()
enqueueStoreWithPriority :: forall sm pm so si a.
(PriorityQueueStrategy sm pm, DequeueStrategy so) =>
Queue si sm so a -> pm -> a -> Event ()
enqueueStoreWithPriority Queue si sm so a
q pm
pm a
a =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall s p i.
PriorityQueueStrategy s p =>
StrategyQueue s i -> p -> i -> Event ()
strategyEnqueueWithPriority (forall si sm so a. Queue si sm so a -> StrategyQueue sm a
queueStore Queue si sm so a
q) pm
pm a
a
Int
c <- forall a. IORef a -> IO a
readIORef (forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)
let c' :: Int
c' = Int
c forall a. Num a => a -> a -> a
+ Int
1
Int
c' seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef (forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q) Int
c'
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall s. DequeueStrategy s => Resource s -> Event ()
releaseResourceWithinEvent (forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q)
dequeueExtract :: (DequeueStrategy si,
DequeueStrategy sm)
=> Queue si sm so a
-> Event a
Queue si sm so a
q =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do a
a <- forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall s i. DequeueStrategy s => StrategyQueue s i -> Event i
strategyDequeue (forall si sm so a. Queue si sm so a -> StrategyQueue sm a
queueStore Queue si sm so a
q)
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> a -> Event a
dequeuePostExtract Queue si sm so a
q a
a
dequeuePostExtract :: (DequeueStrategy si,
DequeueStrategy sm)
=> Queue si sm so a
-> a
-> Event a
Queue si sm so a
q a
a =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
c <- forall a. IORef a -> IO a
readIORef (forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)
let c' :: Int
c' = Int
c forall a. Num a => a -> a -> a
- Int
1
Int
c' seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef (forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q) Int
c'
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall s. DequeueStrategy s => Resource s -> Event ()
releaseResourceWithinEvent (forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q)
forall (m :: * -> *) a. Monad m => a -> m a
return a
a