Copyright | (c) 2010-2011 Bas van Dijk & Roel van Dijk |
---|---|
License | BSD3 (see the file LICENSE) |
Maintainer | Bas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com> |
Safe Haskell | Safe |
Language | Haskell98 |
Control.Concurrent.STM.Lock
Description
This module provides an STM
version of Control.Concurrent.Lock
.
This module is intended to be imported qualified. We suggest importing it like:
import Control.Concurrent.STM.Lock ( Lock ) import qualified Control.Concurrent.STM.Lock as Lock ( ... )
Documentation
A lock is in one of two states: "locked" or "unlocked".
Creating locks
newAcquired :: STM Lock Source #
Create a lock in the "locked" state.
Locking and unlocking
acquire :: Lock -> STM () Source #
- When the state is "locked"
acquire
willretry
the transaction. - When the state is "unlocked"
acquire
will change the state to "locked".
tryAcquire :: Lock -> STM Bool Source #
A non-blocking acquire
.
- When the state is "unlocked"
tryAcquire
changes the state to "locked" and returnsTrue
. - When the state is "locked"
tryAcquire
leaves the state unchanged and returnsFalse
.
release :: Lock -> STM () Source #
release
changes the state to "unlocked" and returns immediately.
Note that it is an error to release a lock in the "unlocked" state!
Convenience functions
with :: Lock -> IO a -> IO a Source #
A convenience function which first acquires the lock and then performs the computation. When the computation terminates, whether normally or by raising an exception, the lock is released.
tryWith :: Lock -> IO a -> IO (Maybe a) Source #
A 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.