-- | Unlifted "Control.Concurrent.MVar".
--
-- @since 0.1.0.0
module UnliftIO.MVar
  ( MVar
  , newEmptyMVar
  , newMVar
  , takeMVar
  , putMVar
  , readMVar
  , swapMVar
  , tryTakeMVar
  , tryPutMVar
  , isEmptyMVar
  , withMVar
  , withMVarMasked
  , modifyMVar
  , modifyMVar_
  , modifyMVarMasked
  , modifyMVarMasked_
  , tryReadMVar
  , mkWeakMVar
  ) where

import System.Mem.Weak (Weak)
import Control.Concurrent.MVar (MVar)
import Control.Monad.IO.Unlift
import qualified Control.Concurrent.MVar as M

-- | Lifted 'M.newEmptyMVar'.
--
-- @since 0.1.0.0
newEmptyMVar :: MonadIO m => m (MVar a)
newEmptyMVar :: forall (m :: * -> *) a. MonadIO m => m (MVar a)
newEmptyMVar = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a. IO (MVar a)
M.newEmptyMVar

-- | Lifted 'M.newMVar'.
--
-- @since 0.1.0.0
newMVar :: MonadIO m => a -> m (MVar a)
newMVar :: forall (m :: * -> *) a. MonadIO m => a -> m (MVar a)
newMVar = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> IO (MVar a)
M.newMVar

-- | Lifted 'M.takeMVar'.
--
-- @since 0.1.0.0
takeMVar :: MonadIO m => MVar a -> m a
takeMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> m a
takeMVar = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MVar a -> IO a
M.takeMVar

-- | Lifted 'M.putMVar'.
--
-- @since 0.1.0.0
putMVar :: MonadIO m => MVar a -> a -> m ()
putMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> a -> m ()
putMVar MVar a
var = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MVar a -> a -> IO ()
M.putMVar MVar a
var

-- | Lifted 'M.readMVar'.
--
-- @since 0.1.0.0
readMVar :: MonadIO m => MVar a -> m a
readMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> m a
readMVar = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MVar a -> IO a
M.readMVar

-- | Lifted 'M.swapMVar'.
--
-- @since 0.1.0.0
swapMVar :: MonadIO m => MVar a -> a -> m a
swapMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> a -> m a
swapMVar MVar a
var = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MVar a -> a -> IO a
M.swapMVar MVar a
var

-- | Lifted 'M.tryTakeMVar'.
--
-- @since 0.1.0.0
tryTakeMVar :: MonadIO m => MVar a -> m (Maybe a)
tryTakeMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> m (Maybe a)
tryTakeMVar = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MVar a -> IO (Maybe a)
M.tryTakeMVar

-- | Lifted 'M.tryPutMVar'.
--
-- @since 0.1.0.0
tryPutMVar :: MonadIO m => MVar a -> a -> m Bool
tryPutMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> a -> m Bool
tryPutMVar MVar a
var = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MVar a -> a -> IO Bool
M.tryPutMVar MVar a
var

-- | Lifted 'M.isEmptyMVar'.
--
-- @since 0.1.0.0
isEmptyMVar :: MonadIO m => MVar a -> m Bool
isEmptyMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> m Bool
isEmptyMVar = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MVar a -> IO Bool
M.isEmptyMVar

-- | Lifted 'M.tryReadMVar'.
--
-- @since 0.1.0.0
tryReadMVar :: MonadIO m => MVar a -> m (Maybe a)
tryReadMVar :: forall (m :: * -> *) a. MonadIO m => MVar a -> m (Maybe a)
tryReadMVar = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MVar a -> IO (Maybe a)
M.tryReadMVar

-- | Unlifted 'M.withMVar'.
--
-- @since 0.1.0.0
withMVar :: MonadUnliftIO m => MVar a -> (a -> m b) -> m b
withMVar :: forall (m :: * -> *) a b.
MonadUnliftIO m =>
MVar a -> (a -> m b) -> m b
withMVar MVar a
var a -> m b
f = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a b. MVar a -> (a -> IO b) -> IO b
M.withMVar MVar a
var (forall a. m a -> IO a
run forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m b
f)

-- | Unlifted 'M.withMVarMasked'.
--
-- @since 0.1.0.0
withMVarMasked :: MonadUnliftIO m => MVar a -> (a -> m b) -> m b
withMVarMasked :: forall (m :: * -> *) a b.
MonadUnliftIO m =>
MVar a -> (a -> m b) -> m b
withMVarMasked MVar a
var a -> m b
f = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a b. MVar a -> (a -> IO b) -> IO b
M.withMVarMasked MVar a
var (forall a. m a -> IO a
run forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m b
f)

-- | Unlifted 'M.modifyMVar_'.
--
-- @since 0.1.0.0
modifyMVar_ :: MonadUnliftIO m => MVar a -> (a -> m a) -> m ()
modifyMVar_ :: forall (m :: * -> *) a.
MonadUnliftIO m =>
MVar a -> (a -> m a) -> m ()
modifyMVar_ MVar a
var a -> m a
f = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a. MVar a -> (a -> IO a) -> IO ()
M.modifyMVar_ MVar a
var (forall a. m a -> IO a
run forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
f)

-- | Unlifted 'M.modifyMVar'.
--
-- @since 0.1.0.0
modifyMVar :: MonadUnliftIO m => MVar a -> (a -> m (a, b)) -> m b
modifyMVar :: forall (m :: * -> *) a b.
MonadUnliftIO m =>
MVar a -> (a -> m (a, b)) -> m b
modifyMVar MVar a
var a -> m (a, b)
f = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a b. MVar a -> (a -> IO (a, b)) -> IO b
M.modifyMVar MVar a
var (forall a. m a -> IO a
run forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m (a, b)
f)

-- | Unlifted 'M.modifyMVarMasked_'.
--
-- @since 0.1.0.0
modifyMVarMasked_ :: MonadUnliftIO m => MVar a -> (a -> m a) -> m ()
modifyMVarMasked_ :: forall (m :: * -> *) a.
MonadUnliftIO m =>
MVar a -> (a -> m a) -> m ()
modifyMVarMasked_ MVar a
var a -> m a
f = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a. MVar a -> (a -> IO a) -> IO ()
M.modifyMVarMasked_ MVar a
var (forall a. m a -> IO a
run forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
f)

-- | Unlifted 'M.modifyMVarMasked'.
--
-- @since 0.1.0.0
modifyMVarMasked :: MonadUnliftIO m => MVar a -> (a -> m (a, b)) -> m b
modifyMVarMasked :: forall (m :: * -> *) a b.
MonadUnliftIO m =>
MVar a -> (a -> m (a, b)) -> m b
modifyMVarMasked MVar a
var a -> m (a, b)
f = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a b. MVar a -> (a -> IO (a, b)) -> IO b
M.modifyMVarMasked MVar a
var (forall a. m a -> IO a
run forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m (a, b)
f)

-- | Unlifted 'M.mkWeakMVar'.
--
-- @since 0.1.0.0
mkWeakMVar :: MonadUnliftIO m => MVar a -> m () -> m (Weak (MVar a))
mkWeakMVar :: forall (m :: * -> *) a.
MonadUnliftIO m =>
MVar a -> m () -> m (Weak (MVar a))
mkWeakMVar MVar a
var m ()
f = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a. MVar a -> IO () -> IO (Weak (MVar a))
M.mkWeakMVar MVar a
var (forall a. m a -> IO a
run m ()
f)