MVar package:concurrency
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:
- As synchronized mutable variables,
- As channels, with takeMVar and putMVar as receive
and send, and
- 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.
Check if a
MVar is empty.
The boolean value returned is just a snapshot of the state of the
MVar, it may have been emptied (or filled) by the time you
actually access it. Generally prefer
tryPutMVar,
tryTakeMVar, and
tryReadMVar.
A slight variation on
modifyMVar_ that allows a value to be
returned (
b) in addition to the modified value of the
MVar.
Like
modifyMVar, but the
IO action in the second
argument is executed with asynchronous exceptions masked.
Like
modifyMVar_, but the
IO action in the second
argument is executed with asynchronous exceptions masked.
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.
Create a new empty
MVar.
newEmptyMVar = newEmptyMVarN ""
Create a new empty
MVar, but it is given a name which may be
used to present more useful debugging information.
newEmptyMVarN _ = newEmptyMVar
Create a new MVar containing a value.
Create a new MVar containing a value, but it is given a name
which may be used to present more useful debugging information.
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.
Block until a value is present in the MVar, and then return
it. This does not "remove" the value, multiple reads are possible.
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.
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.
Attempt to put a value in a
MVar non-blockingly, returning
True (and filling the
MVar) if there was nothing
there, otherwise returning
False.
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.
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.
Like
withMVar, but the
IO action in the second
argument is executed with asynchronous exceptions masked.
Transactional
MVars, for use with
MonadSTM.
Deviations: TMVar as defined here does not have an
Eq instance, this is because the
MonadSTM
TVar type does not have an
Eq constraint.
Furthermore, the
newTMVarIO,
newEmptyTMVarIO, and
mkWeakTMVar functions are not provided.
A TMVar is like an MVar or a mVar, but
using transactional memory. As transactions are atomic, this makes
dealing with multiple TMVars easier than wrangling multiple
mVars.
Check if a
TMVar is empty or not.
Create a new empty
TMVar.
Create a new empty
TMVar with the given name.
Name conflicts are handled as usual for
TVars. The name is
prefixed with "ctmvar-".
Create a
TMVar containing the given value.
Create a
TMVar containing the given value, with the given name.
Name conflicts are handled as usual for
TVars. The name is
prefixed with "ctmvar-".