AVar-0.0.2: Mutable variables with Exception handling and concurrency support.

Data.AVar

Contents

Description

AVars are a form of transactional variables. They internally use a tail recursive function to carry the state of the variable, and allow for use in concurrent systems, where actions are quaranteed to happen. They are designed to cope with exceptions thrown by any modifying functions.

Synopsis

Types

data AVar a Source

AVars are the means through communication with the variable are conducted. They contain a Chan that is connected to the variable, and is read by the variable's handler function.

data Transaction a Source

A Transaction describes what should happen to a variable. They are only used internally, and are here just for reference.

Constructors

Put a

puts the a into the variable

Get (MVar a)

reads the variable

Mod (a -> a) (MVar (Maybe SomeException))

modifies the variable

forall b . Mod' (a -> (a, b)) (MVar (Either SomeException b))

modifies the variable, returning the b result to the caller

Atom (a -> Bool) (a -> a) (a -> a) (MVar (Either SomeException Bool))

conditionally modifies a variable

functions on AVars

newAVar :: a -> IO (AVar a)Source

newAVar creates a new variable. It forks off the handler that does the work for the variable itself and creates a new AVar.

putMVar :: MVar a -> a -> IO ()

Put a value into an MVar. If the MVar is currently full, putMVar will wait until it becomes empty.

There are two further important properties of putMVar:

  • putMVar is single-wakeup. That is, if there are multiple threads blocked in putMVar, and the MVar becomes empty, only one thread will be woken up. The runtime guarantees that the woken thread completes its putMVar operation.
  • When multiple threads are blocked on an MVar, they are woken up in FIFO order. This is useful for providing fairness properties of abstractions built using MVars.

modAVar :: AVar a -> (a -> a) -> IO (Maybe SomeException)Source

modAVar takes a function from a to a, and returns Nothing if nothing went wrong, or Just e, where e is an exception thrown by the function.

modAVar' :: AVar a -> (a -> (a, b)) -> IO (Either SomeException b)Source

modAVar' is like modAVar, but it modifies the variable, along with returning a result of type b, within an Either e b.

getAVar :: AVar a -> IO aSource

getAVar reads the current value inside the AVar.

condModAVar :: AVar a -> (a -> Bool) -> (a -> a) -> (a -> a) -> IO (Either SomeException Bool)Source

condModAVar applies the first finction to the current value in the AVar, and if true will modify the value using the second function if it results in True, or the third function if it results in Fasle.

swapAVar :: AVar a -> a -> IO (Either SomeException a)Source

swapAVar takes a new value, puts it into the AVar, and returns the old value.