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

Copyright (c) 2016 Michael Walker MIT Michael Walker stable portable None Haskell2010

Control.Concurrent.Classy.MVar

Contents

Description

An MVar t is mutable location that is either empty or contains a value of type t. It has two fundamental operations: putMVar which fills an MVar if it is empty and blocks otherwise, and takeMVar which empties an MVar if it is full and blocks otherwise. They can be used in multiple different ways:

1. As synchronized mutable variables,
2. As channels, with takeMVar and putMVar as receive and send, and
3. As a binary semaphore MVar (), with takeMVar and putMVar as wait and signal.

Deviations: There is no Eq instance for MonadConc the MVar type. Furthermore, the mkWeakMVar and addMVarFinalizer functions are not provided. Finally, normal MVars have a fairness guarantee, which dejafu does not currently make use of when generating schedules to test, so your program may be tested with unfair schedules.

Synopsis

# MVars

newEmptyMVar :: MonadConc m => m (MVar m a) Source #

Create a new empty MVar.

newEmptyMVar = newEmptyMVarN ""

newEmptyMVarN :: MonadConc m => String -> m (MVar m a) Source #

Create a new empty MVar, but it is given a name which may be used to present more useful debugging information.

If an empty name is given, a counter starting from 0 is used. If names conflict, successive MVars with the same name are given a numeric suffix, counting up from 1.

newEmptyMVarN _ = newEmptyMVar

newMVar :: MonadConc m => a -> m (MVar m a) Source #

Create a new MVar containing a value.

newMVarN :: MonadConc m => String -> a -> m (MVar m a) Source #

Create a new MVar containing a value, but it is given a name which may be used to present more useful debugging information.

If no name is given, a counter starting from 0 is used. If names conflict, successive MVars with the same name are given a numeric suffix, counting up from 1.

takeMVar :: MonadConc m => MVar m a -> m a Source #

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

putMVar :: MonadConc m => MVar m a -> a -> m () Source #

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

readMVar :: MonadConc m => MVar m a -> m a Source #

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

swapMVar :: MonadConc m => MVar m a -> a -> m a Source #

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

tryTakeMVar :: MonadConc m => MVar m a -> m (Maybe a) Source #

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

tryPutMVar :: MonadConc m => MVar m a -> a -> m Bool Source #

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

isEmptyMVar :: MonadConc m => MVar m a -> m Bool Source #

Check if a MVar is empty.

withMVar :: MonadConc m => MVar m a -> (a -> m b) -> m b Source #

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

withMVarMasked :: MonadConc m => MVar m a -> (a -> m b) -> m b Source #

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

modifyMVar_ :: MonadConc m => MVar m a -> (a -> m a) -> m () Source #

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

modifyMVar :: MonadConc m => MVar m a -> (a -> m (a, b)) -> m b Source #

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

modifyMVarMasked_ :: MonadConc m => MVar m a -> (a -> m a) -> m () Source #

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

modifyMVarMasked :: MonadConc m => MVar m a -> (a -> m (a, b)) -> m b Source #

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