-- |
-- Module     : Simulation.Aivika.Resource
-- Copyright  : Copyright (c) 2009-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- This module defines the resource which can be acquired and 
-- then released by the discontinuous process 'Process'.
-- The resource can be either limited by the upper bound
-- (run-time check), or it can have no upper bound. The latter
-- is useful for modeling the infinite queue, for example.
--
module Simulation.Aivika.Resource
       (-- * Resource Types
        FCFSResource,
        LCFSResource,
        SIROResource,
        PriorityResource,
        Resource,
        -- * Creating Resource
        newFCFSResource,
        newFCFSResourceWithMaxCount,
        newLCFSResource,
        newLCFSResourceWithMaxCount,
        newSIROResource,
        newSIROResourceWithMaxCount,
        newPriorityResource,
        newPriorityResourceWithMaxCount,
        newResource,
        newResourceWithMaxCount,
        -- * Resource Properties
        resourceStrategy,
        resourceMaxCount,
        resourceCount,
        resourceCountStats,
        resourceUtilisationCount,
        resourceUtilisationCountStats,
        resourceQueueCount,
        resourceQueueCountStats,
        resourceTotalWaitTime,
        resourceWaitTime,
        -- * Requesting for and Releasing Resource
        requestResource,
        requestResourceWithPriority,
        tryRequestResourceWithinEvent,
        releaseResource,
        releaseResourceWithinEvent,
        usingResource,
        usingResourceWithPriority,
        -- * Altering Resource
        incResourceCount,
        decResourceCount,
        -- * Statistics Reset
        resetResource,
        -- * Signals
        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

-- | The ordinary FCFS (First Come - First Serviced) resource.
type FCFSResource = Resource FCFS

-- | The ordinary LCFS (Last Come - First Serviced) resource.
type LCFSResource = Resource LCFS

-- | The SIRO (Serviced in Random Order) resource.
type SIROResource = Resource SIRO

-- | The resource with static priorities.
type PriorityResource = Resource StaticPriorities

-- | Represents the resource with strategy @s@ applied for queuing the requests.
data Resource s = 
  Resource { forall s. Resource s -> s
resourceStrategy :: s,
             -- ^ Return the strategy applied for queuing the requests.
             forall s. Resource s -> Maybe Int
resourceMaxCount :: Maybe Int,
             -- ^ Return the maximum count of the resource, where 'Nothing'
             -- means that the resource has no upper bound.
             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  -- unique references

-- | Create a new FCFS resource with the specified initial count which value becomes
-- the upper bound as well.
newFCFSResource :: Int
                   -- ^ the initial count (and maximal count too) of the resource
                   -> Event FCFSResource
newFCFSResource :: Int -> Event FCFSResource
newFCFSResource = FCFS -> Int -> Event FCFSResource
forall s. QueueStrategy s => s -> Int -> Event (Resource s)
newResource FCFS
FCFS

-- | Create a new FCFS resource with the specified initial and maximum counts,
-- where 'Nothing' means that the resource has no upper bound.
newFCFSResourceWithMaxCount :: Int
                               -- ^ the initial count of the resource
                               -> Maybe Int
                               -- ^ the maximum count of the resource, which can be indefinite
                               -> 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

-- | Create a new LCFS resource with the specified initial count which value becomes
-- the upper bound as well.
newLCFSResource :: Int
                   -- ^ the initial count (and maximal count too) of the resource
                   -> Event LCFSResource
newLCFSResource :: Int -> Event LCFSResource
newLCFSResource = LCFS -> Int -> Event LCFSResource
forall s. QueueStrategy s => s -> Int -> Event (Resource s)
newResource LCFS
LCFS

-- | Create a new LCFS resource with the specified initial and maximum counts,
-- where 'Nothing' means that the resource has no upper bound.
newLCFSResourceWithMaxCount :: Int
                               -- ^ the initial count of the resource
                               -> Maybe Int
                               -- ^ the maximum count of the resource, which can be indefinite
                               -> 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

-- | Create a new SIRO resource with the specified initial count which value becomes
-- the upper bound as well.
newSIROResource :: Int
                   -- ^ the initial count (and maximal count too) of the resource
                   -> Event SIROResource
newSIROResource :: Int -> Event SIROResource
newSIROResource = SIRO -> Int -> Event SIROResource
forall s. QueueStrategy s => s -> Int -> Event (Resource s)
newResource SIRO
SIRO

-- | Create a new SIRO resource with the specified initial and maximum counts,
-- where 'Nothing' means that the resource has no upper bound.
newSIROResourceWithMaxCount :: Int
                               -- ^ the initial count of the resource
                               -> Maybe Int
                               -- ^ the maximum count of the resource, which can be indefinite
                               -> 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

-- | Create a new priority resource with the specified initial count which value becomes
-- the upper bound as well.
newPriorityResource :: Int
                       -- ^ the initial count (and maximal count too) of the resource
                       -> Event PriorityResource
newPriorityResource :: Int -> Event PriorityResource
newPriorityResource = StaticPriorities -> Int -> Event PriorityResource
forall s. QueueStrategy s => s -> Int -> Event (Resource s)
newResource StaticPriorities
StaticPriorities

-- | Create a new priority resource with the specified initial and maximum counts,
-- where 'Nothing' means that the resource has no upper bound.
newPriorityResourceWithMaxCount :: Int
                                   -- ^ the initial count of the resource
                                   -> Maybe Int
                                   -- ^ the maximum count of the resource, which can be indefinite
                                   -> 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

-- | Create a new resource with the specified queue strategy and initial count.
-- The last value becomes the upper bound as well.
newResource :: QueueStrategy s
               => s
               -- ^ the strategy for managing the queuing requests
               -> Int
               -- ^ the initial count (and maximal count too) of the resource
               -> 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)

-- | Create a new resource with the specified queue strategy, initial and maximum counts,
-- where 'Nothing' means that the resource has no upper bound.
newResourceWithMaxCount :: QueueStrategy s
                           => s
                           -- ^ the strategy for managing the queuing requests
                           -> Int
                           -- ^ the initial count of the resource
                           -> Maybe Int
                           -- ^ the maximum count of the resource, which can be indefinite
                           -> 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 }

