Skip to content

Commit

Permalink
Use evaluate instead of seq and add a few INLINE pragmas to MVar modules
Browse files Browse the repository at this point in the history
  • Loading branch information
arybczak committed Aug 22, 2024
1 parent 0ef04f2 commit ac6acda
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
6 changes: 6 additions & 0 deletions effectful/src/Effectful/Concurrent/MVar.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,37 @@ tryReadMVar = unsafeEff_ . M.tryReadMVar
withMVar :: Concurrent :> es => MVar a -> (a -> Eff es b) -> Eff es b
withMVar var f = reallyUnsafeUnliftIO $ \unlift -> do
M.withMVar var $ unlift . f
{-# INLINE withMVar #-}

-- | Lifted 'M.withMVarMasked'.
withMVarMasked :: Concurrent :> es => MVar a -> (a -> Eff es b) -> Eff es b
withMVarMasked var f = reallyUnsafeUnliftIO $ \unlift -> do
M.withMVarMasked var $ unlift . f
{-# INLINE withMVarMasked #-}

-- | Lifted 'M.modifyMVar_'.
modifyMVar_ :: Concurrent :> es => MVar a -> (a -> Eff es a) -> Eff es ()
modifyMVar_ var f = reallyUnsafeUnliftIO $ \unlift -> do
M.modifyMVar_ var $ unlift . f
{-# INLINE modifyMVar_ #-}

-- | Lifted 'M.modifyMVar'.
modifyMVar :: Concurrent :> es => MVar a -> (a -> Eff es (a, b)) -> Eff es b
modifyMVar var f = reallyUnsafeUnliftIO $ \unlift -> do
M.modifyMVar var $ unlift . f
{-# INLINE modifyMVar #-}

-- | Lifted 'M.modifyMVarMasked_'.
modifyMVarMasked_ :: Concurrent :> es => MVar a -> (a -> Eff es a) -> Eff es ()
modifyMVarMasked_ var f = reallyUnsafeUnliftIO $ \unlift -> do
M.modifyMVarMasked_ var $ unlift . f
{-# INLINE modifyMVarMasked_ #-}

-- | Lifted 'M.modifyMVarMasked'.
modifyMVarMasked :: Concurrent :> es => MVar a -> (a -> Eff es (a, b)) -> Eff es b
modifyMVarMasked var f = reallyUnsafeUnliftIO $ \unlift -> do
M.modifyMVarMasked var $ unlift . f
{-# INLINE modifyMVarMasked #-}

-- | Lifted 'M.mkWeakMVar'.
mkWeakMVar :: Concurrent :> es => MVar a -> Eff es () -> Eff es (Weak (MVar a))
Expand Down
25 changes: 16 additions & 9 deletions effectful/src/Effectful/Concurrent/MVar/Strict.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Effectful.Concurrent.MVar.Strict
) where

import System.Mem.Weak (Weak)
import Control.Exception (evaluate)
import Control.Concurrent.MVar (MVar)
import Control.Concurrent.MVar qualified as M

Expand All @@ -44,31 +45,31 @@ newEmptyMVar = unsafeEff_ M.newEmptyMVar

-- | Lifted 'M.newMVar' that evaluates the value to WHNF.
newMVar :: Concurrent :> es => a -> Eff es (MVar a)
newMVar a = unsafeEff_ $ a `seq` M.newMVar a
newMVar a = unsafeEff_ $ M.newMVar =<< evaluate a

-- | Lifted 'M.takeMVar'.
takeMVar :: Concurrent :> es => MVar a -> Eff es a
takeMVar = unsafeEff_ . M.takeMVar

-- | Lifted 'M.putMVar'.
putMVar :: Concurrent :> es => MVar a -> a -> Eff es ()
putMVar var a = unsafeEff_ $ a `seq` M.putMVar var a
putMVar var a = unsafeEff_ $ M.putMVar var =<< evaluate a

-- | Lifted 'M.readMVar'.
readMVar :: Concurrent :> es => MVar a -> Eff es a
readMVar = unsafeEff_ . M.readMVar

-- | Lifted 'M.swapMVar' that evaluates the new value to WHNF.
swapMVar :: Concurrent :> es => MVar a -> a -> Eff es a
swapMVar var a = unsafeEff_ $ a `seq` M.swapMVar var a
swapMVar var a = unsafeEff_ $ M.swapMVar var =<< evaluate a

-- | Lifted 'M.tryTakeMVar'.
tryTakeMVar :: Concurrent :> es => MVar a -> Eff es (Maybe a)
tryTakeMVar = unsafeEff_ . M.tryTakeMVar

-- | Lifted 'M.tryPutMVar' that evaluates the new value to WHNF.
tryPutMVar :: Concurrent :> es => MVar a -> a -> Eff es Bool
tryPutMVar var a = unsafeEff_ $ a `seq` M.tryPutMVar var a
tryPutMVar var a = unsafeEff_ $ M.tryPutMVar var =<< evaluate a

-- | Lifted 'M.isEmptyMVar'.
isEmptyMVar :: Concurrent :> es => MVar a -> Eff es Bool
Expand All @@ -82,39 +83,45 @@ tryReadMVar = unsafeEff_ . M.tryReadMVar
withMVar :: Concurrent :> es => MVar a -> (a -> Eff es b) -> Eff es b
withMVar var f = reallyUnsafeUnliftIO $ \unlift -> do
M.withMVar var $ unlift . f
{-# INLINE withMVar #-}

-- | Lifted 'M.withMVarMasked'.
withMVarMasked :: Concurrent :> es => MVar a -> (a -> Eff es b) -> Eff es b
withMVarMasked var f = reallyUnsafeUnliftIO $ \unlift -> do
M.withMVarMasked var $ unlift . f
{-# INLINE withMVarMasked #-}

-- | Lifted 'M.modifyMVar_' that evaluates the new value to WHNF.
modifyMVar_ :: Concurrent :> es => MVar a -> (a -> Eff es a) -> Eff es ()
modifyMVar_ var f = reallyUnsafeUnliftIO $ \unlift -> do
M.modifyMVar_ var $ \a0 -> do
a <- unlift $ f a0
a `seq` pure a
evaluate a
{-# INLINE modifyMVar_ #-}

-- | Lifted 'M.modifyMVar' that evaluates the new value to WHNF.
modifyMVar :: Concurrent :> es => MVar a -> (a -> Eff es (a, b)) -> Eff es b
modifyMVar var f = reallyUnsafeUnliftIO $ \unlift -> do
M.modifyMVar var $ \a0 -> do
(a, b) <- unlift $ f a0
a `seq` pure (a, b)
(, b) <$> evaluate a
{-# INLINE modifyMVar #-}

-- | Lifted 'M.modifyMVarMasked_' that evaluates the new value to WHNF.
modifyMVarMasked_ :: Concurrent :> es => MVar a -> (a -> Eff es a) -> Eff es ()
modifyMVarMasked_ var f = reallyUnsafeUnliftIO $ \unlift -> do
M.modifyMVarMasked_ var $ \a0 -> do
a <- unlift $ f a0
a `seq` pure a
evaluate a
{-# INLINE modifyMVarMasked_ #-}

-- | Lifted 'M.modifyMVarMasked' that evaluates the new value to WHNF.
modifyMVarMasked :: Concurrent :> es => MVar a -> (a -> Eff es (a, b)) -> Eff es b
modifyMVarMasked var f = reallyUnsafeUnliftIO $ \unlift -> do
M.modifyMVarMasked var $ \a0 -> do
a <- unlift $ f a0
a `seq` pure a
(a, b) <- unlift $ f a0
(, b) <$> evaluate a
{-# INLINE modifyMVarMasked #-}

-- | Lifted 'M.mkWeakMVar'.
mkWeakMVar :: Concurrent :> es => MVar a -> Eff es () -> Eff es (Weak (MVar a))
Expand Down

0 comments on commit ac6acda

Please sign in to comment.