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

Modulestm-chans-3.0.0.9Haskell2010

Control.Concurrent.STM.TBChan

A version of Control.Concurrent.STM.TChan where the queue is bounded in length. This variant incorporates ideas from Thomas M. DuBuisson's bounded-tchan package in order to reduce contention between readers and writers.

  • 1 type
  • 13 values
  • Packagestm-chans-3.0.0.9
  • Exports14
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceTBChan.hs

The TBChan type

1 declaration
datadata TBChan a
#

TBChan is an abstract type representing a bounded FIFO channel.

Creating TBChans

valuenewTBChan :: Int -> STM (TBChan a)
#

Build and returns a new instance of TBChan with the given capacity. N.B., we do not verify the capacity is positive, but if it is non-positive then writeTBChan will always retry and isFullTBChan will always be true.

Reading from TBChans

valuereadTBChan :: TBChan a -> STM a
#

Read the next value from the TBChan, retrying if the channel is empty.

valuepeekTBChan :: TBChan a -> STM a
#

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

Writing to TBChans

valuewriteTBChan :: TBChan a -> a -> STM ()
#

Write a value to a TBChan, retrying if the channel is full.

valueunGetTBChan :: TBChan a -> a -> STM ()
#

Put a data item back onto a channel, where it will be the next item read. N.B., this could allow the channel to temporarily become longer than the specified limit, which is necessary to ensure that the item is indeed the next one read.

Predicates

valueisEmptyTBChan :: TBChan a -> STM Bool
#

Returns True if the supplied TBChan is empty (i.e., has no elements). N.B., a TBChan can be both "empty" and "full" at the same time, if the initial limit was non-positive.

valueisFullTBChan :: TBChan a -> STM Bool
#

Returns True if the supplied TBChan is full (i.e., is over its limit). N.B., a TBChan can be both "empty" and "full" at the same time, if the initial limit was non-positive. N.B., a TBChan may still be full after reading, if unGetTBChan was used to go over the initial limit.

This is equivalent to: liftM (<= 0) estimateFreeSlotsTBMChan

Other functionality

Estimate the number of free slots. If the result is positive, then it's a minimum bound; if it's non-positive then it's exact. It will only be negative if the initial limit was negative or if unGetTBChan was used to go over the initial limit.

This function always contends with writers, but only contends with readers when it has to; compare against freeSlotsTBChan.