-- | Return the current available count of the resource.
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)

-- | Return the statistics for the available count of the resource.
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)

-- | Signal triggered when the 'resourceCount' property changes.
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

-- | Signal triggered when the 'resourceCount' property changes.
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

-- | Return the current utilisation count of the resource.
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)

-- | Return the statistics for the utilisation count of the resource.
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)

-- | Signal triggered when the 'resourceUtilisationCount' property changes.
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

-- | Signal triggered when the 'resourceUtilisationCount' property changes.
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

-- | Return the current queue length of the resource.
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)

-- | Return the statistics for the queue length of the resource.
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)

-- | Signal triggered when the 'resourceQueueCount' property changes.
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

-- | Signal triggered when the 'resourceQueueCount' property changes.
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

-- | Return the total wait time of the resource.
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)

-- | Return the statistics for the wait time of the resource.
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)

-- | Signal triggered when the 'resourceTotalWaitTime' and 'resourceWaitTime' properties change.
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

-- | Signal triggered when the 'resourceTotalWaitTime' and 'resourceWaitTime' properties change.
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

-- | Request for the resource decreasing its count in case of success,
-- otherwise suspending the discontinuous process until some other 
-- process releases the resource.
requestResource :: EnqueueStrategy s
                   => Resource s
                   -- ^ the requested resource
                   -> 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 ()

-- | Request with the priority for the resource decreasing its count
-- in case of success, otherwise suspending the discontinuous process
-- until some other process releases the resource.
requestResourceWithPriority :: PriorityQueueStrategy s p
                               => Resource s
                               -- ^ the requested resource
                               -> p
                               -- ^ the priority
                               -> 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 ()

-- | Release the resource increasing its count and resuming one of the
-- previously suspended processes as possible.
releaseResource :: DequeueStrategy s
                   => Resource s
                   -- ^ the resource to release
                   -> 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 ()

-- | Release the resource increasing its count and resuming one of the
-- previously suspended processes as possible.
releaseResourceWithinEvent :: DequeueStrategy s
                              => Resource s
                              -- ^ the resource to release
                              -> 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
  
-- | Release the resource without affecting its utilisation.
releaseResource' :: DequeueStrategy s
                    => Resource s
                    -- ^ the resource to release
                    -> 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 ()

-- | Try to request for the resource decreasing its count in case of success
-- and returning 'True' in the 'Event' monad; otherwise, returning 'False'.
tryRequestResourceWithinEvent :: Resource s
                                 -- ^ the resource which we try to request for
                                 -> 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
               
-- | Acquire the resource, perform some action and safely release the resource               
-- in the end, even if the 'IOException' was raised within the action. 
usingResource :: EnqueueStrategy s
                 => Resource s
                 -- ^ the resource we are going to request for and then release in the end
                 -> Process a
                 -- ^ the action we are going to apply having the resource
                 -> Process a
                 -- ^ the result of the action
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

-- | Acquire the resource with the specified priority, perform some action and
-- safely release the resource in the end, even if the 'IOException' was raised
-- within the action.
usingResourceWithPriority :: PriorityQueueStrategy s p
                             => Resource s
                             -- ^ the resource we are going to request for and then
                             -- release in the end
                             -> p
                             -- ^ the priority
                             -> Process a
                             -- ^ the action we are going to apply having the resource
                             -> Process a
                             -- ^ the result of the action
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

-- | Decrease the count of available resource.
decResourceCount' :: EnqueueStrategy s
                     => Resource s
                     -- ^ the resource for which to decrease the count
                     -> 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
                   
-- | Increase the count of available resource by the specified number,
-- invoking the awaiting processes as needed.
incResourceCount :: DequeueStrategy s
                    => Resource s
                    -- ^ the resource
                    -> Int
                    -- ^ the increment for the resource count
                    -> 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)

-- | Decrease the count of available resource by the specified number,
-- waiting for the processes capturing the resource as needed.
decResourceCount :: EnqueueStrategy s
                    => Resource s
                    -- ^ the resource
                    -> Int
                    -- ^ the decrement for the resource count
                    -> 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)

-- | Signal triggered when one of the resource counters changes.
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

-- | Update the resource count and its statistics.
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'

-- | Update the resource utilisation count and its statistics.
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'

-- | Update the resource queue length and its statistics.
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'

-- | Update the resource wait time and its statistics.
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) ()

-- | Reset the statistics.
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) ()