{-# LANGUAGE CPP, DeriveDataTypeable, MagicHash, UnboxedTuples #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
module Control.Concurrent.STM.TMVar (
#ifdef __GLASGOW_HASKELL__
TMVar,
newTMVar,
newEmptyTMVar,
newTMVarIO,
newEmptyTMVarIO,
takeTMVar,
putTMVar,
readTMVar,
tryReadTMVar,
swapTMVar,
tryTakeTMVar,
tryPutTMVar,
isEmptyTMVar,
mkWeakTMVar
#endif
) where
#ifdef __GLASGOW_HASKELL__
import GHC.Base
import GHC.Conc
import GHC.Weak
import Data.Typeable (Typeable)
newtype TMVar a = TMVar (TVar (Maybe a)) deriving (TMVar a -> TMVar a -> Bool
forall a. TMVar a -> TMVar a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TMVar a -> TMVar a -> Bool
$c/= :: forall a. TMVar a -> TMVar a -> Bool
== :: TMVar a -> TMVar a -> Bool
$c== :: forall a. TMVar a -> TMVar a -> Bool
Eq, Typeable)
newTMVar :: a -> STM (TMVar a)
newTMVar :: forall a. a -> STM (TMVar a)
newTMVar a
a = do
TVar (Maybe a)
t <- forall a. a -> STM (TVar a)
newTVar (forall a. a -> Maybe a
Just a
a)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. TVar (Maybe a) -> TMVar a
TMVar TVar (Maybe a)
t)
newTMVarIO :: a -> IO (TMVar a)
newTMVarIO :: forall a. a -> IO (TMVar a)
newTMVarIO a
a = do
TVar (Maybe a)
t <- forall a. a -> IO (TVar a)
newTVarIO (forall a. a -> Maybe a
Just a
a)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. TVar (Maybe a) -> TMVar a
TMVar TVar (Maybe a)
t)
newEmptyTMVar :: STM (TMVar a)
newEmptyTMVar :: forall a. STM (TMVar a)
newEmptyTMVar = do
TVar (Maybe a)
t <- forall a. a -> STM (TVar a)
newTVar forall a. Maybe a
Nothing
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. TVar (Maybe a) -> TMVar a
TMVar TVar (Maybe a)
t)
newEmptyTMVarIO :: IO (TMVar a)
newEmptyTMVarIO :: forall a. IO (TMVar a)
newEmptyTMVarIO = do
TVar (Maybe a)
t <- forall a. a -> IO (TVar a)
newTVarIO forall a. Maybe a
Nothing
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. TVar (Maybe a) -> TMVar a
TMVar TVar (Maybe a)
t)
takeTMVar :: TMVar a -> STM a
takeTMVar :: forall a. TMVar a -> STM a
takeTMVar (TMVar TVar (Maybe a)
t) = do
Maybe a
m <- forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> forall a. STM a
retry
Just a
a -> do forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe a)
t forall a. Maybe a
Nothing; forall (m :: * -> *) a. Monad m => a -> m a
return a
a
tryTakeTMVar :: TMVar a -> STM (Maybe a)
tryTakeTMVar :: forall a. TMVar a -> STM (Maybe a)
tryTakeTMVar (TMVar TVar (Maybe a)
t) = do
Maybe a
m <- forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
Just a
a -> do forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe a)
t forall a. Maybe a
Nothing; forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
a)
putTMVar :: TMVar a -> a -> STM ()
putTMVar :: forall a. TMVar a -> a -> STM ()
putTMVar (TMVar TVar (Maybe a)
t) a
a = do
Maybe a
m <- forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> do forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe a)
t (forall a. a -> Maybe a
Just a
a); forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just a
_ -> forall a. STM a
retry
tryPutTMVar :: TMVar a -> a -> STM Bool
tryPutTMVar :: forall a. TMVar a -> a -> STM Bool
tryPutTMVar (TMVar TVar (Maybe a)
t) a
a = do
Maybe a
m <- forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> do forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe a)
t (forall a. a -> Maybe a
Just a
a); forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
Just a
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
readTMVar :: TMVar a -> STM a
readTMVar :: forall a. TMVar a -> STM a
readTMVar (TMVar TVar (Maybe a)
t) = do
Maybe a
m <- forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> forall a. STM a
retry
Just a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return a
a
tryReadTMVar :: TMVar a -> STM (Maybe a)
tryReadTMVar :: forall a. TMVar a -> STM (Maybe a)
tryReadTMVar (TMVar TVar (Maybe a)
t) = forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
swapTMVar :: TMVar a -> a -> STM a
swapTMVar :: forall a. TMVar a -> a -> STM a
swapTMVar (TMVar TVar (Maybe a)
t) a
new = do
Maybe a
m <- forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> forall a. STM a
retry
Just a
old -> do forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe a)
t (forall a. a -> Maybe a
Just a
new); forall (m :: * -> *) a. Monad m => a -> m a
return a
old
isEmptyTMVar :: TMVar a -> STM Bool
isEmptyTMVar :: forall a. TMVar a -> STM Bool
isEmptyTMVar (TMVar TVar (Maybe a)
t) = do
Maybe a
m <- forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
Just a
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
mkWeakTMVar :: TMVar a -> IO () -> IO (Weak (TMVar a))
mkWeakTMVar :: forall a. TMVar a -> IO () -> IO (Weak (TMVar a))
mkWeakTMVar tmv :: TMVar a
tmv@(TMVar (TVar TVar# RealWorld (Maybe a)
t#)) (IO State# RealWorld -> (# State# RealWorld, () #)
finalizer) = forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case mkWeak# :: forall a b c.
a
-> b
-> (State# RealWorld -> (# State# RealWorld, c #))
-> State# RealWorld
-> (# State# RealWorld, Weak# b #)
mkWeak# TVar# RealWorld (Maybe a)
t# TMVar a
tmv State# RealWorld -> (# State# RealWorld, () #)
finalizer State# RealWorld
s of (# State# RealWorld
s1, Weak# (TMVar a)
w #) -> (# State# RealWorld
s1, forall v. Weak# v -> Weak v
Weak Weak# (TMVar a)
w #)
#endif