stm-chans-1.3.0: Additional types of channels for STM.

Portabilitynon-portable (STM, CPP)
Stabilityprovisional
Maintainerwren@community.haskell.org
Safe HaskellSafe-Infered

Control.Concurrent.STM.TMVar.Compat

Contents

Description

Compatibility layer for older versions of the stm library. Namely, we define tryReadTMVar which stm-X.X.X lacks. This module uses Cabal-style CPP macros in order to use the package versions when available. This isn't actually used by the stm-chans package, but we provide it anyways since we provide compatibility layers for TVar and TChan.

Since: 1.3.0

Synopsis

The TMVar type

data 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.

Instances

Creating TMVars

newTMVar :: a -> STM (TMVar a)

Create a TMVar which contains the supplied value.

newTMVarIO :: a -> IO (TMVar a)

IO version of newTMVar. This is useful for creating top-level TMVars using unsafePerformIO, because using atomically inside unsafePerformIO isn't possible.

newEmptyTMVar :: STM (TMVar a)

Create a TMVar which is initially empty.

newEmptyTMVarIO :: IO (TMVar a)

IO version of newEmptyTMVar. This is useful for creating top-level TMVars using unsafePerformIO, because using atomically inside unsafePerformIO isn't possible.

Reading from TMVars

readTMVar :: TMVar a -> STM a

This is a combination of takeTMVar and putTMVar; ie. it takes the value from the TMVar, puts it back, and also returns it.

tryReadTMVar :: TMVar a -> STM (Maybe a)Source

A version of readTMVar which does not retry. Instead it returns Nothing if no value is available.

takeTMVar :: TMVar a -> STM a

Return the contents of the TMVar. If the TMVar is currently empty, the transaction will retry. After a takeTMVar, the TMVar is left empty.

tryTakeTMVar :: TMVar a -> STM (Maybe a)

A version of takeTMVar that does not retry. The tryTakeTMVar function returns Nothing if the TMVar was empty, or Just a if the TMVar was full with contents a. After tryTakeTMVar, the TMVar is left empty.

Writing to TMVars

putTMVar :: TMVar a -> a -> STM ()

Put a value into a TMVar. If the TMVar is currently full, putTMVar will retry.

tryPutTMVar :: TMVar a -> a -> STM Bool

A version of putTMVar that does not retry. The tryPutTMVar function attempts to put the value a into the TMVar, returning True if it was successful, or False otherwise.

swapTMVar :: TMVar a -> a -> STM a

Swap the contents of a TMVar for a new value.

Other capabilities

isEmptyTMVar :: TMVar a -> STM Bool

Check whether a given TMVar is empty.

Notice that the boolean value returned is just a snapshot of the state of the TMVar. By the time you get to react on its result, the TMVar may have been filled (or emptied) - so be extremely careful when using this operation. Use tryTakeTMVar instead if possible.