-- | 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 :: m (MVar a)
newEmptyMVar = IO (MVar a) -> m (MVar a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO (MVar a)
forall a. IO (MVar a)
M.newEmptyMVar

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

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

-- | Lifted 'M.putMVar'.
--
-- @since 0.1.0.0
putMVar :: MonadIO m => MVar a -> a -> m ()
putMVar :: MVar a -> a -> m ()
putMVar MVar a
var = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (a -> IO ()) -> a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MVar a -> a -> IO ()
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 :: MVar a -> m a
readMVar = IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> (MVar a -> IO a) -> MVar a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MVar a -> IO a
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 :: MVar a -> a -> m a
swapMVar MVar a
var = IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> (a -> IO a) -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MVar a -> a -> IO a
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 :: MVar a -> m (Maybe a)
tryTakeMVar = IO (Maybe a) -> m (Maybe a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe a) -> m (Maybe a))
-> (MVar a -> IO (Maybe a)) -> MVar a -> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MVar a -> IO (Maybe a)
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 :: MVar a -> a -> m Bool
tryPutMVar MVar a
var = IO Bool -> m Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> (a -> IO Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MVar a -> a -> IO Bool
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 :: MVar a -> m Bool
isEmptyMVar = IO Bool -> m Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> (MVar a -> IO Bool) -> MVar a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MVar a -> IO Bool
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 :: MVar a -> m (Maybe a)
tryReadMVar = IO (Maybe a) -> m (Maybe a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe a) -> m (Maybe a))
-> (MVar a -> IO (Maybe a)) -> MVar a -> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MVar a -> IO (Maybe a)
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 :: MVar a -> (a -> m b) -> m b
withMVar MVar a
var a -> m b
f = ((forall a. m a -> IO a) -> IO b) -> m b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO b) -> m b)
-> ((forall a. m a -> IO a) -> IO b) -> m b
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> MVar a -> (a -> IO b) -> IO b
forall a b. MVar a -> (a -> IO b) -> IO b
M.withMVar MVar a
var (m b -> IO b
forall a. m a -> IO a
run (m b -> IO b) -> (a -> m b) -> a -> IO b
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 :: MVar a -> (a -> m b) -> m b
withMVarMasked MVar a
var a -> m b
f = ((forall a. m a -> IO a) -> IO b) -> m b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO b) -> m b)
-> ((forall a. m a -> IO a) -> IO b) -> m b
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> MVar a -> (a -> IO b) -> IO b
forall a b. MVar a -> (a -> IO b) -> IO b
M.withMVarMasked MVar a
var (m b -> IO b
forall a. m a -> IO a
run (m b -> IO b) -> (a -> m b) -> a -> IO b
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_ :: MVar a -> (a -> m a) -> m ()
modifyMVar_ MVar a
var a -> m a
f = ((forall a. m a -> IO a) -> IO ()) -> m ()
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO ()) -> m ())
-> ((forall a. m a -> IO a) -> IO ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> MVar a -> (a -> IO a) -> IO ()
forall a. MVar a -> (a -> IO a) -> IO ()
M.modifyMVar_ MVar a
var (m a -> IO a
forall a. m a -> IO a
run (m a -> IO a) -> (a -> m a) -> a -> IO a
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 :: MVar a -> (a -> m (a, b)) -> m b
modifyMVar MVar a
var a -> m (a, b)
f = ((forall a. m a -> IO a) -> IO b) -> m b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO b) -> m b)
-> ((forall a. m a -> IO a) -> IO b) -> m b
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> MVar a -> (a -> IO (a, b)) -> IO b
forall a b. MVar a -> (a -> IO (a, b)) -> IO b
M.modifyMVar MVar a
var (m (a, b) -> IO (a, b)
forall a. m a -> IO a
run (m (a, b) -> IO (a, b)) -> (a -> m (a, b)) -> a -> IO (a, b)
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_ :: MVar a -> (a -> m a) -> m ()
modifyMVarMasked_ MVar a
var a -> m a
f = ((forall a. m a -> IO a) -> IO ()) -> m ()
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO ()) -> m ())
-> ((forall a. m a -> IO a) -> IO ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> MVar a -> (a -> IO a) -> IO ()
forall a. MVar a -> (a -> IO a) -> IO ()
M.modifyMVarMasked_ MVar a
var (m a -> IO a
forall a. m a -> IO a
run (m a -> IO a) -> (a -> m a) -> a -> IO a
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 :: MVar a -> (a -> m (a, b)) -> m b
modifyMVarMasked MVar a
var a -> m (a, b)
f = ((forall a. m a -> IO a) -> IO b) -> m b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO b) -> m b)
-> ((forall a. m a -> IO a) -> IO b) -> m b
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> MVar a -> (a -> IO (a, b)) -> IO b
forall a b. MVar a -> (a -> IO (a, b)) -> IO b
M.modifyMVarMasked MVar a
var (m (a, b) -> IO (a, b)
forall a. m a -> IO a
run (m (a, b) -> IO (a, b)) -> (a -> m (a, b)) -> a -> IO (a, b)
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 :: MVar a -> m () -> m (Weak (MVar a))
mkWeakMVar MVar a
var m ()
f = ((forall a. m a -> IO a) -> IO (Weak (MVar a)))
-> m (Weak (MVar a))
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO (Weak (MVar a)))
 -> m (Weak (MVar a)))
-> ((forall a. m a -> IO a) -> IO (Weak (MVar a)))
-> m (Weak (MVar a))
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> MVar a -> IO () -> IO (Weak (MVar a))
forall a. MVar a -> IO () -> IO (Weak (MVar a))
M.mkWeakMVar MVar a
var (m () -> IO ()
forall a. m a -> IO a
run m ()
f)