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
{-# SPECIALIZE instance MonadDES (RT IO) #-}
instance (Monad m, MonadIO m, MonadException m) => EventIOQueueing (RT m) where
{-# SPECIALIZE instance EventIOQueueing (RT IO) #-}
enqueueEventIO :: Double -> Event (RT m) () -> Event (RT m) ()
enqueueEventIO = forall (m :: * -> *).
EventQueueing m =>
Double -> Event m () -> Event m ()
enqueueEvent
invokeEventRT_ :: MonadIO m
=> RTContext m
-> (Event (RT m) () -> Event (RT m) ())
-> Event (RT m) ()
-> m ()
{-# INLINABLE invokeEventRT_ #-}
invokeEventRT_ :: forall (m :: * -> *).
MonadIO m =>
RTContext m
-> (Event (RT m) () -> Event (RT m) ()) -> Event (RT m) () -> m ()
invokeEventRT_ RTContext m
ctx Event (RT m) () -> Event (RT m) ()
f Event (RT m) ()
m =
let ch :: Channel (Event (RT m) ())
ch = forall (m :: * -> *). RTContext m -> Channel (Event (RT m) ())
rtChannel0 RTContext m
ctx
in forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Channel a -> a -> IO ()
writeChannel Channel (Event (RT m) ())
ch forall a b. (a -> b) -> a -> b
$ Event (RT m) () -> Event (RT m) ()
f Event (RT m) ()
m
invokeEventRT :: MonadIO m
=> RTContext m
-> (Event (RT m) () -> Event (RT m) ())
-> Event (RT m) a
-> m (Async a)
{-# INLINABLE invokeEventRT #-}
invokeEventRT :: forall (m :: * -> *) a.
MonadIO m =>
RTContext m
-> (Event (RT m) () -> Event (RT m) ())
-> Event (RT m) a
-> m (Async a)
invokeEventRT RTContext m
ctx Event (RT m) () -> Event (RT m) ()
f Event (RT m) a
m =
do let ch :: Channel (Event (RT m) ())
ch = forall (m :: * -> *). RTContext m -> Channel (Event (RT m) ())
rtChannel0 RTContext m
ctx
TVar (Maybe a)
v <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. a -> IO (TVar a)
newTVarIO forall a. Maybe a
Nothing
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$
forall a. Channel a -> a -> IO ()
writeChannel Channel (Event (RT m) ())
ch forall a b. (a -> b) -> a -> b
$
Event (RT m) () -> Event (RT m) ()
f forall a b. (a -> b) -> a -> b
$
do a
a <- Event (RT m) a
m
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$
forall a. STM a -> IO a
atomically forall a b. (a -> b) -> a -> b
$
forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe a)
v (forall a. a -> Maybe a
Just a
a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$
forall a. IO a -> IO (Async a)
async forall a b. (a -> b) -> a -> b
$
forall a. STM a -> IO a
atomically forall a b. (a -> b) -> a -> b
$
do Maybe a
b <- forall a. TVar a -> STM a
readTVar TVar (Maybe a)
v
case Maybe a
b of
Just a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return a
a
Maybe a
Nothing -> forall a. STM a
retry
applyEventRT :: MonadIO m => RTContext m -> Event (RT m) a -> m (Async a)
{-# INLINABLE applyEventRT #-}
applyEventRT :: forall (m :: * -> *) a.
MonadIO m =>
RTContext m -> Event (RT m) a -> m (Async a)
applyEventRT RTContext m
ctx Event (RT m) a
m = forall (m :: * -> *) a.
MonadIO m =>
RTContext m
-> (Event (RT m) () -> Event (RT m) ())
-> Event (RT m) a
-> m (Async a)
invokeEventRT RTContext m
ctx forall a. a -> a
id Event (RT m) a
m
applyEventRT_ :: MonadIO m => RTContext m -> Event (RT m) () -> m ()
{-# INLINABLE applyEventRT_ #-}
applyEventRT_ :: forall (m :: * -> *).
MonadIO m =>
RTContext m -> Event (RT m) () -> m ()
applyEventRT_ RTContext m
ctx Event (RT m) ()
m = forall (m :: * -> *).
MonadIO m =>
RTContext m
-> (Event (RT m) () -> Event (RT m) ()) -> Event (RT m) () -> m ()
invokeEventRT_ RTContext m
ctx forall a. a -> a
id Event (RT m) ()
m
enqueueEventRT :: MonadIO m => RTContext m -> Double -> Event (RT m) a -> m (Async a)
{-# INLINABLE enqueueEventRT #-}
enqueueEventRT :: forall (m :: * -> *) a.
MonadIO m =>
RTContext m -> Double -> Event (RT m) a -> m (Async a)
enqueueEventRT RTContext m
ctx Double
t Event (RT m) a
m = forall (m :: * -> *) a.
MonadIO m =>
RTContext m
-> (Event (RT m) () -> Event (RT m) ())
-> Event (RT m) a
-> m (Async a)
invokeEventRT RTContext m
ctx (forall (m :: * -> *).
EventQueueing m =>
Double -> Event m () -> Event m ()
enqueueEvent Double
t) Event (RT m) a
m
enqueueEventRT_ :: MonadIO m => RTContext m -> Double -> Event (RT m) () -> m ()
{-# INLINABLE enqueueEventRT_ #-}
enqueueEventRT_ :: forall (m :: * -> *).
MonadIO m =>
RTContext m -> Double -> Event (RT m) () -> m ()
enqueueEventRT_ RTContext m
ctx Double
t Event (RT m) ()
m = forall (m :: * -> *).
MonadIO m =>
RTContext m
-> (Event (RT m) () -> Event (RT m) ()) -> Event (RT m) () -> m ()
invokeEventRT_ RTContext m
ctx (forall (m :: * -> *).
EventQueueing m =>
Double -> Event m () -> Event m ()
enqueueEvent Double
t) Event (RT m) ()
m