{-# LANGUAGE MagicHash, UnboxedTuples, DeriveDataTypeable, CPP #-}
module Data.Primitive.MutVar (
MutVar(..),
newMutVar,
readMutVar,
writeMutVar,
atomicModifyMutVar,
atomicModifyMutVar',
modifyMutVar,
modifyMutVar'
) where
import Control.Monad.Primitive ( PrimMonad(..), primitive_ )
import GHC.Exts ( MutVar#, sameMutVar#, newMutVar#
, readMutVar#, writeMutVar#, atomicModifyMutVar#
, isTrue# )
import Data.Typeable ( Typeable )
data MutVar s a = MutVar (MutVar# s a)
deriving ( Typeable )
instance Eq (MutVar s a) where
MutVar MutVar# s a
mva# == :: MutVar s a -> MutVar s a -> Bool
== MutVar MutVar# s a
mvb# = Int# -> Bool
isTrue# (forall d a. MutVar# d a -> MutVar# d a -> Int#
sameMutVar# MutVar# s a
mva# MutVar# s a
mvb#)
newMutVar :: PrimMonad m => a -> m (MutVar (PrimState m) a)
{-# INLINE newMutVar #-}
newMutVar :: forall (m :: * -> *) a.
PrimMonad m =>
a -> m (MutVar (PrimState m) a)
newMutVar a
initialValue = 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 a d. a -> State# d -> (# State# d, MutVar# d a #)
newMutVar# a
initialValue State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, MutVar# (PrimState m) a
mv# #) -> (# State# (PrimState m)
s'#, forall s a. MutVar# s a -> MutVar s a
MutVar MutVar# (PrimState m) a
mv# #)
readMutVar :: PrimMonad m => MutVar (PrimState m) a -> m a
{-# INLINE readMutVar #-}
readMutVar :: forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar (MutVar MutVar# (PrimState m) a
mv#) = forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (forall d a. MutVar# d a -> State# d -> (# State# d, a #)
readMutVar# MutVar# (PrimState m) a
mv#)
writeMutVar :: PrimMonad m => MutVar (PrimState m) a -> a -> m ()
{-# INLINE writeMutVar #-}
writeMutVar :: forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> a -> m ()
writeMutVar (MutVar MutVar# (PrimState m) a
mv#) a
newValue = forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (forall d a. MutVar# d a -> a -> State# d -> State# d
writeMutVar# MutVar# (PrimState m) a
mv# a
newValue)
atomicModifyMutVar :: PrimMonad m => MutVar (PrimState m) a -> (a -> (a, b)) -> m b
{-# INLINE atomicModifyMutVar #-}
atomicModifyMutVar :: forall (m :: * -> *) a b.
PrimMonad m =>
MutVar (PrimState m) a -> (a -> (a, b)) -> m b
atomicModifyMutVar (MutVar MutVar# (PrimState m) a
mv#) a -> (a, b)
f = forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive forall a b. (a -> b) -> a -> b
$ forall s a b c.
MutVar# s a -> (a -> b) -> State# s -> (# State# s, c #)
atomicModifyMutVar# MutVar# (PrimState m) a
mv# a -> (a, b)
f
atomicModifyMutVar' :: PrimMonad m => MutVar (PrimState m) a -> (a -> (a, b)) -> m b
{-# INLINE atomicModifyMutVar' #-}
atomicModifyMutVar' :: forall (m :: * -> *) a b.
PrimMonad m =>
MutVar (PrimState m) a -> (a -> (a, b)) -> m b
atomicModifyMutVar' MutVar (PrimState m) a
mv a -> (a, b)
f = do
b
b <- forall (m :: * -> *) a b.
PrimMonad m =>
MutVar (PrimState m) a -> (a -> (a, b)) -> m b
atomicModifyMutVar MutVar (PrimState m) a
mv a -> (a, b)
force
b
b seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return b
b
where
force :: a -> (a, b)
force a
x = case a -> (a, b)
f a
x of
v :: (a, b)
v@(a
x', b
_) -> a
x' seq :: forall a b. a -> b -> b
`seq` (a, b)
v
modifyMutVar :: PrimMonad m => MutVar (PrimState m) a -> (a -> a) -> m ()
{-# INLINE modifyMutVar #-}
modifyMutVar :: forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> (a -> a) -> m ()
modifyMutVar (MutVar MutVar# (PrimState m) a
mv#) a -> a
g = forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ forall a b. (a -> b) -> a -> b
$ \State# (PrimState m)
s# ->
case forall d a. MutVar# d a -> State# d -> (# State# d, a #)
readMutVar# MutVar# (PrimState m) a
mv# State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, a
a #) -> forall d a. MutVar# d a -> a -> State# d -> State# d
writeMutVar# MutVar# (PrimState m) a
mv# (a -> a
g a
a) State# (PrimState m)
s'#
modifyMutVar' :: PrimMonad m => MutVar (PrimState m) a -> (a -> a) -> m ()
{-# INLINE modifyMutVar' #-}
modifyMutVar' :: forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> (a -> a) -> m ()
modifyMutVar' (MutVar MutVar# (PrimState m) a
mv#) a -> a
g = forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ forall a b. (a -> b) -> a -> b
$ \State# (PrimState m)
s# ->
case forall d a. MutVar# d a -> State# d -> (# State# d, a #)
readMutVar# MutVar# (PrimState m) a
mv# State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, a
a #) -> let a' :: a
a' = a -> a
g a
a in a
a' seq :: forall a b. a -> b -> b
`seq` forall d a. MutVar# d a -> a -> State# d -> State# d
writeMutVar# MutVar# (PrimState m) a
mv# a
a' State# (PrimState m)
s'#