{-# 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
(TMVar a -> TMVar a -> Bool)
-> (TMVar a -> TMVar a -> Bool) -> Eq (TMVar a)
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 <- Maybe a -> STM (TVar (Maybe a))
forall a. a -> STM (TVar a)
newTVar (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
TMVar a -> STM (TMVar a)
forall (m :: * -> *) a. Monad m => a -> m a
return (TVar (Maybe a) -> TMVar a
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 <- Maybe a -> IO (TVar (Maybe a))
forall a. a -> IO (TVar a)
newTVarIO (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
TMVar a -> IO (TMVar a)
forall (m :: * -> *) a. Monad m => a -> m a
return (TVar (Maybe a) -> TMVar a
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 <- Maybe a -> STM (TVar (Maybe a))
forall a. a -> STM (TVar a)
newTVar Maybe a
forall a. Maybe a
Nothing
TMVar a -> STM (TMVar a)
forall (m :: * -> *) a. Monad m => a -> m a
return (TVar (Maybe a) -> TMVar a
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 <- Maybe a -> IO (TVar (Maybe a))
forall a. a -> IO (TVar a)
newTVarIO Maybe a
forall a. Maybe a
Nothing
TMVar a -> IO (TMVar a)
forall (m :: * -> *) a. Monad m => a -> m a
return (TVar (Maybe a) -> TMVar a
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 <- TVar (Maybe a) -> STM (Maybe a)
forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> STM a
forall a. STM a
retry
Just a
a -> do TVar (Maybe a) -> Maybe a -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe a)
t Maybe a
forall a. Maybe a
Nothing; a -> STM a
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 <- TVar (Maybe a) -> STM (Maybe a)
forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> Maybe a -> STM (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
Just a
a -> do TVar (Maybe a) -> Maybe a -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe a)
t Maybe a
forall a. Maybe a
Nothing; Maybe a -> STM (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
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 <- TVar (Maybe a) -> STM (Maybe a)
forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> do TVar (Maybe a) -> Maybe a -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe a)
t (a -> Maybe a
forall a. a -> Maybe a
Just a
a); () -> STM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just a
_ -> STM ()
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 <- TVar (Maybe a) -> STM (Maybe a)
forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> do TVar (Maybe a) -> Maybe a -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe a)
t (a -> Maybe a
forall a. a -> Maybe a
Just a
a); Bool -> STM Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
Just a
_ -> Bool -> STM Bool
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 <- TVar (Maybe a) -> STM (Maybe a)
forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> STM a
forall a. STM a
retry
Just a
a -> a -> STM 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) = TVar (Maybe a) -> STM (Maybe a)
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 <- TVar (Maybe a) -> STM (Maybe a)
forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> STM a
forall a. STM a
retry
Just a
old -> do TVar (Maybe a) -> Maybe a -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (Maybe a)
t (a -> Maybe a
forall a. a -> Maybe a
Just a
new); a -> STM a
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 <- TVar (Maybe a) -> STM (Maybe a)
forall a. TVar a -> STM a
readTVar TVar (Maybe a)
t
case Maybe a
m of
Maybe a
Nothing -> Bool -> STM Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
Just a
_ -> Bool -> STM Bool
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) = (State# RealWorld -> (# State# RealWorld, Weak (TMVar a) #))
-> IO (Weak (TMVar a))
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Weak (TMVar a) #))
-> IO (Weak (TMVar a)))
-> (State# RealWorld -> (# State# RealWorld, Weak (TMVar a) #))
-> IO (Weak (TMVar a))
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case TVar# RealWorld (Maybe a)
-> TMVar a
-> (State# RealWorld -> (# State# RealWorld, () #))
-> State# RealWorld
-> (# State# RealWorld, Weak# (TMVar a) #)
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, Weak# (TMVar a) -> Weak (TMVar a)
forall v. Weak# v -> Weak v
Weak Weak# (TMVar a)
w #)
#endif