HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulerebase-1.21.2Haskell2010

Rebase.Control.Concurrent.STM

  • 7 types
  • 67 values
  • Packagerebase-1.21.2
  • Exports74
  • LanguageHaskell2010
  • LicenceMIT
  • SourceSync.hs
newtypenewtype STM a
#

A monad supporting atomic memory transactions.

Instances20Monad, Functor, MonadFix, Applicative, Alternative, MonadPlus, …
datadata TArray i e
#

TArray is a transactional array, supporting the usual MArray interface for mutable arrays.

It is conceptually implemented as Array i (TVar e).

Instances3MArray, Eq
  • MArray TArray e STMDefined in stm-2.5.3.1 · Control.Concurrent.STM.TArray
  • MArray TArray e IODefined in stm-2.5.3.1 · Control.Concurrent.STM.TArray

    Writes are slow in IO.

  • (Eq i, Eq e) => Eq (TArray i e)Defined in stm-2.5.3.1 · Control.Concurrent.STM.TArray
datadata TBQueue a
#

TBQueue is an abstract type representing a bounded FIFO channel.

Instances1Eq
  • Eq (TBQueue a)Defined in stm-2.5.3.1 · Control.Concurrent.STM.TBQueue
datadata TChan a
#

TChan is an abstract type representing an unbounded FIFO channel.

Instances1Eq
  • Eq (TChan a)Defined in stm-2.5.3.1 · Control.Concurrent.STM.TChan
newtypenewtype TMVar a
#

A TMVar is a synchronising variable, used for communication between concurrent threads. It can be thought of as a box, which may be empty or full.

Instances1Eq
  • Eq (TMVar a)Defined in stm-2.5.3.1 · Control.Concurrent.STM.TMVar
datadata TQueue a
#

TQueue is an abstract type representing an unbounded FIFO channel.

Instances1Eq
  • Eq (TQueue a)Defined in stm-2.5.3.1 · Control.Concurrent.STM.TQueue
datadata TVar a
#

Shared memory locations that support atomic memory transactions.

Instances4Eq, HasGetter, HasSetter, HasUpdate
  • Eq (TVar a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Conc.Sync
  • HasGetter (TVar a) aDefined in StateVar-1.2.2 · Data.StateVar
  • HasSetter (TVar a) aDefined in StateVar-1.2.2 · Data.StateVar
  • HasUpdate (TVar a) a aDefined in StateVar-1.2.2 · Data.StateVar
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.

valuepeekTBQueue :: TBQueue a -> STM a
#

Get the next value from the TBQueue without removing it, retrying if the channel is empty.

valueunGetTBQueue :: TBQueue a -> a -> STM ()
#

Put a data item back onto a channel, where it will be the next item read. Blocks if the queue is full.

valuecloneTChan :: TChan a -> STM (TChan a)
#

Clone a TChan: similar to dupTChan, but the cloned channel starts with the same content available as the original channel.

valuedupTChan :: TChan a -> STM (TChan a)
#

Duplicate a TChan: the duplicate channel begins empty, but data written to either channel from then on will be available from both. Hence this creates a kind of broadcast channel, where data written by anyone is seen by everyone else.

valuenewBroadcastTChan :: STM (TChan a)
#

Create a write-only TChan. More precisely, readTChan will retry even after items have been written to the channel. The only way to read a broadcast channel is to duplicate it with dupTChan.

Consider a server that broadcasts messages to clients:

serve :: TChan Message -> Client -> IO loop
serve broadcastChan client = do
    myChan <- dupTChan broadcastChan
    forever $ do
        message <- readTChan myChan
        send client message

The problem with using newTChan to create the broadcast channel is that if it is only written to and never read, items will pile up in memory. By using newBroadcastTChan to create the broadcast channel, items can be garbage collected after clients have seen them.

valuepeekTChan :: TChan a -> STM a
#

Get the next value from the TChan without removing it, retrying if the channel is empty.

valueunGetTChan :: TChan a -> a -> STM ()
#

Put a data item back onto a channel, where it will be the next item read.

valuewriteTMVar :: TMVar a -> a -> STM ()
#

Non-blocking write of a new value to a TMVar Puts if empty. Replaces if populated.

valueflushTQueue :: TQueue a -> STM [a]
#

Efficiently read the entire contents of a TQueue into a list. This function never retries.

valuepeekTQueue :: TQueue a -> STM a
#

Get the next value from the TQueue without removing it, retrying if the channel is empty.

valueunGetTQueue :: TQueue a -> a -> STM ()
#

Put a data item back onto a channel, where it will be the next item read.

valuemodifyTVar :: TVar a -> (a -> a) -> STM ()
#

Mutate the contents of a TVar. N.B., this version is non-strict.

valuestateTVar :: TVar s -> (s -> (a, s)) -> STM a
#

Like modifyTVar' but the function is a simple state transition that can return a side value which is passed on as the result of the STM.

valuecheck :: Bool -> STM ()
#

Check that the boolean condition is true and, if not, retry.

In other words, check b = unless b retry.

valueregisterDelay :: Int -> IO (TVar Bool)
#

Switch the value of returned TVar from initial value False to True after a given number of microseconds. The caveats associated with threadDelay also apply.

Be careful not to exceed maxBound :: Int, which on 32-bit machines is only 2147483647 μs, less than 36 minutes.

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, newTChanIO, newBroadcastTChanIO, newTQueueIO, newTBQueueIO, and newTMVarIO.

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

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.

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.

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)

valuethrowSTM :: 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.