thespian-0.999: Lightweight Erlang-style actors for Haskell

PortabilityGHC only (requires throwTo)
Stabilityalpha
Maintaineralexander.the.average@gmail.com

Control.Concurrent.Actor

Contents

Description

This module implements Erlang-style actors (what Erlang calls processes). It does not implement network distribution (yet?). Here is an example:

act1 :: Actor
act1 = do
    me <- self
    liftIO $ print act1 started
    forever $ receive
      [ Case $ ((n, a) :: (Int, Address)) ->
            if n > 10000
                then do
                    liftIO . throwIO $ NonTermination 
                else do
                    liftIO . putStrLn $ act1 got  ++ (show n) ++  from  ++ (show a)
                    send a (n+1, me)
      , Case $ (e :: RemoteException) -> 
            liftIO . print $ act1 received a remote exception
      , Default $ liftIO . print $ act1: received a malformed message
      ]

act2 :: Address -> Actor
act2 addr = do
    monitor addr
    -- setFlag TrapRemoteExceptions
    me <- self
    send addr (0 :: Int, me)
    forever $ receive 
      [ Case $ ((n, a) :: (Int, Address)) -> do
                    liftIO . putStrLn $ act2 got  ++ (show n) ++  from  ++ (show a)
                    send a (n+1, me)
      , Case $ (e :: RemoteException) -> 
            liftIO . print $ act2 received a remote exception:  ++ (show e)
      ]

act3 :: Address -> Actor
act3 addr = do
    monitor addr
    setFlag TrapRemoteExceptions
    forever $ receive
      [ Case $ (e :: RemoteException) -> 
            liftIO . print $ act3 received a remote exception:  ++ (show e)
      ]

main = do
    addr1 <- spawn act1
    addr2 <- spawn (act2 addr1)
    spawn (act3 addr2)
    threadDelay 20000000

Synopsis

Types

data Address Source

The address of an actor, used to send messages

data Handler Source

Constructors

forall m . Typeable m => Case (m -> ActorM ()) 
Default (ActorM ()) 

type ActorM = ReaderT Context IOSource

The actor monad, just a reader monad on top of IO.

type Actor = ActorM ()Source

The type of an actor. It is just a monadic action in the ActorM monad, returning ()

data ActorExitNormal Source

Exception raised by an actor on exit

data Flag Source

Constructors

TrapRemoteExceptions 

Instances

Actor actions

send :: Typeable m => Address -> m -> ActorM ()Source

Sends a message from inside the ActorM monad

(◁) :: Typeable m => Address -> m -> ActorM ()Source

Infix form of send

(▷) :: Typeable m => m -> Address -> ActorM ()Source

Infix form of send with the arguments flipped

self :: ActorM AddressSource

Used to obtain an actor's own address inside the actor

receive :: [Handler] -> ActorM ()Source

Try to handle a message using a list of handlers. The first handler matching the type of the message is used.

receiveWithTimeout :: Int -> [Handler] -> ActorM () -> ActorM ()Source

Same as receive, but times out after a specified amount of time and runs a default action

spawn :: Actor -> IO AddressSource

Spawn a new actor with default flags

monitor :: Address -> ActorM ()Source

Monitors the actor at the specified address. If an exception is raised in the monitored actor's thread, it is wrapped in an ActorException and forwarded to the monitoring actor. If the monitored actor terminates, an ActorException is raised in the monitoring Actor

link :: Address -> ActorM ()Source

Like monitor, but bi-directional

setFlag :: Flag -> ActorM ()Source

Sets the specified flag in the actor's environment

clearFlag :: Flag -> ActorM ()Source

Clears the specified flag in the actor's environment

toggleFlag :: Flag -> ActorM ()Source

Toggles the specified flag in the actor's environment

testFlag :: Flag -> ActorM BoolSource

Checks if the specified flag is set in the actor's environment