base-4.18.1.0: Basic libraries

Safe Haskell | Safe-Inferred
Language | Haskell2010

GHC.IO.Handle.Lock

Synopsis

- data FileLockingNotSupported = FileLockingNotSupported
- data LockMode
  - = SharedLock
  - | ExclusiveLock
- hLock :: Handle -> LockMode -> IO ()
- hTryLock :: Handle -> LockMode -> IO Bool
- hUnlock :: Handle -> IO ()

# Documentation

data FileLockingNotSupported Source #

Exception thrown by `hLock` on non-Windows platforms that don't support `flock`.

Since: base-4.10.0.0

Constructors

FileLockingNotSupported |

#### Instances

Instances details

Exception FileLockingNotSupported Source # |
Instance details Defined in GHC.IO.Handle.Lock.Common Methods toException :: FileLockingNotSupported -> SomeException Source # fromException :: SomeException -> Maybe FileLockingNotSupported Source # displayException :: FileLockingNotSupported -> String Source #
Show FileLockingNotSupported Source # | Since: base-4.10.0.0
Instance details Defined in GHC.IO.Handle.Lock.Common Methods showsPrec :: Int -> FileLockingNotSupported -> ShowS Source # show :: FileLockingNotSupported -> String Source # showList :: [FileLockingNotSupported] -> ShowS Source #

data LockMode Source #

Indicates a mode in which a file should be locked.

Constructors

SharedLock |
ExclusiveLock |

hLock :: Handle -> LockMode -> IO () Source #

If a `Handle` references a file descriptor, attempt to lock contents of the underlying file in appropriate mode. If the file is already locked in incompatible mode, this function blocks until the lock is established. The lock is automatically released upon closing a `Handle`.

Things to be aware of:

1) This function may block inside a C call. If it does, in order to be able to interrupt it with asynchronous exceptions and/or for other threads to continue working, you MUST use threaded version of the runtime system.

2) The implementation uses `LockFileEx` on Windows and `flock` otherwise, hence all of their caveats also apply here.

3) On non-Windows platforms that don't support `flock` (e.g. Solaris) this function throws `FileLockingNotImplemented`. We deliberately choose to not provide fcntl based locking instead because of its broken semantics.

Since: base-4.10.0.0

hTryLock :: Handle -> LockMode -> IO Bool Source #

Non-blocking version of `hLock`.

Returns `True` if taking the lock was successful and `False` otherwise.

Since: base-4.10.0.0

hUnlock :: Handle -> IO () Source #

Release a lock taken with `hLock` or `hTryLock`.

Since: base-4.11.0.0