diff options
author | Joey Hess <joey@kitenet.net> | 2014-08-20 16:45:58 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2014-08-20 16:45:58 -0400 |
commit | 679d9c0027ac996eea9f41a6f0f39af436801e89 (patch) | |
tree | d98ae7ba049e2daa4a99e6bcf55054bde5e5ab4d /Utility/LockFile/Posix.hs | |
parent | 86f9d7734d0c638d4c966fd87a5d7e97759e125b (diff) |
reorganize and refactor lock code
Added a convenience Utility.LockFile that is not a windows/posix
portability shim, but still manages to cut down on the boilerplate around
locking.
This commit was sponsored by Johan Herland.
Diffstat (limited to 'Utility/LockFile/Posix.hs')
-rw-r--r-- | Utility/LockFile/Posix.hs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/Utility/LockFile/Posix.hs b/Utility/LockFile/Posix.hs new file mode 100644 index 000000000..1538b491a --- /dev/null +++ b/Utility/LockFile/Posix.hs @@ -0,0 +1,47 @@ +{- Posix lock files + - + - Copyright 2014 Joey Hess <joey@kitenet.net> + - + - License: BSD-2-clause + -} + +module Utility.LockFile.Posix ( + lockShared, + lockExclusive, + dropLock, + createLockFile, + LockHandle +) where + +import System.IO +import System.Posix + +type LockFile = FilePath + +newtype LockHandle = LockHandle Fd + +-- Takes a shared lock, blocking until the lock is available. +lockShared :: Maybe FileMode -> LockFile -> IO LockHandle +lockShared = lock ReadLock + +-- Takes an exclusive lock, blocking until the lock is available. +lockExclusive :: Maybe FileMode -> LockFile -> IO LockHandle +lockExclusive = lock WriteLock + +-- The FileMode is used when creating a new lock file. +lock :: LockRequest -> Maybe FileMode -> LockFile -> IO LockHandle +lock lockreq mode lockfile = do + l <- createLockFile mode lockfile + waitToSetLock l (lockreq, AbsoluteSeek, 0, 0) + return (LockHandle l) + +-- Create and opens lock file, does not lock it. +-- Close on exec flag is set so child processes do not inherit the lock. +createLockFile :: Maybe FileMode -> LockFile -> IO Fd +createLockFile mode lockfile = do + l <- openFd lockfile ReadWrite mode defaultFileFlags + setFdOption l CloseOnExec True + return l + +dropLock :: LockHandle -> IO () +dropLock (LockHandle fd) = closeFd fd |