module Simulation.Aivika.Trans.Experiment.Concurrent.MVar
(maybeReadMVarComp,
maybeReadMVarParameter,
maybeReadMVarSimulation,
maybeReadMVarDynamics,
maybeReadMVarEvent,
maybeReadMVarProcess,
maybePutMVarComp,
maybePutMVarParameter,
maybePutMVarSimulation,
maybePutMVarDynamics,
maybePutMVarEvent,
maybePutMVarProcess) where
import Control.Exception
import Control.Concurrent.MVar
import Control.Monad
import Control.Monad.Trans
import Data.Maybe
import Simulation.Aivika.Trans
maybeReadMVarComp :: (MonadComp m, MonadIO m) => b -> (a -> m b) -> MVar (Maybe a) -> m b
maybeReadMVarComp b0 f x =
do a <- liftIO $ readMVar x
case a of
Just a -> f a
Nothing -> return b0
maybeReadMVarParameter :: (MonadComp m, MonadIO m) => b -> (a -> Parameter m b) -> MVar (Maybe a) -> Parameter m b
maybeReadMVarParameter b0 f x =
do a <- liftIO $ readMVar x
case a of
Just a -> f a
Nothing -> return b0
maybeReadMVarSimulation :: (MonadComp m, MonadIO m) => b -> (a -> Simulation m b) -> MVar (Maybe a) -> Simulation m b
maybeReadMVarSimulation b0 f x =
do a <- liftIO $ readMVar x
case a of
Just a -> f a
Nothing -> return b0
maybeReadMVarDynamics :: (MonadComp m, MonadIO m) => b -> (a -> Dynamics m b) -> MVar (Maybe a) -> Dynamics m b
maybeReadMVarDynamics b0 f x =
do a <- liftIO $ readMVar x
case a of
Just a -> f a
Nothing -> return b0
maybeReadMVarEvent :: (MonadComp m, MonadIO m) => b -> (a -> Event m b) -> MVar (Maybe a) -> Event m b
maybeReadMVarEvent b0 f x =
do a <- liftIO $ readMVar x
case a of
Just a -> f a
Nothing -> return b0
maybeReadMVarProcess :: (MonadDES m, MonadIO m) => b -> (a -> Process m b) -> MVar (Maybe a) -> Process m b
maybeReadMVarProcess b0 f x =
do a <- liftIO $ readMVar x
case a of
Just a -> f a
Nothing -> return b0
maybePutMVarComp :: (MonadComp m, MonadIO m) => MVar (Maybe a) -> m a -> (a -> m b) -> m b
maybePutMVarComp x m0 f =
do a <- liftIO $ takeMVar x
case a of
Just a ->
do liftIO $ putMVar x (Just a)
f a
Nothing ->
do let handle :: (MonadComp m, MonadIO m) => SomeException -> m a
handle e = do liftIO $ putMVar x Nothing
throwComp e
a0 <- catchComp m0 handle
liftIO $ putMVar x (Just a0)
f a0
maybePutMVarParameter :: (MonadComp m, MonadIO m) => MVar (Maybe a) -> Parameter m a -> (a -> Parameter m b) -> Parameter m b
maybePutMVarParameter x m0 f =
do a <- liftIO $ takeMVar x
case a of
Just a ->
do liftIO $ putMVar x (Just a)
f a
Nothing ->
do let handle :: (MonadComp m, MonadIO m) => SomeException -> Parameter m a
handle e = do liftIO $ putMVar x Nothing
throwParameter e
a0 <- catchParameter m0 handle
liftIO $ putMVar x (Just a0)
f a0
maybePutMVarSimulation :: (MonadComp m, MonadIO m) => MVar (Maybe a) -> Simulation m a -> (a -> Simulation m b) -> Simulation m b
maybePutMVarSimulation x m0 f =
do a <- liftIO $ takeMVar x
case a of
Just a ->
do liftIO $ putMVar x (Just a)
f a
Nothing ->
do let handle :: (MonadComp m, MonadIO m) => SomeException -> Simulation m a
handle e = do liftIO $ putMVar x Nothing
throwSimulation e
a0 <- catchSimulation m0 handle
liftIO $ putMVar x (Just a0)
f a0
maybePutMVarDynamics :: (MonadComp m, MonadIO m) => MVar (Maybe a) -> Dynamics m a -> (a -> Dynamics m b) -> Dynamics m b
maybePutMVarDynamics x m0 f =
do a <- liftIO $ takeMVar x
case a of
Just a ->
do liftIO $ putMVar x (Just a)
f a
Nothing ->
do let handle :: (MonadComp m, MonadIO m) => SomeException -> Dynamics m a
handle e = do liftIO $ putMVar x Nothing
throwDynamics e
a0 <- catchDynamics m0 handle
liftIO $ putMVar x (Just a0)
f a0
maybePutMVarEvent :: (MonadComp m, MonadIO m) => MVar (Maybe a) -> Event m a -> (a -> Event m b) -> Event m b
maybePutMVarEvent x m0 f =
do a <- liftIO $ takeMVar x
case a of
Just a ->
do liftIO $ putMVar x (Just a)
f a
Nothing ->
do let handle :: (MonadComp m, MonadIO m) => SomeException -> Event m a
handle e = do liftIO $ putMVar x Nothing
throwEvent e
a0 <- catchEvent m0 handle
liftIO $ putMVar x (Just a0)
f a0
maybePutMVarProcess :: (MonadDES m, MonadIO m) => MVar (Maybe a) -> Process m a -> (a -> Process m b) -> Process m b
maybePutMVarProcess x m0 f =
do a <- liftIO $ takeMVar x
case a of
Just a ->
do liftIO $ putMVar x (Just a)
f a
Nothing ->
do let handle :: (MonadDES m, MonadIO m) => SomeException -> Process m a
handle e = do liftIO $ putMVar x Nothing
throwProcess e
a0 <- catchProcess m0 handle
liftIO $ putMVar x (Just a0)
f a0