Copyright | (c) 2016 Michael Walker |
---|---|
License | MIT |
Maintainer | Michael Walker <[email protected]> |
Stability | experimental |
Portability | CPP, RankNTypes, TemplateHaskell, TypeFamilies |
Safe Haskell | Safe |
Language | Haskell2010 |
Control.Monad.STM.Class
Contents
Description
This module provides an abstraction over STM
, which can be used
with MonadConc
.
This module only defines the STM
class; you probably want to
import Control.Concurrent.Classy.STM (which exports
Control.Monad.STM.Class).
Deviations: An instance of MonadSTM
is not required to be an
Alternative
, MonadPlus
, and MonadFix
, unlike STM
. The
always
and alwaysSucceeds
functions are not provided; if you
need these file an issue and I'll look into it.
- class MonadCatch stm => MonadSTM stm where
- check :: MonadSTM stm => Bool -> stm ()
- throwSTM :: (MonadSTM stm, Exception e) => e -> stm a
- catchSTM :: (MonadSTM stm, Exception e) => stm a -> (e -> stm a) -> stm a
- liftedOrElse :: (MonadTransControl t, MonadSTM stm) => (forall x. StT t x -> x) -> t stm a -> t stm a -> t stm a
Documentation
class MonadCatch stm => MonadSTM stm where Source #
MonadSTM
is an abstraction over STM
.
This class does not provide any way to run transactions, rather
each MonadConc
has an associated MonadSTM
from which it can
atomically run a transaction.
Associated Types
type TVar stm :: * -> * Source #
The mutable reference type. These behave like TVar
s, in that
they always contain a value and updates are non-blocking and
synchronised.
Methods
Retry execution of this transaction because it has seen values
in TVar
s that it shouldn't have. This will result in the
thread running the transaction being blocked until any TVar
s
referenced in it have been mutated.
orElse :: stm a -> stm a -> stm a Source #
Run the first transaction and, if it retry
s, run the second
instead. If the monad is an instance of
'Alternative'/'MonadPlus', orElse
should be the '(|)'/'mplus'
function.
newTVar :: a -> stm (TVar stm a) Source #
Create a new TVar
containing the given value.
newTVar = newTVarN ""
newTVarN :: String -> a -> stm (TVar stm a) Source #
Create a new TVar
containing the given value, 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 TVar
s with the same name are given
a numeric suffix, counting up from 1.
newTVarN _ = newTVar
readTVar :: TVar stm a -> stm a Source #
Return the current value stored in a TVar
.
writeTVar :: TVar stm a -> a -> stm () Source #
Write the supplied value into the TVar
.
Instances
MonadSTM STM Source # | |
MonadSTM stm => MonadSTM (StateT s stm) Source # | |
MonadSTM stm => MonadSTM (StateT s stm) Source # | |
(MonadSTM stm, Monoid w) => MonadSTM (WriterT w stm) Source # | |
(MonadSTM stm, Monoid w) => MonadSTM (WriterT w stm) Source # | |
MonadSTM stm => MonadSTM (IdentityT * stm) Source # | |
MonadSTM stm => MonadSTM (ReaderT * r stm) Source # | |
(MonadSTM stm, Monoid w) => MonadSTM (RWST r w s stm) Source # | |
(MonadSTM stm, Monoid w) => MonadSTM (RWST r w s stm) Source # | |
check :: MonadSTM stm => Bool -> stm () Source #
Check whether a condition is true and, if not, call retry
.
throwSTM :: (MonadSTM stm, Exception e) => e -> stm a Source #
Throw an exception. This aborts the transaction and propagates the exception.
catchSTM :: (MonadSTM stm, Exception e) => stm a -> (e -> stm a) -> stm a Source #
Handling exceptions from throwSTM
.
Utilities for instance writers
liftedOrElse :: (MonadTransControl t, MonadSTM stm) => (forall x. StT t x -> x) -> t stm a -> t stm a -> t stm a Source #
Given a function to remove the transformer-specific state, lift
an orElse
invocation.