Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Combinators using CVar
s. These provide many of the helpful
functions found in Control.Concurrent.MVar, but for CVar
s.
- newEmptyCVar :: MonadConc m => m (CVar m a)
- newCVar :: MonadConc m => a -> m (CVar m a)
- takeCVar :: MonadConc m => CVar m a -> m a
- putCVar :: MonadConc m => CVar m a -> a -> m ()
- readCVar :: MonadConc m => CVar m a -> m a
- swapCVar :: MonadConc m => CVar m a -> a -> m a
- tryTakeCVar :: MonadConc m => CVar m a -> m (Maybe a)
- tryPutCVar :: MonadConc m => CVar m a -> a -> m Bool
- isEmptyCVar :: MonadConc m => CVar m a -> m Bool
- withCVar :: MonadConc m => CVar m a -> (a -> m b) -> m b
- withCVarMasked :: MonadConc m => CVar m a -> (a -> m b) -> m b
- modifyCVar_ :: MonadConc m => CVar m a -> (a -> m a) -> m ()
- modifyCVar :: MonadConc m => CVar m a -> (a -> m (a, b)) -> m b
- modifyCVarMasked_ :: MonadConc m => CVar m a -> (a -> m a) -> m ()
- modifyCVarMasked :: MonadConc m => CVar m a -> (a -> m (a, b)) -> m b
- lock :: MonadConc m => CVar m () -> m ()
- unlock :: MonadConc m => CVar m () -> m ()
CVar
s
newEmptyCVar :: MonadConc m => m (CVar m a) Source
Create a new empty CVar
.
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
tryPutCVar :: MonadConc m => CVar m a -> a -> m Bool Source
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 CVar
s is in making binary semaphores to
control mutual exclusion over a resource, so a couple of helper
functions are provided.