git-annex/Annex/LockFile.hs

117 lines
3.2 KiB
Haskell
Raw Normal View History

2014-07-10 04:32:23 +00:00
{- git-annex lock files.
-
- Copyright 2012-2019 Joey Hess <id@joeyh.name>
2014-07-10 04:32:23 +00:00
-
- Licensed under the GNU AGPL version 3 or higher.
2014-07-10 04:32:23 +00:00
-}
{-# LANGUAGE CPP #-}
module Annex.LockFile (
lockFileCached,
2014-07-10 04:32:23 +00:00
unlockFile,
getLockCache,
fromLockCache,
withSharedLock,
2014-07-10 04:32:23 +00:00
withExclusiveLock,
takeExclusiveLock,
tryExclusiveLock,
2014-07-10 04:32:23 +00:00
) where
import Annex.Common
2014-07-10 04:32:23 +00:00
import Annex
import Types.LockCache
2014-07-10 04:32:23 +00:00
import qualified Git
2014-07-14 19:55:48 +00:00
import Annex.Perms
import Annex.LockPool
2014-07-10 04:32:23 +00:00
import qualified Data.Map as M
{- Create a specified lock file, and takes a shared lock, which is retained
- in the cache. -}
lockFileCached :: FilePath -> Annex ()
lockFileCached file = go =<< fromLockCache file
2014-07-10 04:32:23 +00:00
where
go (Just _) = noop -- already locked
go Nothing = do
#ifndef mingw32_HOST_OS
mode <- annexFileMode
lockhandle <- noUmask mode $ lockShared (Just mode) file
2014-07-10 04:32:23 +00:00
#else
lockhandle <- liftIO $ waitToLock $ lockShared file
#endif
changeLockCache $ M.insert file lockhandle
2014-07-10 04:32:23 +00:00
unlockFile :: FilePath -> Annex ()
unlockFile file = maybe noop go =<< fromLockCache file
2014-07-10 04:32:23 +00:00
where
go lockhandle = do
liftIO $ dropLock lockhandle
changeLockCache $ M.delete file
2014-07-10 04:32:23 +00:00
getLockCache :: Annex LockCache
getLockCache = getState lockcache
2014-07-10 04:32:23 +00:00
fromLockCache :: FilePath -> Annex (Maybe LockHandle)
fromLockCache file = M.lookup file <$> getLockCache
2014-07-10 04:32:23 +00:00
changeLockCache :: (LockCache -> LockCache) -> Annex ()
changeLockCache a = do
m <- getLockCache
changeState $ \s -> s { lockcache = a m }
2014-07-10 04:32:23 +00:00
{- Runs an action with a shared lock held. If an exclusive lock is held,
- blocks until it becomes free. -}
withSharedLock :: (Git.Repo -> FilePath) -> Annex a -> Annex a
withSharedLock getlockfile a = debugLocks $ do
lockfile <- fromRepo getlockfile
createAnnexDirectory $ takeDirectory lockfile
mode <- annexFileMode
bracket (lock mode lockfile) (liftIO . dropLock) (const a)
where
#ifndef mingw32_HOST_OS
lock mode = noUmask mode . lockShared (Just mode)
#else
lock _mode = liftIO . waitToLock . lockShared
#endif
2014-07-10 04:32:23 +00:00
{- Runs an action with an exclusive lock held. If the lock is already
- held, blocks until it becomes free. -}
withExclusiveLock :: (Git.Repo -> FilePath) -> Annex a -> Annex a
withExclusiveLock getlockfile a = bracket
(takeExclusiveLock getlockfile)
(liftIO . dropLock)
(const a)
{- Takes an exclusive lock, blocking until it's free. -}
takeExclusiveLock :: (Git.Repo -> FilePath) -> Annex LockHandle
takeExclusiveLock getlockfile = debugLocks $ do
2014-07-10 04:32:23 +00:00
lockfile <- fromRepo getlockfile
createAnnexDirectory $ takeDirectory lockfile
mode <- annexFileMode
lock mode lockfile
2014-07-10 04:32:23 +00:00
where
#ifndef mingw32_HOST_OS
lock mode = noUmask mode . lockExclusive (Just mode)
2014-07-10 04:32:23 +00:00
#else
lock _mode = liftIO . waitToLock . lockExclusive
2014-07-10 04:32:23 +00:00
#endif
{- Tries to take an exclusive lock and run an action. If the lock is
- already held, returns Nothing. -}
tryExclusiveLock :: (Git.Repo -> FilePath) -> Annex a -> Annex (Maybe a)
tryExclusiveLock getlockfile a = debugLocks $ do
lockfile <- fromRepo getlockfile
createAnnexDirectory $ takeDirectory lockfile
mode <- annexFileMode
bracket (lock mode lockfile) (liftIO . unlock) go
where
#ifndef mingw32_HOST_OS
lock mode = noUmask mode . tryLockExclusive (Just mode)
#else
lock _mode = liftIO . lockExclusive
#endif
unlock = maybe noop dropLock
go Nothing = return Nothing
go (Just _) = Just <$> a