{-# LANGUAGE CPP, MagicHash, UnboxedTuples #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
module Control.Concurrent.STM.TVar (
TVar,
newTVar,
newTVarIO,
readTVar,
readTVarIO,
writeTVar,
modifyTVar,
modifyTVar',
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 var f = do
x <- readTVar var
writeTVar var (f x)
{-# INLINE modifyTVar #-}
modifyTVar' :: TVar a -> (a -> a) -> STM ()
modifyTVar' var f = do
x <- readTVar var
writeTVar var $! f x
{-# INLINE modifyTVar' #-}
swapTVar :: TVar a -> a -> STM a
swapTVar var new = do
old <- readTVar var
writeTVar var new
return old
{-# INLINE swapTVar #-}
mkWeakTVar :: TVar a -> IO () -> IO (Weak (TVar a))
mkWeakTVar t@(TVar t#) (IO finalizer) = IO $ \s ->
case mkWeak# t# t finalizer s of (# s1, w #) -> (# s1, Weak w #)