System.IO.Lock provides thread-friendly file locks. The locking functions in
System.Posix.IO (actually, it's just System.Posix.IO.waitToSetLock
) will
block the entire program, not just the calling thread (even with the
threaded runtime). This module avoids the problem by spawning a new process
for each lock and communicating with it over pipes.
Advantages:
- Only blocks the calling thread
- Works both with and without
-threaded
Disadvantages:
- Forks one new process per lock
- Consumes one file descriptor per lock
Oddities:
- Closing the file descriptor doesn't affect the lock (because it's really in
a separate process); you must call
unLock
instead.
- data LockMode
- data LockDescriptor
- setLock :: Fd -> (LockMode, SeekMode, FileOffset, FileOffset) -> IO LockDescriptor
- setLockAll :: Fd -> LockMode -> IO LockDescriptor
- unLock :: LockDescriptor -> IO ()
Documentation
setLock :: Fd -> (LockMode, SeekMode, FileOffset, FileOffset) -> IO LockDescriptorSource
setLock
locks the specified region of the file. It blocks the calling thread
until the lock is granted.
setLockAll :: Fd -> LockMode -> IO LockDescriptorSource
is equivalent to setLockAll
fd lm
. It locks the entire file, no matter
how big it is.
setLock
fd
(lm, AbsoluteSeek
, 0, 0)