{-# language UnboxedTuples #-}
{-# language UnboxedSums #-}
{-# language RoleAnnotations #-}
{-# language ScopedTypeVariables #-}
{-# language TypeFamilies #-}
{-# language TypeOperators #-}
{-# language MagicHash #-}
{-# language RankNTypes #-}
{-# language PatternSynonyms #-}
{-# language ViewPatterns #-}
{-# language BangPatterns #-}
{- options_ghc -ddump-simpl #-}

-- | This module includes all the features of "Control.Concurrent.MVar", except
-- that the functions in "Data.Primitive.Unlifted.Weak" subsume the functionality
-- of @mkWeakMV@ and @addMVarFinalizer@, so we do not include analogues of those
-- functions.
module Data.Primitive.Unlifted.MVar
  ( UnliftedMVar_ (..)
  , UnliftedMVar
  , newUnliftedMVar
  , newEmptyUnliftedMVar
  , takeUnliftedMVar
  , tryTakeUnliftedMVar
  , putUnliftedMVar
  , tryPutUnliftedMVar
  , readUnliftedMVar
  , tryReadUnliftedMVar
  , isEmptyUnliftedMVar
  , swapUnliftedMVar
  , withUnliftedMVar
  , withUnliftedMVarMasked
  , modifyUnliftedMVar
  , modifyUnliftedMVar_
  , modifyUnliftedMVarMasked
  , modifyUnliftedMVarMasked_
  ) where
import qualified Data.Primitive.Unlifted.MVar.ST as MV
import Data.Primitive.Unlifted.MVar.ST
         ( UnliftedMVar_ (..), type UnliftedMVar )
import Data.Primitive.Unlifted.Class (PrimUnlifted (..))
import GHC.Exts (RealWorld)
import Control.Monad.Primitive (stToPrim, PrimMonad (..), PrimBase, primToST)

newUnliftedMVar
  :: (PrimUnlifted a, PrimMonad m)
  => a -> m (UnliftedMVar (PrimState m) a)
newUnliftedMVar :: forall a (m :: * -> *).
(PrimUnlifted a, PrimMonad m) =>
a -> m (UnliftedMVar (PrimState m) a)
newUnliftedMVar a
a = ST (PrimState m) (UnliftedMVar (PrimState m) a)
-> m (UnliftedMVar (PrimState m) a)
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) (UnliftedMVar (PrimState m) a)
 -> m (UnliftedMVar (PrimState m) a))
-> ST (PrimState m) (UnliftedMVar (PrimState m) a)
-> m (UnliftedMVar (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ a -> ST (PrimState m) (UnliftedMVar (PrimState m) a)
forall a s. PrimUnlifted a => a -> ST s (UnliftedMVar s a)
MV.newUnliftedMVar a
a

newEmptyUnliftedMVar
  :: PrimMonad m
  => m (UnliftedMVar (PrimState m) a)
{-# INLINE newEmptyUnliftedMVar #-}
newEmptyUnliftedMVar :: forall (m :: * -> *) a.
PrimMonad m =>
m (UnliftedMVar (PrimState m) a)
newEmptyUnliftedMVar = ST (PrimState m) (UnliftedMVar (PrimState m) a)
-> m (UnliftedMVar (PrimState m) a)
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) (UnliftedMVar (PrimState m) a)
 -> m (UnliftedMVar (PrimState m) a))
-> ST (PrimState m) (UnliftedMVar (PrimState m) a)
-> m (UnliftedMVar (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ ST (PrimState m) (UnliftedMVar (PrimState m) a)
forall s a. ST s (UnliftedMVar s a)
MV.newEmptyUnliftedMVar

takeUnliftedMVar
  :: (PrimMonad m, PrimUnlifted a)
  => UnliftedMVar (PrimState m) a -> m a
{-# INLINE takeUnliftedMVar #-}
takeUnliftedMVar :: forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
UnliftedMVar (PrimState m) a -> m a
takeUnliftedMVar UnliftedMVar (PrimState m) a
mv = ST (PrimState m) a -> m a
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) a -> m a) -> ST (PrimState m) a -> m a
forall a b. (a -> b) -> a -> b
$ UnliftedMVar (PrimState m) a -> ST (PrimState m) a
forall a s. PrimUnlifted a => UnliftedMVar s a -> ST s a
MV.takeUnliftedMVar UnliftedMVar (PrimState m) a
mv

tryTakeUnliftedMVar
  :: (PrimMonad m, PrimUnlifted a)
  => UnliftedMVar (PrimState m) a -> m (Maybe a)
{-# INLINE tryTakeUnliftedMVar #-}
tryTakeUnliftedMVar :: forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
UnliftedMVar (PrimState m) a -> m (Maybe a)
tryTakeUnliftedMVar UnliftedMVar (PrimState m) a
mv = ST (PrimState m) (Maybe a) -> m (Maybe a)
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) (Maybe a) -> m (Maybe a))
-> ST (PrimState m) (Maybe a) -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ UnliftedMVar (PrimState m) a -> ST (PrimState m) (Maybe a)
forall a s. PrimUnlifted a => UnliftedMVar s a -> ST s (Maybe a)
MV.tryTakeUnliftedMVar UnliftedMVar (PrimState m) a
mv

putUnliftedMVar
  :: (PrimMonad m, PrimUnlifted a)
  => UnliftedMVar (PrimState m) a -> a -> m ()
{-# INLINE putUnliftedMVar #-}
putUnliftedMVar :: forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
UnliftedMVar (PrimState m) a -> a -> m ()
putUnliftedMVar UnliftedMVar (PrimState m) a
mv a
a = ST (PrimState m) () -> m ()
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) () -> m ()) -> ST (PrimState m) () -> m ()
forall a b. (a -> b) -> a -> b
$ UnliftedMVar (PrimState m) a -> a -> ST (PrimState m) ()
forall a s. PrimUnlifted a => UnliftedMVar s a -> a -> ST s ()
MV.putUnliftedMVar UnliftedMVar (PrimState m) a
mv a
a

tryPutUnliftedMVar
  :: (PrimMonad m, PrimUnlifted a)
  => UnliftedMVar (PrimState m) a -> a -> m Bool
{-# INLINE tryPutUnliftedMVar #-}
tryPutUnliftedMVar :: forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
UnliftedMVar (PrimState m) a -> a -> m Bool
tryPutUnliftedMVar UnliftedMVar (PrimState m) a
mv a
a = ST (PrimState m) Bool -> m Bool
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) Bool -> m Bool)
-> ST (PrimState m) Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ UnliftedMVar (PrimState m) a -> a -> ST (PrimState m) Bool
forall a s. PrimUnlifted a => UnliftedMVar s a -> a -> ST s Bool
MV.tryPutUnliftedMVar UnliftedMVar (PrimState m) a
mv a
a

readUnliftedMVar
  :: (PrimMonad m, PrimUnlifted a)
  => UnliftedMVar (PrimState m) a -> m a
{-# INLINE readUnliftedMVar #-}
readUnliftedMVar :: forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
UnliftedMVar (PrimState m) a -> m a
readUnliftedMVar UnliftedMVar (PrimState m) a
mv = ST (PrimState m) a -> m a
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) a -> m a) -> ST (PrimState m) a -> m a
forall a b. (a -> b) -> a -> b
$ UnliftedMVar (PrimState m) a -> ST (PrimState m) a
forall a s. PrimUnlifted a => UnliftedMVar s a -> ST s a
MV.readUnliftedMVar UnliftedMVar (PrimState m) a
mv

tryReadUnliftedMVar
  :: (PrimMonad m, PrimUnlifted a)
  => UnliftedMVar (PrimState m) a -> m (Maybe a)
{-# INLINE tryReadUnliftedMVar #-}
tryReadUnliftedMVar :: forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
UnliftedMVar (PrimState m) a -> m (Maybe a)
tryReadUnliftedMVar UnliftedMVar (PrimState m) a
mv = ST (PrimState m) (Maybe a) -> m (Maybe a)
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) (Maybe a) -> m (Maybe a))
-> ST (PrimState m) (Maybe a) -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ UnliftedMVar (PrimState m) a -> ST (PrimState m) (Maybe a)
forall a s. PrimUnlifted a => UnliftedMVar s a -> ST s (Maybe a)
MV.tryReadUnliftedMVar UnliftedMVar (PrimState m) a
mv

isEmptyUnliftedMVar
  :: PrimMonad m
  => UnliftedMVar (PrimState m) a -> m Bool
{-# INLINE isEmptyUnliftedMVar #-}
isEmptyUnliftedMVar :: forall (m :: * -> *) a.
PrimMonad m =>
UnliftedMVar (PrimState m) a -> m Bool
isEmptyUnliftedMVar UnliftedMVar (PrimState m) a
mv = ST (PrimState m) Bool -> m Bool
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) Bool -> m Bool)
-> ST (PrimState m) Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ UnliftedMVar (PrimState m) a -> ST (PrimState m) Bool
forall s a. UnliftedMVar s a -> ST s Bool
MV.isEmptyUnliftedMVar UnliftedMVar (PrimState m) a
mv

swapUnliftedMVar
  :: (PrimMonad m, PrimState m ~ RealWorld, PrimUnlifted a)
  => UnliftedMVar RealWorld a -> a -> m a
{-# INLINE swapUnliftedMVar #-}
swapUnliftedMVar :: forall (m :: * -> *) a.
(PrimMonad m, PrimState m ~ RealWorld, PrimUnlifted a) =>
UnliftedMVar RealWorld a -> a -> m a
swapUnliftedMVar UnliftedMVar RealWorld a
mvar a
new = ST (PrimState m) a -> m a
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) a -> m a) -> ST (PrimState m) a -> m a
forall a b. (a -> b) -> a -> b
$ UnliftedMVar RealWorld a -> a -> ST RealWorld a
forall a.
PrimUnlifted a =>
UnliftedMVar RealWorld a -> a -> ST RealWorld a
MV.swapUnliftedMVar UnliftedMVar RealWorld a
mvar a
new

withUnliftedMVar
  :: (PrimBase m, PrimState m ~ RealWorld, PrimUnlifted a)
  => UnliftedMVar RealWorld a -> (a -> m b) -> m b
{-# INLINE withUnliftedMVar #-}
withUnliftedMVar :: forall (m :: * -> *) a b.
(PrimBase m, PrimState m ~ RealWorld, PrimUnlifted a) =>
UnliftedMVar RealWorld a -> (a -> m b) -> m b
withUnliftedMVar UnliftedMVar RealWorld a
m a -> m b
f = ST (PrimState m) b -> m b
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) b -> m b) -> ST (PrimState m) b -> m b
forall a b. (a -> b) -> a -> b
$ UnliftedMVar RealWorld a -> (a -> ST RealWorld b) -> ST RealWorld b
forall a b.
PrimUnlifted a =>
UnliftedMVar RealWorld a -> (a -> ST RealWorld b) -> ST RealWorld b
MV.withUnliftedMVar UnliftedMVar RealWorld a
m (m b -> ST RealWorld b
m b -> ST (PrimState m) b
forall (m :: * -> *) a. PrimBase m => m a -> ST (PrimState m) a
primToST (m b -> ST RealWorld b) -> (a -> m b) -> a -> ST RealWorld b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m b
f)

withUnliftedMVarMasked
  :: (PrimBase m, PrimState m ~ RealWorld, PrimUnlifted a)
  => UnliftedMVar RealWorld a -> (a -> m b) -> m b
{-# INLINE withUnliftedMVarMasked #-}
withUnliftedMVarMasked :: forall (m :: * -> *) a b.
(PrimBase m, PrimState m ~ RealWorld, PrimUnlifted a) =>
UnliftedMVar RealWorld a -> (a -> m b) -> m b
withUnliftedMVarMasked UnliftedMVar RealWorld a
m a -> m b
st = ST (PrimState m) b -> m b
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) b -> m b) -> ST (PrimState m) b -> m b
forall a b. (a -> b) -> a -> b
$ UnliftedMVar RealWorld a -> (a -> ST RealWorld b) -> ST RealWorld b
forall a b.
PrimUnlifted a =>
UnliftedMVar RealWorld a -> (a -> ST RealWorld b) -> ST RealWorld b
MV.withUnliftedMVarMasked UnliftedMVar RealWorld a
m (m b -> ST RealWorld b
m b -> ST (PrimState m) b
forall (m :: * -> *) a. PrimBase m => m a -> ST (PrimState m) a
primToST (m b -> ST RealWorld b) -> (a -> m b) -> a -> ST RealWorld b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m b
st)

modifyUnliftedMVar
  :: (PrimBase m, PrimState m ~ RealWorld, PrimUnlifted a)
  => UnliftedMVar RealWorld a -> (a -> m (a, b)) -> m b
{-# INLINE modifyUnliftedMVar #-}
modifyUnliftedMVar :: forall (m :: * -> *) a b.
(PrimBase m, PrimState m ~ RealWorld, PrimUnlifted a) =>
UnliftedMVar RealWorld a -> (a -> m (a, b)) -> m b
modifyUnliftedMVar UnliftedMVar RealWorld a
m a -> m (a, b)
st = ST (PrimState m) b -> m b
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) b -> m b) -> ST (PrimState m) b -> m b
forall a b. (a -> b) -> a -> b
$ UnliftedMVar RealWorld a
-> (a -> ST RealWorld (a, b)) -> ST RealWorld b
forall a b.
PrimUnlifted a =>
UnliftedMVar RealWorld a
-> (a -> ST RealWorld (a, b)) -> ST RealWorld b
MV.modifyUnliftedMVar UnliftedMVar RealWorld a
m (m (a, b) -> ST RealWorld (a, b)
m (a, b) -> ST (PrimState m) (a, b)
forall (m :: * -> *) a. PrimBase m => m a -> ST (PrimState m) a
primToST (m (a, b) -> ST RealWorld (a, b))
-> (a -> m (a, b)) -> a -> ST RealWorld (a, b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m (a, b)
st)

modifyUnliftedMVar_
  :: (PrimBase m, PrimState m ~ RealWorld, PrimUnlifted a)
  => UnliftedMVar RealWorld a -> (a -> m a) -> m ()
{-# INLINE modifyUnliftedMVar_ #-}
modifyUnliftedMVar_ :: forall (m :: * -> *) a.
(PrimBase m, PrimState m ~ RealWorld, PrimUnlifted a) =>
UnliftedMVar RealWorld a -> (a -> m a) -> m ()
modifyUnliftedMVar_ UnliftedMVar RealWorld a
m a -> m a
st = ST (PrimState m) () -> m ()
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) () -> m ()) -> ST (PrimState m) () -> m ()
forall a b. (a -> b) -> a -> b
$ UnliftedMVar RealWorld a
-> (a -> ST RealWorld a) -> ST RealWorld ()
forall a.
PrimUnlifted a =>
UnliftedMVar RealWorld a
-> (a -> ST RealWorld a) -> ST RealWorld ()
MV.modifyUnliftedMVar_ UnliftedMVar RealWorld a
m (m a -> ST RealWorld a
m a -> ST (PrimState m) a
forall (m :: * -> *) a. PrimBase m => m a -> ST (PrimState m) a
primToST (m a -> ST RealWorld a) -> (a -> m a) -> a -> ST RealWorld a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
st)

modifyUnliftedMVarMasked
  :: (PrimBase m, PrimState m ~ RealWorld, PrimUnlifted a)
  => UnliftedMVar RealWorld a -> (a -> m (a, b)) -> m b
{-# INLINE modifyUnliftedMVarMasked #-}
modifyUnliftedMVarMasked :: forall (m :: * -> *) a b.
(PrimBase m, PrimState m ~ RealWorld, PrimUnlifted a) =>
UnliftedMVar RealWorld a -> (a -> m (a, b)) -> m b
modifyUnliftedMVarMasked UnliftedMVar RealWorld a
m a -> m (a, b)
st = ST (PrimState m) b -> m b
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) b -> m b) -> ST (PrimState m) b -> m b
forall a b. (a -> b) -> a -> b
$ UnliftedMVar RealWorld a
-> (a -> ST RealWorld (a, b)) -> ST RealWorld b
forall a b.
PrimUnlifted a =>
UnliftedMVar RealWorld a
-> (a -> ST RealWorld (a, b)) -> ST RealWorld b
MV.modifyUnliftedMVarMasked UnliftedMVar RealWorld a
m (m (a, b) -> ST RealWorld (a, b)
m (a, b) -> ST (PrimState m) (a, b)
forall (m :: * -> *) a. PrimBase m => m a -> ST (PrimState m) a
primToST (m (a, b) -> ST RealWorld (a, b))
-> (a -> m (a, b)) -> a -> ST RealWorld (a, b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m (a, b)
st)

modifyUnliftedMVarMasked_
  :: (PrimBase m, PrimState m ~ RealWorld, PrimUnlifted a)
  => UnliftedMVar RealWorld a -> (a -> m a) -> m ()
{-# INLINE modifyUnliftedMVarMasked_ #-}
modifyUnliftedMVarMasked_ :: forall (m :: * -> *) a.
(PrimBase m, PrimState m ~ RealWorld, PrimUnlifted a) =>
UnliftedMVar RealWorld a -> (a -> m a) -> m ()
modifyUnliftedMVarMasked_ UnliftedMVar RealWorld a
m a -> m a
st = ST (PrimState m) () -> m ()
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) () -> m ()) -> ST (PrimState m) () -> m ()
forall a b. (a -> b) -> a -> b
$ UnliftedMVar RealWorld a
-> (a -> ST RealWorld a) -> ST RealWorld ()
forall a.
PrimUnlifted a =>
UnliftedMVar RealWorld a
-> (a -> ST RealWorld a) -> ST RealWorld ()
MV.modifyUnliftedMVarMasked_ UnliftedMVar RealWorld a
m (m a -> ST RealWorld a
m a -> ST (PrimState m) a
forall (m :: * -> *) a. PrimBase m => m a -> ST (PrimState m) a
primToST (m a -> ST RealWorld a) -> (a -> m a) -> a -> ST RealWorld a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
st)