{-# LANGUAGE CPP, MagicHash, UnboxedTuples #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
module Control.Concurrent.STM.TVar (
TVar,
newTVar,
newTVarIO,
readTVar,
readTVarIO,
writeTVar,
modifyTVar,
modifyTVar',
stateTVar,
swapTVar,
#ifdef __GLASGOW_HASKELL__
registerDelay,
#endif
mkWeakTVar
) where
#ifdef __GLASGOW_HASKELL__
import GHC.Base
import GHC.Conc
import GHC.Weak
#else
import Control.Sequential.STM
#endif
modifyTVar :: TVar a -> (a -> a) -> STM ()
modifyTVar :: forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar a
var a -> a
f = do
a
x <- TVar a -> STM a
forall a. TVar a -> STM a
readTVar TVar a
var
TVar a -> a -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar a
var (a -> a
f a
x)
{-# INLINE modifyTVar #-}
modifyTVar' :: TVar a -> (a -> a) -> STM ()
modifyTVar' :: forall a. TVar a -> (a -> a) -> STM ()
modifyTVar' TVar a
var a -> a
f = do
a
x <- TVar a -> STM a
forall a. TVar a -> STM a
readTVar TVar a
var
TVar a -> a -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar a
var (a -> STM ()) -> a -> STM ()
forall a b. (a -> b) -> a -> b
$! a -> a
f a
x
{-# INLINE modifyTVar' #-}
stateTVar :: TVar s -> (s -> (a, s)) -> STM a
stateTVar :: forall s a. TVar s -> (s -> (a, s)) -> STM a
stateTVar TVar s
var s -> (a, s)
f = do
s
s <- TVar s -> STM s
forall a. TVar a -> STM a
readTVar TVar s
var
let (a
a, s
s') = s -> (a, s)
f s
s
TVar s -> s -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar s
var s
s'
a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
{-# INLINE stateTVar #-}
swapTVar :: TVar a -> a -> STM a
swapTVar :: forall a. TVar a -> a -> STM a
swapTVar TVar a
var a
new = do
a
old <- TVar a -> STM a
forall a. TVar a -> STM a
readTVar TVar a
var
TVar a -> a -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar a
var a
new
a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return a
old
{-# INLINE swapTVar #-}
mkWeakTVar :: TVar a -> IO () -> IO (Weak (TVar a))
mkWeakTVar :: forall a. TVar a -> IO () -> IO (Weak (TVar a))
mkWeakTVar t :: TVar a
t@(TVar TVar# RealWorld a
t#) (IO State# RealWorld -> (# State# RealWorld, () #)
finalizer) = (State# RealWorld -> (# State# RealWorld, Weak (TVar a) #))
-> IO (Weak (TVar a))
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Weak (TVar a) #))
-> IO (Weak (TVar a)))
-> (State# RealWorld -> (# State# RealWorld, Weak (TVar a) #))
-> IO (Weak (TVar a))
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case TVar# RealWorld a
-> TVar a
-> (State# RealWorld -> (# State# RealWorld, () #))
-> State# RealWorld
-> (# State# RealWorld, Weak# (TVar a) #)
mkWeak# TVar# RealWorld a
t# TVar a
t State# RealWorld -> (# State# RealWorld, () #)
finalizer State# RealWorld
s of (# State# RealWorld
s1, Weak# (TVar a)
w #) -> (# State# RealWorld
s1, Weak# (TVar a) -> Weak (TVar a)
forall v. Weak# v -> Weak v
Weak Weak# (TVar a)
w #)