module Control.Concurrent.Classy.MVar
(
MVar
, newEmptyMVar
, newEmptyMVarN
, newMVar
, newMVarN
, takeMVar
, putMVar
, readMVar
, swapMVar
, tryTakeMVar
, tryPutMVar
, isEmptyMVar
, withMVar
, withMVarMasked
, modifyMVar_
, modifyMVar
, modifyMVarMasked_
, modifyMVarMasked
) where
import Control.Monad.Catch (onException)
import Control.Monad.Conc.Class
import Data.Maybe (isJust)
swapMVar :: MonadConc m => MVar m a -> a -> m a
swapMVar :: forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m a
swapMVar MVar m a
cvar a
a = forall (m :: * -> *) a. MonadMask m => m a -> m a
mask_ forall a b. (a -> b) -> a -> b
$ do
a
old <- forall (m :: * -> *) a. MonadConc m => MVar m a -> m a
takeMVar MVar m a
cvar
forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m ()
putMVar MVar m a
cvar a
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
old
isEmptyMVar :: MonadConc m => MVar m a -> m Bool
isEmptyMVar :: forall (m :: * -> *) a. MonadConc m => MVar m a -> m Bool
isEmptyMVar = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Maybe a -> Bool
isJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadConc m => MVar m a -> m (Maybe a)
tryReadMVar
{-# INLINE withMVar #-}
withMVar :: MonadConc m => MVar m a -> (a -> m b) -> m b
withMVar :: forall (m :: * -> *) a b.
MonadConc m =>
MVar m a -> (a -> m b) -> m b
withMVar MVar m a
cvar a -> m b
f = forall (m :: * -> *) b.
MonadConc m =>
((forall a. m a -> m a) -> m b) -> m b
mask forall a b. (a -> b) -> a -> b
$ \forall a. m a -> m a
restore -> do
a
val <- forall (m :: * -> *) a. MonadConc m => MVar m a -> m a
takeMVar MVar m a
cvar
b
out <- forall a. m a -> m a
restore (a -> m b
f a
val) forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`onException` forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m ()
putMVar MVar m a
cvar a
val
forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m ()
putMVar MVar m a
cvar a
val
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
out
{-# INLINE withMVarMasked #-}
withMVarMasked :: MonadConc m => MVar m a -> (a -> m b) -> m b
withMVarMasked :: forall (m :: * -> *) a b.
MonadConc m =>
MVar m a -> (a -> m b) -> m b
withMVarMasked MVar m a
cvar a -> m b
f = forall (m :: * -> *) a. MonadMask m => m a -> m a
mask_ forall a b. (a -> b) -> a -> b
$ do
a
val <- forall (m :: * -> *) a. MonadConc m => MVar m a -> m a
takeMVar MVar m a
cvar
b
out <- a -> m b
f a
val forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`onException` forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m ()
putMVar MVar m a
cvar a
val
forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m ()
putMVar MVar m a
cvar a
val
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
out
{-# INLINE modifyMVar_ #-}
modifyMVar_ :: MonadConc m => MVar m a -> (a -> m a) -> m ()
modifyMVar_ :: forall (m :: * -> *) a.
MonadConc m =>
MVar m a -> (a -> m a) -> m ()
modifyMVar_ MVar m a
cvar a -> m a
f = forall (m :: * -> *) a b.
MonadConc m =>
MVar m a -> (a -> m (a, b)) -> m b
modifyMVar MVar m a
cvar forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
a -> (a
a,())) forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
f
{-# INLINE modifyMVar #-}
modifyMVar :: MonadConc m => MVar m a -> (a -> m (a, b)) -> m b
modifyMVar :: forall (m :: * -> *) a b.
MonadConc m =>
MVar m a -> (a -> m (a, b)) -> m b
modifyMVar MVar m a
cvar a -> m (a, b)
f = forall (m :: * -> *) b.
MonadConc m =>
((forall a. m a -> m a) -> m b) -> m b
mask forall a b. (a -> b) -> a -> b
$ \forall a. m a -> m a
restore -> do
a
val <- forall (m :: * -> *) a. MonadConc m => MVar m a -> m a
takeMVar MVar m a
cvar
(a
val', b
out) <- forall a. m a -> m a
restore (a -> m (a, b)
f a
val) forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`onException` forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m ()
putMVar MVar m a
cvar a
val
forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m ()
putMVar MVar m a
cvar a
val'
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
out
{-# INLINE modifyMVarMasked_ #-}
modifyMVarMasked_ :: MonadConc m => MVar m a -> (a -> m a) -> m ()
modifyMVarMasked_ :: forall (m :: * -> *) a.
MonadConc m =>
MVar m a -> (a -> m a) -> m ()
modifyMVarMasked_ MVar m a
cvar a -> m a
f = forall (m :: * -> *) a b.
MonadConc m =>
MVar m a -> (a -> m (a, b)) -> m b
modifyMVarMasked MVar m a
cvar forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
a -> (a
a,())) forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
f
{-# INLINE modifyMVarMasked #-}
modifyMVarMasked :: MonadConc m => MVar m a -> (a -> m (a, b)) -> m b
modifyMVarMasked :: forall (m :: * -> *) a b.
MonadConc m =>
MVar m a -> (a -> m (a, b)) -> m b
modifyMVarMasked MVar m a
cvar a -> m (a, b)
f = forall (m :: * -> *) a. MonadMask m => m a -> m a
mask_ forall a b. (a -> b) -> a -> b
$ do
a
val <- forall (m :: * -> *) a. MonadConc m => MVar m a -> m a
takeMVar MVar m a
cvar
(a
val', b
out) <- a -> m (a, b)
f a
val forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`onException` forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m ()
putMVar MVar m a
cvar a
val
forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m ()
putMVar MVar m a
cvar a
val'
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
out