{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
module Data.Primitive.MVar
( MVar(..)
, newMVar
, isEmptyMVar
, newEmptyMVar
, putMVar
, readMVar
, takeMVar
, tryPutMVar
, tryReadMVar
, tryTakeMVar
) where
import Control.Monad.Primitive
import GHC.Exts
( MVar#, newMVar#, takeMVar#, sameMVar#, putMVar#, tryTakeMVar#, isEmptyMVar#, tryPutMVar#, (/=#)
, readMVar#, tryReadMVar#, isTrue# )
data MVar s a = MVar (MVar# s a)
instance Eq (MVar s a) where
MVar MVar# s a
mvar1# == :: MVar s a -> MVar s a -> Bool
== MVar MVar# s a
mvar2# = Int# -> Bool
isTrue# (forall d a. MVar# d a -> MVar# d a -> Int#
sameMVar# MVar# s a
mvar1# MVar# s a
mvar2#)
newEmptyMVar :: PrimMonad m => m (MVar (PrimState m) a)
newEmptyMVar :: forall (m :: * -> *) a. PrimMonad m => m (MVar (PrimState m) a)
newEmptyMVar = forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive forall a b. (a -> b) -> a -> b
$ \ State# (PrimState m)
s# ->
case forall d a. State# d -> (# State# d, MVar# d a #)
newMVar# State# (PrimState m)
s# of
(# State# (PrimState m)
s2#, MVar# (PrimState m) a
svar# #) -> (# State# (PrimState m)
s2#, forall s a. MVar# s a -> MVar s a
MVar MVar# (PrimState m) a
svar# #)
newMVar :: PrimMonad m => a -> m (MVar (PrimState m) a)
newMVar :: forall (m :: * -> *) a.
PrimMonad m =>
a -> m (MVar (PrimState m) a)
newMVar a
value = do
MVar (PrimState m) a
mvar <- forall (m :: * -> *) a. PrimMonad m => m (MVar (PrimState m) a)
newEmptyMVar
forall (m :: * -> *) a.
PrimMonad m =>
MVar (PrimState m) a -> a -> m ()
putMVar MVar (PrimState m) a
mvar a
value
forall (m :: * -> *) a. Monad m => a -> m a
return MVar (PrimState m) a
mvar
takeMVar :: PrimMonad m => MVar (PrimState m) a -> m a
takeMVar :: forall (m :: * -> *) a. PrimMonad m => MVar (PrimState m) a -> m a
takeMVar (MVar MVar# (PrimState m) a
mvar#) = forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive forall a b. (a -> b) -> a -> b
$ \ State# (PrimState m)
s# -> forall d a. MVar# d a -> State# d -> (# State# d, a #)
takeMVar# MVar# (PrimState m) a
mvar# State# (PrimState m)
s#
readMVar :: PrimMonad m => MVar (PrimState m) a -> m a
readMVar :: forall (m :: * -> *) a. PrimMonad m => MVar (PrimState m) a -> m a
readMVar (MVar MVar# (PrimState m) a
mvar#) = forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive forall a b. (a -> b) -> a -> b
$ \ State# (PrimState m)
s# -> forall d a. MVar# d a -> State# d -> (# State# d, a #)
readMVar# MVar# (PrimState m) a
mvar# State# (PrimState m)
s#
putMVar :: PrimMonad m => MVar (PrimState m) a -> a -> m ()
putMVar :: forall (m :: * -> *) a.
PrimMonad m =>
MVar (PrimState m) a -> a -> m ()
putMVar (MVar MVar# (PrimState m) a
mvar#) a
x = forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (forall d a. MVar# d a -> a -> State# d -> State# d
putMVar# MVar# (PrimState m) a
mvar# a
x)
tryTakeMVar :: PrimMonad m => MVar (PrimState m) a -> m (Maybe a)
tryTakeMVar :: forall (m :: * -> *) a.
PrimMonad m =>
MVar (PrimState m) a -> m (Maybe a)
tryTakeMVar (MVar MVar# (PrimState m) a
m) = forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive forall a b. (a -> b) -> a -> b
$ \ State# (PrimState m)
s ->
case forall d a. MVar# d a -> State# d -> (# State# d, Int#, a #)
tryTakeMVar# MVar# (PrimState m) a
m State# (PrimState m)
s of
(# State# (PrimState m)
s', Int#
0#, a
_ #) -> (# State# (PrimState m)
s', forall a. Maybe a
Nothing #)
(# State# (PrimState m)
s', Int#
_, a
a #) -> (# State# (PrimState m)
s', forall a. a -> Maybe a
Just a
a #)
tryPutMVar :: PrimMonad m => MVar (PrimState m) a -> a -> m Bool
tryPutMVar :: forall (m :: * -> *) a.
PrimMonad m =>
MVar (PrimState m) a -> a -> m Bool
tryPutMVar (MVar MVar# (PrimState m) a
mvar#) a
x = forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive forall a b. (a -> b) -> a -> b
$ \ State# (PrimState m)
s# ->
case forall d a. MVar# d a -> a -> State# d -> (# State# d, Int# #)
tryPutMVar# MVar# (PrimState m) a
mvar# a
x State# (PrimState m)
s# of
(# State# (PrimState m)
s, Int#
0# #) -> (# State# (PrimState m)
s, Bool
False #)
(# State# (PrimState m)
s, Int#
_ #) -> (# State# (PrimState m)
s, Bool
True #)
tryReadMVar :: PrimMonad m => MVar (PrimState m) a -> m (Maybe a)
tryReadMVar :: forall (m :: * -> *) a.
PrimMonad m =>
MVar (PrimState m) a -> m (Maybe a)
tryReadMVar (MVar MVar# (PrimState m) a
m) = forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive forall a b. (a -> b) -> a -> b
$ \ State# (PrimState m)
s ->
case forall d a. MVar# d a -> State# d -> (# State# d, Int#, a #)
tryReadMVar# MVar# (PrimState m) a
m State# (PrimState m)
s of
(# State# (PrimState m)
s', Int#
0#, a
_ #) -> (# State# (PrimState m)
s', forall a. Maybe a
Nothing #)
(# State# (PrimState m)
s', Int#
_, a
a #) -> (# State# (PrimState m)
s', forall a. a -> Maybe a
Just a
a #)
isEmptyMVar :: PrimMonad m => MVar (PrimState m) a -> m Bool
isEmptyMVar :: forall (m :: * -> *) a.
PrimMonad m =>
MVar (PrimState m) a -> m Bool
isEmptyMVar (MVar MVar# (PrimState m) a
mv#) = forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive forall a b. (a -> b) -> a -> b
$ \ State# (PrimState m)
s# ->
case forall d a. MVar# d a -> State# d -> (# State# d, Int# #)
isEmptyMVar# MVar# (PrimState m) a
mv# State# (PrimState m)
s# of
(# State# (PrimState m)
s2#, Int#
flg #) -> (# State# (PrimState m)
s2#, Int# -> Bool
isTrue# (Int#
flg Int# -> Int# -> Int#
/=# Int#
0#) #)