close pid lock only once no threads use it
This fixes a FD leak when annex.pidlock is set and -J is used. Also, it fixes bugs where the pid lock file got deleted because one thread was done with it, while another thread was still holding it open. The LockPool now has two distinct types of resources, one is per-LockHandle and is used for file Handles, which get closed when the associated LockHandle is closed. The other one is per lock file, and gets closed when no more LockHandles use that lock file, including other shared locks of the same file. That latter kind is used for the pid lock file, so it's opened by the first thread to use a lock, and closed when the last thread closes a lock. In practice, this means that eg git-annex get of several files opens and closes the pidlock file a few times per file. While with -J5 it will open the pidlock file, process a number of files, until all the threads happen to finish together, at which point the pidlock file gets closed, and then that repeats. So in either case, another process still gets a chance to take the pidlock. registerPostRelease has a rather intricate dance, there are fine-grained STM locks, a STM lock of the pidfile itself, and the actual pidlock file on disk that are all resolved in stages by it. Sponsored-by: Dartmouth College's Datalad project
This commit is contained in:
parent
774c7dab2f
commit
ef3ab0769e
6 changed files with 128 additions and 90 deletions
|
@ -53,7 +53,7 @@ pidLockChildProcess cmd ps f a = do
|
||||||
cleanup
|
cleanup
|
||||||
(go gonopidlock p pidlock)
|
(go gonopidlock p pidlock)
|
||||||
where
|
where
|
||||||
setup pidlock = PidP.tryLock' pidlock
|
setup pidlock = fmap fst <$> PidP.tryLock' pidlock
|
||||||
|
|
||||||
cleanup (Just h) = dropLock h
|
cleanup (Just h) = dropLock h
|
||||||
cleanup Nothing = return ()
|
cleanup Nothing = return ()
|
||||||
|
@ -83,7 +83,7 @@ runsGitAnnexChildProcessViaGit a = pidLockFile >>= \case
|
||||||
Nothing -> a
|
Nothing -> a
|
||||||
Just pidlock -> bracket (setup pidlock) cleanup (go pidlock)
|
Just pidlock -> bracket (setup pidlock) cleanup (go pidlock)
|
||||||
where
|
where
|
||||||
setup pidlock = liftIO $ PidP.tryLock' pidlock
|
setup pidlock = liftIO $ fmap fst <$> PidP.tryLock' pidlock
|
||||||
|
|
||||||
cleanup (Just h) = liftIO $ dropLock h
|
cleanup (Just h) = liftIO $ dropLock h
|
||||||
cleanup Nothing = return ()
|
cleanup Nothing = return ()
|
||||||
|
@ -112,7 +112,7 @@ runsGitAnnexChildProcessViaGit' r a = pidLockFile >>= \case
|
||||||
Nothing -> liftIO $ a r
|
Nothing -> liftIO $ a r
|
||||||
Just pidlock -> liftIO $ bracket (setup pidlock) cleanup (go pidlock)
|
Just pidlock -> liftIO $ bracket (setup pidlock) cleanup (go pidlock)
|
||||||
where
|
where
|
||||||
setup pidlock = PidP.tryLock' pidlock
|
setup pidlock = fmap fst <$> PidP.tryLock' pidlock
|
||||||
|
|
||||||
cleanup (Just h) = dropLock h
|
cleanup (Just h) = dropLock h
|
||||||
cleanup Nothing = return ()
|
cleanup Nothing = return ()
|
||||||
|
|
|
@ -274,7 +274,7 @@ waitLock (Seconds timeout) lockfile displaymessage sem = go timeout
|
||||||
liftIO $ sem False
|
liftIO $ sem False
|
||||||
waitedLock (Seconds timeout) lockfile displaymessage
|
waitedLock (Seconds timeout) lockfile displaymessage
|
||||||
|
|
||||||
waitedLock :: MonadIO m => Seconds -> PidLockFile -> (String -> m ()) -> m LockHandle
|
waitedLock :: MonadIO m => Seconds -> PidLockFile -> (String -> m ()) -> m a
|
||||||
waitedLock (Seconds timeout) lockfile displaymessage = do
|
waitedLock (Seconds timeout) lockfile displaymessage = do
|
||||||
displaymessage $ show timeout ++ " second timeout exceeded while waiting for pid lock file " ++ fromRawFilePath lockfile
|
displaymessage $ show timeout ++ " second timeout exceeded while waiting for pid lock file " ++ fromRawFilePath lockfile
|
||||||
giveup $ "Gave up waiting for pid lock file " ++ fromRawFilePath lockfile
|
giveup $ "Gave up waiting for pid lock file " ++ fromRawFilePath lockfile
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{- Handles for lock pools.
|
{- Handles for lock pools.
|
||||||
-
|
-
|
||||||
- Copyright 2015-2020 Joey Hess <id@joeyh.name>
|
- Copyright 2015-2021 Joey Hess <id@joeyh.name>
|
||||||
-
|
-
|
||||||
- License: BSD-2-clause
|
- License: BSD-2-clause
|
||||||
-}
|
-}
|
||||||
|
@ -25,7 +25,6 @@ import Utility.DebugLocks
|
||||||
import Control.Concurrent.STM
|
import Control.Concurrent.STM
|
||||||
import Control.Monad.Catch
|
import Control.Monad.Catch
|
||||||
import Control.Monad.IO.Class (liftIO, MonadIO)
|
import Control.Monad.IO.Class (liftIO, MonadIO)
|
||||||
import Control.Applicative
|
|
||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
data LockHandle = LockHandle P.LockHandle FileLockOps
|
data LockHandle = LockHandle P.LockHandle FileLockOps
|
||||||
|
@ -53,21 +52,24 @@ makeLockHandle
|
||||||
=> P.LockPool
|
=> P.LockPool
|
||||||
-> LockFile
|
-> LockFile
|
||||||
-> (P.LockPool -> LockFile -> STM (P.LockHandle, P.FirstLock))
|
-> (P.LockPool -> LockFile -> STM (P.LockHandle, P.FirstLock))
|
||||||
-> (LockFile -> P.FirstLock -> m FileLockOps)
|
-> (LockFile -> P.FirstLock -> m (FileLockOps, t))
|
||||||
-> m LockHandle
|
-> m (LockHandle, t)
|
||||||
makeLockHandle pool file pa fa = bracketOnError setup cleanup go
|
makeLockHandle pool file pa fa = bracketOnError setup cleanup go
|
||||||
where
|
where
|
||||||
setup = debugLocks $ liftIO $ atomically (pa pool file)
|
setup = debugLocks $ liftIO $ atomically (pa pool file)
|
||||||
cleanup (ph, _) = debugLocks $ liftIO $ P.releaseLock ph
|
cleanup (ph, _) = debugLocks $ liftIO $ P.releaseLock ph
|
||||||
go (ph, firstlock) = liftIO . mkLockHandle ph =<< fa file firstlock
|
go (ph, firstlock) = do
|
||||||
|
(flo, t) <- fa file firstlock
|
||||||
|
h <- liftIO $ mkLockHandle ph flo
|
||||||
|
return (h, t)
|
||||||
|
|
||||||
tryMakeLockHandle
|
tryMakeLockHandle
|
||||||
:: (MonadIO m, MonadMask m)
|
:: (MonadIO m, MonadMask m)
|
||||||
=> P.LockPool
|
=> P.LockPool
|
||||||
-> LockFile
|
-> LockFile
|
||||||
-> (P.LockPool -> LockFile -> STM (Maybe (P.LockHandle, P.FirstLock)))
|
-> (P.LockPool -> LockFile -> STM (Maybe (P.LockHandle, P.FirstLock)))
|
||||||
-> (LockFile -> P.FirstLock -> m (Maybe FileLockOps))
|
-> (LockFile -> P.FirstLock -> m (Maybe (FileLockOps, t)))
|
||||||
-> m (Maybe LockHandle)
|
-> m (Maybe (LockHandle, t))
|
||||||
tryMakeLockHandle pool file pa fa = bracketOnError setup cleanup go
|
tryMakeLockHandle pool file pa fa = bracketOnError setup cleanup go
|
||||||
where
|
where
|
||||||
setup = liftIO $ atomically (pa pool file)
|
setup = liftIO $ atomically (pa pool file)
|
||||||
|
@ -80,7 +82,9 @@ tryMakeLockHandle pool file pa fa = bracketOnError setup cleanup go
|
||||||
Nothing -> do
|
Nothing -> do
|
||||||
liftIO $ cleanup (Just (ph, firstlock))
|
liftIO $ cleanup (Just (ph, firstlock))
|
||||||
return Nothing
|
return Nothing
|
||||||
Just fo -> liftIO $ Just <$> mkLockHandle ph fo
|
Just (fo, t) -> do
|
||||||
|
h <- liftIO $ mkLockHandle ph fo
|
||||||
|
return (Just (h, t))
|
||||||
|
|
||||||
mkLockHandle :: P.LockHandle -> FileLockOps -> IO LockHandle
|
mkLockHandle :: P.LockHandle -> FileLockOps -> IO LockHandle
|
||||||
mkLockHandle ph fo = do
|
mkLockHandle ph fo = do
|
||||||
|
|
|
@ -36,14 +36,16 @@ import Control.Applicative
|
||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
-- Does locking using a pid lock, blocking until the lock is available
|
-- Does locking using a pid lock, blocking until the lock is available
|
||||||
-- or the timeout.
|
-- or the Seconds timeout if the pid lock is held by another process.
|
||||||
--
|
--
|
||||||
-- There are two levels of locks. A STM lock is used to handle
|
-- There are two levels of locks. A STM lock is used to handle
|
||||||
-- fine-grained locking amoung threads, locking a specific lockfile,
|
-- fine-grained locking amoung threads, locking a specific lockfile,
|
||||||
-- but only in memory. The pid lock handles locking between processes.
|
-- but only in memory. The pid lock handles locking between processes.
|
||||||
--
|
--
|
||||||
-- The Seconds is how long to delay if the pid lock is held by another
|
-- The pid lock is only taken once, and LockShared is used for it,
|
||||||
-- process.
|
-- so multiple threads can have it locked. Only the first thread
|
||||||
|
-- will create the pid lock, and it remains until all threads drop
|
||||||
|
-- their locks.
|
||||||
waitLock
|
waitLock
|
||||||
:: (MonadIO m, MonadMask m)
|
:: (MonadIO m, MonadMask m)
|
||||||
=> LockFile
|
=> LockFile
|
||||||
|
@ -52,67 +54,87 @@ waitLock
|
||||||
-> F.PidLockFile
|
-> F.PidLockFile
|
||||||
-> (String -> m ())
|
-> (String -> m ())
|
||||||
-> m LockHandle
|
-> m LockHandle
|
||||||
waitLock stmlockfile lockmode timeout pidlockfile displaymessage = do
|
waitLock finelockfile lockmode timeout pidlockfile displaymessage = do
|
||||||
sl@(LockHandle ph _) <- takestmlock
|
fl <- takefinelock
|
||||||
pl <- takepidlock
|
pl <- takepidlock
|
||||||
-- When the STM lock gets dropped, also drop the pid lock.
|
`onException` liftIO (dropLock fl)
|
||||||
liftIO $ atomically $
|
registerPostRelease fl pl
|
||||||
P.registerPostReleaseLock ph (dropLock pl)
|
return fl
|
||||||
return sl
|
|
||||||
where
|
where
|
||||||
takestmlock = makeLockHandle P.lockPool stmlockfile
|
takefinelock = fst <$> makeLockHandle P.lockPool finelockfile
|
||||||
(\p f -> P.waitTakeLock p f lockmode)
|
(\p f -> P.waitTakeLock p f lockmode)
|
||||||
(\_ _ -> pure stmonlyflo)
|
(\_ _ -> pure (stmonlyflo, ()))
|
||||||
|
-- A shared STM lock is taken for each use of the pid lock,
|
||||||
|
-- but only the first thread to take it actually creates the pid
|
||||||
|
-- lock file.
|
||||||
takepidlock = makeLockHandle P.lockPool pidlockfile
|
takepidlock = makeLockHandle P.lockPool pidlockfile
|
||||||
-- LockShared because multiple threads can share the pid lock;
|
|
||||||
-- it remains locked until all threads using it drop
|
|
||||||
-- their locks.
|
|
||||||
(\p f -> P.waitTakeLock p f LockShared)
|
(\p f -> P.waitTakeLock p f LockShared)
|
||||||
(\f (P.FirstLock firstlock firstlocksem) -> mkflo
|
(\f (P.FirstLock firstlock firstlocksem) -> if firstlock
|
||||||
<$> if firstlock
|
then waitlock f firstlocksem
|
||||||
then F.waitLock timeout f displaymessage $
|
else liftIO (atomically $ readTMVar firstlocksem) >>= \case
|
||||||
void . atomically . tryPutTMVar firstlocksem . P.FirstLockSemWaited
|
P.FirstLockSemWaited True -> alreadylocked f
|
||||||
else liftIO (atomically $ readTMVar firstlocksem) >>= \case
|
P.FirstLockSemTried True -> alreadylocked f
|
||||||
P.FirstLockSemWaited True -> F.alreadyLocked f
|
P.FirstLockSemWaited False -> F.waitedLock timeout f displaymessage
|
||||||
P.FirstLockSemTried True -> F.alreadyLocked f
|
P.FirstLockSemTried False -> waitlock f firstlocksem
|
||||||
P.FirstLockSemWaited False -> F.waitedLock timeout f displaymessage
|
|
||||||
P.FirstLockSemTried False -> F.waitLock timeout f displaymessage $
|
|
||||||
void . atomically . tryPutTMVar firstlocksem . P.FirstLockSemWaited
|
|
||||||
)
|
)
|
||||||
|
waitlock f firstlocksem = do
|
||||||
|
h <- F.waitLock timeout f displaymessage $
|
||||||
|
void . atomically . tryPutTMVar firstlocksem . P.FirstLockSemWaited
|
||||||
|
return (mkflo h, Just h)
|
||||||
|
alreadylocked f = do
|
||||||
|
lh <- F.alreadyLocked f
|
||||||
|
return (mkflo lh, Nothing)
|
||||||
|
|
||||||
|
registerPostRelease :: MonadIO m => LockHandle -> (LockHandle, Maybe F.LockHandle) -> m ()
|
||||||
|
registerPostRelease (LockHandle flh _) (pl@(LockHandle plh _), mpidlock) = do
|
||||||
|
-- After the fine-grained lock gets dropped (and any shared locks
|
||||||
|
-- of it are also dropped), drop the associated pid lock.
|
||||||
|
liftIO $ atomically $
|
||||||
|
P.registerPostReleaseLock flh (dropLock pl)
|
||||||
|
-- When the last thread to use the pid lock has dropped it,
|
||||||
|
-- close the pid lock file itself.
|
||||||
|
case mpidlock of
|
||||||
|
Just pidlock -> liftIO $ atomically $
|
||||||
|
P.registerPostReleaseLock plh (F.dropLock pidlock)
|
||||||
|
Nothing -> return ()
|
||||||
|
|
||||||
-- Tries to take a pid lock, but does not block.
|
-- Tries to take a pid lock, but does not block.
|
||||||
tryLock :: LockFile -> LockMode -> F.PidLockFile -> IO (Maybe LockHandle)
|
tryLock :: LockFile -> LockMode -> F.PidLockFile -> IO (Maybe LockHandle)
|
||||||
tryLock stmlockfile lockmode pidlockfile = takestmlock >>= \case
|
tryLock finelockfile lockmode pidlockfile = takefinelock >>= \case
|
||||||
Just (sl@(LockHandle ph _)) -> tryLock' pidlockfile >>= \case
|
Just fl -> tryLock' pidlockfile >>= \case
|
||||||
Just pl -> do
|
Just pl -> do
|
||||||
liftIO $ atomically $
|
registerPostRelease fl pl
|
||||||
P.registerPostReleaseLock ph (dropLock pl)
|
return (Just fl)
|
||||||
return (Just sl)
|
|
||||||
Nothing -> do
|
Nothing -> do
|
||||||
dropLock sl
|
dropLock fl
|
||||||
return Nothing
|
return Nothing
|
||||||
Nothing -> return Nothing
|
Nothing -> return Nothing
|
||||||
where
|
where
|
||||||
takestmlock = tryMakeLockHandle P.lockPool stmlockfile
|
takefinelock = fmap fst <$> tryMakeLockHandle P.lockPool finelockfile
|
||||||
(\p f -> P.tryTakeLock p f lockmode)
|
(\p f -> P.tryTakeLock p f lockmode)
|
||||||
(\_ _ -> pure (Just stmonlyflo))
|
(\_ _ -> pure (Just (stmonlyflo, ())))
|
||||||
|
|
||||||
tryLock' :: F.PidLockFile -> IO (Maybe LockHandle)
|
tryLock' :: F.PidLockFile -> IO (Maybe (LockHandle, Maybe F.LockHandle))
|
||||||
tryLock' pidlockfile = tryMakeLockHandle P.lockPool pidlockfile
|
tryLock' pidlockfile = tryMakeLockHandle P.lockPool pidlockfile
|
||||||
(\p f -> P.tryTakeLock p f LockShared)
|
(\p f -> P.tryTakeLock p f LockShared)
|
||||||
(\f (P.FirstLock firstlock firstlocksem) -> fmap mkflo
|
(\f (P.FirstLock firstlock firstlocksem) -> if firstlock
|
||||||
<$> if firstlock
|
then do
|
||||||
then do
|
mlh <- F.tryLock f
|
||||||
lh <- F.tryLock f
|
void $ atomically $ tryPutTMVar firstlocksem
|
||||||
void $ atomically $ tryPutTMVar firstlocksem
|
(P.FirstLockSemTried (isJust mlh))
|
||||||
(P.FirstLockSemTried (isJust lh))
|
case mlh of
|
||||||
return lh
|
Just lh -> return (Just (mkflo lh, Just lh))
|
||||||
else liftIO (atomically $ readTMVar firstlocksem) >>= \case
|
Nothing -> return Nothing
|
||||||
P.FirstLockSemWaited True -> Just <$> F.alreadyLocked f
|
else liftIO (atomically $ readTMVar firstlocksem) >>= \case
|
||||||
P.FirstLockSemTried True -> Just <$> F.alreadyLocked f
|
P.FirstLockSemWaited True -> alreadylocked f
|
||||||
P.FirstLockSemWaited False -> return Nothing
|
P.FirstLockSemTried True -> alreadylocked f
|
||||||
P.FirstLockSemTried False -> return Nothing
|
P.FirstLockSemWaited False -> return Nothing
|
||||||
|
P.FirstLockSemTried False -> return Nothing
|
||||||
)
|
)
|
||||||
|
where
|
||||||
|
alreadylocked f = do
|
||||||
|
lh <- F.alreadyLocked f
|
||||||
|
return (Just (mkflo lh, Nothing))
|
||||||
|
|
||||||
checkLocked :: LockFile -> IO (Maybe Bool)
|
checkLocked :: LockFile -> IO (Maybe Bool)
|
||||||
checkLocked file = P.getLockStatus P.lockPool file
|
checkLocked file = P.getLockStatus P.lockPool file
|
||||||
|
@ -126,7 +148,7 @@ getLockStatus file = P.getLockStatus P.lockPool file
|
||||||
|
|
||||||
mkflo :: F.LockHandle -> FileLockOps
|
mkflo :: F.LockHandle -> FileLockOps
|
||||||
mkflo h = FileLockOps
|
mkflo h = FileLockOps
|
||||||
{ fDropLock = F.dropLock h
|
{ fDropLock = return ()
|
||||||
, fCheckSaneLock = \f -> F.checkSaneLock f h
|
, fCheckSaneLock = \f -> F.checkSaneLock f h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,25 +33,25 @@ import Prelude
|
||||||
|
|
||||||
-- Takes a shared lock, blocking until the lock is available.
|
-- Takes a shared lock, blocking until the lock is available.
|
||||||
lockShared :: Maybe FileMode -> LockFile -> IO LockHandle
|
lockShared :: Maybe FileMode -> LockFile -> IO LockHandle
|
||||||
lockShared mode file = makeLockHandle P.lockPool file
|
lockShared mode file = fst <$> makeLockHandle P.lockPool file
|
||||||
(\p f -> P.waitTakeLock p f LockShared)
|
(\p f -> P.waitTakeLock p f LockShared)
|
||||||
(\f _ -> mk <$> F.lockShared mode f)
|
(\f _ -> mk <$> F.lockShared mode f)
|
||||||
|
|
||||||
-- Takes an exclusive lock, blocking until the lock is available.
|
-- Takes an exclusive lock, blocking until the lock is available.
|
||||||
lockExclusive :: Maybe FileMode -> LockFile -> IO LockHandle
|
lockExclusive :: Maybe FileMode -> LockFile -> IO LockHandle
|
||||||
lockExclusive mode file = makeLockHandle P.lockPool file
|
lockExclusive mode file = fst <$> makeLockHandle P.lockPool file
|
||||||
(\p f -> P.waitTakeLock p f LockExclusive)
|
(\p f -> P.waitTakeLock p f LockExclusive)
|
||||||
(\f _ -> mk <$> F.lockExclusive mode f)
|
(\f _ -> mk <$> F.lockExclusive mode f)
|
||||||
|
|
||||||
-- Tries to take a shared lock, but does not block.
|
-- Tries to take a shared lock, but does not block.
|
||||||
tryLockShared :: Maybe FileMode -> LockFile -> IO (Maybe LockHandle)
|
tryLockShared :: Maybe FileMode -> LockFile -> IO (Maybe LockHandle)
|
||||||
tryLockShared mode file = tryMakeLockHandle P.lockPool file
|
tryLockShared mode file = fmap fst <$> tryMakeLockHandle P.lockPool file
|
||||||
(\p f -> P.tryTakeLock p f LockShared)
|
(\p f -> P.tryTakeLock p f LockShared)
|
||||||
(\f _ -> fmap mk <$> F.tryLockShared mode f)
|
(\f _ -> fmap mk <$> F.tryLockShared mode f)
|
||||||
|
|
||||||
-- Tries to take an exclusive lock, but does not block.
|
-- Tries to take an exclusive lock, but does not block.
|
||||||
tryLockExclusive :: Maybe FileMode -> LockFile -> IO (Maybe LockHandle)
|
tryLockExclusive :: Maybe FileMode -> LockFile -> IO (Maybe LockHandle)
|
||||||
tryLockExclusive mode file = tryMakeLockHandle P.lockPool file
|
tryLockExclusive mode file = fmap fst <$> tryMakeLockHandle P.lockPool file
|
||||||
(\p f -> P.tryTakeLock p f LockExclusive)
|
(\p f -> P.tryTakeLock p f LockExclusive)
|
||||||
(\f _ -> fmap mk <$> F.tryLockExclusive mode f)
|
(\f _ -> fmap mk <$> F.tryLockExclusive mode f)
|
||||||
|
|
||||||
|
@ -67,8 +67,8 @@ getLockStatus file = P.getLockStatus P.lockPool file
|
||||||
(StatusLockedBy <$> getProcessID)
|
(StatusLockedBy <$> getProcessID)
|
||||||
(F.getLockStatus file)
|
(F.getLockStatus file)
|
||||||
|
|
||||||
mk :: F.LockHandle -> FileLockOps
|
mk :: F.LockHandle -> (FileLockOps, ())
|
||||||
mk h = FileLockOps
|
mk h = (FileLockOps
|
||||||
{ fDropLock = F.dropLock h
|
{ fDropLock = F.dropLock h
|
||||||
, fCheckSaneLock = \f -> F.checkSaneLock f h
|
, fCheckSaneLock = \f -> F.checkSaneLock f h
|
||||||
}
|
}, ())
|
||||||
|
|
|
@ -37,7 +37,7 @@ data LockMode = LockExclusive | LockShared
|
||||||
|
|
||||||
-- This TMVar is full when the handle is open, and is emptied when it's
|
-- This TMVar is full when the handle is open, and is emptied when it's
|
||||||
-- closed.
|
-- closed.
|
||||||
type LockHandle = TMVar (LockPool, LockFile, CloseLockFile, PostReleaseLock)
|
type LockHandle = TMVar (LockPool, LockFile, CloseLockFile)
|
||||||
|
|
||||||
-- When a shared lock is taken, this will only be true for the first
|
-- When a shared lock is taken, this will only be true for the first
|
||||||
-- process, not subsequent processes. The first process should
|
-- process, not subsequent processes. The first process should
|
||||||
|
@ -52,13 +52,15 @@ data FirstLockSemVal = FirstLockSemWaited Bool | FirstLockSemTried Bool
|
||||||
|
|
||||||
type LockCount = Integer
|
type LockCount = Integer
|
||||||
|
|
||||||
data LockStatus = LockStatus LockMode LockCount FirstLockSem
|
-- Action that closes the underlying lock file. When this is used
|
||||||
|
-- in a LockHandle, it closes a resource that is specific to that
|
||||||
-- Action that closes the underlying lock file.
|
-- LockHandle (such as eg a file handle), but does not release
|
||||||
|
-- any other shared locks. When this is used in a LockStatus,
|
||||||
|
-- it closes a resource that should only be closed when there are no
|
||||||
|
-- other shared locks.
|
||||||
type CloseLockFile = IO ()
|
type CloseLockFile = IO ()
|
||||||
|
|
||||||
-- Action that is run after the LockHandle is released.
|
data LockStatus = LockStatus LockMode LockCount FirstLockSem CloseLockFile
|
||||||
type PostReleaseLock = IO ()
|
|
||||||
|
|
||||||
-- This TMVar is normally kept full.
|
-- This TMVar is normally kept full.
|
||||||
type LockPool = TMVar (M.Map LockFile LockStatus)
|
type LockPool = TMVar (M.Map LockFile LockStatus)
|
||||||
|
@ -86,36 +88,44 @@ tryTakeLock pool file mode = do
|
||||||
m <- takeTMVar pool
|
m <- takeTMVar pool
|
||||||
let success firstlock v = do
|
let success firstlock v = do
|
||||||
putTMVar pool (M.insert file v m)
|
putTMVar pool (M.insert file v m)
|
||||||
tmv <- newTMVar (pool, file, noop, noop)
|
tmv <- newTMVar (pool, file, noop)
|
||||||
return (Just (tmv, firstlock))
|
return (Just (tmv, firstlock))
|
||||||
case M.lookup file m of
|
case M.lookup file m of
|
||||||
Just (LockStatus mode' n firstlocksem)
|
Just (LockStatus mode' n firstlocksem postreleaselock)
|
||||||
| mode == LockShared && mode' == LockShared -> do
|
| mode == LockShared && mode' == LockShared -> do
|
||||||
fl@(FirstLock _ firstlocksem') <- if n == 0
|
fl@(FirstLock _ firstlocksem') <- if n == 0
|
||||||
then FirstLock True <$> newEmptyTMVar
|
then FirstLock True <$> newEmptyTMVar
|
||||||
else pure (FirstLock False firstlocksem)
|
else pure (FirstLock False firstlocksem)
|
||||||
success fl $ LockStatus mode (succ n) firstlocksem'
|
success fl $ LockStatus mode (succ n) firstlocksem' postreleaselock
|
||||||
| n > 0 -> do
|
| n > 0 -> do
|
||||||
putTMVar pool m
|
putTMVar pool m
|
||||||
return Nothing
|
return Nothing
|
||||||
_ -> do
|
_ -> do
|
||||||
firstlocksem <- newEmptyTMVar
|
firstlocksem <- newEmptyTMVar
|
||||||
success (FirstLock True firstlocksem) $
|
success (FirstLock True firstlocksem) $
|
||||||
LockStatus mode 1 firstlocksem
|
LockStatus mode 1 firstlocksem noop
|
||||||
|
|
||||||
-- Call after waitTakeLock or tryTakeLock, to register a CloseLockFile
|
-- Call after waitTakeLock or tryTakeLock, to register a CloseLockFile
|
||||||
-- action to run when releasing the lock.
|
-- action to run when releasing the lock. This action should only
|
||||||
|
-- close the lock file associated with the LockHandle, while
|
||||||
|
-- leaving any other shared locks of the same file open.
|
||||||
registerCloseLockFile :: LockHandle -> CloseLockFile -> STM ()
|
registerCloseLockFile :: LockHandle -> CloseLockFile -> STM ()
|
||||||
registerCloseLockFile h closelockfile = do
|
registerCloseLockFile h closelockfile = do
|
||||||
(p, f, c, r) <- takeTMVar h
|
(p, f, c) <- takeTMVar h
|
||||||
putTMVar h (p, f, c >> closelockfile, r)
|
putTMVar h (p, f, c >> closelockfile)
|
||||||
|
|
||||||
-- Call after waitTakeLock or tryTakeLock, to register a PostReleaseLock
|
-- Register an action that should be run only once a lock has been
|
||||||
-- action to run after releasing the lock.
|
-- released. When there are multiple shared locks of the same file,
|
||||||
registerPostReleaseLock :: LockHandle -> PostReleaseLock -> STM ()
|
-- the action will only be run after all are released.
|
||||||
|
registerPostReleaseLock :: LockHandle -> CloseLockFile -> STM ()
|
||||||
registerPostReleaseLock h postreleaselock = do
|
registerPostReleaseLock h postreleaselock = do
|
||||||
(p, f, c, r) <- takeTMVar h
|
(p, f, _) <- readTMVar h
|
||||||
putTMVar h (p, f, c, r >> postreleaselock)
|
m <- takeTMVar p
|
||||||
|
case M.lookup f m of
|
||||||
|
Nothing -> putTMVar p m
|
||||||
|
Just (LockStatus mode cnt firstlocksem c) -> do
|
||||||
|
let c' = c >> postreleaselock
|
||||||
|
putTMVar p $ M.insert f (LockStatus mode cnt firstlocksem c') m
|
||||||
|
|
||||||
-- Checks if a lock is being held. If it's held by the current process,
|
-- Checks if a lock is being held. If it's held by the current process,
|
||||||
-- runs the getdefault action; otherwise runs the checker action.
|
-- runs the getdefault action; otherwise runs the checker action.
|
||||||
|
@ -130,7 +140,7 @@ getLockStatus pool file getdefault checker = do
|
||||||
v <- atomically $ do
|
v <- atomically $ do
|
||||||
m <- takeTMVar pool
|
m <- takeTMVar pool
|
||||||
let threadlocked = case M.lookup file m of
|
let threadlocked = case M.lookup file m of
|
||||||
Just (LockStatus _ n _) | n > 0 -> True
|
Just (LockStatus _ n _ _) | n > 0 -> True
|
||||||
_ -> False
|
_ -> False
|
||||||
if threadlocked
|
if threadlocked
|
||||||
then do
|
then do
|
||||||
|
@ -151,17 +161,19 @@ getLockStatus pool file getdefault checker = do
|
||||||
releaseLock :: LockHandle -> IO ()
|
releaseLock :: LockHandle -> IO ()
|
||||||
releaseLock h = go =<< atomically (tryTakeTMVar h)
|
releaseLock h = go =<< atomically (tryTakeTMVar h)
|
||||||
where
|
where
|
||||||
go (Just (pool, file, closelockfile, postreleaselock)) = do
|
go (Just (pool, file, closelockfile)) = do
|
||||||
m <- atomically $ do
|
(m, postreleaselock) <- atomically $ do
|
||||||
m <- takeTMVar pool
|
m <- takeTMVar pool
|
||||||
return $ case M.lookup file m of
|
return $ case M.lookup file m of
|
||||||
Just (LockStatus mode n firstlocksem)
|
Just (LockStatus mode n firstlocksem postreleaselock)
|
||||||
| n == 1 -> (M.delete file m)
|
| n == 1 -> (M.delete file m, postreleaselock)
|
||||||
| otherwise ->
|
| otherwise ->
|
||||||
(M.insert file (LockStatus mode (pred n) firstlocksem) m)
|
(M.insert file (LockStatus mode (pred n) firstlocksem postreleaselock) m, noop)
|
||||||
Nothing -> m
|
Nothing -> (m, noop)
|
||||||
() <- closelockfile
|
() <- closelockfile
|
||||||
atomically $ putTMVar pool m
|
atomically $ putTMVar pool m
|
||||||
|
-- This action may access the pool, so run it only
|
||||||
|
-- after the pool is restored.
|
||||||
postreleaselock
|
postreleaselock
|
||||||
-- The LockHandle was already closed.
|
-- The LockHandle was already closed.
|
||||||
go Nothing = return ()
|
go Nothing = return ()
|
||||||
|
|
Loading…
Reference in a new issue