module Simulation.Aivika.RealTime.RT
(
RT,
RTParams(..),
RTContext,
RTScaling(..),
runRT,
defaultRTParams,
newRTContext,
rtParams,
rtScale,
applyEventRT,
applyEventRT_,
enqueueEventRT,
enqueueEventRT_) where
import Control.Monad
import Control.Monad.Trans
import Control.Concurrent.STM
import Control.Concurrent.Async
import Simulation.Aivika.Trans
import Simulation.Aivika.IO.Comp
import Simulation.Aivika.IO.Ref.Base
import Simulation.Aivika.IO.QueueStrategy
import Simulation.Aivika.IO.Exception
import Simulation.Aivika.RealTime.Internal.RT
import Simulation.Aivika.RealTime.Internal.Channel
import Simulation.Aivika.RealTime.Event
import Simulation.Aivika.RealTime.QueueStrategy
import Simulation.Aivika.RealTime.Comp
import Simulation.Aivika.RealTime.Ref.Base.Lazy
import Simulation.Aivika.RealTime.Ref.Base.Strict
instance (Monad m, MonadIO m, MonadException m, MonadComp m) => MonadDES (RT m) where
instance (Monad m, MonadIO m, MonadException m) => EventIOQueueing (RT m) where
enqueueEventIO = enqueueEvent
invokeEventRT_ :: MonadIO m
=> RTContext m
-> (Event (RT m) () -> Event (RT m) ())
-> Event (RT m) ()
-> m ()
invokeEventRT_ ctx f m =
let ch = rtChannel0 ctx
in liftIO $ writeChannel ch $ f m
invokeEventRT :: MonadIO m
=> RTContext m
-> (Event (RT m) () -> Event (RT m) ())
-> Event (RT m) a
-> m (Async a)
invokeEventRT ctx f m =
do let ch = rtChannel0 ctx
v <- liftIO $ newTVarIO Nothing
liftIO $
writeChannel ch $
f $
do a <- m
liftIO $
atomically $
writeTVar v (Just a)
liftIO $
async $
atomically $
do b <- readTVar v
case b of
Just a -> return a
Nothing -> retry
applyEventRT :: MonadIO m => RTContext m -> Event (RT m) a -> m (Async a)
applyEventRT ctx m = invokeEventRT ctx id m
applyEventRT_ :: MonadIO m => RTContext m -> Event (RT m) () -> m ()
applyEventRT_ ctx m = invokeEventRT_ ctx id m
enqueueEventRT :: MonadIO m => RTContext m -> Double -> Event (RT m) a -> m (Async a)
enqueueEventRT ctx t m = invokeEventRT ctx (enqueueEvent t) m
enqueueEventRT_ :: MonadIO m => RTContext m -> Double -> Event (RT m) () -> m ()
enqueueEventRT_ ctx t m = invokeEventRT_ ctx (enqueueEvent t) m