chp-2.2.0: An implementation of concurrency ideas from Communicating Sequential Processes

Control.Concurrent.CHP.Monad

Contents

Description

This module contains all the central monads in the CHP library.

Synopsis

CHP Monad

data CHP a Source

The central monad of the library. You can use Control.Concurrent.CHP.Monad.runCHP and Control.Concurrent.CHP.Monad.runCHP_ to execute programs in this monad.

The Alternative instance was added in version 2.2.0.

class Monad m => MonadCHP m whereSource

A monad transformer class that is very similar to MonadIO. This can be useful if you want to add monad transformers (such as StateT, ReaderT) on top of the CHP monad.

Methods

liftCHP :: CHP a -> m aSource

Instances

liftIO_CHP :: IO a -> CHP aSource

An implementation of liftIO for the CHP monad; this function lifts IO actions into the CHP monad.

Added in version 2.2.0.

runCHP :: CHP a -> IO (Maybe a)Source

Runs a CHP program. You should use this once, at the top-level of your program. Do not ever use this function twice in parallel and attempt to communicate between those processes using channels. Instead, run this function once and use it to spawn off the parallel processes that you need.

runCHP_ :: CHP a -> IO ()Source

Runs a CHP program. Like runCHP but discards the output.

embedCHP :: CHP a -> CHP (IO (Maybe a))Source

Allows embedding of the CHP monad back into the IO monad. The argument that this function takes is a CHP action (with arbitrary behaviour). The function is monadic, and returns something of type: IO a. This is an IO action that you can now use in the IO monad wherever you like. What it returns is the result of your original action.

This function is intended for use wherever you need to supply an IO callback (for example with the OpenGL libraries) that needs to perform CHP communications. It is the safe way to do things, rather than using runCHP twice (and also works with CSP and VCR traces -- but not structural traces!).

embedCHP_ :: CHP a -> CHP (IO ())Source

A convenient version of embedCHP that ignores the result

embedCHP1 :: (a -> CHP b) -> CHP (a -> IO (Maybe b))Source

A helper like embedCHP for callbacks that take an argument

embedCHP1_ :: (a -> CHP b) -> CHP (a -> IO ())Source

A convenient version of embedCHP1 that ignores the result

onPoisonTrap :: CHP a -> CHP a -> CHP aSource

Allows you to provide a handler for sections with poison. It is usually used in an infix form as follows:

 (readChannel c >>= writeChannel d) `onPoisonTrap` (poison c >> poison d)

It handles the poison and does not rethrow it (unless your handler does so). If you want to rethrow (and actually, you'll find you usually do), use onPoisonRethrow

onPoisonRethrow :: CHP a -> CHP () -> CHP aSource

Like onPoisonTrap, this function allows you to provide a handler for poison. The difference with this function is that even if the poison handler does not throw, the poison exception will always be re-thrown after the handler anyway. That is, the following lines of code all have identical behaviour:

 foo
 foo `onPoisonRethrow` throwPoison
 foo `onPoisonRethrow` return ()

throwPoison :: CHP aSource

Throws a poison exception.

class Poisonable c whereSource

A class indicating that something is poisonable.

Methods

poison :: MonadCHP m => c -> m ()Source

Poisons the given item.

checkForPoison :: MonadCHP m => c -> m ()Source

Checks if the given item is poisoned. If it is, a poison exception will be thrown.

Added in version 1.0.2.

poisonAll :: (Poisonable c, MonadCHP m) => [c] -> m ()Source

Poisons all the given items. A handy shortcut for mapM_ poison.

Primitive actions

skip :: CHP ()Source

The classic skip process/guard. Does nothing, and is always ready.

Suitable for use in an Control.Concurrent.CHP.Alt.alt.

stop :: CHP aSource

The stop guard. Its main use is that it is never ready in a choice, so can be used to mask out guards. If you actually execute stop, that process will do nothing more. Any parent process waiting for it to complete will wait forever.

The type of this function was generalised in CHP 1.6.0.

waitFor :: Int -> CHP ()Source

Waits for the specified number of microseconds (millionths of a second). There is no guaranteed precision, but the wait will never complete in less time than the parameter given.

Suitable for use in an Control.Concurrent.CHP.Alt.alt, but note that waitFor 0 is not the same as skip. waitFor 0 Control.Concurrent.CHP.Alt.</> x will not always select the first guard, depending on x. Included in this is the lack of guarantee that waitFor 0 Control.Concurrent.CHP.Alt.</> waitFor n will select the first guard for any value of n (including 0). It is not useful to use two waitFor guards in a single Control.Concurrent.CHP.Alt.alt anyway.

NOTE: If you wish to use this as part of a choice, you must use -threaded as a GHC compilation option (at least under 6.8.2).

chp-spec items

foreverP :: CHP a -> CHP bSource

Exactly the same as forever. Useful only because it mirrors a different definition from the chp-spec library.

Added in version 2.2.0.

liftIO_CHP' :: String -> IO a -> CHP aSource

Acts as const liftIO_CHP. Useful only because it mirrors a different definition in the chp-spec library.

Added in version 2.2.0.

process :: String -> a -> aSource

Acts as const id. Useful only because it mirrors a different definition in the chp-spec library.

Added in version 2.2.0.

subProcess :: String -> a -> aSource

Acts as const id. Useful only because it mirrors a different definition in the chp-spec library.

Added in version 2.2.0.