|Maintainer||Bas van Dijk <firstname.lastname@example.org> , Roel van Dijk <email@example.com>|
This module provides the
Lock synchronization mechanism. It was inspired by
the Python and Java
Lock objects and should behave in a similar way. See:
All functions are exception safe. Throwing asynchronous exceptions will not
compromise the internal state of a
This module is intended to be imported qualified. We suggest importing it like:
import Control.Concurrent.Lock ( Lock ) import qualified Control.Concurrent.Lock as Lock ( ... )
Locking and unlocking
When the state is unlocked,
acquire changes the state to locked and
returns immediately. When the state is locked,
acquire blocks until a call to
release in another thread changes it to unlocked, then the
resets it to locked and returns.
There are two further important properties of
acquireis single-wakeup. That is, if there are multiple threads blocked on
acquire, and the lock is released, only one thread will be woken up. The runtime guarantees that the woken thread completes its
- When multiple threads are blocked on
acquire, they are woken up in FIFO order. This is useful for providing fairness properties of abstractions built using locks. (Note that this differs from the Python implementation where the wake-up order is undefined)
acquire. When the state is unlocked,
the state to locked and returns immediately with
True. When the state is
tryAcquire leaves the state unchanged and returns immediately with
release changes the state to unlocked and returns immediately.
Note that it is an error to release an unlocked lock!
If there are any threads blocked on
acquire the thread that first called
acquire will be woken up.
tryWith is a convenience function which first tries
to acquire the lock. If that fails,
Nothing is returned. If it succeeds, the
computation is performed. When the computation terminates, whether normally or
by raising an exception, the lock is released and
Just the result of the
computation is returned.