{-# LANGUAGE BangPatterns #-}
{-# 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 Data.Primitive.Internal.Compat (isTrue#)
import GHC.Exts (MVar#,newMVar#,takeMVar#,sameMVar#,putMVar#,tryTakeMVar#,
isEmptyMVar#,tryPutMVar#,(/=#))
#if __GLASGOW_HASKELL__ >= 708
import GHC.Exts (readMVar#,tryReadMVar#)
#endif
data MVar s a = MVar (MVar# s a)
instance Eq (MVar s a) where
MVar mvar1# :: MVar# s a
mvar1# == :: MVar s a -> MVar s a -> Bool
== MVar mvar2# :: MVar# s a
mvar2# = Int# -> Bool
isTrue# (MVar# s a -> MVar# s a -> Int#
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 :: m (MVar (PrimState m) a)
newEmptyMVar = (State# (PrimState m)
-> (# State# (PrimState m), MVar (PrimState m) a #))
-> m (MVar (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m)
-> (# State# (PrimState m), MVar (PrimState m) a #))
-> m (MVar (PrimState m) a))
-> (State# (PrimState m)
-> (# State# (PrimState m), MVar (PrimState m) a #))
-> m (MVar (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ \ s# :: State# (PrimState m)
s# ->
case State# (PrimState m)
-> (# State# (PrimState m), MVar# (PrimState m) a #)
forall d a. State# d -> (# State# d, MVar# d a #)
newMVar# State# (PrimState m)
s# of
(# s2# :: State# (PrimState m)
s2#, svar# :: MVar# (PrimState m) a
svar# #) -> (# State# (PrimState m)
s2#, MVar# (PrimState m) a -> MVar (PrimState m) a
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 :: a -> m (MVar (PrimState m) a)
newMVar value :: a
value =
m (MVar (PrimState m) a)
forall (m :: * -> *) a. PrimMonad m => m (MVar (PrimState m) a)
newEmptyMVar m (MVar (PrimState m) a)
-> (MVar (PrimState m) a -> m (MVar (PrimState m) a))
-> m (MVar (PrimState m) a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ mvar :: MVar (PrimState m) a
mvar ->
MVar (PrimState m) a -> a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MVar (PrimState m) a -> a -> m ()
putMVar MVar (PrimState m) a
mvar a
value m () -> m (MVar (PrimState m) a) -> m (MVar (PrimState m) a)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
MVar (PrimState m) a -> m (MVar (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return MVar (PrimState m) a
mvar
takeMVar :: PrimMonad m => MVar (PrimState m) a -> m a
takeMVar :: MVar (PrimState m) a -> m a
takeMVar (MVar mvar# :: MVar# (PrimState m) a
mvar#) = (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a)
-> (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall a b. (a -> b) -> a -> b
$ \ s# :: State# (PrimState m)
s# -> MVar# (PrimState m) a
-> State# (PrimState m) -> (# State# (PrimState m), a #)
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
#if __GLASGOW_HASKELL__ >= 708
readMVar :: MVar (PrimState m) a -> m a
readMVar (MVar mvar# :: MVar# (PrimState m) a
mvar#) = (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a)
-> (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall a b. (a -> b) -> a -> b
$ \ s# :: State# (PrimState m)
s# -> MVar# (PrimState m) a
-> State# (PrimState m) -> (# State# (PrimState m), a #)
forall d a. MVar# d a -> State# d -> (# State# d, a #)
readMVar# MVar# (PrimState m) a
mvar# State# (PrimState m)
s#
#else
readMVar mv = do
a <- takeMVar mv
putMVar mv a
return a
#endif
putMVar :: PrimMonad m => MVar (PrimState m) a -> a -> m ()
putMVar :: MVar (PrimState m) a -> a -> m ()
putMVar (MVar mvar# :: MVar# (PrimState m) a
mvar#) x :: a
x = (State# (PrimState m) -> State# (PrimState m)) -> m ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (MVar# (PrimState m) a
-> a -> State# (PrimState m) -> State# (PrimState m)
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 :: MVar (PrimState m) a -> m (Maybe a)
tryTakeMVar (MVar m :: MVar# (PrimState m) a
m) = (State# (PrimState m) -> (# State# (PrimState m), Maybe a #))
-> m (Maybe a)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m) -> (# State# (PrimState m), Maybe a #))
-> m (Maybe a))
-> (State# (PrimState m) -> (# State# (PrimState m), Maybe a #))
-> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ \ s :: State# (PrimState m)
s ->
case MVar# (PrimState m) a
-> State# (PrimState m) -> (# State# (PrimState m), Int#, a #)
forall d a. MVar# d a -> State# d -> (# State# d, Int#, a #)
tryTakeMVar# MVar# (PrimState m) a
m State# (PrimState m)
s of
(# s' :: State# (PrimState m)
s', 0#, _ #) -> (# State# (PrimState m)
s', Maybe a
forall a. Maybe a
Nothing #)
(# s' :: State# (PrimState m)
s', _, a :: a
a #) -> (# State# (PrimState m)
s', a -> Maybe a
forall a. a -> Maybe a
Just a
a #)
tryPutMVar :: PrimMonad m => MVar (PrimState m) a -> a -> m Bool
tryPutMVar :: MVar (PrimState m) a -> a -> m Bool
tryPutMVar (MVar mvar# :: MVar# (PrimState m) a
mvar#) x :: a
x = (State# (PrimState m) -> (# State# (PrimState m), Bool #))
-> m Bool
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m) -> (# State# (PrimState m), Bool #))
-> m Bool)
-> (State# (PrimState m) -> (# State# (PrimState m), Bool #))
-> m Bool
forall a b. (a -> b) -> a -> b
$ \ s# :: State# (PrimState m)
s# ->
case MVar# (PrimState m) a
-> a -> State# (PrimState m) -> (# State# (PrimState m), Int# #)
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
(# s :: State# (PrimState m)
s, 0# #) -> (# State# (PrimState m)
s, Bool
False #)
(# s :: State# (PrimState m)
s, _ #) -> (# State# (PrimState m)
s, Bool
True #)
tryReadMVar :: PrimMonad m => MVar (PrimState m) a -> m (Maybe a)
#if __GLASGOW_HASKELL__ >= 708
tryReadMVar :: MVar (PrimState m) a -> m (Maybe a)
tryReadMVar (MVar m :: MVar# (PrimState m) a
m) = (State# (PrimState m) -> (# State# (PrimState m), Maybe a #))
-> m (Maybe a)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m) -> (# State# (PrimState m), Maybe a #))
-> m (Maybe a))
-> (State# (PrimState m) -> (# State# (PrimState m), Maybe a #))
-> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ \ s :: State# (PrimState m)
s ->
case MVar# (PrimState m) a
-> State# (PrimState m) -> (# State# (PrimState m), Int#, a #)
forall d a. MVar# d a -> State# d -> (# State# d, Int#, a #)
tryReadMVar# MVar# (PrimState m) a
m State# (PrimState m)
s of
(# s' :: State# (PrimState m)
s', 0#, _ #) -> (# State# (PrimState m)
s', Maybe a
forall a. Maybe a
Nothing #)
(# s' :: State# (PrimState m)
s', _, a :: a
a #) -> (# State# (PrimState m)
s', a -> Maybe a
forall a. a -> Maybe a
Just a
a #)
#else
tryReadMVar mv = do
ma <- tryTakeMVar mv
case ma of
Just a -> do
putMVar mv a
return (Just a)
Nothing -> return Nothing
#endif
isEmptyMVar :: PrimMonad m => MVar (PrimState m) a -> m Bool
isEmptyMVar :: MVar (PrimState m) a -> m Bool
isEmptyMVar (MVar mv# :: MVar# (PrimState m) a
mv#) = (State# (PrimState m) -> (# State# (PrimState m), Bool #))
-> m Bool
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m) -> (# State# (PrimState m), Bool #))
-> m Bool)
-> (State# (PrimState m) -> (# State# (PrimState m), Bool #))
-> m Bool
forall a b. (a -> b) -> a -> b
$ \ s# :: State# (PrimState m)
s# ->
case MVar# (PrimState m) a
-> State# (PrimState m) -> (# State# (PrimState m), Int# #)
forall d a. MVar# d a -> State# d -> (# State# d, Int# #)
isEmptyMVar# MVar# (PrimState m) a
mv# State# (PrimState m)
s# of
(# s2# :: State# (PrimState m)
s2#, flg :: Int#
flg #) -> (# State# (PrimState m)
s2#, Int# -> Bool
isTrue# (Int#
flg Int# -> Int# -> Int#
/=# 0#) #)