{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE NoImplicitPrelude, UnboxedTuples, MagicHash #-}
module Control.Concurrent.MVar
(
MVar
, newEmptyMVar
, newMVar
, takeMVar
, putMVar
, readMVar
, swapMVar
, tryTakeMVar
, tryPutMVar
, isEmptyMVar
, withMVar
, withMVarMasked
, modifyMVar_
, modifyMVar
, modifyMVarMasked_
, modifyMVarMasked
, tryReadMVar
, mkWeakMVar
, addMVarFinalizer
) where
import GHC.MVar ( MVar(..), newEmptyMVar, newMVar, takeMVar, putMVar,
tryTakeMVar, tryPutMVar, isEmptyMVar, readMVar,
tryReadMVar
)
import qualified GHC.MVar
import GHC.Weak
import GHC.Base
import Control.Exception.Base
swapMVar :: MVar a -> a -> IO a
swapMVar :: MVar a -> a -> IO a
swapMVar mvar :: MVar a
mvar new :: a
new =
IO a -> IO a
forall a. IO a -> IO a
mask_ (IO a -> IO a) -> IO a -> IO a
forall a b. (a -> b) -> a -> b
$ do
a
old <- MVar a -> IO a
forall a. MVar a -> IO a
takeMVar MVar a
mvar
MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
mvar a
new
a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
old
{-# INLINE withMVar #-}
withMVar :: MVar a -> (a -> IO b) -> IO b
withMVar :: MVar a -> (a -> IO b) -> IO b
withMVar m :: MVar a
m io :: a -> IO b
io =
((forall a. IO a -> IO a) -> IO b) -> IO b
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
mask (((forall a. IO a -> IO a) -> IO b) -> IO b)
-> ((forall a. IO a -> IO a) -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \restore :: forall a. IO a -> IO a
restore -> do
a
a <- MVar a -> IO a
forall a. MVar a -> IO a
takeMVar MVar a
m
b
b <- IO b -> IO b
forall a. IO a -> IO a
restore (a -> IO b
io a
a) IO b -> IO () -> IO b
forall a b. IO a -> IO b -> IO a
`onException` MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
m a
a
MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
m a
a
b -> IO b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
{-# INLINE withMVarMasked #-}
withMVarMasked :: MVar a -> (a -> IO b) -> IO b
withMVarMasked :: MVar a -> (a -> IO b) -> IO b
withMVarMasked m :: MVar a
m io :: a -> IO b
io =
IO b -> IO b
forall a. IO a -> IO a
mask_ (IO b -> IO b) -> IO b -> IO b
forall a b. (a -> b) -> a -> b
$ do
a
a <- MVar a -> IO a
forall a. MVar a -> IO a
takeMVar MVar a
m
b
b <- a -> IO b
io a
a IO b -> IO () -> IO b
forall a b. IO a -> IO b -> IO a
`onException` MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
m a
a
MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
m a
a
b -> IO b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
{-# INLINE modifyMVar_ #-}
modifyMVar_ :: MVar a -> (a -> IO a) -> IO ()
modifyMVar_ :: MVar a -> (a -> IO a) -> IO ()
modifyMVar_ m :: MVar a
m io :: a -> IO a
io =
((forall a. IO a -> IO a) -> IO ()) -> IO ()
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
mask (((forall a. IO a -> IO a) -> IO ()) -> IO ())
-> ((forall a. IO a -> IO a) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \restore :: forall a. IO a -> IO a
restore -> do
a
a <- MVar a -> IO a
forall a. MVar a -> IO a
takeMVar MVar a
m
a
a' <- IO a -> IO a
forall a. IO a -> IO a
restore (a -> IO a
io a
a) IO a -> IO () -> IO a
forall a b. IO a -> IO b -> IO a
`onException` MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
m a
a
MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
m a
a'
{-# INLINE modifyMVar #-}
modifyMVar :: MVar a -> (a -> IO (a,b)) -> IO b
modifyMVar :: MVar a -> (a -> IO (a, b)) -> IO b
modifyMVar m :: MVar a
m io :: a -> IO (a, b)
io =
((forall a. IO a -> IO a) -> IO b) -> IO b
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
mask (((forall a. IO a -> IO a) -> IO b) -> IO b)
-> ((forall a. IO a -> IO a) -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \restore :: forall a. IO a -> IO a
restore -> do
a
a <- MVar a -> IO a
forall a. MVar a -> IO a
takeMVar MVar a
m
(a' :: a
a',b :: b
b) <- IO (a, b) -> IO (a, b)
forall a. IO a -> IO a
restore (a -> IO (a, b)
io a
a IO (a, b) -> ((a, b) -> IO (a, b)) -> IO (a, b)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (a, b) -> IO (a, b)
forall a. a -> IO a
evaluate) IO (a, b) -> IO () -> IO (a, b)
forall a b. IO a -> IO b -> IO a
`onException` MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
m a
a
MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
m a
a'
b -> IO b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
{-# INLINE modifyMVarMasked_ #-}
modifyMVarMasked_ :: MVar a -> (a -> IO a) -> IO ()
modifyMVarMasked_ :: MVar a -> (a -> IO a) -> IO ()
modifyMVarMasked_ m :: MVar a
m io :: a -> IO a
io =
IO () -> IO ()
forall a. IO a -> IO a
mask_ (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
a
a <- MVar a -> IO a
forall a. MVar a -> IO a
takeMVar MVar a
m
a
a' <- a -> IO a
io a
a IO a -> IO () -> IO a
forall a b. IO a -> IO b -> IO a
`onException` MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
m a
a
MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
m a
a'
{-# INLINE modifyMVarMasked #-}
modifyMVarMasked :: MVar a -> (a -> IO (a,b)) -> IO b
modifyMVarMasked :: MVar a -> (a -> IO (a, b)) -> IO b
modifyMVarMasked m :: MVar a
m io :: a -> IO (a, b)
io =
IO b -> IO b
forall a. IO a -> IO a
mask_ (IO b -> IO b) -> IO b -> IO b
forall a b. (a -> b) -> a -> b
$ do
a
a <- MVar a -> IO a
forall a. MVar a -> IO a
takeMVar MVar a
m
(a' :: a
a',b :: b
b) <- (a -> IO (a, b)
io a
a IO (a, b) -> ((a, b) -> IO (a, b)) -> IO (a, b)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (a, b) -> IO (a, b)
forall a. a -> IO a
evaluate) IO (a, b) -> IO () -> IO (a, b)
forall a b. IO a -> IO b -> IO a
`onException` MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
m a
a
MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
m a
a'
b -> IO b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
{-# DEPRECATED addMVarFinalizer "use 'mkWeakMVar' instead" #-}
addMVarFinalizer :: MVar a -> IO () -> IO ()
addMVarFinalizer :: MVar a -> IO () -> IO ()
addMVarFinalizer = MVar a -> IO () -> IO ()
forall a. MVar a -> IO () -> IO ()
GHC.MVar.addMVarFinalizer
mkWeakMVar :: MVar a -> IO () -> IO (Weak (MVar a))
mkWeakMVar :: MVar a -> IO () -> IO (Weak (MVar a))
mkWeakMVar m :: MVar a
m@(MVar m# :: MVar# RealWorld a
m#) (IO f :: State# RealWorld -> (# State# RealWorld, () #)
f) = (State# RealWorld -> (# State# RealWorld, Weak (MVar a) #))
-> IO (Weak (MVar a))
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Weak (MVar a) #))
-> IO (Weak (MVar a)))
-> (State# RealWorld -> (# State# RealWorld, Weak (MVar a) #))
-> IO (Weak (MVar a))
forall a b. (a -> b) -> a -> b
$ \s :: State# RealWorld
s ->
case MVar# RealWorld a
-> MVar a
-> (State# RealWorld -> (# State# RealWorld, () #))
-> State# RealWorld
-> (# State# RealWorld, Weak# (MVar a) #)
forall a b c.
a
-> b
-> (State# RealWorld -> (# State# RealWorld, c #))
-> State# RealWorld
-> (# State# RealWorld, Weak# b #)
mkWeak# MVar# RealWorld a
m# MVar a
m State# RealWorld -> (# State# RealWorld, () #)
f State# RealWorld
s of (# s1 :: State# RealWorld
s1, w :: Weak# (MVar a)
w #) -> (# State# RealWorld
s1, Weak# (MVar a) -> Weak (MVar a)
forall v. Weak# v -> Weak v
Weak Weak# (MVar a)
w #)