summaryrefslogtreecommitdiff
path: root/Utility/LockFile
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2015-10-08 13:40:23 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2015-10-08 13:40:23 -0400
commitdc7576766528bf5b9951b3f355f49a9f8fc1f183 (patch)
treed7c3f68d402a4586c469f6177b47e42bfc0aa8ff /Utility/LockFile
parente2693da16db892d002b2993443bcbe38b6c6efa0 (diff)
add tryLockShared
Diffstat (limited to 'Utility/LockFile')
-rw-r--r--Utility/LockFile/Posix.hs25
1 files changed, 17 insertions, 8 deletions
diff --git a/Utility/LockFile/Posix.hs b/Utility/LockFile/Posix.hs
index 8f06ae69e..cf88fa87d 100644
--- a/Utility/LockFile/Posix.hs
+++ b/Utility/LockFile/Posix.hs
@@ -9,6 +9,7 @@ module Utility.LockFile.Posix (
LockHandle,
lockShared,
lockExclusive,
+ tryLockShared,
tryLockExclusive,
checkLocked,
getLockStatus,
@@ -36,16 +37,13 @@ lockShared = lock ReadLock
lockExclusive :: Maybe FileMode -> LockFile -> IO LockHandle
lockExclusive = lock WriteLock
+-- Tries to take a shared lock, but does not block.
+tryLockShared :: Maybe FileMode -> LockFile -> IO (Maybe LockHandle)
+tryLockShared = tryLock ReadLock
+
-- Tries to take an exclusive lock, but does not block.
tryLockExclusive :: Maybe FileMode -> LockFile -> IO (Maybe LockHandle)
-tryLockExclusive mode lockfile = do
- l <- openLockFile WriteLock mode lockfile
- v <- tryIO $ setLock l (WriteLock, AbsoluteSeek, 0, 0)
- case v of
- Left _ -> do
- closeFd l
- return Nothing
- Right _ -> return $ Just $ LockHandle l
+tryLockExclusive = tryLock WriteLock
-- Setting the FileMode allows creation of a new lock file.
-- If it's Nothing then this only succeeds when the lock file already exists.
@@ -55,6 +53,17 @@ lock lockreq mode lockfile = do
waitToSetLock l (lockreq, AbsoluteSeek, 0, 0)
return (LockHandle l)
+-- Tries to take an lock, but does not block.
+tryLock :: LockRequest -> Maybe FileMode -> LockFile -> IO (Maybe LockHandle)
+tryLock lockreq mode lockfile = do
+ l <- openLockFile lockreq mode lockfile
+ v <- tryIO $ setLock l (lockreq, AbsoluteSeek, 0, 0)
+ case v of
+ Left _ -> do
+ closeFd l
+ return Nothing
+ Right _ -> return $ Just $ LockHandle l
+
-- Close on exec flag is set so child processes do not inherit the lock.
openLockFile :: LockRequest -> Maybe FileMode -> LockFile -> IO Fd
openLockFile lockreq filemode lockfile = do