dejafu-0.1.0.0: Overloadable primitives for testable, potentially non-deterministic, concurrency.

Safe HaskellSafe-Inferred
LanguageHaskell2010

Control.Concurrent.CVar

Contents

Description

Combinators using CVars. These provide many of the helpful functions found in Control.Concurrent.MVar, but for CVars.

Synopsis

CVars

newEmptyCVar :: MonadConc m => m (CVar m a) Source

Create a new empty CVar.

newCVar :: MonadConc m => a -> m (CVar m a) Source

Create a new CVar containing a value.

takeCVar :: MonadConc m => CVar m a -> m a Source

Take a value from a CVar. This "empties" the CVar, allowing a new value to be put in. This will block if there is no value in the CVar already, until one has been put.

putCVar :: MonadConc m => CVar m a -> a -> m () Source

Put a value into a CVar. If there is already a value there, this will block until that value has been taken, at which point the value will be stored.

readCVar :: MonadConc m => CVar m a -> m a Source

Block until a value is present in the CVar, and then return it. As with readMVar, this does not "remove" the value, multiple reads are possible.

swapCVar :: MonadConc m => CVar m a -> a -> m a Source

Swap the contents of a CVar, and return the value taken. This function is atomic only if there are no other producers fro this CVar.

tryTakeCVar :: MonadConc m => CVar m a -> m (Maybe a) Source

Attempt to take a value from a CVar non-blockingly, returning a Just (and emptying the CVar) if there was something there, otherwise returning Nothing.

tryPutCVar :: MonadConc m => CVar m a -> a -> m Bool Source

Attempt to put a value in a CVar non-blockingly, returning True (and filling the CVar) if there was nothing there, otherwise returning False.

isEmptyCVar :: MonadConc m => CVar m a -> m Bool Source

Check if a CVar is empty.

withCVar :: MonadConc m => CVar m a -> (a -> m b) -> m b Source

Operate on the contents of a CVar, replacing the contents after finishing. This operation is exception-safe: it will replace the original contents of the CVar if an exception is raised. However, it is only atomic if there are no other producers for this CVar.

withCVarMasked :: MonadConc m => CVar m a -> (a -> m b) -> m b Source

Like withCVar, but the IO action in the second argument is executed with asynchronous exceptions masked.

modifyCVar_ :: MonadConc m => CVar m a -> (a -> m a) -> m () Source

An exception-safe wrapper for modifying the contents of a CVar. Like withCVar, modifyCVar will replace the original contents of the CVar if an exception is raised during the operation. This function is only atomic if there are no other producers for this CVar.

modifyCVar :: MonadConc m => CVar m a -> (a -> m (a, b)) -> m b Source

A slight variation on modifyCVar_ that allows a value to be returned (b) in addition to the modified value of the CVar.

modifyCVarMasked_ :: MonadConc m => CVar m a -> (a -> m a) -> m () Source

Like modifyCVar_, but the IO action in the second argument is executed with asynchronous exceptions masked.

modifyCVarMasked :: MonadConc m => CVar m a -> (a -> m (a, b)) -> m b Source

Like modifyCVar, but the IO action in the second argument is executed with asynchronous exceptions masked.

Binary semaphores

A common use of CVars is in making binary semaphores to control mutual exclusion over a resource, so a couple of helper functions are provided.

lock :: MonadConc m => CVar m () -> m () Source

Put a () into a CVar, claiming the lock. This is atomic.

unlock :: MonadConc m => CVar m () -> m () Source

Empty a CVar, releasing the lock. This is atomic.