colock-0.2.2: thread-friendly file locks that don't block the entire program



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.


  • Only blocks the calling thread
  • Works both with and without -threaded


  • Forks one new process per lock
  • Consumes one file descriptor per lock


  • Closing the file descriptor doesn't affect the lock (because it's really in a separate process); you must call unLock instead.



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

setLockAll fd lm is equivalent to setLock fd (lm, AbsoluteSeek, 0, 0). It locks the entire file, no matter how big it is.

unLock :: LockDescriptor -> IO ()Source

unLock destroys the given lock.