Control.Monad.STM (original) (raw)
Description
Software Transactional Memory: a modular composable concurrency abstraction. See
- Composable memory transactions, by Tim Harris, Simon Marlow, Simon Peyton Jones, and Maurice Herlihy, in_ACM Conference on Principles and Practice of Parallel Programming_ 2005.https://www.microsoft.com/en-us/research/publication/composable-memory-transactions/
This module only defines the [STM](Control-Monad-STM.html#t:STM "Control.Monad.STM")
monad; you probably want to import Control.Concurrent.STM (which exports Control.Monad.STM).
Note that invariant checking (namely the always
and alwaysSucceeds
functions) has been removed. See ticket #14324 and the removal proposal. Existing users are encouraged to encapsulate their STM operations in safe abstractions which can perform the invariant checking without help from the runtime system.
Synopsis
- data STM a
- atomically :: STM a -> IO a
- retry :: STM a
- orElse :: STM a -> STM a -> STM a
- check :: Bool -> STM ()
- throwSTM :: Exception e => e -> STM a
- catchSTM :: Exception e => STM a -> (e -> STM a) -> STM a
Documentation
data STM a #
A monad supporting atomic memory transactions.
Perform a series of STM actions atomically.
Using [atomically](Control-Monad-STM.html#v:atomically "Control.Monad.STM")
inside an [unsafePerformIO](/package/base-4.18.1.0/docs/System-IO-Unsafe.html#v:unsafePerformIO "System.IO.Unsafe")
or [unsafeInterleaveIO](/package/base-4.18.1.0/docs/System-IO-Unsafe.html#v:unsafeInterleaveIO "System.IO.Unsafe")
subverts some of guarantees that STM provides. It makes it possible to run a transaction inside of another transaction, depending on when the thunk is evaluated. If a nested transaction is attempted, an exception is thrown by the runtime. It is possible to safely use [atomically](Control-Monad-STM.html#v:atomically "Control.Monad.STM")
inside[unsafePerformIO](/package/base-4.18.1.0/docs/System-IO-Unsafe.html#v:unsafePerformIO "System.IO.Unsafe")
or [unsafeInterleaveIO](/package/base-4.18.1.0/docs/System-IO-Unsafe.html#v:unsafeInterleaveIO "System.IO.Unsafe")
, but the typechecker does not rule out programs that may attempt nested transactions, meaning that the programmer must take special care to prevent these.
However, there are functions for creating transactional variables that can always be safely called in [unsafePerformIO](/package/base-4.18.1.0/docs/System-IO-Unsafe.html#v:unsafePerformIO "System.IO.Unsafe")
. See: [newTVarIO](Control-Concurrent-STM-TVar.html#v:newTVarIO "Control.Concurrent.STM.TVar")
,[newTChanIO](Control-Concurrent-STM-TChan.html#v:newTChanIO "Control.Concurrent.STM.TChan")
,[newBroadcastTChanIO](Control-Concurrent-STM-TChan.html#v:newBroadcastTChanIO "Control.Concurrent.STM.TChan")
,[newTQueueIO](Control-Concurrent-STM-TQueue.html#v:newTQueueIO "Control.Concurrent.STM.TQueue")
,[newTBQueueIO](Control-Concurrent-STM-TBQueue.html#v:newTBQueueIO "Control.Concurrent.STM.TBQueue")
, and[newTMVarIO](Control-Concurrent-STM-TMVar.html#v:newTMVarIO "Control.Concurrent.STM.TMVar")
.
Using [unsafePerformIO](/package/base-4.18.1.0/docs/System-IO-Unsafe.html#v:unsafePerformIO "System.IO.Unsafe")
inside of [atomically](Control-Monad-STM.html#v:atomically "Control.Monad.STM")
is also dangerous but for different reasons. See [unsafeIOToSTM](/package/base-4.18.1.0/docs/GHC-Conc.html#v:unsafeIOToSTM "GHC.Conc")
for more on this.
Retry execution of the current memory transaction because it has seen values in [TVar](Control-Concurrent-STM-TVar.html#t:TVar "Control.Concurrent.STM.TVar")
s which mean that it should not continue (e.g. the [TVar](Control-Concurrent-STM-TVar.html#t:TVar "Control.Concurrent.STM.TVar")
s represent a shared buffer that is now empty). The implementation may block the thread until one of the [TVar](Control-Concurrent-STM-TVar.html#t:TVar "Control.Concurrent.STM.TVar")
s that it has read from has been updated. (GHC only)
orElse :: STM a -> STM a -> STM a #
Compose two alternative STM actions (GHC only).
If the first action completes without retrying then it forms the result of the [orElse](Control-Monad-STM.html#v:orElse "Control.Monad.STM")
. Otherwise, if the first action retries, then the second action is tried in its place. If both actions retry then the [orElse](Control-Monad-STM.html#v:orElse "Control.Monad.STM")
as a whole retries.
check :: Bool -> STM () Source #
Check that the boolean condition is true and, if not, [retry](Control-Monad-STM.html#v:retry "Control.Monad.STM")
.
In other words, check b = unless b retry
.
Since: 2.1.1
throwSTM :: Exception e => e -> STM a #
A variant of [throw](/package/base-4.18.1.0/docs/Control-Exception-Base.html#v:throw "Control.Exception.Base")
that can only be used within the [STM](Control-Monad-STM.html#t:STM "Control.Monad.STM")
monad.
Throwing an exception in STM
aborts the transaction and propagates the exception. If the exception is caught via [catchSTM](Control-Monad-STM.html#v:catchSTM "Control.Monad.STM")
, only the changes enclosed by the catch are rolled back; changes made outside of [catchSTM](Control-Monad-STM.html#v:catchSTM "Control.Monad.STM")
persist.
If the exception is not caught inside of the [STM](Control-Monad-STM.html#t:STM "Control.Monad.STM")
, it is re-thrown by[atomically](Control-Monad-STM.html#v:atomically "Control.Monad.STM")
, and the entire [STM](Control-Monad-STM.html#t:STM "Control.Monad.STM")
is rolled back.
Although [throwSTM](Control-Monad-STM.html#v:throwSTM "Control.Monad.STM")
has a type that is an instance of the type of [throw](/package/base-4.18.1.0/docs/Control-Exception-Base.html#v:throw "Control.Exception.Base")
, the two functions are subtly different:
throw e seq
x ===> throw e
throwSTM e seq
x ===> x
The first example will cause the exception e
to be raised, whereas the second one won't. In fact, [throwSTM](Control-Monad-STM.html#v:throwSTM "Control.Monad.STM")
will only cause an exception to be raised when it is used within the [STM](Control-Monad-STM.html#t:STM "Control.Monad.STM")
monad. The [throwSTM](Control-Monad-STM.html#v:throwSTM "Control.Monad.STM")
variant should be used in preference to [throw](/package/base-4.18.1.0/docs/Control-Exception-Base.html#v:throw "Control.Exception.Base")
to raise an exception within the [STM](Control-Monad-STM.html#t:STM "Control.Monad.STM")
monad because it guarantees ordering with respect to other [STM](Control-Monad-STM.html#t:STM "Control.Monad.STM")
operations, whereas [throw](/package/base-4.18.1.0/docs/Control-Exception-Base.html#v:throw "Control.Exception.Base")
does not.
catchSTM :: Exception e => STM a -> (e -> STM a) -> STM a #
Exception handling within STM actions.
`[catchSTM](Control-Monad-STM.html#v:catchSTM "Control.Monad.STM")` m f
catches any exception thrown by m
using [throwSTM](Control-Monad-STM.html#v:throwSTM "Control.Monad.STM")
, using the function f
to handle the exception. If an exception is thrown, any changes made by m
are rolled back, but changes prior tom
persist.