{-# 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 MVar# RealWorld a
mvar1#) == :: MVar a -> MVar a -> Bool
== (MVar MVar# RealWorld a
mvar2#) = Int# -> Bool
isTrue# (MVar# RealWorld a -> MVar# RealWorld a -> Int#
forall s a. MVar# s a -> MVar# s a -> Int#
sameMVar# MVar# RealWorld a
mvar1# MVar# RealWorld a
mvar2#)
newEmptyMVar :: IO (MVar a)
newEmptyMVar :: forall a. 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
$ \ 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
(# State# RealWorld
s2#, 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 :: forall a. a -> IO (MVar a)
newMVar a
value =
IO (MVar a)
forall a. IO (MVar a)
newEmptyMVar IO (MVar a) -> (MVar a -> IO (MVar a)) -> IO (MVar a)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ 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 a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
MVar a -> IO (MVar a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return MVar a
mvar
takeMVar :: MVar a -> IO a
takeMVar :: forall a. MVar a -> IO a
takeMVar (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
$ \ 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 :: forall a. MVar a -> IO a
readMVar (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
$ \ 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 :: forall a. MVar a -> a -> IO ()
putMVar (MVar MVar# RealWorld a
mvar#) 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
$ \ 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
State# RealWorld
s2# -> (# State# RealWorld
s2#, () #)
tryTakeMVar :: MVar a -> IO (Maybe a)
tryTakeMVar :: forall a. MVar a -> IO (Maybe a)
tryTakeMVar (MVar 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
$ \ 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
(# State# RealWorld
s', Int#
0#, a
_ #) -> (# State# RealWorld
s', Maybe a
forall a. Maybe a
Nothing #)
(# State# RealWorld
s', Int#
_, a
a #) -> (# State# RealWorld
s', a -> Maybe a
forall a. a -> Maybe a
Just a
a #)
tryPutMVar :: MVar a -> a -> IO Bool
tryPutMVar :: forall a. MVar a -> a -> IO Bool
tryPutMVar (MVar MVar# RealWorld a
mvar#) 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
$ \ 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
(# State# RealWorld
s, Int#
0# #) -> (# State# RealWorld
s, Bool
False #)
(# State# RealWorld
s, Int#
_ #) -> (# State# RealWorld
s, Bool
True #)
tryReadMVar :: MVar a -> IO (Maybe a)
tryReadMVar :: forall a. MVar a -> IO (Maybe a)
tryReadMVar (MVar 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
$ \ 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
(# State# RealWorld
s', Int#
0#, a
_ #) -> (# State# RealWorld
s', Maybe a
forall a. Maybe a
Nothing #)
(# State# RealWorld
s', Int#
_, a
a #) -> (# State# RealWorld
s', a -> Maybe a
forall a. a -> Maybe a
Just a
a #)
isEmptyMVar :: MVar a -> IO Bool
isEmptyMVar :: forall a. MVar a -> IO Bool
isEmptyMVar (MVar 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
$ \ 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
(# State# RealWorld
s2#, Int#
flg #) -> (# State# RealWorld
s2#, Int# -> Bool
isTrue# (Int#
flg Int# -> Int# -> Int#
/=# Int#
0#) #)
addMVarFinalizer :: MVar a -> IO () -> IO ()
addMVarFinalizer :: forall a. MVar a -> IO () -> IO ()
addMVarFinalizer (MVar MVar# RealWorld a
m) (IO 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
$ \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 { (# State# RealWorld
s1, Weak# ()
_ #) -> (# State# RealWorld
s1, () #) }