{-# LANGUAGE MagicHash, UnboxedTuples, DeriveDataTypeable, CPP #-}

-- |
-- Module      : Data.Primitive.MutVar
-- Copyright   : (c) Justin Bonnar 2011, Roman Leshchinskiy 2011-2012
-- License     : BSD-style
--
-- Maintainer  : Roman Leshchinskiy <rl@cse.unsw.edu.au>
-- Portability : non-portable
--
-- Primitive boxed mutable variables. This is a generalization of
-- "Data.IORef", "Data.STRef" and "Data.STRef.Lazy" to work in
-- any 'PrimMonad'.

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 )

-- | A 'MutVar' behaves like a single-element mutable array associated
-- with a primitive state token.
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#)

-- | Create a new 'MutVar' with the specified initial value.
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# #)

-- | Read the value of a 'MutVar'.
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#)

-- | Write a new value into a 'MutVar'.
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)

-- | Atomically mutate the contents of a 'MutVar'.
--
-- This function is useful for using 'MutVar' in a safe way in a multithreaded program.
-- If you only have one 'MutVar', then using 'atomicModifyMutVar' to access and modify
-- it will prevent race conditions.
--
-- Extending the atomicity to multiple 'MutVar's is problematic,
-- so if you need to do anything more complicated,
-- using 'Data.Primitive.MVar.MVar' instead is a good idea.
--
-- 'atomicModifyMutVar' does not apply the function strictly. This means if a program
-- calls 'atomicModifyMutVar' many times, but seldom uses the value, thunks will pile up
-- in memory resulting in a space leak.
-- To avoid this problem, use 'atomicModifyMutVar'' instead.
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

-- | Strict version of 'atomicModifyMutVar'. This forces both the value stored
-- in the 'MutVar' as well as the value returned.
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

-- | Mutate the contents of a 'MutVar'.
--
-- 'modifyMutVar' does not apply the function strictly. This means if a program
-- calls 'modifyMutVar' many times, but seldom uses the value, thunks will pile up
-- in memory resulting in a space leak.
-- To avoid this problem, use 'modifyMutVar'' instead.
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'#

-- | Strict version of 'modifyMutVar'.
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'#