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

Control.Concurrent.CHP.Channels.Communication

Description

All the channel-ends in CHP are instances of ReadableChannel (for ends that you can read from) or WriteableChannel (for ends that you can write to).

The readChannel and writeChannel functions are the standard way to communicate on a channel. These functions wait for the other party in the communication to arrive, then exchange the data, then complete. In pseudo-code, the semantics are like this when two parties (shown here as two columns) communicate:

 do sync                          sync
    x                        <-   return y
    done                          done

Further options are offered by the extReadChannel and extWriteChannel channels, which allow either side to perform additional (so-called extended) actions during the communication. The semantics when both sides are performing extended actions are:

 do sync                          sync
                                  y <- extWriteAction
    x                        <-   return y
    x' <- extReadAction x         done
    done                          done
    return x'

Neither end need know that the other is performing an extended action, and any combination is possible (e.g. a normal writeChannel with an extReadChannel).

Synopsis

Documentation

class ReadableChannel chanEnd whereSource

A class indicating that a channel can be read from.

Methods

readChannel :: chanEnd a -> CHP aSource

Reads from the given reading channel-end

extReadChannel :: chanEnd a -> (a -> CHP b) -> CHP bSource

Performs an extended read from the channel, performing the given action before freeing the writer

class WriteableChannel chanEnd whereSource

A class indicating that a channel can be written to.

Methods

writeChannel :: chanEnd a -> a -> CHP ()Source

Writes from the given writing channel-end

extWriteChannel :: chanEnd a -> CHP a -> CHP ()Source

Starts the communication, then performs the given extended action, then sends the result of that down the channel.

extWriteChannel' :: chanEnd a -> CHP (a, b) -> CHP bSource

Like extWriteChannel, but allows a value to be returned from the inner action.

This function was added in version 1.4.0.

writeValue :: WriteableChannel chanEnd => a -> chanEnd a -> CHP ()Source

A useful synonym for flip writeChannel. Especially useful with claim so that instead of writing claim output (flip writeChannel 6) you can write claim output (writeValue 6).

Added in version 1.5.0.

writeChannelStrict :: (NFData a, WriteableChannel chanEnd) => chanEnd a -> a -> CHP ()Source

A helper function that uses the parallel strategies library (see the paper: "Algorithm + Strategy = Parallelism", P.W. Trinder et al, JFP 8(1) 1998, http://www.macs.hw.ac.uk/~dsg/gph/papers/html/Strategies/strategies.html) to make sure that the value sent down a channel is strictly evaluated by the sender before transmission.

This is useful when you want to write worker processes that evaluate data and send it back to some "harvester" process. By default the values sent back may be unevaluated, and thus the harvester might end up doing the evaluation. If you use this function, the value is guaranteed to be completely evaluated before sending.

Added in version 1.0.2.