module Simulation.Aivika.Trans.Concurrent.MVar
(withMVarComp,
withMVarParameter,
withMVarSimulation,
withMVarDynamics,
withMVarEvent) where
import Control.Concurrent.MVar
import Control.Monad
import Control.Monad.Trans
import qualified Control.Monad.Catch as MC
import Simulation.Aivika.Trans.Exception
import Simulation.Aivika.Trans.Comp
import Simulation.Aivika.Trans.DES
import Simulation.Aivika.Trans.Parameter
import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Dynamics
import Simulation.Aivika.Trans.Event
import Simulation.Aivika.Trans.Process
withMVarComp :: (MonadComp m, MonadIO m, MC.MonadMask m) => MVar a -> (a -> m b) -> m b
withMVarComp v f =
MC.mask $ \restore ->
do a <- liftIO $ takeMVar v
finallyComp
(f a)
(liftIO $ putMVar v a)
withMVarParameter :: (MonadComp m, MonadIO m, MC.MonadMask m) => MVar a -> (a -> Parameter m b) -> Parameter m b
withMVarParameter v f =
MC.mask $ \restore ->
do a <- liftIO $ takeMVar v
finallyParameter
(f a)
(liftIO $ putMVar v a)
withMVarSimulation :: (MonadComp m, MonadIO m, MC.MonadMask m) => MVar a -> (a -> Simulation m b) -> Simulation m b
withMVarSimulation v f =
MC.mask $ \restore ->
do a <- liftIO $ takeMVar v
finallySimulation
(f a)
(liftIO $ putMVar v a)
withMVarDynamics :: (MonadComp m, MonadIO m, MC.MonadMask m) => MVar a -> (a -> Dynamics m b) -> Dynamics m b
withMVarDynamics v f =
MC.mask $ \restore ->
do a <- liftIO $ takeMVar v
finallyDynamics
(f a)
(liftIO $ putMVar v a)
withMVarEvent :: (MonadComp m, MonadIO m, MC.MonadMask m) => MVar a -> (a -> Event m b) -> Event m b
withMVarEvent v f =
MC.mask $ \restore ->
do a <- liftIO $ takeMVar v
finallyEvent
(f a)
(liftIO $ putMVar v a)