sync-0.1: A fast implementation of synchronous channels with a CML-like API

Control.Concurrent.Sync

Description

This library supports synchronous message-passing with choice in Haskell. It is similar to the CML package for Haskell (http://hackage.haskell.org/package/cml), and shares a similar API. It avoids some of the problems the CML package has with choose on GHC 6.12.1 (http://www.haskell.org/pipermail/haskell-cafe/2010-March/074134.html), and also deliberately leaves out some of the features in the CML package.

The implementation is explained in this blog post: http://chplib.wordpress.com/2010/03/04/choice-over-events-using-stm/. The algorithm uses STM rather than spawning threads to implement choice.

At the moment the library is fairly unfeatured; if you want more features I would suggest using my more powerful CHP library (http://hackage.haskell.org/package/chp) -- but then I am biased!

Synopsis

Documentation

data Event a Source

A synchronisation that is yet to be executed (and that returns a value). The functor instance allows you to modify the value after the synchronisation has occurred.

Instances

data Channel a Source

A synchronous communication channel (i.e. the writer must wait until the read is willing to read the value). Should only ever be used by one writer and one reader -- the algorithm is not currently designed for anything else.

Instances

Eq (Channel a) 

choose :: [Event a] -> Event aSource

Creates an event that is the choice of the given list of events.

If the list is a singleton this is equivalent to calling head. If the list is empty, and you call sync on the resulting event, it will block forever (or GHC will throw you an exception because of it).

You should not pass more than one event from each channel in the list (nor combine two events that are themselves choices, such that you end up combining more than one event from a channel) or undefined behaviour will result.

send :: Channel a -> a -> Event ()Source

Creates an event that represents sending the given value on the given channel.

recv :: Channel a -> Event aSource

Creates an event that represents receiving a value from the given channel.

newChannel :: IO (Channel a)Source

Creates a new communication channel.

sync :: Event a -> IO aSource

Synchronises on an event. This blocks the thread until the Event can occur. This may be a choice of several different events, via the choose function.