HORIZON HASKELLDocslts/ghc-10.0.xfc4e5982026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · fc4e598 · 2026-10-11

Moduleghc-internal-10.0.0Haskell2010

GHC.Internal.STM

  • 2 types
  • 11 values

the STM monad

7 declarations
newtypenewtype STM a
#

A monad supporting atomic memory transactions.

Constructors

Instances7Monad, Functor, Applicative, Alternative, MonadPlus, Semigroup, …
  • Monad STMDefined in ghc-internal-10.0.0 · GHC.Internal.STM
  • Functor STMDefined in ghc-internal-10.0.0 · GHC.Internal.STM
  • Applicative STMDefined in ghc-internal-10.0.0 · GHC.Internal.STM
  • Alternative STMDefined in ghc-internal-10.0.0 · GHC.Internal.STM

    Takes the first non-retrying STM action.

  • MonadPlus STMDefined in ghc-internal-10.0.0 · GHC.Internal.STM

    Takes the first non-retrying STM action.

  • Semigroup a => Semigroup (STM a)Defined in ghc-internal-10.0.0 · GHC.Internal.STM
  • Monoid a => Monoid (STM a)Defined in ghc-internal-10.0.0 · GHC.Internal.STM
valueatomically :: STM a -> IO a
#

Perform a series of STM actions atomically.

Using atomically inside an unsafePerformIO or unsafeInterleaveIO 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 inside unsafePerformIO or unsafeInterleaveIO, 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. See: newTVarIO, Control.Concurrent.STM.TChan.newTChanIO, Control.Concurrent.STM.TChan.newBroadcastTChanIO, Control.Concurrent.STM.TQueue.newTQueueIO, Control.Concurrent.STM.TBQueue.newTBQueueIO, and Control.Concurrent.STM.TMVar.newTMVarIO.

Using unsafePerformIO inside of atomically is also dangerous but for different reasons. See unsafeIOToSTM for more on this.

valueretry :: STM a
#

Retry execution of the current memory transaction because it has seen values in TVars which mean that it should not continue (e.g. the TVars represent a shared buffer that is now empty). The implementation may block the thread until one of the TVars that it has read from has been updated. (GHC only)

valueorElse :: 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. Otherwise, if the first action retries, then the second action is tried in its place. If both actions retry then the orElse as a whole retries.

valuethrowSTM :: (HasCallStack, Exception e) => e -> STM a
#

A variant of throw that can only be used within the STM monad.

Throwing an exception in STM aborts the transaction and propagates the exception. If the exception is caught via catchSTM, only the changes enclosed by the catch are rolled back; changes made outside of catchSTM persist.

If the exception is not caught inside of the STM, it is re-thrown by atomically, and the entire STM is rolled back.

Although throwSTM has a type that is an instance of the type of throw, 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 will only cause an exception to be raised when it is used within the STM monad. The throwSTM variant should be used in preference to throw to raise an exception within the STM monad because it guarantees ordering with respect to other STM operations, whereas throw does not.

valuecatchSTM :: Exception e => STM a -> (e -> STM a) -> STM a
#

Exception handling within STM actions.

catchSTM m f catches any exception thrown by m using throwSTM, using the function f to handle the exception. If an exception is thrown, any changes made by m are rolled back, but changes prior to m persist.

valueunsafeIOToSTM :: IO a -> STM a
#

Unsafely performs IO in the STM monad. Beware: this is a highly dangerous thing to do.

  • The STM implementation will often run transactions multiple times, so you need to be prepared for this if your IO has any side effects.

  • The STM implementation will abort transactions that are known to be invalid and need to be restarted. This may happen in the middle of unsafeIOToSTM, so make sure you don't acquire any resources that need releasing (exception handlers are ignored when aborting the transaction). That includes doing any IO using Handles, for example. Getting this wrong will probably lead to random deadlocks.

  • The transaction may have seen an inconsistent view of memory when the IO runs. Invariants that you expect to be true throughout your program may not be true inside a transaction, due to the way transactions are implemented. Normally this wouldn't be visible to the programmer, but using unsafeIOToSTM can expose it.

TVars

6 declarations
datadata TVar a
#

Shared memory locations that support atomic memory transactions.

Constructors

Instances1Eq
  • Eq (TVar a)Defined in ghc-internal-10.0.0 · GHC.Internal.STM
valuenewTVarIO :: a -> IO (TVar a)
#

IO version of newTVar. This is useful for creating top-level TVars using System.IO.Unsafe.unsafePerformIO, because using atomically inside System.IO.Unsafe.unsafePerformIO isn't possible.

valuereadTVarIO :: TVar a -> IO a
#

Return the current value stored in a TVar. This is equivalent to

 readTVarIO = atomically . readTVar

but works much faster, because it doesn't perform a complete transaction, it just reads the current value of the TVar.