concurrent-extra-0.7.0.12: Extra concurrency primitives

Copyright (c) 2010-2011 Bas van Dijk & Roel van Dijk BSD3 (see the file LICENSE) Bas van Dijk , Roel van Dijk Safe Haskell98

Description

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.

This module is designed to be imported qualified. We suggest importing it like:

import           Control.Concurrent.ReadWriteLock        ( RWLock )
import qualified Control.Concurrent.ReadWriteLock as RWL ( ... )


Synopsis

Documentation

data RWLock Source #

Multiple-reader, single-writer lock. Is in one of three states:

• "Free": Read or write access can be acquired without blocking.
• "Write": A single thread has acquired write access. Blocks other threads from acquiring both read and write access.

Instances

 Source # Methods(==) :: RWLock -> RWLock -> Bool #(/=) :: RWLock -> RWLock -> Bool #

Create a new RWLock in the "free" state; either read or write access can be acquired without blocking.

Create a new RWLock in the "read" state; only read can be acquired without blocking.

Create a new RWLock in the "write" state; either acquiring read or write will block.

Blocking

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 (maxBound :: Int) simultaneous threads acquire the read lock. But that is unlikely.

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.

withRead :: RWLock -> IO a -> IO 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.

waitRead :: RWLock -> IO () Source #

• When the state is "write", waitRead blocks until a call to releaseWrite 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:

Non-blocking

Try to acquire the write lock; non blocking.

Like acquireWrite, but doesn't block. Returns True if the resulting state is "write", False otherwise.

tryWithWrite :: RWLock -> IO a -> IO (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.