add tryLockShared
This commit is contained in:
parent
9461019e9a
commit
c8fad345f2
2 changed files with 26 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue