summaryrefslogtreecommitdiff
path: root/Utility
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
parente2693da16db892d002b2993443bcbe38b6c6efa0 (diff)
add tryLockShared
Diffstat (limited to 'Utility')
-rw-r--r--Utility/LockFile/Posix.hs25
-rw-r--r--Utility/LockPool/Posix.hs9
2 files changed, 26 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
diff --git a/Utility/LockPool/Posix.hs b/Utility/LockPool/Posix.hs
index 506d7b560..82e0c8e5e 100644
--- a/Utility/LockPool/Posix.hs
+++ b/Utility/LockPool/Posix.hs
@@ -9,6 +9,7 @@ module Utility.LockPool.Posix (
LockHandle,
lockShared,
lockExclusive,
+ tryLockShared,
tryLockExclusive,
checkLocked,
getLockStatus,
@@ -35,11 +36,19 @@ lockShared mode file = makeLockHandle
(P.waitTakeLock P.lockPool file LockShared)
(F.lockShared mode file)
+-- Takes an exclusive lock, blocking until the lock is available.
lockExclusive :: Maybe FileMode -> LockFile -> IO LockHandle
lockExclusive mode file = makeLockHandle
(P.waitTakeLock P.lockPool file LockExclusive)
(F.lockExclusive mode file)
+-- Tries to take a shared lock, but does not block.
+tryLockShared :: Maybe FileMode -> LockFile -> IO (Maybe LockHandle)
+tryLockShared mode file = tryMakeLockHandle
+ (P.tryTakeLock P.lockPool file LockShared)
+ (F.tryLockShared mode file)
+
+-- Tries to take an exclusive lock, but does not block.
tryLockExclusive :: Maybe FileMode -> LockFile -> IO (Maybe LockHandle)
tryLockExclusive mode file = tryMakeLockHandle
(P.tryTakeLock P.lockPool file LockExclusive)