From 86b3857a4c1edafef817935ad3c5d63e6d2d3b25 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 29 Oct 2012 19:07:10 -0400 Subject: moved ThreadedMonad to Types --- Assistant/Monad.hs | 3 ++- Assistant/ThreadedMonad.hs | 38 -------------------------------------- Assistant/Threads/WebApp.hs | 2 +- Assistant/Types/ThreadedMonad.hs | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 40 deletions(-) delete mode 100644 Assistant/ThreadedMonad.hs create mode 100644 Assistant/Types/ThreadedMonad.hs (limited to 'Assistant') diff --git a/Assistant/Monad.hs b/Assistant/Monad.hs index 1f8ccacbe..c13d3a372 100644 --- a/Assistant/Monad.hs +++ b/Assistant/Monad.hs @@ -26,7 +26,7 @@ import "mtl" Control.Monad.Reader import Control.Monad.Base (liftBase, MonadBase) import Common.Annex -import Assistant.ThreadedMonad +import Assistant.Types.ThreadedMonad import Assistant.DaemonStatus import Assistant.ScanRemotes import Assistant.TransferQueue @@ -112,6 +112,7 @@ asIO2 a = do (<<~) :: (a -> IO b) -> (AssistantData -> a) -> Assistant b io <<~ v = reader v >>= liftIO . io +withAssistant :: (AssistantData -> a) -> (a -> IO b) -> Assistant b withAssistant v io = io <<~ v daemonStatus :: Assistant DaemonStatus diff --git a/Assistant/ThreadedMonad.hs b/Assistant/ThreadedMonad.hs deleted file mode 100644 index 7b915e12c..000000000 --- a/Assistant/ThreadedMonad.hs +++ /dev/null @@ -1,38 +0,0 @@ -{- making the Annex monad available across threads - - - - Copyright 2012 Joey Hess - - - - Licensed under the GNU GPL version 3 or higher. - -} - -module Assistant.ThreadedMonad where - -import Common.Annex -import qualified Annex - -import Control.Concurrent -import Data.Tuple - -{- The Annex state is stored in a MVar, so that threaded actions can access - - it. -} -type ThreadState = MVar Annex.AnnexState - -{- Stores the Annex state in a MVar. - - - - Once the action is finished, retrieves the state from the MVar. - -} -withThreadState :: (ThreadState -> Annex a) -> Annex a -withThreadState a = do - state <- Annex.getState id - mvar <- liftIO $ newMVar state - r <- a mvar - newstate <- liftIO $ takeMVar mvar - Annex.changeState (const newstate) - return r - -{- Runs an Annex action, using the state from the MVar. - - - - This serializes calls by threads; only one thread can run in Annex at a - - time. -} -runThreadState :: ThreadState -> Annex a -> IO a -runThreadState mvar a = modifyMVar mvar $ \state -> swap <$> Annex.run state a diff --git a/Assistant/Threads/WebApp.hs b/Assistant/Threads/WebApp.hs index 126c78166..be9a9a16f 100644 --- a/Assistant/Threads/WebApp.hs +++ b/Assistant/Threads/WebApp.hs @@ -27,7 +27,7 @@ import Assistant.WebApp.Configurators.S3 import Assistant.WebApp.Configurators.XMPP import Assistant.WebApp.Documentation import Assistant.WebApp.OtherRepos -import Assistant.ThreadedMonad +import Assistant.Types.ThreadedMonad import Utility.WebApp import Utility.FileMode import Utility.TempFile diff --git a/Assistant/Types/ThreadedMonad.hs b/Assistant/Types/ThreadedMonad.hs new file mode 100644 index 000000000..1a2aa7eb7 --- /dev/null +++ b/Assistant/Types/ThreadedMonad.hs @@ -0,0 +1,38 @@ +{- making the Annex monad available across threads + - + - Copyright 2012 Joey Hess + - + - Licensed under the GNU GPL version 3 or higher. + -} + +module Assistant.Types.ThreadedMonad where + +import Common.Annex +import qualified Annex + +import Control.Concurrent +import Data.Tuple + +{- The Annex state is stored in a MVar, so that threaded actions can access + - it. -} +type ThreadState = MVar Annex.AnnexState + +{- Stores the Annex state in a MVar. + - + - Once the action is finished, retrieves the state from the MVar. + -} +withThreadState :: (ThreadState -> Annex a) -> Annex a +withThreadState a = do + state <- Annex.getState id + mvar <- liftIO $ newMVar state + r <- a mvar + newstate <- liftIO $ takeMVar mvar + Annex.changeState (const newstate) + return r + +{- Runs an Annex action, using the state from the MVar. + - + - This serializes calls by threads; only one thread can run in Annex at a + - time. -} +runThreadState :: ThreadState -> Annex a -> IO a +runThreadState mvar a = modifyMVar mvar $ \state -> swap <$> Annex.run state a -- cgit v1.2.3