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 = Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceCountRef Resource s
x IORef Int -> IORef Int -> Bool
forall a. Eq a => a -> a -> Bool
== Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceCountRef Resource s
y
newFCFSResource :: Int
-> Event FCFSResource
newFCFSResource :: Int -> Event FCFSResource
newFCFSResource = FCFS -> Int -> Event FCFSResource
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 = FCFS -> Int -> Maybe Int -> Event FCFSResource
forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Event (Resource s)
newResourceWithMaxCount FCFS
FCFS
newLCFSResource :: Int
-> Event LCFSResource
newLCFSResource :: Int -> Event LCFSResource
newLCFSResource = LCFS -> Int -> Event LCFSResource
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 = LCFS -> Int -> Maybe Int -> Event LCFSResource
forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Event (Resource s)
newResourceWithMaxCount LCFS
LCFS
newSIROResource :: Int
-> Event SIROResource
newSIROResource :: Int -> Event SIROResource
newSIROResource = SIRO -> Int -> Event SIROResource
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 = SIRO -> Int -> Maybe Int -> Event SIROResource
forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Event (Resource s)
newResourceWithMaxCount SIRO
SIRO
newPriorityResource :: Int
-> Event PriorityResource
newPriorityResource :: Int -> Event PriorityResource
newPriorityResource = StaticPriorities -> Int -> Event PriorityResource
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 = StaticPriorities -> Int -> Maybe Int -> Event PriorityResource
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 =
s -> Int -> Maybe Int -> Event (Resource s)
forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Event (Resource s)
newResourceWithMaxCount s
s Int
count (Int -> Maybe Int
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 =
(Point -> IO (Resource s)) -> Event (Resource s)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (Resource s)) -> Event (Resource s))
-> (Point -> IO (Resource s)) -> Event (Resource s)
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
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
count Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
SimulationRetry -> IO ()
forall e a. Exception e => e -> IO a
throwIO (SimulationRetry -> IO ()) -> SimulationRetry -> IO ()
forall a b. (a -> b) -> a -> b
$
String -> SimulationRetry
SimulationRetry (String -> SimulationRetry) -> String -> SimulationRetry
forall a b. (a -> b) -> a -> b
$
String
"The resource count cannot be negative: " String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"newResourceWithMaxCount."
case Maybe Int
maxCount of
Just Int
maxCount | Int
count Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
maxCount ->
SimulationRetry -> IO ()
forall e a. Exception e => e -> IO a
throwIO (SimulationRetry -> IO ()) -> SimulationRetry -> IO ()
forall a b. (a -> b) -> a -> b
$
String -> SimulationRetry
SimulationRetry (String -> SimulationRetry) -> String -> SimulationRetry
forall a b. (a -> b) -> a -> b
$
String
"The resource count cannot be greater than " String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"its maximum value: newResourceWithMaxCount."
Maybe Int
_ ->
() -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
IORef Int
countRef <- Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
count
IORef (TimingStats Int)
countStatsRef <- TimingStats Int -> IO (IORef (TimingStats Int))
forall a. a -> IO (IORef a)
newIORef (TimingStats Int -> IO (IORef (TimingStats Int)))
-> TimingStats Int -> IO (IORef (TimingStats Int))
forall a b. (a -> b) -> a -> b
$ Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
count
SignalSource Int
countSource <- Run -> Simulation (SignalSource Int) -> IO (SignalSource Int)
forall a. Run -> Simulation a -> IO a
invokeSimulation Run
r Simulation (SignalSource Int)
forall a. Simulation (SignalSource a)
newSignalSource
IORef Int
utilCountRef <- Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
0
IORef (TimingStats Int)
utilCountStatsRef <- TimingStats Int -> IO (IORef (TimingStats Int))
forall a. a -> IO (IORef a)
newIORef (TimingStats Int -> IO (IORef (TimingStats Int)))
-> TimingStats Int -> IO (IORef (TimingStats Int))
forall a b. (a -> b) -> a -> b
$ Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
0
SignalSource Int
utilCountSource <- Run -> Simulation (SignalSource Int) -> IO (SignalSource Int)
forall a. Run -> Simulation a -> IO a
invokeSimulation Run
r Simulation (SignalSource Int)
forall a. Simulation (SignalSource a)
newSignalSource
IORef Int
queueCountRef <- Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
0
IORef (TimingStats Int)
queueCountStatsRef <- TimingStats Int -> IO (IORef (TimingStats Int))
forall a. a -> IO (IORef a)
newIORef (TimingStats Int -> IO (IORef (TimingStats Int)))
-> TimingStats Int -> IO (IORef (TimingStats Int))
forall a b. (a -> b) -> a -> b
$ Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
0
SignalSource Int
queueCountSource <- Run -> Simulation (SignalSource Int) -> IO (SignalSource Int)
forall a. Run -> Simulation a -> IO a
invokeSimulation Run
r Simulation (SignalSource Int)
forall a. Simulation (SignalSource a)
newSignalSource
IORef Double
totalWaitTimeRef <- Double -> IO (IORef Double)
forall a. a -> IO (IORef a)
newIORef Double
0
IORef (SamplingStats Double)
waitTimeRef <- SamplingStats Double -> IO (IORef (SamplingStats Double))
forall a. a -> IO (IORef a)
newIORef SamplingStats Double
forall a. SamplingData a => SamplingStats a
emptySamplingStats
SignalSource ()
waitTimeSource <- Run -> Simulation (SignalSource ()) -> IO (SignalSource ())
forall a. Run -> Simulation a -> IO a
invokeSimulation Run
r Simulation (SignalSource ())
forall a. Simulation (SignalSource a)
newSignalSource
StrategyQueue s ResourceItem
waitList <- Run
-> Simulation (StrategyQueue s ResourceItem)
-> IO (StrategyQueue s ResourceItem)
forall a. Run -> Simulation a -> IO a
invokeSimulation Run
r (Simulation (StrategyQueue s ResourceItem)
-> IO (StrategyQueue s ResourceItem))
-> Simulation (StrategyQueue s ResourceItem)
-> IO (StrategyQueue s ResourceItem)
forall a b. (a -> b) -> a -> b
$ s -> Simulation (StrategyQueue s ResourceItem)
forall i. s -> Simulation (StrategyQueue s i)
forall s i. QueueStrategy s => s -> Simulation (StrategyQueue s i)
newStrategyQueue s
s
Resource s -> IO (Resource s)
forall a. a -> IO a
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 =
(Point -> IO Int) -> Event Int
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Int) -> Event Int) -> (Point -> IO Int) -> Event Int
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Int
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 =
(Point -> IO (TimingStats Int)) -> Event (TimingStats Int)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (TimingStats Int)) -> Event (TimingStats Int))
-> (Point -> IO (TimingStats Int)) -> Event (TimingStats Int)
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef (TimingStats Int) -> IO (TimingStats Int)
forall a. IORef a -> IO a
readIORef (Resource s -> IORef (TimingStats Int)
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 =
SignalSource Int -> Signal Int
forall a. SignalSource a -> Signal a
publishSignal (SignalSource Int -> Signal Int) -> SignalSource Int -> Signal Int
forall a b. (a -> b) -> a -> b
$ Resource s -> SignalSource Int
forall s. Resource s -> SignalSource Int
resourceCountSource Resource s
r
resourceCountChanged_ :: Resource s -> Signal ()
resourceCountChanged_ :: forall s. Resource s -> Signal ()
resourceCountChanged_ Resource s
r =
(Int -> ()) -> Signal Int -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> Int -> ()
forall a b. a -> b -> a
const ()) (Signal Int -> Signal ()) -> Signal Int -> Signal ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Signal Int
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 =
(Point -> IO Int) -> Event Int
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Int) -> Event Int) -> (Point -> IO Int) -> Event Int
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Int
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 =
(Point -> IO (TimingStats Int)) -> Event (TimingStats Int)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (TimingStats Int)) -> Event (TimingStats Int))
-> (Point -> IO (TimingStats Int)) -> Event (TimingStats Int)
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef (TimingStats Int) -> IO (TimingStats Int)
forall a. IORef a -> IO a
readIORef (Resource s -> IORef (TimingStats Int)
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 =
SignalSource Int -> Signal Int
forall a. SignalSource a -> Signal a
publishSignal (SignalSource Int -> Signal Int) -> SignalSource Int -> Signal Int
forall a b. (a -> b) -> a -> b
$ Resource s -> SignalSource Int
forall s. Resource s -> SignalSource Int
resourceUtilisationCountSource Resource s
r
resourceUtilisationCountChanged_ :: Resource s -> Signal ()
resourceUtilisationCountChanged_ :: forall s. Resource s -> Signal ()
resourceUtilisationCountChanged_ Resource s
r =
(Int -> ()) -> Signal Int -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> Int -> ()
forall a b. a -> b -> a
const ()) (Signal Int -> Signal ()) -> Signal Int -> Signal ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Signal Int
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 =
(Point -> IO Int) -> Event Int
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Int) -> Event Int) -> (Point -> IO Int) -> Event Int
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Int
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 =
(Point -> IO (TimingStats Int)) -> Event (TimingStats Int)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (TimingStats Int)) -> Event (TimingStats Int))
-> (Point -> IO (TimingStats Int)) -> Event (TimingStats Int)
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef (TimingStats Int) -> IO (TimingStats Int)
forall a. IORef a -> IO a
readIORef (Resource s -> IORef (TimingStats Int)
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 =
SignalSource Int -> Signal Int
forall a. SignalSource a -> Signal a
publishSignal (SignalSource Int -> Signal Int) -> SignalSource Int -> Signal Int
forall a b. (a -> b) -> a -> b
$ Resource s -> SignalSource Int
forall s. Resource s -> SignalSource Int
resourceQueueCountSource Resource s
r
resourceQueueCountChanged_ :: Resource s -> Signal ()
resourceQueueCountChanged_ :: forall s. Resource s -> Signal ()
resourceQueueCountChanged_ Resource s
r =
(Int -> ()) -> Signal Int -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> Int -> ()
forall a b. a -> b -> a
const ()) (Signal Int -> Signal ()) -> Signal Int -> Signal ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Signal Int
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 =
(Point -> IO Double) -> Event Double
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Double) -> Event Double)
-> (Point -> IO Double) -> Event Double
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef Double -> IO Double
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Double
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 =
(Point -> IO (SamplingStats Double))
-> Event (SamplingStats Double)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (SamplingStats Double))
-> Event (SamplingStats Double))
-> (Point -> IO (SamplingStats Double))
-> Event (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef (SamplingStats Double) -> IO (SamplingStats Double)
forall a. IORef a -> IO a
readIORef (Resource s -> IORef (SamplingStats Double)
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 =
(() -> Event (SamplingStats Double))
-> Signal () -> Signal (SamplingStats Double)
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (\() -> Resource s -> Event (SamplingStats Double)
forall s. Resource s -> Event (SamplingStats Double)
resourceWaitTime Resource s
r) (Signal () -> Signal (SamplingStats Double))
-> Signal () -> Signal (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Resource s -> Signal ()
forall s. Resource s -> Signal ()
resourceWaitTimeChanged_ Resource s
r
resourceWaitTimeChanged_ :: Resource s -> Signal ()
resourceWaitTimeChanged_ :: forall s. Resource s -> Signal ()
resourceWaitTimeChanged_ Resource s
r =
SignalSource () -> Signal ()
forall a. SignalSource a -> Signal a
publishSignal (SignalSource () -> Signal ()) -> SignalSource () -> Signal ()
forall a b. (a -> b) -> a -> b
$ Resource s -> SignalSource ()
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 =
(ProcessId -> Cont ()) -> Process ()
forall a. (ProcessId -> Cont a) -> Process a
Process ((ProcessId -> Cont ()) -> Process ())
-> (ProcessId -> Cont ()) -> Process ()
forall a b. (a -> b) -> a -> b
$ \ProcessId
pid ->
(ContParams () -> Event ()) -> Cont ()
forall a. (ContParams a -> Event ()) -> Cont a
Cont ((ContParams () -> Event ()) -> Cont ())
-> (ContParams () -> Event ()) -> Cont ()
forall a b. (a -> b) -> a -> b
$ \ContParams ()
c ->
(Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceCountRef Resource s
r)
if Int
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then do FrozenCont ()
c <- Point -> Event (FrozenCont ()) -> IO (FrozenCont ())
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event (FrozenCont ()) -> IO (FrozenCont ()))
-> Event (FrozenCont ()) -> IO (FrozenCont ())
forall a b. (a -> b) -> a -> b
$
ContParams () -> () -> Event () -> Event (FrozenCont ())
forall a. ContParams a -> a -> Event () -> Event (FrozenCont a)
freezeContReentering ContParams ()
c () (Event () -> Event (FrozenCont ()))
-> Event () -> Event (FrozenCont ())
forall a b. (a -> b) -> a -> b
$
ContParams () -> Cont () -> Event ()
forall a. ContParams a -> Cont a -> Event ()
invokeCont ContParams ()
c (Cont () -> Event ()) -> Cont () -> Event ()
forall a b. (a -> b) -> a -> b
$
ProcessId -> Process () -> Cont ()
forall a. ProcessId -> Process a -> Cont a
invokeProcess ProcessId
pid (Process () -> Cont ()) -> Process () -> Cont ()
forall a b. (a -> b) -> a -> b
$
Resource s -> Process ()
forall s. EnqueueStrategy s => Resource s -> Process ()
requestResource Resource s
r
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
StrategyQueue s ResourceItem -> ResourceItem -> Event ()
forall s i. EnqueueStrategy s => StrategyQueue s i -> i -> Event ()
forall i. StrategyQueue s i -> i -> Event ()
strategyEnqueue (Resource s -> StrategyQueue s ResourceItem
forall s. Resource s -> StrategyQueue s ResourceItem
resourceWaitList Resource s
r) (ResourceItem -> Event ()) -> ResourceItem -> Event ()
forall a b. (a -> b) -> a -> b
$
Double -> FrozenCont () -> ResourceItem
ResourceItem (Point -> Double
pointTime Point
p) FrozenCont ()
c
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Int -> Event ()
forall s. Resource s -> Int -> Event ()
updateResourceQueueCount Resource s
r Int
1
else do Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Double -> Event ()
forall s. Resource s -> Double -> Event ()
updateResourceWaitTime Resource s
r Double
0
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Int -> Event ()
forall s. Resource s -> Int -> Event ()
updateResourceCount Resource s
r (-Int
1)
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Int -> Event ()
forall s. Resource s -> Int -> Event ()
updateResourceUtilisationCount Resource s
r Int
1
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ ContParams () -> () -> Event ()
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 =
(ProcessId -> Cont ()) -> Process ()
forall a. (ProcessId -> Cont a) -> Process a
Process ((ProcessId -> Cont ()) -> Process ())
-> (ProcessId -> Cont ()) -> Process ()
forall a b. (a -> b) -> a -> b
$ \ProcessId
pid ->
(ContParams () -> Event ()) -> Cont ()
forall a. (ContParams a -> Event ()) -> Cont a
Cont ((ContParams () -> Event ()) -> Cont ())
-> (ContParams () -> Event ()) -> Cont ()
forall a b. (a -> b) -> a -> b
$ \ContParams ()
c ->
(Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceCountRef Resource s
r)
if Int
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then do FrozenCont ()
c <- Point -> Event (FrozenCont ()) -> IO (FrozenCont ())
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event (FrozenCont ()) -> IO (FrozenCont ()))
-> Event (FrozenCont ()) -> IO (FrozenCont ())
forall a b. (a -> b) -> a -> b
$
ContParams () -> () -> Event () -> Event (FrozenCont ())
forall a. ContParams a -> a -> Event () -> Event (FrozenCont a)
freezeContReentering ContParams ()
c () (Event () -> Event (FrozenCont ()))
-> Event () -> Event (FrozenCont ())
forall a b. (a -> b) -> a -> b
$
ContParams () -> Cont () -> Event ()
forall a. ContParams a -> Cont a -> Event ()
invokeCont ContParams ()
c (Cont () -> Event ()) -> Cont () -> Event ()
forall a b. (a -> b) -> a -> b
$
ProcessId -> Process () -> Cont ()
forall a. ProcessId -> Process a -> Cont a
invokeProcess ProcessId
pid (Process () -> Cont ()) -> Process () -> Cont ()
forall a b. (a -> b) -> a -> b
$
Resource s -> p -> Process ()
forall s p.
PriorityQueueStrategy s p =>
Resource s -> p -> Process ()
requestResourceWithPriority Resource s
r p
priority
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
StrategyQueue s ResourceItem -> p -> ResourceItem -> Event ()
forall i. StrategyQueue s i -> p -> i -> Event ()
forall s p i.
PriorityQueueStrategy s p =>
StrategyQueue s i -> p -> i -> Event ()
strategyEnqueueWithPriority (Resource s -> StrategyQueue s ResourceItem
forall s. Resource s -> StrategyQueue s ResourceItem
resourceWaitList Resource s
r) p
priority (ResourceItem -> Event ()) -> ResourceItem -> Event ()
forall a b. (a -> b) -> a -> b
$
Double -> FrozenCont () -> ResourceItem
ResourceItem (Point -> Double
pointTime Point
p) FrozenCont ()
c
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Int -> Event ()
forall s. Resource s -> Int -> Event ()
updateResourceQueueCount Resource s
r Int
1
else do Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Double -> Event ()
forall s. Resource s -> Double -> Event ()
updateResourceWaitTime Resource s
r Double
0
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Int -> Event ()
forall s. Resource s -> Int -> Event ()
updateResourceCount Resource s
r (-Int
1)
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Int -> Event ()
forall s. Resource s -> Int -> Event ()
updateResourceUtilisationCount Resource s
r Int
1
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ ContParams () -> () -> Event ()
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 =
(ProcessId -> Cont ()) -> Process ()
forall a. (ProcessId -> Cont a) -> Process a
Process ((ProcessId -> Cont ()) -> Process ())
-> (ProcessId -> Cont ()) -> Process ()
forall a b. (a -> b) -> a -> b
$ \ProcessId
_ ->
(ContParams () -> Event ()) -> Cont ()
forall a. (ContParams a -> Event ()) -> Cont a
Cont ((ContParams () -> Event ()) -> Cont ())
-> (ContParams () -> Event ()) -> Cont ()
forall a b. (a -> b) -> a -> b
$ \ContParams ()
c ->
(Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Event ()
forall s. DequeueStrategy s => Resource s -> Event ()
releaseResourceWithinEvent Resource s
r
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ ContParams () -> () -> Event ()
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 =
(Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Int -> Event ()
forall s. Resource s -> Int -> Event ()
updateResourceUtilisationCount Resource s
r (-Int
1)
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Event ()
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 =
(Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceCountRef Resource s
r)
let a' :: Int
a' = Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
case Resource s -> Maybe Int
forall s. Resource s -> Maybe Int
resourceMaxCount Resource s
r of
Just Int
maxCount | Int
a' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
maxCount ->
SimulationRetry -> IO ()
forall e a. Exception e => e -> IO a
throwIO (SimulationRetry -> IO ()) -> SimulationRetry -> IO ()
forall a b. (a -> b) -> a -> b
$
String -> SimulationRetry
SimulationRetry (String -> SimulationRetry) -> String -> SimulationRetry
forall a b. (a -> b) -> a -> b
$
String
"The resource count cannot be greater than " String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"its maximum value: releaseResource'."
Maybe Int
_ ->
() -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Bool
f <- Point -> Event Bool -> IO Bool
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event Bool -> IO Bool) -> Event Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$
StrategyQueue s ResourceItem -> Event Bool
forall s i. QueueStrategy s => StrategyQueue s i -> Event Bool
forall i. StrategyQueue s i -> Event Bool
strategyQueueNull (Resource s -> StrategyQueue s ResourceItem
forall s. Resource s -> StrategyQueue s ResourceItem
resourceWaitList Resource s
r)
if Bool
f
then Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Int -> Event ()
forall s. Resource s -> Int -> Event ()
updateResourceCount Resource s
r Int
1
else do ResourceItem
x <- Point -> Event ResourceItem -> IO ResourceItem
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event ResourceItem -> IO ResourceItem)
-> Event ResourceItem -> IO ResourceItem
forall a b. (a -> b) -> a -> b
$
StrategyQueue s ResourceItem -> Event ResourceItem
forall s i. DequeueStrategy s => StrategyQueue s i -> Event i
forall i. StrategyQueue s i -> Event i
strategyDequeue (Resource s -> StrategyQueue s ResourceItem
forall s. Resource s -> StrategyQueue s ResourceItem
resourceWaitList Resource s
r)
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Int -> Event ()
forall s. Resource s -> Int -> Event ()
updateResourceQueueCount Resource s
r (-Int
1)
Maybe (ContParams ())
c <- Point
-> Event (Maybe (ContParams ())) -> IO (Maybe (ContParams ()))
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event (Maybe (ContParams ())) -> IO (Maybe (ContParams ())))
-> Event (Maybe (ContParams ())) -> IO (Maybe (ContParams ()))
forall a b. (a -> b) -> a -> b
$ FrozenCont () -> Event (Maybe (ContParams ()))
forall a. FrozenCont a -> Event (Maybe (ContParams a))
unfreezeCont (ResourceItem -> FrozenCont ()
resourceItemCont ResourceItem
x)
case Maybe (ContParams ())
c of
Maybe (ContParams ())
Nothing ->
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Event ()
forall s. DequeueStrategy s => Resource s -> Event ()
releaseResource' Resource s
r
Just ContParams ()
c ->
do Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Double -> Event ()
forall s. Resource s -> Double -> Event ()
updateResourceWaitTime Resource s
r (Point -> Double
pointTime Point
p Double -> Double -> Double
forall a. Num a => a -> a -> a
- ResourceItem -> Double
resourceItemTime ResourceItem
x)
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Int -> Event ()
forall s. Resource s -> Int -> Event ()
updateResourceUtilisationCount Resource s
r Int
1
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Double -> Event () -> Event ()
enqueueEvent (Point -> Double
pointTime Point
p) (Event () -> Event ()) -> Event () -> Event ()
forall a b. (a -> b) -> a -> b
$ ContParams () -> () -> Event ()
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 =
(Point -> IO Bool) -> Event Bool
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Bool) -> Event Bool)
-> (Point -> IO Bool) -> Event Bool
forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceCountRef Resource s
r)
if Int
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
else do Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Double -> Event ()
forall s. Resource s -> Double -> Event ()
updateResourceWaitTime Resource s
r Double
0
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Int -> Event ()
forall s. Resource s -> Int -> Event ()
updateResourceCount Resource s
r (-Int
1)
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$ Resource s -> Int -> Event ()
forall s. Resource s -> Int -> Event ()
updateResourceUtilisationCount Resource s
r Int
1
Bool -> IO Bool
forall a. a -> IO a
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 Resource s -> Process ()
forall s. EnqueueStrategy s => Resource s -> Process ()
requestResource Resource s
r
Process a -> Process () -> Process a
forall a b. Process a -> Process b -> Process a
finallyProcess Process a
m (Process () -> Process a) -> Process () -> Process a
forall a b. (a -> b) -> a -> b
$ Resource s -> Process ()
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 Resource s -> p -> Process ()
forall s p.
PriorityQueueStrategy s p =>
Resource s -> p -> Process ()
requestResourceWithPriority Resource s
r p
priority
Process a -> Process () -> Process a
forall a b. Process a -> Process b -> Process a
finallyProcess Process a
m (Process () -> Process a) -> Process () -> Process a
forall a b. (a -> b) -> a -> b
$ Resource s -> Process ()
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 Event () -> Process ()
forall a. Event a -> Process a
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Event () -> Process ()) -> Event () -> Process ()
forall a b. (a -> b) -> a -> b
$
Resource s -> Int -> Event ()
forall s. Resource s -> Int -> Event ()
updateResourceUtilisationCount Resource s
r (-Int
1)
Resource s -> Process ()
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = SimulationRetry -> Event ()
forall e a. Exception e => e -> Event a
throwEvent (SimulationRetry -> Event ()) -> SimulationRetry -> Event ()
forall a b. (a -> b) -> a -> b
$ String -> SimulationRetry
SimulationRetry String
"The increment cannot be negative: incResourceCount"
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = () -> Event ()
forall a. a -> Event a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise =
do Resource s -> Event ()
forall s. DequeueStrategy s => Resource s -> Event ()
releaseResource' Resource s
r
Resource s -> Int -> Event ()
forall s. DequeueStrategy s => Resource s -> Int -> Event ()
incResourceCount Resource s
r (Int
n Int -> Int -> Int
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = SimulationRetry -> Process ()
forall e a. Exception e => e -> Process a
throwProcess (SimulationRetry -> Process ()) -> SimulationRetry -> Process ()
forall a b. (a -> b) -> a -> b
$ String -> SimulationRetry
SimulationRetry String
"The decrement cannot be negative: decResourceCount"
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = () -> Process ()
forall a. a -> Process a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise =
do Resource s -> Process ()
forall s. EnqueueStrategy s => Resource s -> Process ()
decResourceCount' Resource s
r
Resource s -> Int -> Process ()
forall s. EnqueueStrategy s => Resource s -> Int -> Process ()
decResourceCount Resource s
r (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
resourceChanged_ :: Resource s -> Signal ()
resourceChanged_ :: forall s. Resource s -> Signal ()
resourceChanged_ Resource s
r =
Resource s -> Signal ()
forall s. Resource s -> Signal ()
resourceCountChanged_ Resource s
r Signal () -> Signal () -> Signal ()
forall a. Semigroup a => a -> a -> a
<>
Resource s -> Signal ()
forall s. Resource s -> Signal ()
resourceUtilisationCountChanged_ Resource s
r Signal () -> Signal () -> Signal ()
forall a. Semigroup a => a -> a -> a
<>
Resource s -> Signal ()
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 =
(Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceCountRef Resource s
r)
let a' :: Int
a' = Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
delta
Int
a' Int -> IO () -> IO ()
forall a b. a -> b -> b
`seq` IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceCountRef Resource s
r) Int
a'
IORef (TimingStats Int)
-> (TimingStats Int -> TimingStats Int) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Resource s -> IORef (TimingStats Int)
forall s. Resource s -> IORef (TimingStats Int)
resourceCountStatsRef Resource s
r) ((TimingStats Int -> TimingStats Int) -> IO ())
-> (TimingStats Int -> TimingStats Int) -> IO ()
forall a b. (a -> b) -> a -> b
$
Double -> Int -> TimingStats Int -> TimingStats Int
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (Point -> Double
pointTime Point
p) Int
a'
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
SignalSource Int -> Int -> Event ()
forall a. SignalSource a -> a -> Event ()
triggerSignal (Resource s -> SignalSource Int
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 =
(Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceUtilisationCountRef Resource s
r)
let a' :: Int
a' = Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
delta
Int
a' Int -> IO () -> IO ()
forall a b. a -> b -> b
`seq` IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceUtilisationCountRef Resource s
r) Int
a'
IORef (TimingStats Int)
-> (TimingStats Int -> TimingStats Int) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Resource s -> IORef (TimingStats Int)
forall s. Resource s -> IORef (TimingStats Int)
resourceUtilisationCountStatsRef Resource s
r) ((TimingStats Int -> TimingStats Int) -> IO ())
-> (TimingStats Int -> TimingStats Int) -> IO ()
forall a b. (a -> b) -> a -> b
$
Double -> Int -> TimingStats Int -> TimingStats Int
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (Point -> Double
pointTime Point
p) Int
a'
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
SignalSource Int -> Int -> Event ()
forall a. SignalSource a -> a -> Event ()
triggerSignal (Resource s -> SignalSource Int
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 =
(Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Int
a <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceQueueCountRef Resource s
r)
let a' :: Int
a' = Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
delta
Int
a' Int -> IO () -> IO ()
forall a b. a -> b -> b
`seq` IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceQueueCountRef Resource s
r) Int
a'
IORef (TimingStats Int)
-> (TimingStats Int -> TimingStats Int) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Resource s -> IORef (TimingStats Int)
forall s. Resource s -> IORef (TimingStats Int)
resourceQueueCountStatsRef Resource s
r) ((TimingStats Int -> TimingStats Int) -> IO ())
-> (TimingStats Int -> TimingStats Int) -> IO ()
forall a b. (a -> b) -> a -> b
$
Double -> Int -> TimingStats Int -> TimingStats Int
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (Point -> Double
pointTime Point
p) Int
a'
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
SignalSource Int -> Int -> Event ()
forall a. SignalSource a -> a -> Event ()
triggerSignal (Resource s -> SignalSource Int
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 =
(Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
do Double
a <- IORef Double -> IO Double
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Double
forall s. Resource s -> IORef Double
resourceTotalWaitTimeRef Resource s
r)
let a' :: Double
a' = Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
delta
Double
a' Double -> IO () -> IO ()
forall a b. a -> b -> b
`seq` IORef Double -> Double -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Resource s -> IORef Double
forall s. Resource s -> IORef Double
resourceTotalWaitTimeRef Resource s
r) Double
a'
IORef (SamplingStats Double)
-> (SamplingStats Double -> SamplingStats Double) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Resource s -> IORef (SamplingStats Double)
forall s. Resource s -> IORef (SamplingStats Double)
resourceWaitTimeRef Resource s
r) ((SamplingStats Double -> SamplingStats Double) -> IO ())
-> (SamplingStats Double -> SamplingStats Double) -> IO ()
forall a b. (a -> b) -> a -> b
$
Double -> SamplingStats Double -> SamplingStats Double
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats Double
delta
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
SignalSource () -> () -> Event ()
forall a. SignalSource a -> a -> Event ()
triggerSignal (Resource s -> SignalSource ()
forall s. Resource s -> SignalSource ()
resourceWaitTimeSource Resource s
r) ()
resetResource :: Resource s -> Event ()
resetResource :: forall s. Resource s -> Event ()
resetResource Resource s
r =
(Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
do let t :: Double
t = Point -> Double
pointTime Point
p
Int
count <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceCountRef Resource s
r)
IORef (TimingStats Int) -> TimingStats Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Resource s -> IORef (TimingStats Int)
forall s. Resource s -> IORef (TimingStats Int)
resourceCountStatsRef Resource s
r) (TimingStats Int -> IO ()) -> TimingStats Int -> IO ()
forall a b. (a -> b) -> a -> b
$
Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
count
Int
utilCount <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceUtilisationCountRef Resource s
r)
IORef (TimingStats Int) -> TimingStats Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Resource s -> IORef (TimingStats Int)
forall s. Resource s -> IORef (TimingStats Int)
resourceUtilisationCountStatsRef Resource s
r) (TimingStats Int -> IO ()) -> TimingStats Int -> IO ()
forall a b. (a -> b) -> a -> b
$
Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
utilCount
Int
queueCount <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Resource s -> IORef Int
forall s. Resource s -> IORef Int
resourceQueueCountRef Resource s
r)
IORef (TimingStats Int) -> TimingStats Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Resource s -> IORef (TimingStats Int)
forall s. Resource s -> IORef (TimingStats Int)
resourceQueueCountStatsRef Resource s
r) (TimingStats Int -> IO ()) -> TimingStats Int -> IO ()
forall a b. (a -> b) -> a -> b
$
Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
queueCount
IORef Double -> Double -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Resource s -> IORef Double
forall s. Resource s -> IORef Double
resourceTotalWaitTimeRef Resource s
r) Double
0
IORef (SamplingStats Double) -> SamplingStats Double -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Resource s -> IORef (SamplingStats Double)
forall s. Resource s -> IORef (SamplingStats Double)
resourceWaitTimeRef Resource s
r) SamplingStats Double
forall a. SamplingData a => SamplingStats a
emptySamplingStats
Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
SignalSource () -> () -> Event ()
forall a. SignalSource a -> a -> Event ()
triggerSignal (Resource s -> SignalSource ()
forall s. Resource s -> SignalSource ()
resourceWaitTimeSource Resource s
r) ()