{-# LANGUAGE BangPatterns, 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
x <- TVar a -> STM a
forall a. TVar a -> STM a
readTVar TVar a
var
writeTVar var (f 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
x <- TVar a -> STM a
forall a. TVar a -> STM a
readTVar TVar a
var
writeTVar var $! f 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 <- TVar s -> STM s
forall a. TVar a -> STM a
readTVar TVar s
var
let !(a, s') = f s
writeTVar var s'
return a
{-# INLINE stateTVar #-}
swapTVar :: TVar a -> a -> STM a
swapTVar :: forall a. TVar a -> a -> STM a
swapTVar TVar a
var a
new = do
old <- TVar a -> STM a
forall a. TVar a -> STM a
readTVar TVar a
var
writeTVar var new
return 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) #)
forall a b c.
a
-> b
-> (State# RealWorld -> (# State# RealWorld, c #))
-> State# RealWorld
-> (# State# RealWorld, Weak# b #)
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 #)