{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Safe #-}
module Universum.Lifted.Concurrent
(
MVar
, newEmptyMVar
, newMVar
, putMVar
, readMVar
, swapMVar
, takeMVar
, tryPutMVar
, tryReadMVar
, tryTakeMVar
, updateMVar'
, STM
, TVar
, atomically
, newTVarIO
, readTVarIO
, STM.modifyTVar'
, updateTVar'
, STM.newTVar
, STM.readTVar
, STM.writeTVar
) where
import Control.Concurrent.MVar (MVar)
import Control.Concurrent.STM.TVar (TVar)
import Control.Monad (return)
import Control.Monad.STM (STM)
import Control.Monad.State (StateT (..))
import Control.Monad.Trans (MonadIO, liftIO)
import Data.Bool (Bool)
import Data.Function (($), (.))
import Data.Maybe (Maybe)
import System.IO (IO)
import qualified Control.Concurrent.MVar as CCM (modifyMVar, newEmptyMVar, newMVar, putMVar,
readMVar, swapMVar, takeMVar, tryPutMVar,
tryReadMVar, tryTakeMVar)
import qualified Control.Concurrent.STM.TVar as STM (modifyTVar', newTVar, newTVarIO, readTVar,
readTVarIO, writeTVar)
import qualified Control.Monad.STM as STM (atomically)
newEmptyMVar :: MonadIO m => m (MVar a)
newEmptyMVar :: forall (m :: * -> *) a. MonadIO m => m (MVar a)
newEmptyMVar = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a. IO (MVar a)
CCM.newEmptyMVar
{-# INLINE newEmptyMVar #-}
newMVar :: MonadIO m => a -> m (MVar a)
newMVar :: forall (m :: * -> *) a. MonadIO m => a -> m (MVar a)
newMVar = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> IO (MVar a)
CCM.newMVar
{-# INLINE newMVar #-}
putMVar :: MonadIO m => MVar a -> a -> m ()
putMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> a -> m ()
putMVar MVar a
m a
a = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. MVar a -> a -> IO ()
CCM.putMVar MVar a
m a
a
{-# INLINE putMVar #-}
readMVar :: MonadIO m => MVar a -> m a
readMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> m a
readMVar = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MVar a -> IO a
CCM.readMVar
{-# INLINE readMVar #-}
swapMVar :: MonadIO m => MVar a -> a -> m a
swapMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> a -> m a
swapMVar MVar a
m a
v = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. MVar a -> a -> IO a
CCM.swapMVar MVar a
m a
v
{-# INLINE swapMVar #-}
takeMVar :: MonadIO m => MVar a -> m a
takeMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> m a
takeMVar = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MVar a -> IO a
CCM.takeMVar
{-# INLINE takeMVar #-}
tryPutMVar :: MonadIO m => MVar a -> a -> m Bool
tryPutMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> a -> m Bool
tryPutMVar MVar a
m a
v = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. MVar a -> a -> IO Bool
CCM.tryPutMVar MVar a
m a
v
{-# INLINE tryPutMVar #-}
tryReadMVar :: MonadIO m => MVar a -> m (Maybe a)
tryReadMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> m (Maybe a)
tryReadMVar = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MVar a -> IO (Maybe a)
CCM.tryReadMVar
{-# INLINE tryReadMVar #-}
tryTakeMVar :: MonadIO m => MVar a -> m (Maybe a)
tryTakeMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> m (Maybe a)
tryTakeMVar = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MVar a -> IO (Maybe a)
CCM.tryTakeMVar
{-# INLINE tryTakeMVar #-}
atomically :: MonadIO m => STM a -> m a
atomically :: forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. STM a -> IO a
STM.atomically
{-# INLINE atomically #-}
newTVarIO :: MonadIO m => a -> m (TVar a)
newTVarIO :: forall (m :: * -> *) a. MonadIO m => a -> m (TVar a)
newTVarIO = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> IO (TVar a)
STM.newTVarIO
{-# INLINE newTVarIO #-}
readTVarIO :: MonadIO m => TVar a -> m a
readTVarIO :: forall (m :: * -> *) a. MonadIO m => TVar a -> m a
readTVarIO = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. TVar a -> IO a
STM.readTVarIO
{-# INLINE readTVarIO #-}
updateMVar' :: MonadIO m => MVar s -> StateT s IO a -> m a
updateMVar' :: forall (m :: * -> *) s a.
MonadIO m =>
MVar s -> StateT s IO a -> m a
updateMVar' MVar s
var (StateT s -> IO (a, s)
f) =
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. MVar a -> (a -> IO (a, b)) -> IO b
CCM.modifyMVar MVar s
var forall a b. (a -> b) -> a -> b
$ \s
s -> do
(a
a, !s
s') <- s -> IO (a, s)
f s
s
forall (m :: * -> *) a. Monad m => a -> m a
return (s
s', a
a)
{-# INLINE updateMVar' #-}
updateTVar' :: TVar s -> StateT s STM a -> STM a
updateTVar' :: forall s a. TVar s -> StateT s STM a -> STM a
updateTVar' TVar s
var (StateT s -> STM (a, s)
f) = do
s
s <- forall a. TVar a -> STM a
STM.readTVar TVar s
var
(a
a, !s
s') <- s -> STM (a, s)
f s
s
forall a. TVar a -> a -> STM ()
STM.writeTVar TVar s
var s
s'
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
{-# INLINE updateTVar' #-}