A lock is in one of two states: "locked" or "unlocked".
Moduleconcurrent-extra-0.7.0.12Haskell98
Control.Concurrent.Lock
This module provides the Lock synchronisation mechanism. It was inspired by
the Python and Java Lock objects and should behave in a similar way. See:
http://docs.python.org/3.1/library/threading.html#lock-objects
and:
http://java.sun.com/javase/7/docs/api/java/util/concurrent/locks/Lock.html
All functions are exception safe. Throwing asynchronous exceptions will not compromise the internal state of a Lock.
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 ( ... )
- 1 type
- 9 values
- Packageconcurrent-extra-0.7.0.12
- Exports10
- LanguageHaskell98
- LicenceBSD-3-Clause
- SourceLock.hs
Creating locks
2 declarationsCreate a lock in the "unlocked" state.
Create a lock in the "locked" state.
Locking and unlocking
3 declarationsAcquires the Lock. Blocks if another thread has acquired the Lock.
acquire behaves as follows:
When the state is "unlocked"
acquirechanges the state to "locked".When the state is "locked"
acquireblocks until a call to release in another thread wakes the calling thread. Upon awakening it will change the state to "locked".
There are two further important properties of acquire:
acquireis single-wakeup. That is, if there are multiple threads blocked onacquireand the lock is released, only one thread will be woken up. The runtime guarantees that the woken thread completes itsacquireoperation.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.)
A non-blocking acquire.
When the state is "unlocked"
tryAcquirechanges the state to "locked" and returnsTrue.When the state is "locked"
tryAcquireleaves the state unchanged and returnsFalse.
release changes the state to "unlocked" and returns immediately.
Note that it is an error to release a lock in the "unlocked" state!
If there are any threads blocked on acquire the thread that first called
acquire will be woken up.
Convenience functions
3 declarationsA non-blocking with. 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.
When the state is "locked",
waitblocks until a call to release in another thread changes it to "unlocked".waitis multiple-wakeup, so when multiple waiters are blocked on aLock, all of them are woken up at the same time.When the state is "unlocked"
waitreturns immediately.
wait does not alter the state of the lock.
Querying locks
1 declarationDetermines if the lock is in the "locked" state.
Note that this is only a snapshot of the state. By the time a program reacts on its result it may already be out of date.