Copyright | © 2010-2011 Bas van Dijk & Roel van Dijk © 2018 DFINITY Stiftung |
---|---|
Maintainer | DFINITY USA Research <team@dfinity.org> |
Safe Haskell | None |
Language | Haskell2010 |
Multiple-reader, single-writer locks. Used to protect shared resources which may be concurrently read, but only sequentially written.
All functions are exception safe. Throwing asynchronous exceptions will not
compromise the internal state of an RWLock
. This means it is perfectly safe
to kill a thread that is blocking on, for example, acquireRead
.
Synopsis
- data RWLock m
- newRWLock :: MonadConc m => m (RWLock m)
- newAcquiredRead :: MonadConc m => m (RWLock m)
- newAcquiredWrite :: MonadConc m => m (RWLock m)
- acquireRead :: MonadConc m => RWLock m -> m ()
- releaseRead :: MonadConc m => RWLock m -> m ()
- withRead :: MonadConc m => RWLock m -> m a -> m a
- waitRead :: MonadConc m => RWLock m -> m ()
- tryAcquireRead :: MonadConc m => RWLock m -> m Bool
- tryWithRead :: MonadConc m => RWLock m -> m a -> m (Maybe a)
- acquireWrite :: MonadConc m => RWLock m -> m ()
- releaseWrite :: MonadConc m => RWLock m -> m ()
- withWrite :: MonadConc m => RWLock m -> m a -> m a
- waitWrite :: MonadConc m => RWLock m -> m ()
- tryAcquireWrite :: MonadConc m => RWLock m -> m Bool
- tryWithWrite :: MonadConc m => RWLock m -> m a -> m (Maybe a)
RWLock
Multiple-reader, single-writer lock. Is in one of three states:
- "Free": Read or write access can be acquired without blocking.
- "Read": One or more threads have acquired read access. Blocks write access.
- "Write": A single thread has acquired write access. Blocks other threads from acquiring both read and write access.
Since: 1.6.2.0
Creating locks
newRWLock :: MonadConc m => m (RWLock m) Source #
Create a new RWLock
in the "free" state; either read or write access
can be acquired without blocking.
Since: 1.6.2.0
newAcquiredRead :: MonadConc m => m (RWLock m) Source #
Create a new RWLock
in the "read" state; only read can be acquired
without blocking.
Since: 1.6.2.0
newAcquiredWrite :: MonadConc m => m (RWLock m) Source #
Create a new RWLock
in the "write" state; either acquiring read or
write will block.
Since: 1.6.2.0
Read access
Blocking
acquireRead :: MonadConc m => RWLock m -> m () Source #
Acquire the read lock.
Blocks if another thread has acquired write access.
If acquireRead
terminates without throwing an exception the state of
the RWLock
will be "read".
Implementation note: throws an exception when more than
simultaneous threads acquire the read lock. But that is unlikely.maxBound
:: Int
Since: 1.6.2.0
releaseRead :: MonadConc m => RWLock m -> m () Source #
Release the read lock.
If the calling thread was the last one to relinquish read access the state will revert to "free".
It is an error to release read access to an RWLock
which is not in
the "read" state.
Since: 1.6.2.0
withRead :: MonadConc m => RWLock m -> m a -> m a Source #
A convenience function wich first acquires read access and then performs the computation. When the computation terminates, whether normally or by raising an exception, the read lock is released.
Since: 1.6.2.0
waitRead :: MonadConc m => RWLock m -> m () Source #
- When the state is "write",
waitRead
blocks until a call toreleaseWrite
in another thread changes the state to "free". - When the state is "free" or "read"
waitRead
returns immediately.
waitRead
does not alter the state of the lock.
Note that waitRead
is just a convenience function defined as:
waitRead l =mask_
$
acquireRead
l>>
releaseRead
l
Since: 1.6.2.0
Non-blocking
tryAcquireRead :: MonadConc m => RWLock m -> m Bool Source #
Try to acquire the read lock; non blocking.
Like acquireRead
, but doesn't block. Returns True
if the resulting
state is "read", False
otherwise.
Since: 1.6.2.0
tryWithRead :: MonadConc m => RWLock m -> m a -> m (Maybe a) Source #
A non-blocking withRead
. 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.
Since: 1.6.2.0
Write access
Blocking
acquireWrite :: MonadConc m => RWLock m -> m () Source #
Acquire the write lock.
Blocks if another thread has acquired either read or write access.
If acquireWrite
terminates without throwing an exception the state of
the RWLock
will be "write".
Since: 1.6.2.0
releaseWrite :: MonadConc m => RWLock m -> m () Source #
Release the write lock.
If releaseWrite
terminates without throwing an exception the state
will be "free".
It is an error to release write access to an RWLock
which is not
in the "write" state.
Since: 1.6.2.0
withWrite :: MonadConc m => RWLock m -> m a -> m a Source #
A convenience function wich first acquires write access and then performs the computation. When the computation terminates, whether normally or by raising an exception, the write lock is released.
Since: 1.6.2.0
waitWrite :: MonadConc m => RWLock m -> m () Source #
- When the state is "write" or "read"
waitWrite
blocks until a call toreleaseWrite
orreleaseRead
in another thread changes the state to "free". - When the state is "free"
waitWrite
returns immediately.
waitWrite
does not alter the state of the lock.
Note that waitWrite
is just a convenience function defined as:
waitWrite l =mask_
$
acquireWrite
l>>
releaseWrite
l
Since: 1.6.2.0
Non-blocking
tryAcquireWrite :: MonadConc m => RWLock m -> m Bool Source #
Try to acquire the write lock; non blocking.
Like acquireWrite
, but doesn't block.
Returns True
if the resulting state is "write", False
otherwise.
Since: 1.6.2.0
tryWithWrite :: MonadConc m => RWLock m -> m a -> m (Maybe a) Source #
A non-blocking withWrite
. 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.
Since: 1.6.2.0