| Copyright | (c) 2010-2011 Bas van Dijk & Roel van Dijk |
|---|---|
| License | BSD3 |
| Maintainer | Iago Abal <mail@iagoabal.eu>, David Castro <david.castro.dcp@gmail.com> |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Z3.Lock
Description
A minimal implementation of a re-entrant lock, adopted from https://github.com/basvandijk/concurrent-extra
Documentation
A lock is in one of two states: "locked" or "unlocked".
acquire :: Lock -> IO () Source #
Acquires 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 toreleasein 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.)
release :: Lock -> IO () Source #
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.
wait :: Lock -> IO () Source #
- When the state is "locked",
waitblocks until a call toreleasein 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.