convert TMVars that are never left empty into TVars
This is probably more efficient, and it avoids mistakenly leaving them empty.
This commit is contained in:
parent
b025500352
commit
166d70db77
6 changed files with 46 additions and 62 deletions
|
@ -30,7 +30,7 @@ import qualified Data.Set as S
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
|
||||||
getDaemonStatus :: Assistant DaemonStatus
|
getDaemonStatus :: Assistant DaemonStatus
|
||||||
getDaemonStatus = (atomically . readTMVar) <<~ daemonStatusHandle
|
getDaemonStatus = (atomically . readTVar) <<~ daemonStatusHandle
|
||||||
|
|
||||||
modifyDaemonStatus_ :: (DaemonStatus -> DaemonStatus) -> Assistant ()
|
modifyDaemonStatus_ :: (DaemonStatus -> DaemonStatus) -> Assistant ()
|
||||||
modifyDaemonStatus_ a = modifyDaemonStatus $ \s -> (a s, ())
|
modifyDaemonStatus_ a = modifyDaemonStatus $ \s -> (a s, ())
|
||||||
|
@ -40,8 +40,8 @@ modifyDaemonStatus a = do
|
||||||
dstatus <- getAssistant daemonStatusHandle
|
dstatus <- getAssistant daemonStatusHandle
|
||||||
liftIO $ do
|
liftIO $ do
|
||||||
(s, b) <- atomically $ do
|
(s, b) <- atomically $ do
|
||||||
r@(!s, _) <- a <$> takeTMVar dstatus
|
r@(!s, _) <- a <$> readTVar dstatus
|
||||||
putTMVar dstatus s
|
writeTVar dstatus s
|
||||||
return r
|
return r
|
||||||
sendNotification $ changeNotifier s
|
sendNotification $ changeNotifier s
|
||||||
return b
|
return b
|
||||||
|
@ -102,7 +102,7 @@ startDaemonStatus = do
|
||||||
flip catchDefaultIO (readDaemonStatusFile file) =<< newDaemonStatus
|
flip catchDefaultIO (readDaemonStatusFile file) =<< newDaemonStatus
|
||||||
transfers <- M.fromList <$> getTransfers
|
transfers <- M.fromList <$> getTransfers
|
||||||
addsync <- calcSyncRemotes
|
addsync <- calcSyncRemotes
|
||||||
liftIO $ atomically $ newTMVar $ addsync $ status
|
liftIO $ atomically $ newTVar $ addsync $ status
|
||||||
{ scanComplete = False
|
{ scanComplete = False
|
||||||
, sanityCheckRunning = False
|
, sanityCheckRunning = False
|
||||||
, currentTransfers = transfers
|
, currentTransfers = transfers
|
||||||
|
@ -162,14 +162,14 @@ tenMinutes = 10 * 60
|
||||||
- to the caller. -}
|
- to the caller. -}
|
||||||
adjustTransfersSTM :: DaemonStatusHandle -> (TransferMap -> TransferMap) -> STM ()
|
adjustTransfersSTM :: DaemonStatusHandle -> (TransferMap -> TransferMap) -> STM ()
|
||||||
adjustTransfersSTM dstatus a = do
|
adjustTransfersSTM dstatus a = do
|
||||||
s <- takeTMVar dstatus
|
s <- readTVar dstatus
|
||||||
let !v = a (currentTransfers s)
|
let !v = a (currentTransfers s)
|
||||||
putTMVar dstatus $ s { currentTransfers = v }
|
writeTVar dstatus $ s { currentTransfers = v }
|
||||||
|
|
||||||
{- Checks if a transfer is currently running. -}
|
{- Checks if a transfer is currently running. -}
|
||||||
checkRunningTransferSTM :: DaemonStatusHandle -> Transfer -> STM Bool
|
checkRunningTransferSTM :: DaemonStatusHandle -> Transfer -> STM Bool
|
||||||
checkRunningTransferSTM dstatus t = M.member t . currentTransfers
|
checkRunningTransferSTM dstatus t = M.member t . currentTransfers
|
||||||
<$> readTMVar dstatus
|
<$> readTVar dstatus
|
||||||
|
|
||||||
{- Alters a transfer's info, if the transfer is in the map. -}
|
{- Alters a transfer's info, if the transfer is in the map. -}
|
||||||
alterTransferInfo :: Transfer -> (TransferInfo -> TransferInfo) -> Assistant ()
|
alterTransferInfo :: Transfer -> (TransferInfo -> TransferInfo) -> Assistant ()
|
||||||
|
@ -207,14 +207,14 @@ notifyTransfer :: Assistant ()
|
||||||
notifyTransfer = do
|
notifyTransfer = do
|
||||||
dstatus <- getAssistant daemonStatusHandle
|
dstatus <- getAssistant daemonStatusHandle
|
||||||
liftIO $ sendNotification
|
liftIO $ sendNotification
|
||||||
=<< transferNotifier <$> atomically (readTMVar dstatus)
|
=<< transferNotifier <$> atomically (readTVar dstatus)
|
||||||
|
|
||||||
{- Send a notification when alerts are changed. -}
|
{- Send a notification when alerts are changed. -}
|
||||||
notifyAlert :: Assistant ()
|
notifyAlert :: Assistant ()
|
||||||
notifyAlert = do
|
notifyAlert = do
|
||||||
dstatus <- getAssistant daemonStatusHandle
|
dstatus <- getAssistant daemonStatusHandle
|
||||||
liftIO $ sendNotification
|
liftIO $ sendNotification
|
||||||
=<< alertNotifier <$> atomically (readTMVar dstatus)
|
=<< alertNotifier <$> atomically (readTVar dstatus)
|
||||||
|
|
||||||
{- Returns the alert's identifier, which can be used to remove it. -}
|
{- Returns the alert's identifier, which can be used to remove it. -}
|
||||||
addAlert :: Alert -> Assistant AlertId
|
addAlert :: Alert -> Assistant AlertId
|
||||||
|
|
|
@ -86,8 +86,7 @@ data DaemonStatus = DaemonStatus
|
||||||
|
|
||||||
type TransferMap = M.Map Transfer TransferInfo
|
type TransferMap = M.Map Transfer TransferInfo
|
||||||
|
|
||||||
{- This TMVar is never left empty, so accessing it will never block. -}
|
type DaemonStatusHandle = TVar DaemonStatus
|
||||||
type DaemonStatusHandle = TMVar DaemonStatus
|
|
||||||
|
|
||||||
newDaemonStatus :: IO DaemonStatus
|
newDaemonStatus :: IO DaemonStatus
|
||||||
newDaemonStatus = DaemonStatus
|
newDaemonStatus = DaemonStatus
|
||||||
|
|
|
@ -13,8 +13,7 @@ import Assistant.Types.DaemonStatus
|
||||||
|
|
||||||
import Control.Concurrent.STM hiding (check)
|
import Control.Concurrent.STM hiding (check)
|
||||||
|
|
||||||
{- This TMVar is never left empty. -}
|
type TransferrerPool = TVar (MkCheckTransferrer, [TransferrerPoolItem])
|
||||||
type TransferrerPool = TMVar (MkCheckTransferrer, [TransferrerPoolItem])
|
|
||||||
|
|
||||||
type CheckTransferrer = IO Bool
|
type CheckTransferrer = IO Bool
|
||||||
type MkCheckTransferrer = IO (IO Bool)
|
type MkCheckTransferrer = IO (IO Bool)
|
||||||
|
@ -31,24 +30,22 @@ data Transferrer = Transferrer
|
||||||
}
|
}
|
||||||
|
|
||||||
newTransferrerPool :: MkCheckTransferrer -> IO TransferrerPool
|
newTransferrerPool :: MkCheckTransferrer -> IO TransferrerPool
|
||||||
newTransferrerPool c = newTMVarIO (c, [])
|
newTransferrerPool c = newTVarIO (c, [])
|
||||||
|
|
||||||
popTransferrerPool :: TransferrerPool -> STM (Maybe TransferrerPoolItem, Int)
|
popTransferrerPool :: TransferrerPool -> STM (Maybe TransferrerPoolItem, Int)
|
||||||
popTransferrerPool p = do
|
popTransferrerPool p = do
|
||||||
(c, l) <- takeTMVar p
|
(c, l) <- readTVar p
|
||||||
case l of
|
case l of
|
||||||
[] -> do
|
[] -> return (Nothing, 0)
|
||||||
putTMVar p (c, [])
|
|
||||||
return (Nothing, 0)
|
|
||||||
(i:is) -> do
|
(i:is) -> do
|
||||||
putTMVar p (c, is)
|
writeTVar p (c, is)
|
||||||
return $ (Just i, length is)
|
return $ (Just i, length is)
|
||||||
|
|
||||||
pushTransferrerPool :: TransferrerPool -> TransferrerPoolItem -> STM ()
|
pushTransferrerPool :: TransferrerPool -> TransferrerPoolItem -> STM ()
|
||||||
pushTransferrerPool p i = do
|
pushTransferrerPool p i = do
|
||||||
(c, l) <- takeTMVar p
|
(c, l) <- readTVar p
|
||||||
let l' = i:l
|
let l' = i:l
|
||||||
putTMVar p (c, l')
|
writeTVar p (c, l')
|
||||||
|
|
||||||
{- Note that making a CheckTransferrer may allocate resources,
|
{- Note that making a CheckTransferrer may allocate resources,
|
||||||
- such as a NotificationHandle, so it's important that the returned
|
- such as a NotificationHandle, so it's important that the returned
|
||||||
|
@ -56,12 +53,12 @@ pushTransferrerPool p i = do
|
||||||
- garbage collected. -}
|
- garbage collected. -}
|
||||||
mkTransferrerPoolItem :: TransferrerPool -> Transferrer -> IO TransferrerPoolItem
|
mkTransferrerPoolItem :: TransferrerPool -> Transferrer -> IO TransferrerPoolItem
|
||||||
mkTransferrerPoolItem p t = do
|
mkTransferrerPoolItem p t = do
|
||||||
mkcheck <- atomically $ fst <$> readTMVar p
|
mkcheck <- atomically $ fst <$> readTVar p
|
||||||
check <- mkcheck
|
check <- mkcheck
|
||||||
return $ TransferrerPoolItem (Just t) check
|
return $ TransferrerPoolItem (Just t) check
|
||||||
|
|
||||||
checkNetworkConnections :: DaemonStatusHandle -> MkCheckTransferrer
|
checkNetworkConnections :: DaemonStatusHandle -> MkCheckTransferrer
|
||||||
checkNetworkConnections dstatushandle = do
|
checkNetworkConnections dstatushandle = do
|
||||||
dstatus <- atomically $ readTMVar dstatushandle
|
dstatus <- atomically $ readTVar dstatushandle
|
||||||
h <- newNotificationHandle False (networkConnectedNotifier dstatus)
|
h <- newNotificationHandle False (networkConnectedNotifier dstatus)
|
||||||
return $ not <$> checkNotification h
|
return $ not <$> checkNotification h
|
||||||
|
|
|
@ -154,7 +154,7 @@ firstRun o = do
|
||||||
- threadstate. -}
|
- threadstate. -}
|
||||||
let st = error "annex state not available"
|
let st = error "annex state not available"
|
||||||
{- Get a DaemonStatus without running in the Annex monad. -}
|
{- Get a DaemonStatus without running in the Annex monad. -}
|
||||||
dstatus <- atomically . newTMVar =<< newDaemonStatus
|
dstatus <- atomically . newTVar =<< newDaemonStatus
|
||||||
d <- newAssistantData st dstatus
|
d <- newAssistantData st dstatus
|
||||||
urlrenderer <- newUrlRenderer
|
urlrenderer <- newUrlRenderer
|
||||||
v <- newEmptyMVar
|
v <- newEmptyMVar
|
||||||
|
|
|
@ -127,7 +127,7 @@ externalSetup mu _ c gc = do
|
||||||
INITREMOTE_FAILURE errmsg -> Just $ error errmsg
|
INITREMOTE_FAILURE errmsg -> Just $ error errmsg
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
withExternalState external $
|
withExternalState external $
|
||||||
liftIO . atomically . readTMVar . externalConfig
|
liftIO . atomically . readTVar . externalConfig
|
||||||
|
|
||||||
gitConfigSpecialRemote u c'' "externaltype" externaltype
|
gitConfigSpecialRemote u c'' "externaltype" externaltype
|
||||||
return (c'', u)
|
return (c'', u)
|
||||||
|
@ -234,24 +234,22 @@ handleRequest' st external req mp responsehandler
|
||||||
handleRemoteRequest (DIRHASH_LOWER k) =
|
handleRemoteRequest (DIRHASH_LOWER k) =
|
||||||
send $ VALUE $ hashDirLower def k
|
send $ VALUE $ hashDirLower def k
|
||||||
handleRemoteRequest (SETCONFIG setting value) =
|
handleRemoteRequest (SETCONFIG setting value) =
|
||||||
liftIO $ atomically $ do
|
liftIO $ atomically $ modifyTVar' (externalConfig st) $
|
||||||
let v = externalConfig st
|
M.insert setting value
|
||||||
m <- takeTMVar v
|
|
||||||
putTMVar v $ M.insert setting value m
|
|
||||||
handleRemoteRequest (GETCONFIG setting) = do
|
handleRemoteRequest (GETCONFIG setting) = do
|
||||||
value <- fromMaybe "" . M.lookup setting
|
value <- fromMaybe "" . M.lookup setting
|
||||||
<$> liftIO (atomically $ readTMVar $ externalConfig st)
|
<$> liftIO (atomically $ readTVar $ externalConfig st)
|
||||||
send $ VALUE value
|
send $ VALUE value
|
||||||
handleRemoteRequest (SETCREDS setting login password) = do
|
handleRemoteRequest (SETCREDS setting login password) = do
|
||||||
let v = externalConfig st
|
let v = externalConfig st
|
||||||
c <- liftIO $ atomically $ readTMVar v
|
c <- liftIO $ atomically $ readTVar v
|
||||||
let gc = externalGitConfig external
|
let gc = externalGitConfig external
|
||||||
c' <- setRemoteCredPair encryptionAlreadySetup c gc
|
c' <- setRemoteCredPair encryptionAlreadySetup c gc
|
||||||
(credstorage setting)
|
(credstorage setting)
|
||||||
(Just (login, password))
|
(Just (login, password))
|
||||||
void $ liftIO $ atomically $ swapTMVar v c'
|
void $ liftIO $ atomically $ swapTVar v c'
|
||||||
handleRemoteRequest (GETCREDS setting) = do
|
handleRemoteRequest (GETCREDS setting) = do
|
||||||
c <- liftIO $ atomically $ readTMVar $ externalConfig st
|
c <- liftIO $ atomically $ readTVar $ externalConfig st
|
||||||
let gc = externalGitConfig external
|
let gc = externalGitConfig external
|
||||||
creds <- fromMaybe ("", "") <$>
|
creds <- fromMaybe ("", "") <$>
|
||||||
getRemoteCredPair c gc (credstorage setting)
|
getRemoteCredPair c gc (credstorage setting)
|
||||||
|
@ -356,19 +354,15 @@ withExternalState external = bracket alloc dealloc
|
||||||
|
|
||||||
alloc = do
|
alloc = do
|
||||||
ms <- liftIO $ atomically $ do
|
ms <- liftIO $ atomically $ do
|
||||||
l <- takeTMVar v
|
l <- readTVar v
|
||||||
case l of
|
case l of
|
||||||
[] -> do
|
[] -> return Nothing
|
||||||
putTMVar v l
|
|
||||||
return Nothing
|
|
||||||
(st:rest) -> do
|
(st:rest) -> do
|
||||||
putTMVar v rest
|
writeTVar v rest
|
||||||
return (Just st)
|
return (Just st)
|
||||||
maybe (startExternal external) return ms
|
maybe (startExternal external) return ms
|
||||||
|
|
||||||
dealloc st = liftIO $ atomically $ do
|
dealloc st = liftIO $ atomically $ modifyTVar' v (st:)
|
||||||
l <- takeTMVar v
|
|
||||||
putTMVar v (st:l)
|
|
||||||
|
|
||||||
{- Starts an external remote process running, and checks VERSION. -}
|
{- Starts an external remote process running, and checks VERSION. -}
|
||||||
startExternal :: External -> Annex ExternalState
|
startExternal :: External -> Annex ExternalState
|
||||||
|
@ -396,11 +390,11 @@ startExternal external = do
|
||||||
fileEncoding herr
|
fileEncoding herr
|
||||||
stderrelay <- async $ errrelayer herr
|
stderrelay <- async $ errrelayer herr
|
||||||
checkearlytermination =<< getProcessExitCode ph
|
checkearlytermination =<< getProcessExitCode ph
|
||||||
cv <- newTMVarIO $ externalDefaultConfig external
|
cv <- newTVarIO $ externalDefaultConfig external
|
||||||
pv <- newTMVarIO Unprepared
|
pv <- newTVarIO Unprepared
|
||||||
pid <- atomically $ do
|
pid <- atomically $ do
|
||||||
n <- succ <$> takeTMVar (externalLastPid external)
|
n <- succ <$> readTVar (externalLastPid external)
|
||||||
putTMVar (externalLastPid external) n
|
writeTVar (externalLastPid external) n
|
||||||
return n
|
return n
|
||||||
return $ ExternalState
|
return $ ExternalState
|
||||||
{ externalSend = hin
|
{ externalSend = hin
|
||||||
|
@ -431,17 +425,13 @@ startExternal external = do
|
||||||
|
|
||||||
stopExternal :: External -> Annex ()
|
stopExternal :: External -> Annex ()
|
||||||
stopExternal external = liftIO $ do
|
stopExternal external = liftIO $ do
|
||||||
l <- atomically $ do
|
l <- atomically $ swapTVar (externalState external) []
|
||||||
l <- takeTMVar v
|
|
||||||
putTMVar v []
|
|
||||||
return l
|
|
||||||
mapM_ stop l
|
mapM_ stop l
|
||||||
where
|
where
|
||||||
stop st = do
|
stop st = do
|
||||||
hClose $ externalSend st
|
hClose $ externalSend st
|
||||||
hClose $ externalReceive st
|
hClose $ externalReceive st
|
||||||
externalShutdown st
|
externalShutdown st
|
||||||
v = externalState external
|
|
||||||
|
|
||||||
externalRemoteProgram :: ExternalType -> String
|
externalRemoteProgram :: ExternalType -> String
|
||||||
externalRemoteProgram externaltype = "git-annex-remote-" ++ externaltype
|
externalRemoteProgram externaltype = "git-annex-remote-" ++ externaltype
|
||||||
|
@ -459,7 +449,7 @@ checkVersion _ _ _ = Nothing
|
||||||
- the error message. -}
|
- the error message. -}
|
||||||
checkPrepared :: ExternalState -> External -> Annex ()
|
checkPrepared :: ExternalState -> External -> Annex ()
|
||||||
checkPrepared st external = do
|
checkPrepared st external = do
|
||||||
v <- liftIO $ atomically $ readTMVar $ externalPrepared st
|
v <- liftIO $ atomically $ readTVar $ externalPrepared st
|
||||||
case v of
|
case v of
|
||||||
Prepared -> noop
|
Prepared -> noop
|
||||||
FailedPrepare errmsg -> error errmsg
|
FailedPrepare errmsg -> error errmsg
|
||||||
|
@ -474,7 +464,7 @@ checkPrepared st external = do
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
where
|
where
|
||||||
setprepared status = liftIO $ atomically $ void $
|
setprepared status = liftIO $ atomically $ void $
|
||||||
swapTMVar (externalPrepared st) status
|
swapTVar (externalPrepared st) status
|
||||||
|
|
||||||
{- Caches the cost in the git config to avoid needing to start up an
|
{- Caches the cost in the git config to avoid needing to start up an
|
||||||
- external special remote every time time just to ask it what its
|
- external special remote every time time just to ask it what its
|
||||||
|
|
18
Remote/External/Types.hs
vendored
18
Remote/External/Types.hs
vendored
|
@ -45,10 +45,10 @@ import Network.URI
|
||||||
data External = External
|
data External = External
|
||||||
{ externalType :: ExternalType
|
{ externalType :: ExternalType
|
||||||
, externalUUID :: UUID
|
, externalUUID :: UUID
|
||||||
, externalState :: TMVar [ExternalState]
|
, externalState :: TVar [ExternalState]
|
||||||
-- ^ TMVar is never left empty; list contains states for external
|
-- ^ Contains states for external special remote processes
|
||||||
-- special remote processes that are not currently in use.
|
-- that are not currently in use.
|
||||||
, externalLastPid :: TMVar PID
|
, externalLastPid :: TVar PID
|
||||||
, externalDefaultConfig :: RemoteConfig
|
, externalDefaultConfig :: RemoteConfig
|
||||||
, externalGitConfig :: RemoteGitConfig
|
, externalGitConfig :: RemoteGitConfig
|
||||||
}
|
}
|
||||||
|
@ -57,8 +57,8 @@ newExternal :: ExternalType -> UUID -> RemoteConfig -> RemoteGitConfig -> Annex
|
||||||
newExternal externaltype u c gc = liftIO $ External
|
newExternal externaltype u c gc = liftIO $ External
|
||||||
<$> pure externaltype
|
<$> pure externaltype
|
||||||
<*> pure u
|
<*> pure u
|
||||||
<*> atomically (newTMVar [])
|
<*> atomically (newTVar [])
|
||||||
<*> atomically (newTMVar 0)
|
<*> atomically (newTVar 0)
|
||||||
<*> pure c
|
<*> pure c
|
||||||
<*> pure gc
|
<*> pure gc
|
||||||
|
|
||||||
|
@ -69,10 +69,8 @@ data ExternalState = ExternalState
|
||||||
, externalReceive :: Handle
|
, externalReceive :: Handle
|
||||||
, externalShutdown :: IO ()
|
, externalShutdown :: IO ()
|
||||||
, externalPid :: PID
|
, externalPid :: PID
|
||||||
, externalPrepared :: TMVar PrepareStatus
|
, externalPrepared :: TVar PrepareStatus
|
||||||
-- ^ Never left empty.
|
, externalConfig :: TVar RemoteConfig
|
||||||
, externalConfig :: TMVar RemoteConfig
|
|
||||||
-- ^ Never left empty.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type PID = Int
|
type PID = Int
|
||||||
|
|
Loading…
Reference in a new issue