modifyMVar_ catches exceptions, so no need to roll my own
This commit is contained in:
Joey Hess 2012-06-28 23:40:16 -04:00
parent 247099f628
commit 397117429c

View file

@ -5,15 +5,13 @@
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
{-# LANGUAGE BangPatterns #-}
module Assistant.ThreadedMonad where module Assistant.ThreadedMonad where
import Common.Annex import Common.Annex
import qualified Annex import qualified Annex
import Control.Concurrent import Control.Concurrent
import Control.Exception (throw) import Data.Tuple
{- The Annex state is stored in a MVar, so that threaded actions can access {- The Annex state is stored in a MVar, so that threaded actions can access
- it. -} - it. -}
@ -37,11 +35,4 @@ withThreadState a = do
- This serializes calls by threads; only one thread can run in Annex at a - This serializes calls by threads; only one thread can run in Annex at a
- time. -} - time. -}
runThreadState :: ThreadState -> Annex a -> IO a runThreadState :: ThreadState -> Annex a -> IO a
runThreadState mvar a = do runThreadState mvar a = modifyMVar mvar $ \state -> swap <$> Annex.run state a
startstate <- takeMVar mvar
-- catch IO errors and rethrow after restoring the MVar
!(r, newstate) <- catchIO (Annex.run startstate a) $ \e -> do
putMVar mvar startstate
throw e
putMVar mvar newstate
return r