{-# LANGUAGE Unsafe #-}
{-# LANGUAGE NoImplicitPrelude, MagicHash, UnboxedTuples #-}
{-# OPTIONS_GHC -funbox-strict-fields #-}
{-# OPTIONS_HADDOCK not-home #-}
module GHC.MVar (
MVar(..)
, newMVar
, newEmptyMVar
, takeMVar
, readMVar
, putMVar
, tryTakeMVar
, tryPutMVar
, tryReadMVar
, isEmptyMVar
, addMVarFinalizer
) where
import GHC.Base
data MVar a = MVar (MVar# RealWorld a)
instance Eq (MVar a) where
(MVar mvar1# :: MVar# RealWorld a
mvar1#) == :: MVar a -> MVar a -> Bool
== (MVar mvar2# :: MVar# RealWorld a
mvar2#) = Int# -> Bool
isTrue# (MVar# RealWorld a -> MVar# RealWorld a -> Int#
forall d a. MVar# d a -> MVar# d a -> Int#
sameMVar# MVar# RealWorld a
mvar1# MVar# RealWorld a
mvar2#)
newEmptyMVar :: IO (MVar a)
newEmptyMVar :: IO (MVar a)
newEmptyMVar = (State# RealWorld -> (# State# RealWorld, MVar a #)) -> IO (MVar a)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, MVar a #))
-> IO (MVar a))
-> (State# RealWorld -> (# State# RealWorld, MVar a #))
-> IO (MVar a)
forall a b. (a -> b) -> a -> b
$ \ s# :: State# RealWorld
s# ->
case State# RealWorld -> (# State# RealWorld, MVar# RealWorld a #)
forall d a. State# d -> (# State# d, MVar# d a #)
newMVar# State# RealWorld
s# of
(# s2# :: State# RealWorld
s2#, svar# :: MVar# RealWorld a
svar# #) -> (# State# RealWorld
s2#, MVar# RealWorld a -> MVar a
forall a. MVar# RealWorld a -> MVar a
MVar MVar# RealWorld a
svar# #)
newMVar :: a -> IO (MVar a)
newMVar :: a -> IO (MVar a)
newMVar value :: a
value =
IO (MVar a)
forall a. IO (MVar a)
newEmptyMVar IO (MVar a) -> (MVar a -> IO (MVar a)) -> IO (MVar a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ mvar :: MVar a
mvar ->
MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
mvar a
value IO () -> IO (MVar a) -> IO (MVar a)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
MVar a -> IO (MVar a)
forall (m :: * -> *) a. Monad m => a -> m a
return MVar a
mvar
takeMVar :: MVar a -> IO a
takeMVar :: MVar a -> IO a
takeMVar (MVar mvar# :: MVar# RealWorld a
mvar#) = (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, a #)) -> IO a)
-> (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a b. (a -> b) -> a -> b
$ \ s# :: State# RealWorld
s# -> MVar# RealWorld a -> State# RealWorld -> (# State# RealWorld, a #)
forall d a. MVar# d a -> State# d -> (# State# d, a #)
takeMVar# MVar# RealWorld a
mvar# State# RealWorld
s#
readMVar :: MVar a -> IO a
readMVar :: MVar a -> IO a
readMVar (MVar mvar# :: MVar# RealWorld a
mvar#) = (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, a #)) -> IO a)
-> (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a b. (a -> b) -> a -> b
$ \ s# :: State# RealWorld
s# -> MVar# RealWorld a -> State# RealWorld -> (# State# RealWorld, a #)
forall d a. MVar# d a -> State# d -> (# State# d, a #)
readMVar# MVar# RealWorld a
mvar# State# RealWorld
s#
putMVar :: MVar a -> a -> IO ()
putMVar :: MVar a -> a -> IO ()
putMVar (MVar mvar# :: MVar# RealWorld a
mvar#) x :: a
x = (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ s# :: State# RealWorld
s# ->
case MVar# RealWorld a -> a -> State# RealWorld -> State# RealWorld
forall d a. MVar# d a -> a -> State# d -> State# d
putMVar# MVar# RealWorld a
mvar# a
x State# RealWorld
s# of
s2# :: State# RealWorld
s2# -> (# State# RealWorld
s2#, () #)
tryTakeMVar :: MVar a -> IO (Maybe a)
tryTakeMVar :: MVar a -> IO (Maybe a)
tryTakeMVar (MVar m :: MVar# RealWorld a
m) = (State# RealWorld -> (# State# RealWorld, Maybe a #))
-> IO (Maybe a)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Maybe a #))
-> IO (Maybe a))
-> (State# RealWorld -> (# State# RealWorld, Maybe a #))
-> IO (Maybe a)
forall a b. (a -> b) -> a -> b
$ \ s :: State# RealWorld
s ->
case MVar# RealWorld a
-> State# RealWorld -> (# State# RealWorld, Int#, a #)
forall d a. MVar# d a -> State# d -> (# State# d, Int#, a #)
tryTakeMVar# MVar# RealWorld a
m State# RealWorld
s of
(# s' :: State# RealWorld
s', 0#, _ #) -> (# State# RealWorld
s', Maybe a
forall a. Maybe a
Nothing #)
(# s' :: State# RealWorld
s', _, a :: a
a #) -> (# State# RealWorld
s', a -> Maybe a
forall a. a -> Maybe a
Just a
a #)
tryPutMVar :: MVar a -> a -> IO Bool
tryPutMVar :: MVar a -> a -> IO Bool
tryPutMVar (MVar mvar# :: MVar# RealWorld a
mvar#) x :: a
x = (State# RealWorld -> (# State# RealWorld, Bool #)) -> IO Bool
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Bool #)) -> IO Bool)
-> (State# RealWorld -> (# State# RealWorld, Bool #)) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ s# :: State# RealWorld
s# ->
case MVar# RealWorld a
-> a -> State# RealWorld -> (# State# RealWorld, Int# #)
forall d a. MVar# d a -> a -> State# d -> (# State# d, Int# #)
tryPutMVar# MVar# RealWorld a
mvar# a
x State# RealWorld
s# of
(# s :: State# RealWorld
s, 0# #) -> (# State# RealWorld
s, Bool
False #)
(# s :: State# RealWorld
s, _ #) -> (# State# RealWorld
s, Bool
True #)
tryReadMVar :: MVar a -> IO (Maybe a)
tryReadMVar :: MVar a -> IO (Maybe a)
tryReadMVar (MVar m :: MVar# RealWorld a
m) = (State# RealWorld -> (# State# RealWorld, Maybe a #))
-> IO (Maybe a)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Maybe a #))
-> IO (Maybe a))
-> (State# RealWorld -> (# State# RealWorld, Maybe a #))
-> IO (Maybe a)
forall a b. (a -> b) -> a -> b
$ \ s :: State# RealWorld
s ->
case MVar# RealWorld a
-> State# RealWorld -> (# State# RealWorld, Int#, a #)
forall d a. MVar# d a -> State# d -> (# State# d, Int#, a #)
tryReadMVar# MVar# RealWorld a
m State# RealWorld
s of
(# s' :: State# RealWorld
s', 0#, _ #) -> (# State# RealWorld
s', Maybe a
forall a. Maybe a
Nothing #)
(# s' :: State# RealWorld
s', _, a :: a
a #) -> (# State# RealWorld
s', a -> Maybe a
forall a. a -> Maybe a
Just a
a #)
isEmptyMVar :: MVar a -> IO Bool
isEmptyMVar :: MVar a -> IO Bool
isEmptyMVar (MVar mv# :: MVar# RealWorld a
mv#) = (State# RealWorld -> (# State# RealWorld, Bool #)) -> IO Bool
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Bool #)) -> IO Bool)
-> (State# RealWorld -> (# State# RealWorld, Bool #)) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ s# :: State# RealWorld
s# ->
case MVar# RealWorld a
-> State# RealWorld -> (# State# RealWorld, Int# #)
forall d a. MVar# d a -> State# d -> (# State# d, Int# #)
isEmptyMVar# MVar# RealWorld a
mv# State# RealWorld
s# of
(# s2# :: State# RealWorld
s2#, flg :: Int#
flg #) -> (# State# RealWorld
s2#, Int# -> Bool
isTrue# (Int#
flg Int# -> Int# -> Int#
/=# 0#) #)
addMVarFinalizer :: MVar a -> IO () -> IO ()
addMVarFinalizer :: MVar a -> IO () -> IO ()
addMVarFinalizer (MVar m :: MVar# RealWorld a
m) (IO finalizer :: State# RealWorld -> (# State# RealWorld, () #)
finalizer) =
(State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \s :: State# RealWorld
s -> case MVar# RealWorld a
-> ()
-> (State# RealWorld -> (# State# RealWorld, () #))
-> State# RealWorld
-> (# State# RealWorld, Weak# () #)
forall a b c.
a
-> b
-> (State# RealWorld -> (# State# RealWorld, c #))
-> State# RealWorld
-> (# State# RealWorld, Weak# b #)
mkWeak# MVar# RealWorld a
m () State# RealWorld -> (# State# RealWorld, () #)
finalizer State# RealWorld
s of { (# s1 :: State# RealWorld
s1, _ #) -> (# State# RealWorld
s1, () #) }