module Simulation.Aivika.Resource
(
FCFSResource,
LCFSResource,
SIROResource,
PriorityResource,
Resource,
newFCFSResource,
newFCFSResourceWithMaxCount,
newLCFSResource,
newLCFSResourceWithMaxCount,
newSIROResource,
newSIROResourceWithMaxCount,
newPriorityResource,
newPriorityResourceWithMaxCount,
newResource,
newResourceWithMaxCount,
resourceStrategy,
resourceMaxCount,
resourceCount,
resourceCountStats,
resourceUtilisationCount,
resourceUtilisationCountStats,
resourceQueueCount,
resourceQueueCountStats,
resourceTotalWaitTime,
resourceWaitTime,
requestResource,
requestResourceWithPriority,
tryRequestResourceWithinEvent,
releaseResource,
releaseResourceWithinEvent,
usingResource,
usingResourceWithPriority,
incResourceCount,
decResourceCount,
resetResource,
resourceCountChanged,
resourceCountChanged_,
resourceUtilisationCountChanged,
resourceUtilisationCountChanged_,
resourceQueueCountChanged,
resourceQueueCountChanged_,
resourceWaitTimeChanged,
resourceWaitTimeChanged_,
resourceChanged_) where
import Data.IORef
import Data.Monoid
import Control.Monad
import Control.Monad.Trans
import Control.Exception
import Simulation.Aivika.Internal.Specs
import Simulation.Aivika.Internal.Simulation
import Simulation.Aivika.Internal.Event
import Simulation.Aivika.Internal.Cont
import Simulation.Aivika.Internal.Process
import Simulation.Aivika.QueueStrategy
import Simulation.Aivika.Statistics
import Simulation.Aivika.Signal
import qualified Simulation.Aivika.DoubleLinkedList as DLL
import qualified Simulation.Aivika.Vector as V
import qualified Simulation.Aivika.PriorityQueue as PQ
type FCFSResource = Resource FCFS
type LCFSResource = Resource LCFS
type SIROResource = Resource SIRO
type PriorityResource = Resource StaticPriorities
data Resource s =
Resource { forall s. Resource s -> s
resourceStrategy :: s,
forall s. Resource s -> Maybe Int
resourceMaxCount :: Maybe Int,
forall s. Resource s -> IORef Int
resourceCountRef :: IORef Int,
forall s. Resource s -> IORef (TimingStats Int)
resourceCountStatsRef :: IORef (TimingStats Int),
forall s. Resource s -> SignalSource Int
resourceCountSource :: SignalSource Int,
forall s. Resource s -> IORef Int
resourceUtilisationCountRef :: IORef Int,
forall s. Resource s -> IORef (TimingStats Int)
resourceUtilisationCountStatsRef :: IORef (TimingStats Int),
forall s. Resource s -> SignalSource Int
resourceUtilisationCountSource :: SignalSource Int,
forall s. Resource s -> IORef Int
resourceQueueCountRef :: IORef Int,
forall s. Resource s -> IORef (TimingStats Int)
resourceQueueCountStatsRef :: IORef (TimingStats Int),
forall s. Resource s -> SignalSource Int
resourceQueueCountSource :: SignalSource Int,
forall s. Resource s -> IORef Double
resourceTotalWaitTimeRef :: IORef Double,
forall s. Resource s -> IORef (SamplingStats Double)
resourceWaitTimeRef :: IORef (SamplingStats Double),
forall s. Resource s -> SignalSource ()
resourceWaitTimeSource :: SignalSource (),
forall s. Resource s -> StrategyQueue s ResourceItem
resourceWaitList :: StrategyQueue s ResourceItem }
data ResourceItem =
ResourceItem { ResourceItem -> Double
resourceItemTime :: Double,
ResourceItem -> FrozenCont ()
resourceItemCont :: FrozenCont () }
instance Eq (Resource s) where
Resource s
x == :: Resource s -> Resource s -> Bool
== Resource s
y = forall s. Resource s -> IORef Int
resourceCountRef Resource s
x forall a. Eq a => a -> a -> Bool
== forall s. Resource s -> IORef Int
resourceCountRef Resource s
y
newFCFSResource :: Int
-> Event FCFSResource
newFCFSResource :: Int -> Event FCFSResource
newFCFSResource = forall s. QueueStrategy s => s -> Int -> Event (Resource s)
newResource FCFS
FCFS
newFCFSResourceWithMaxCount :: Int
-> Maybe Int
-> Event FCFSResource
newFCFSResourceWithMaxCount :: Int -> Maybe Int -> Event FCFSResource
newFCFSResourceWithMaxCount = forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Event (Resource s)
newResourceWithMaxCount FCFS
FCFS
newLCFSResource :: Int
-> Event LCFSResource
newLCFSResource :: Int -> Event LCFSResource
newLCFSResource = forall s. QueueStrategy s => s -> Int -> Event (Resource s)
newResource LCFS
LCFS
newLCFSResourceWithMaxCount :: Int
-> Maybe Int
-> Event LCFSResource
newLCFSResourceWithMaxCount :: Int -> Maybe Int -> Event LCFSResource
newLCFSResourceWithMaxCount = forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Event (Resource s)
newResourceWithMaxCount LCFS
LCFS
newSIROResource :: Int
-> Event SIROResource
newSIROResource :: Int -> Event SIROResource
newSIROResource = forall s. QueueStrategy s => s -> Int -> Event (Resource s)
newResource SIRO
SIRO
newSIROResourceWithMaxCount :: Int
-> Maybe Int
-> Event SIROResource
newSIROResourceWithMaxCount :: Int -> Maybe Int -> Event SIROResource
newSIROResourceWithMaxCount = forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Event (Resource s)
newResourceWithMaxCount SIRO
SIRO
newPriorityResource :: Int
-> Event PriorityResource
newPriorityResource :: Int -> Event PriorityResource
newPriorityResource = forall s. QueueStrategy s => s -> Int -> Event (Resource s)
newResource StaticPriorities
StaticPriorities
newPriorityResourceWithMaxCount :: Int
-> Maybe Int
-> Event PriorityResource
newPriorityResourceWithMaxCount :: Int -> Maybe Int -> Event PriorityResource
newPriorityResourceWithMaxCount = forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Event (Resource s)
newResourceWithMaxCount StaticPriorities
StaticPriorities
newResource :: QueueStrategy s
=> s
-> Int
-> Event (Resource s)
newResource :: forall s. QueueStrategy s => s -> Int -> Event (Resource s)
newResource s
s Int
count =
forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Event (Resource s)
newResourceWithMaxCount s
s Int
count (forall a. a -> Maybe a
Just Int
count)
newResourceWithMaxCount :: QueueStrategy s
=> s
-> Int
-> Maybe Int
-> Event (Resource s)
newResourceWithMaxCount :: forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Event (Resource s)
newResourceWithMaxCount s
s Int
count Maybe Int
maxCount =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do let r :: Run
r = Point -> Run
pointRun Point
p
t :: Double
t = Point -> Double
pointTime Point
p
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
count forall a. Ord a => a -> a -> Bool
< Int
0) forall a b. (a -> b) -> a -> b
$
forall e a. Exception e => e -> IO a
throwIO forall a b. (a -> b) -> a -> b
$
String -> SimulationRetry
SimulationRetry forall a b. (a -> b) -> a -> b
$
String
"The resource count cannot be negative: " forall a. [a] -> [a] -> [a]
++
String
"newResourceWithMaxCount."
case Maybe Int
maxCount of
Just Int
maxCount | Int
count forall a. Ord a => a -> a -> Bool
> Int
maxCount ->
forall e a. Exception e => e -> IO a
throwIO forall a b. (a -> b) -> a -> b
$
String -> SimulationRetry
SimulationRetry forall a b. (a -> b) -> a -> b
$
String
"The resource count cannot be greater than " forall a. [a] -> [a] -> [a]
++
String
"its maximum value: newResourceWithMaxCount."
Maybe Int
_ ->
forall (m :: * -> *) a. Monad m => a -> m a
return ()
IORef Int
countRef <- forall a. a -> IO (IORef a)
newIORef Int
count
IORef (TimingStats Int)
countStatsRef <- forall a. a -> IO (IORef a)
newIORef forall a b. (a -> b) -> a -> b
$ forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
count
SignalSource Int
countSource <- forall a. Run -> Simulation a -> IO a
invokeSimulation Run
r forall a. Simulation (SignalSource a)
newSignalSource
IORef Int
utilCountRef <- forall a. a -> IO (IORef a)
newIORef Int
0
IORef (TimingStats Int)
utilCountStatsRef <- forall a. a -> IO (IORef a)
newIORef forall a b. (a -> b) -> a -> b
$ forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
0
SignalSource Int
utilCountSource <- forall a. Run -> Simulation a -> IO a
invokeSimulation Run
r forall a. Simulation (SignalSource a)
newSignalSource
IORef Int
queueCountRef <- forall a. a -> IO (IORef a)
newIORef Int
0
IORef (TimingStats Int)
queueCountStatsRef <- forall a. a -> IO (IORef a)
newIORef forall a b. (a -> b) -> a -> b
$ forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
0
SignalSource Int
queueCountSource <- forall a. Run -> Simulation a -> IO a
invokeSimulation Run
r forall a. Simulation (SignalSource a)
newSignalSource
IORef Double
totalWaitTimeRef <- forall a. a -> IO (IORef a)
newIORef Double
0
IORef (SamplingStats Double)
waitTimeRef <- forall a. a -> IO (IORef a)
newIORef forall a. SamplingData a => SamplingStats a
emptySamplingStats
SignalSource ()
waitTimeSource <- forall a. Run -> Simulation a -> IO a
invokeSimulation Run
r forall a. Simulation (SignalSource a)
newSignalSource
StrategyQueue s ResourceItem
waitList <- forall a. Run -> Simulation a -> IO a
invokeSimulation Run
r forall a b. (a -> b) -> a -> b
$ forall s i. QueueStrategy s => s -> Simulation (StrategyQueue s i)
newStrategyQueue s
s
forall (m :: * -> *) a. Monad m => a -> m a
return Resource { resourceStrategy :: s
resourceStrategy = s
s,
resourceMaxCount :: Maybe Int
resourceMaxCount = Maybe Int
maxCount,
resourceCountRef :: IORef Int
resourceCountRef = IORef Int
countRef,
resourceCountStatsRef :: IORef (TimingStats Int)
resourceCountStatsRef = IORef (TimingStats Int)
countStatsRef,
resourceCountSource :: SignalSource Int
resourceCountSource = SignalSource Int
countSource,
resourceUtilisationCountRef :: IORef Int
resourceUtilisationCountRef = IORef Int
utilCountRef,
resourceUtilisationCountStatsRef :: IORef (TimingStats Int)
resourceUtilisationCountStatsRef = IORef (TimingStats Int)
utilCountStatsRef,
resourceUtilisationCountSource :: SignalSource Int
resourceUtilisationCountSource = SignalSource Int
utilCountSource,
resourceQueueCountRef :: IORef Int
resourceQueueCountRef = IORef Int
queueCountRef,
resourceQueueCountStatsRef :: IORef (TimingStats Int)
resourceQueueCountStatsRef = IORef (TimingStats Int)
queueCountStatsRef,
resourceQueueCountSource :: SignalSource Int
resourceQueueCountSource = SignalSource Int
queueCountSource,
resourceTotalWaitTimeRef :: IORef Double
resourceTotalWaitTimeRef = IORef Double
totalWaitTimeRef,
resourceWaitTimeRef :: IORef (SamplingStats Double)
resourceWaitTimeRef = IORef (SamplingStats Double)
waitTimeRef,
resourceWaitTimeSource :: SignalSource ()
resourceWaitTimeSource = SignalSource ()
waitTimeSource,
resourceWaitList :: StrategyQueue s ResourceItem
resourceWaitList = StrategyQueue s ResourceItem
waitList }
resourceCount :: Resource s -> Event Int
resourceCount :: forall s. Resource s -> Event Int
resourceCount Resource s
r =
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 s. Resource s -> IORef Int
resourceCountRef Resource s
r)
resourceCountStats :: Resource s -> Event (TimingStats Int)
resourceCountStats :: forall s. Resource s -> Event (TimingStats Int)
resourceCountStats Resource s
r =
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 s. Resource s -> IORef (TimingStats Int)
resourceCountStatsRef Resource s
r)
resourceCountChanged :: Resource s -> Signal Int
resourceCountChanged :: forall s. Resource s -> Signal Int
resourceCountChanged Resource s
r =
forall a. SignalSource a -> Signal a
publishSignal forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> SignalSource Int
resourceCountSource Resource s
r
resourceCountChanged_ :: Resource s -> Signal ()
resourceCountChanged_ :: forall s. Resource s -> Signal ()
resourceCountChanged_ Resource s
r =
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Signal Int
resourceCountChanged Resource s
r
resourceUtilisationCount :: Resource s -> Event Int
resourceUtilisationCount :: forall s. Resource s -> Event Int
resourceUtilisationCount Resource s
r =
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 s. Resource s -> IORef Int
resourceUtilisationCountRef Resource s
r)
resourceUtilisationCountStats :: Resource s -> Event (TimingStats Int)
resourceUtilisationCountStats :: forall s. Resource s -> Event (TimingStats Int)
resourceUtilisationCountStats Resource s
r =
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 s. Resource s -> IORef (TimingStats Int)
resourceUtilisationCountStatsRef Resource s
r)
resourceUtilisationCountChanged :: Resource s -> Signal Int
resourceUtilisationCountChanged :: forall s. Resource s -> Signal Int
resourceUtilisationCountChanged Resource s
r =
forall a. SignalSource a -> Signal a
publishSignal forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> SignalSource Int
resourceUtilisationCountSource Resource s
r
resourceUtilisationCountChanged_ :: Resource s -> Signal ()
resourceUtilisationCountChanged_ :: forall s. Resource s -> Signal ()
resourceUtilisationCountChanged_ Resource s
r =
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Signal Int
resourceUtilisationCountChanged Resource s
r
resourceQueueCount :: Resource s -> Event Int
resourceQueueCount :: forall s. Resource s -> Event Int
resourceQueueCount Resource s
r =
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 s. Resource s -> IORef Int
resourceQueueCountRef Resource s
r)
resourceQueueCountStats :: Resource s -> Event (TimingStats Int)
resourceQueueCountStats :: forall s. Resource s -> Event (TimingStats Int)
resourceQueueCountStats Resource s
r =
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 s. Resource s -> IORef (TimingStats Int)
resourceQueueCountStatsRef Resource s
r)
resourceQueueCountChanged :: Resource s -> Signal Int
resourceQueueCountChanged :: forall s. Resource s -> Signal Int
resourceQueueCountChanged Resource s
r =
forall a. SignalSource a -> Signal a
publishSignal forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> SignalSource Int
resourceQueueCountSource Resource s
r
resourceQueueCountChanged_ :: Resource s -> Signal ()
resourceQueueCountChanged_ :: forall s. Resource s -> Signal ()
resourceQueueCountChanged_ Resource s
r =
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Signal Int
resourceQueueCountChanged Resource s
r
resourceTotalWaitTime :: Resource s -> Event Double
resourceTotalWaitTime :: forall s. Resource s -> Event Double
resourceTotalWaitTime Resource s
r =
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 s. Resource s -> IORef Double
resourceTotalWaitTimeRef Resource s
r)
resourceWaitTime :: Resource s -> Event (SamplingStats Double)
resourceWaitTime :: forall s. Resource s -> Event (SamplingStats Double)
resourceWaitTime Resource s
r =
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 s. Resource s -> IORef (SamplingStats Double)
resourceWaitTimeRef Resource s
r)
resourceWaitTimeChanged :: Resource s -> Signal (SamplingStats Double)
resourceWaitTimeChanged :: forall s. Resource s -> Signal (SamplingStats Double)
resourceWaitTimeChanged Resource s
r =
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (\() -> forall s. Resource s -> Event (SamplingStats Double)
resourceWaitTime Resource s
r) forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Signal ()
resourceWaitTimeChanged_ Resource s
r
resourceWaitTimeChanged_ :: Resource s -> Signal ()
resourceWaitTimeChanged_ :: forall s. Resource s -> Signal ()
resourceWaitTimeChanged_ Resource s
r =
forall a. SignalSource a -> Signal a
publishSignal forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> SignalSource ()
resourceWaitTimeSource Resource s
r
requestResource :: EnqueueStrategy s
=> Resource s
-> Process ()
requestResource :: forall s. EnqueueStrategy s => Resource s -> Process ()
requestResource Resource s
r =
forall a. (ProcessId -> Cont a) -> Process a
Process forall a b. (a -> b) -> a -> b
$ \ProcessId
pid ->
forall a. (ContParams a -> Event ()) -> Cont a
Cont forall a b. (a -> b) -> a -> b
$ \ContParams ()
c ->
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- forall a. IORef a -> IO a
readIORef (forall s. Resource s -> IORef Int
resourceCountRef Resource s
r)
if Int
a forall a. Eq a => a -> a -> Bool
== Int
0
then do FrozenCont ()
c <- forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall a. ContParams a -> a -> Event () -> Event (FrozenCont a)
freezeContReentering ContParams ()
c () forall a b. (a -> b) -> a -> b
$
forall a. ContParams a -> Cont a -> Event ()
invokeCont ContParams ()
c forall a b. (a -> b) -> a -> b
$
forall a. ProcessId -> Process a -> Cont a
invokeProcess ProcessId
pid forall a b. (a -> b) -> a -> b
$
forall s. EnqueueStrategy s => Resource s -> Process ()
requestResource Resource s
r
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 s. Resource s -> StrategyQueue s ResourceItem
resourceWaitList Resource s
r) forall a b. (a -> b) -> a -> b
$
Double -> FrozenCont () -> ResourceItem
ResourceItem (Point -> Double
pointTime Point
p) FrozenCont ()
c
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Int -> Event ()
updateResourceQueueCount Resource s
r Int
1
else do forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Double -> Event ()
updateResourceWaitTime Resource s
r Double
0
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Int -> Event ()
updateResourceCount Resource s
r (-Int
1)
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Int -> Event ()
updateResourceUtilisationCount Resource s
r Int
1
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. ContParams a -> a -> Event ()
resumeCont ContParams ()
c ()
requestResourceWithPriority :: PriorityQueueStrategy s p
=> Resource s
-> p
-> Process ()
requestResourceWithPriority :: forall s p.
PriorityQueueStrategy s p =>
Resource s -> p -> Process ()
requestResourceWithPriority Resource s
r p
priority =
forall a. (ProcessId -> Cont a) -> Process a
Process forall a b. (a -> b) -> a -> b
$ \ProcessId
pid ->
forall a. (ContParams a -> Event ()) -> Cont a
Cont forall a b. (a -> b) -> a -> b
$ \ContParams ()
c ->
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- forall a. IORef a -> IO a
readIORef (forall s. Resource s -> IORef Int
resourceCountRef Resource s
r)
if Int
a forall a. Eq a => a -> a -> Bool
== Int
0
then do FrozenCont ()
c <- forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall a. ContParams a -> a -> Event () -> Event (FrozenCont a)
freezeContReentering ContParams ()
c () forall a b. (a -> b) -> a -> b
$
forall a. ContParams a -> Cont a -> Event ()
invokeCont ContParams ()
c forall a b. (a -> b) -> a -> b
$
forall a. ProcessId -> Process a -> Cont a
invokeProcess ProcessId
pid forall a b. (a -> b) -> a -> b
$
forall s p.
PriorityQueueStrategy s p =>
Resource s -> p -> Process ()
requestResourceWithPriority Resource s
r p
priority
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 s. Resource s -> StrategyQueue s ResourceItem
resourceWaitList Resource s
r) p
priority forall a b. (a -> b) -> a -> b
$
Double -> FrozenCont () -> ResourceItem
ResourceItem (Point -> Double
pointTime Point
p) FrozenCont ()
c
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Int -> Event ()
updateResourceQueueCount Resource s
r Int
1
else do forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Double -> Event ()
updateResourceWaitTime Resource s
r Double
0
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Int -> Event ()
updateResourceCount Resource s
r (-Int
1)
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Int -> Event ()
updateResourceUtilisationCount Resource s
r Int
1
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. ContParams a -> a -> Event ()
resumeCont ContParams ()
c ()
releaseResource :: DequeueStrategy s
=> Resource s
-> Process ()
releaseResource :: forall s. DequeueStrategy s => Resource s -> Process ()
releaseResource Resource s
r =
forall a. (ProcessId -> Cont a) -> Process a
Process forall a b. (a -> b) -> a -> b
$ \ProcessId
_ ->
forall a. (ContParams a -> Event ()) -> Cont a
Cont forall a b. (a -> b) -> a -> b
$ \ContParams ()
c ->
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. DequeueStrategy s => Resource s -> Event ()
releaseResourceWithinEvent Resource s
r
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. ContParams a -> a -> Event ()
resumeCont ContParams ()
c ()
releaseResourceWithinEvent :: DequeueStrategy s
=> Resource s
-> Event ()
releaseResourceWithinEvent :: forall s. DequeueStrategy s => Resource s -> Event ()
releaseResourceWithinEvent Resource s
r =
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. Resource s -> Int -> Event ()
updateResourceUtilisationCount Resource s
r (-Int
1)
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. DequeueStrategy s => Resource s -> Event ()
releaseResource' Resource s
r
releaseResource' :: DequeueStrategy s
=> Resource s
-> Event ()
releaseResource' :: forall s. DequeueStrategy s => Resource s -> Event ()
releaseResource' Resource s
r =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- forall a. IORef a -> IO a
readIORef (forall s. Resource s -> IORef Int
resourceCountRef Resource s
r)
let a' :: Int
a' = Int
a forall a. Num a => a -> a -> a
+ Int
1
case forall s. Resource s -> Maybe Int
resourceMaxCount Resource s
r of
Just Int
maxCount | Int
a' forall a. Ord a => a -> a -> Bool
> Int
maxCount ->
forall e a. Exception e => e -> IO a
throwIO forall a b. (a -> b) -> a -> b
$
String -> SimulationRetry
SimulationRetry forall a b. (a -> b) -> a -> b
$
String
"The resource count cannot be greater than " forall a. [a] -> [a] -> [a]
++
String
"its maximum value: releaseResource'."
Maybe Int
_ ->
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Bool
f <- forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall s i. QueueStrategy s => StrategyQueue s i -> Event Bool
strategyQueueNull (forall s. Resource s -> StrategyQueue s ResourceItem
resourceWaitList Resource s
r)
if Bool
f
then forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Int -> Event ()
updateResourceCount Resource s
r Int
1
else do ResourceItem
x <- 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 s. Resource s -> StrategyQueue s ResourceItem
resourceWaitList Resource s
r)
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Int -> Event ()
updateResourceQueueCount Resource s
r (-Int
1)
Maybe (ContParams ())
c <- forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. FrozenCont a -> Event (Maybe (ContParams a))
unfreezeCont (ResourceItem -> FrozenCont ()
resourceItemCont ResourceItem
x)
case Maybe (ContParams ())
c of
Maybe (ContParams ())
Nothing ->
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. DequeueStrategy s => Resource s -> Event ()
releaseResource' Resource s
r
Just ContParams ()
c ->
do forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Double -> Event ()
updateResourceWaitTime Resource s
r (Point -> Double
pointTime Point
p forall a. Num a => a -> a -> a
- ResourceItem -> Double
resourceItemTime ResourceItem
x)
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Int -> Event ()
updateResourceUtilisationCount Resource s
r Int
1
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ Double -> Event () -> Event ()
enqueueEvent (Point -> Double
pointTime Point
p) forall a b. (a -> b) -> a -> b
$ forall a. ContParams a -> a -> Event ()
resumeCont ContParams ()
c ()
tryRequestResourceWithinEvent :: Resource s
-> Event Bool
tryRequestResourceWithinEvent :: forall s. Resource s -> Event Bool
tryRequestResourceWithinEvent Resource s
r =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- forall a. IORef a -> IO a
readIORef (forall s. Resource s -> IORef Int
resourceCountRef Resource s
r)
if Int
a forall a. Eq a => a -> a -> Bool
== Int
0
then forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
else do forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Double -> Event ()
updateResourceWaitTime Resource s
r Double
0
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Int -> Event ()
updateResourceCount Resource s
r (-Int
1)
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall s. Resource s -> Int -> Event ()
updateResourceUtilisationCount Resource s
r Int
1
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
usingResource :: EnqueueStrategy s
=> Resource s
-> Process a
-> Process a
usingResource :: forall s a.
EnqueueStrategy s =>
Resource s -> Process a -> Process a
usingResource Resource s
r Process a
m =
do forall s. EnqueueStrategy s => Resource s -> Process ()
requestResource Resource s
r
forall a b. Process a -> Process b -> Process a
finallyProcess Process a
m forall a b. (a -> b) -> a -> b
$ forall s. DequeueStrategy s => Resource s -> Process ()
releaseResource Resource s
r
usingResourceWithPriority :: PriorityQueueStrategy s p
=> Resource s
-> p
-> Process a
-> Process a
usingResourceWithPriority :: forall s p a.
PriorityQueueStrategy s p =>
Resource s -> p -> Process a -> Process a
usingResourceWithPriority Resource s
r p
priority Process a
m =
do forall s p.
PriorityQueueStrategy s p =>
Resource s -> p -> Process ()
requestResourceWithPriority Resource s
r p
priority
forall a b. Process a -> Process b -> Process a
finallyProcess Process a
m forall a b. (a -> b) -> a -> b
$ forall s. DequeueStrategy s => Resource s -> Process ()
releaseResource Resource s
r
decResourceCount' :: EnqueueStrategy s
=> Resource s
-> Process ()
decResourceCount' :: forall s. EnqueueStrategy s => Resource s -> Process ()
decResourceCount' Resource s
r =
do forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent forall a b. (a -> b) -> a -> b
$
forall s. Resource s -> Int -> Event ()
updateResourceUtilisationCount Resource s
r (-Int
1)
forall s. EnqueueStrategy s => Resource s -> Process ()
requestResource Resource s
r
incResourceCount :: DequeueStrategy s
=> Resource s
-> Int
-> Event ()
incResourceCount :: forall s. DequeueStrategy s => Resource s -> Int -> Event ()
incResourceCount Resource s
r Int
n
| Int
n forall a. Ord a => a -> a -> Bool
< Int
0 = forall e a. Exception e => e -> Event a
throwEvent forall a b. (a -> b) -> a -> b
$ String -> SimulationRetry
SimulationRetry String
"The increment cannot be negative: incResourceCount"
| Int
n forall a. Eq a => a -> a -> Bool
== Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise =
do forall s. DequeueStrategy s => Resource s -> Event ()
releaseResource' Resource s
r
forall s. DequeueStrategy s => Resource s -> Int -> Event ()
incResourceCount Resource s
r (Int
n forall a. Num a => a -> a -> a
- Int
1)
decResourceCount :: EnqueueStrategy s
=> Resource s
-> Int
-> Process ()
decResourceCount :: forall s. EnqueueStrategy s => Resource s -> Int -> Process ()
decResourceCount Resource s
r Int
n
| Int
n forall a. Ord a => a -> a -> Bool
< Int
0 = forall e a. Exception e => e -> Process a
throwProcess forall a b. (a -> b) -> a -> b
$ String -> SimulationRetry
SimulationRetry String
"The decrement cannot be negative: decResourceCount"
| Int
n forall a. Eq a => a -> a -> Bool
== Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise =
do forall s. EnqueueStrategy s => Resource s -> Process ()
decResourceCount' Resource s
r
forall s. EnqueueStrategy s => Resource s -> Int -> Process ()
decResourceCount Resource s
r (Int
n forall a. Num a => a -> a -> a
- Int
1)
resourceChanged_ :: Resource s -> Signal ()
resourceChanged_ :: forall s. Resource s -> Signal ()
resourceChanged_ Resource s
r =
forall s. Resource s -> Signal ()
resourceCountChanged_ Resource s
r forall a. Semigroup a => a -> a -> a
<>
forall s. Resource s -> Signal ()
resourceUtilisationCountChanged_ Resource s
r forall a. Semigroup a => a -> a -> a
<>
forall s. Resource s -> Signal ()
resourceQueueCountChanged_ Resource s
r
updateResourceCount :: Resource s -> Int -> Event ()
updateResourceCount :: forall s. Resource s -> Int -> Event ()
updateResourceCount Resource s
r Int
delta =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- forall a. IORef a -> IO a
readIORef (forall s. Resource s -> IORef Int
resourceCountRef Resource s
r)
let a' :: Int
a' = Int
a forall a. Num a => a -> a -> a
+ Int
delta
Int
a' seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef (forall s. Resource s -> IORef Int
resourceCountRef Resource s
r) Int
a'
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (forall s. Resource s -> IORef (TimingStats Int)
resourceCountStatsRef Resource s
r) forall a b. (a -> b) -> a -> b
$
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (Point -> Double
pointTime Point
p) Int
a'
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall a. SignalSource a -> a -> Event ()
triggerSignal (forall s. Resource s -> SignalSource Int
resourceCountSource Resource s
r) Int
a'
updateResourceUtilisationCount :: Resource s -> Int -> Event ()
updateResourceUtilisationCount :: forall s. Resource s -> Int -> Event ()
updateResourceUtilisationCount Resource s
r Int
delta =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- forall a. IORef a -> IO a
readIORef (forall s. Resource s -> IORef Int
resourceUtilisationCountRef Resource s
r)
let a' :: Int
a' = Int
a forall a. Num a => a -> a -> a
+ Int
delta
Int
a' seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef (forall s. Resource s -> IORef Int
resourceUtilisationCountRef Resource s
r) Int
a'
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (forall s. Resource s -> IORef (TimingStats Int)
resourceUtilisationCountStatsRef Resource s
r) forall a b. (a -> b) -> a -> b
$
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (Point -> Double
pointTime Point
p) Int
a'
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall a. SignalSource a -> a -> Event ()
triggerSignal (forall s. Resource s -> SignalSource Int
resourceUtilisationCountSource Resource s
r) Int
a'
updateResourceQueueCount :: Resource s -> Int -> Event ()
updateResourceQueueCount :: forall s. Resource s -> Int -> Event ()
updateResourceQueueCount Resource s
r Int
delta =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- forall a. IORef a -> IO a
readIORef (forall s. Resource s -> IORef Int
resourceQueueCountRef Resource s
r)
let a' :: Int
a' = Int
a forall a. Num a => a -> a -> a
+ Int
delta
Int
a' seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef (forall s. Resource s -> IORef Int
resourceQueueCountRef Resource s
r) Int
a'
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (forall s. Resource s -> IORef (TimingStats Int)
resourceQueueCountStatsRef Resource s
r) forall a b. (a -> b) -> a -> b
$
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (Point -> Double
pointTime Point
p) Int
a'
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall a. SignalSource a -> a -> Event ()
triggerSignal (forall s. Resource s -> SignalSource Int
resourceQueueCountSource Resource s
r) Int
a'
updateResourceWaitTime :: Resource s -> Double -> Event ()
updateResourceWaitTime :: forall s. Resource s -> Double -> Event ()
updateResourceWaitTime Resource s
r Double
delta =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Double
a <- forall a. IORef a -> IO a
readIORef (forall s. Resource s -> IORef Double
resourceTotalWaitTimeRef Resource s
r)
let a' :: Double
a' = Double
a forall a. Num a => a -> a -> a
+ Double
delta
Double
a' seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef (forall s. Resource s -> IORef Double
resourceTotalWaitTimeRef Resource s
r) Double
a'
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (forall s. Resource s -> IORef (SamplingStats Double)
resourceWaitTimeRef Resource s
r) forall a b. (a -> b) -> a -> b
$
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats Double
delta
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall a. SignalSource a -> a -> Event ()
triggerSignal (forall s. Resource s -> SignalSource ()
resourceWaitTimeSource Resource s
r) ()
resetResource :: Resource s -> Event ()
resetResource :: forall s. Resource s -> Event ()
resetResource Resource s
r =
forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
do let t :: Double
t = Point -> Double
pointTime Point
p
Int
count <- forall a. IORef a -> IO a
readIORef (forall s. Resource s -> IORef Int
resourceCountRef Resource s
r)
forall a. IORef a -> a -> IO ()
writeIORef (forall s. Resource s -> IORef (TimingStats Int)
resourceCountStatsRef Resource s
r) forall a b. (a -> b) -> a -> b
$
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
count
Int
utilCount <- forall a. IORef a -> IO a
readIORef (forall s. Resource s -> IORef Int
resourceUtilisationCountRef Resource s
r)
forall a. IORef a -> a -> IO ()
writeIORef (forall s. Resource s -> IORef (TimingStats Int)
resourceUtilisationCountStatsRef Resource s
r) forall a b. (a -> b) -> a -> b
$
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
utilCount
Int
queueCount <- forall a. IORef a -> IO a
readIORef (forall s. Resource s -> IORef Int
resourceQueueCountRef Resource s
r)
forall a. IORef a -> a -> IO ()
writeIORef (forall s. Resource s -> IORef (TimingStats Int)
resourceQueueCountStatsRef Resource s
r) forall a b. (a -> b) -> a -> b
$
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
queueCount
forall a. IORef a -> a -> IO ()
writeIORef (forall s. Resource s -> IORef Double
resourceTotalWaitTimeRef Resource s
r) Double
0
forall a. IORef a -> a -> IO ()
writeIORef (forall s. Resource s -> IORef (SamplingStats Double)
resourceWaitTimeRef Resource s
r) forall a. SamplingData a => SamplingStats a
emptySamplingStats
forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
forall a. SignalSource a -> a -> Event ()
triggerSignal (forall s. Resource s -> SignalSource ()
resourceWaitTimeSource Resource s
r) ()