epass-0.2.1: Baisc, Erlang-like message passing supporting sockets.

Control.Concurrent.Mailbox

Contents

Description

This module provides Erlang like functionality for message passing.

Instead of mailboxes attached to each process you have to create the needed mailboxes yourself. This means that messages cannot be send to processes or threads directly, but only to mailboxes. On the other hand multiple threads may share a mailbox and one thread may have multiple mailboxes.

For a simple example on how to receive messages have a look at the MsgHandler type.

Synopsis

Mailbox

class MailboxClass b whereSource

Any instance of MailboxClass may be used as a mailbox for message passing. b is the mailbox type and m is the message type.

Methods

getMessageSource

Arguments

:: b m

the mailbox

-> IO m

the message

Get a message from the mailbox (with Mailbox it is the first one).

unGetMessageSource

Arguments

:: b m

the mailbox

-> m

the message

-> IO () 

Put a message back to the mailbox (with Mailbox it will be placed at the beginning of the mailbox).

putMessageSource

Arguments

:: b m

the mailbox

-> m

the message

-> IO () 

Add a new message to the mailbox (with Mailbox it will be placed at the end of the mailbox).

isEmptySource

Arguments

:: b m

the mailbox

-> IO Bool

True if empty

Checks wether the mailbox is empty.

close :: b m -> IO ()Source

Call this function to cleanup before exit or when the mailbox is no longer needed.

data Mailbox m Source

A Chan based mailbox.

newMailbox :: IO (Mailbox m)Source

Creates a new mailbox.

Sending messages

sendSource

Arguments

:: MailboxClass b 
=> b m

the mailbox

-> m

the message

-> IO () 

Send the given message to the given mailbox.

(<!) :: MailboxClass b => b m -> m -> IO ()Source

An alias for send in the flavor of Erlang's !.

Receiving messages

receiveSource

Arguments

:: MailboxClass b 
=> b m

mailbox to receive on

-> [MsgHandler m a]

message handlers

-> IO a 

Receive messages in the flavour of Erlang's receive.

For each message in the mailbox all message handlers are matched until a matching message is found. It will be removed from the mailbox and the matching message handler's action will be performed.

If no message matches any of the message handler, receive will block and check new incoming messages until a match is found.

receiveNonBlockingSource

Arguments

:: MailboxClass b 
=> b m

the mailbox

-> [MsgHandler m a]

message handlers

-> IO a

default handler

-> IO a 

Like receive, but doesn't block. If no match was found, the default handler is executed.

receiveTimeoutSource

Arguments

:: MailboxClass b 
=> b m

the mailbox

-> Int

timeout in us

-> [MsgHandler m a]

message handlers

-> IO a

timeout handler

-> IO a 

Like receive, but times out after a given time. In case of timeout the timeout handler is executed.

Message handlers

type MsgHandler m a = m -> Handler aSource

A function that matches a given message and returns the corresponding handler.

In case of an pattern matching error receive will continue matching the next MsgHandler / message.

For example you may write somthing like this:

 receive mbox
     [ \ True  -> handler $ return 1
     , \ False -> handler $ return 2
     ]

data Handler a Source

The action to perfom in case of successful matching.

handler :: IO a -> Handler aSource

Generate a handler from an IO action.

Message handler combinators

(.>)Source

Arguments

:: MsgHandler m a

message handler

-> (a -> b)

function

-> MsgHandler m b

new message handler

Apply a function to the result of an message handler.

(<|>)Source

Arguments

:: [MsgHandler m a]

message handlers

-> [MsgHandler m b]

more message handlers

-> [MsgHandler m (Either a b)]

combined message handlers

Combine to lists of message handlers into one list. The results of the message handler will be wrapped in Either.