monadIO- Overloading of concurrency variables

Portabilityconcurrency, requires STM
MaintainerJohn Launchbury,
Safe HaskellNone



Overloads the standard operations on TVars, and TMVars as defined in Control.Concurrent.STM.

TVars and MVars are often thought of as variables to be used in the STM monad. But in practice, they should be used just as frequently (if not more so) in any IO-like monad, with STM being used purely when a new atomic transaction is being defined. Thus we reverse the naming convention, and use the plain access names when in the IO-like monad, and use an explicit STM suffix when using the variables tentatively within the STM monad itself.

TMVars are particularly valuable when used in an IO-like monad, because operations like readTMVar and modifyTMvar can guarantee the atomicity of the operation (unlike the corresponding operations over MVars).

The standard operations on TVar and TMVar (such as writeTVar or newEmptyTMVar) are overloaded over the MonadIO class. A monad m is declared an instance of MonadIO by defining a function

 liftIO :: IO a -> m a

It also overloads the atomically function, so that STM transactions can be defined from within any MonadIO monad.



data STM a

A monad supporting atomic memory transactions.

atomically :: MonadIO io => STM a -> io aSource

The atomically function allows STM to be called directly from any monad which contains IO, i.e. is a member of MonadIO.

always :: STM Bool -> STM ()


alwaysSucceeds :: STM a -> STM ()


retry :: STM a




check :: Bool -> STM ()


Exception handling within STM actions.

data TVar a

Shared memory locations that support atomic memory transactions.


newTVar :: MonadIO io => a -> io (TVar a)Source

readTVar :: MonadIO io => TVar a -> io aSource

writeTVar :: MonadIO io => TVar a -> a -> io ()Source

registerDelay :: MonadIO io => Int -> io (TVar Bool)Source

modifyTVar :: MonadIO io => TVar a -> (a -> a) -> io (a, a)Source

modifyTVar is an atomic update operation which provides both the former value and the newly computed value as a result.


newTVarSTM :: a -> STM (TVar a)Source

readTVarSTM :: TVar a -> STM aSource

writeTVarSTM :: TVar a -> a -> STM ()Source

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.


newTMVar :: MonadIO io => a -> io (TMVar a)Source

newEmptyTMVar :: MonadIO io => io (TMVar a)Source

takeTMVar :: MonadIO io => TMVar a -> io aSource

putTMVar :: MonadIO io => TMVar a -> a -> io ()Source

readTMVar :: MonadIO io => TMVar a -> io aSource

swapTMVar :: MonadIO io => TMVar a -> a -> io aSource

tryTakeTMVar :: MonadIO io => TMVar a -> io (Maybe a)Source

tryPutTMVar :: MonadIO io => TMVar a -> a -> io BoolSource

isEmptyTMVar :: MonadIO io => TMVar a -> io BoolSource

modifyTMVar :: MonadIO io => TMVar a -> (a -> a) -> io (a, a)Source


newTMVarSTM :: a -> STM (TMVar a)Source

takeTMVarSTM :: TMVar a -> STM aSource

putTMVarSTM :: TMVar a -> a -> STM ()Source

readTMVarSTM :: TMVar a -> STM aSource

swapTMVarSTM :: TMVar a -> a -> STM aSource

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

tryPutTMVarSTM :: TMVar a -> a -> STM BoolSource