Copyright | (c) 2016 Michael Walker |
---|---|

License | MIT |

Maintainer | Michael Walker <mike@barrucadu.co.uk> |

Stability | stable |

Portability | portable |

Safe Haskell | Safe-Inferred |

Language | Haskell2010 |

An

is mutable location that is either empty or contains
a value of type `MVar`

t`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

, with`MVar`

()`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 `MVar`

s 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

- type family MVar m :: Type -> Type
- newEmptyMVar :: MonadConc m => m (MVar m a)
- newEmptyMVarN :: MonadConc m => String -> m (MVar m a)
- newMVar :: MonadConc m => a -> m (MVar m a)
- newMVarN :: MonadConc m => String -> a -> m (MVar m a)
- takeMVar :: MonadConc m => MVar m a -> m a
- putMVar :: MonadConc m => MVar m a -> a -> m ()
- readMVar :: MonadConc m => MVar m a -> m a
- swapMVar :: MonadConc m => MVar m a -> a -> m a
- tryTakeMVar :: MonadConc m => MVar m a -> m (Maybe a)
- tryPutMVar :: MonadConc m => MVar m a -> a -> m Bool
- isEmptyMVar :: MonadConc m => MVar m a -> m Bool
- withMVar :: MonadConc m => MVar m a -> (a -> m b) -> m b
- withMVarMasked :: MonadConc m => MVar m a -> (a -> m b) -> m b
- modifyMVar_ :: MonadConc m => MVar m a -> (a -> m a) -> m ()
- modifyMVar :: MonadConc m => MVar m a -> (a -> m (a, b)) -> m b
- modifyMVarMasked_ :: MonadConc m => MVar m a -> (a -> m a) -> m ()
- modifyMVarMasked :: MonadConc m => MVar m a -> (a -> m (a, b)) -> m b

`MVar`

s

type family MVar m :: Type -> Type Source #

The mutable reference type, like `MVar`

s. This may contain one
value at a time, attempting to read or take from an "empty"
`MVar`

will block until it is full, and attempting to put to a
"full" `MVar`

will block until it is empty.

*Since: 1.0.0.0*

#### Instances

type MVar IO Source # | |

Defined in Control.Monad.Conc.Class | |

type MVar (IsConc m) Source # | |

Defined in Control.Monad.Conc.Class | |

type MVar (IdentityT m) Source # | |

Defined in Control.Monad.Conc.Class | |

type MVar (ReaderT r m) Source # | |

Defined in Control.Monad.Conc.Class | |

type MVar (StateT s m) Source # | |

Defined in Control.Monad.Conc.Class | |

type MVar (StateT s m) Source # | |

Defined in Control.Monad.Conc.Class | |

type MVar (WriterT w m) Source # | |

Defined in Control.Monad.Conc.Class | |

type MVar (WriterT w m) Source # | |

Defined in Control.Monad.Conc.Class | |

type MVar (RWST r w s m) Source # | |

Defined in Control.Monad.Conc.Class | |

type MVar (RWST r w s m) Source # | |

Defined in Control.Monad.Conc.Class |

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

Create a new empty `MVar`

.

newEmptyMVar = newEmptyMVarN ""

*Since: 1.0.0.0*

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.

newEmptyMVarN _ = newEmptyMVar

*Since: 1.0.0.0*

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

Create a new `MVar`

containing a value.

*Since: 1.0.0.0*

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.

*Since: 1.0.0.0*

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.

*Since: 1.0.0.0*

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.

*Since: 1.0.0.0*

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

Block until a value is present in the `MVar`

, and then return
it. This does not "remove" the value, multiple reads are
possible.

*Since: 1.0.0.0*

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`

.

*Since: 1.0.0.0*

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

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`

.

*Since: 1.0.0.0*

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`

.

*Since: 1.0.0.0*

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.

*Since: 1.0.0.0*

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`

.

*Since: 1.0.0.0*

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`

.

*Since: 1.0.0.0*

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.

*Since: 1.0.0.0*

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.

*Since: 1.0.0.0*