split-channel-0.1.0.0: Control.Concurrent.Chan split into sending and receiving halves.

Maintainerleon@melding-monads.com
Safe HaskellSafe-Infered

Control.Concurrent.Chan.Split

Description

This package provides an unbounded, imperative queue that is supposed to be thread safe and asynchronous exception safe. It is essentially the same communication abstraction as Chan, except that each channel is split into separate sending and receiving ends, called SendPort and ReceivePort respectively. This has at least two advantages:

  1. Program behavior can be more finely constrained via the type system.
  2. Channels can have zero ReceivePorts associated with them. Messages written to such a channel disappear into the aether and can be garbage collected. Note that ReceivePorts can be subsequently attached to such a channel via listen.

A channel can have multiple ReceivePorts. This results in a publish- subscribe pattern of communication: every element will be delivered to every port. Alternatively, multiple threads can read from a single port. This results in a push-pull pattern of communication, similar to ZeroMQ: every element will be delivered to exactly one thread. Of course both can be used together to form hybrid patterns of communication.

A channel can only have one SendPort. However multiple threads can can safely write to a single port, allowing effects similar to multiple SendPorts.

Some of the tradeoffs of split-channel compared to Cloud Haskell's Remote.Channel are:

  1. split-channel is restricted to in-process communications only.
  2. split-channel has no restriction on the type of values that may be communicated.
  3. There is a quasi-duality between the two approaches: Cloud Haskell's ReceivePorts are special whereas split-channel's SendPorts are special.
  4. ReceivePorts can be duplicated, which allows for considerably more efficient publish-subscribe communications than supported by Cloud Haskell at the present time.

Synopsis

Documentation

data SendPort a Source

SendPorts represent one end of the channel. There is only one SendPort per channel, though it can be used from multiple threads. Messages can be sent to the channel using send.

data ReceivePort a Source

ReceivePorts represent the other end of a channel. A channel can have many ReceivePorts, which all receive the same messages in a publish/subscribe like manner. A single ReceivePort can be used from multiple threads, where every message will be delivered to a single thread in a push/pull like manner. Use receive to fetch messages from the channel.

new :: IO (SendPort a, ReceivePort a)Source

Creates a new channel and a (SendPort, ReceivePort) pair representing the two sides of the channel.

newSendPort :: IO (SendPort a)Source

Produces a new channel that initially has zero ReceivePorts. Any elements written to this channel before a reader is listening will be eligible for garbage collection.

listen :: SendPort a -> IO (ReceivePort a)Source

Create a new ReceivePort attached the same channel as a given SendPort. This ReceivePort starts out empty, and remains so until more elements are written to the SendPort.

duplicate :: ReceivePort a -> IO (ReceivePort a)Source

Create a new ReceivePort attached to the same channel as another ReceivePort. These two ports will receive the same messages. Any messages in the channel that have not been consumed by the existing port will also appear in the new port.

receive :: ReceivePort a -> IO aSource

Fetch an element from a channel. If no element is available, it blocks until one is. Can be used in conjunction with System.Timeout.

send :: SendPort a -> a -> IO ()Source

Send an element to a channel. This is asynchronous and does not block.

fold :: (a -> b -> b) -> ReceivePort a -> IO bSource

A right fold over a receiver, a generalization of getChanContents where getChanContents = fold (:). Note that the type of fold implies that the folding function needs to be sufficienctly non-strict, otherwise the result cannot be productive.

unsafeFold :: (a -> b -> b) -> ReceivePort a -> IO bSource

unsafeFold should usually be called only on readers that are not subsequently used in other channel operations. Otherwise it may be possible that the (non-)evaluation of pure values will cause race conditions inside IO computations. The safer fold uses duplicate to satisfy this condition.