gochan-0.0.2: Go-style channels

Control.Concurrent.GoChan

Contents

Description

This module provides bounded channels similar to those seen in the Go programming language.

Synopsis

# Types

data Chan a Source #

The core data type. A Chan α provides a conduit through which messages of type α can be sent and received.

data Result a Source #

The Result record represents the result of waiting to recieve a message on a given channel.

Constructors

 Msg a Closed

data Case a Source #

When used with chanSelect, a Case α provides a means of waiting for a Send or Recv to complete on a given channel, subsequently invoking the given callback.

Recv (chan :: Chan β) (act :: Result β -> IO α)
Wait to receive a msg on chan, invoking act result when selected.
Send (chan :: Chan β) (msg :: β) (act :: IO α)
Wait to send msg on chan, invoking act when selected.

Constructors

 Recv (Chan b) (Result b -> IO a) Send (Chan b) b (IO a)

# Functions

chanMake :: Int -> IO (Chan a) Source #

Make a channel with the given buffer size.

chanClose :: Chan a -> IO () Source #

Close a channel.

chanRecv :: Chan a -> IO (Result a) Source #

Wait to receive a message on a channel.

chanTryRecv :: Chan a -> IO (Maybe (Result a)) Source #

Attempt to receive a message on a channel. A message will be recieved iff the channel has a message in its buffer or another thread is waiting to send a message on this channel.

chanSend :: Chan a -> a -> IO () Source #

Wait to successfully send a message on a channel.

Throws an exception when sending on a closed channel.

chanTrySend :: Chan a -> a -> IO Bool Source #

Attempt to send a message on a channel. The message will be sent iff the channel has spare space in its buffer or another thread is waiting to recieve a message on this channel.

Returns True iff the message was sent.

Throws an exception when sending on a closed channel.

Arguments

 :: [Case a] The list of cases. -> Maybe (IO a) When Nothing, wait synchronously to select a case; when Just act, run act as a default instead of blocking. -> IO a The value resulting from the selected Case (or default action, if given).

When no default action is given, blocks until one of the cases can run, then it executes that case. It chooses one at random if multiple are ready.

If given a default action, and no cases can run, immediately executes the default action.