Copyright | (c) 2014 Forkk |
---|---|
License | MIT |
Maintainer | forkk@forkk.net |
Stability | experimental |
Portability | GHC only (requires throwTo) |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
This module implements Erlang-style actors (what Erlang calls processes).
- data ActorMessage msg => ActorHandle msg
- class ActorMessage msg
- type ActorM msg = ReaderT (ActorContext msg) IO
- send :: (ActorMessage msg, ActorMessage msg') => ActorHandle msg -> msg -> ActorM msg' ()
- sendIO :: ActorMessage msg => ActorHandle msg -> msg -> IO ()
- receive :: ActorMessage msg => ActorM msg msg
- receiveMaybe :: ActorMessage msg => ActorM msg (Maybe msg)
- receiveSTM :: ActorMessage msg => ActorM msg (STM msg)
- spawnActor :: ActorMessage msg => ActorM msg () -> IO (ActorHandle msg)
- runActor :: ActorMessage msg => ActorM msg () -> IO ()
- self :: ActorMessage msg => ActorM msg (ActorHandle msg)
- actorThread :: ActorMessage msg => ActorHandle msg -> ThreadId
Types
data ActorMessage msg => ActorHandle msg Source
An ActorHandle
acts as a reference to a specific actor.
class ActorMessage msg Source
The ActorMessage
class must be implemented by any type that will be sent
as a message to actors.
Any given type of actor will have one ActorMessage
type that is sent to
that actor. This ensures type safety.
Currently this is simply a dummy class with nothing in it, but things may be
added in the future.
ActorMessage () |
type ActorM msg = ReaderT (ActorContext msg) IO Source
The base actor monad.
Sending Messages
send :: (ActorMessage msg, ActorMessage msg') => ActorHandle msg -> msg -> ActorM msg' () Source
Sends a message to the given actor handle.
This is secretly just sendIO
lifted into an actor monad.
sendIO :: ActorMessage msg => ActorHandle msg -> msg -> IO () Source
Sends a message to the given actor handle from within the IO monad.
Receiving Messages
receive :: ActorMessage msg => ActorM msg msg Source
Reads a message from the actor's mail box.
If there are no messages, blocks until one is received. If you don't want
this, use receiveMaybe
instead.
receiveMaybe :: ActorMessage msg => ActorM msg (Maybe msg) Source
Reads a message from the actor's mail box.
If there are no messages, returns Nothing
.
receiveSTM :: ActorMessage msg => ActorM msg (STM msg) Source
An ActorM
action which returns an STM
action to receive a message.
Spawning Actors
spawnActor :: ActorMessage msg => ActorM msg () -> IO (ActorHandle msg) Source
Spawns the given actor on another thread and returns a handle to it.
runActor :: ActorMessage msg => ActorM msg () -> IO () Source
Runs the given actor on the current thread. This function effectively turns the current thread into the actor's thread. Obviously, this means that this function will block until the actor exits. You probably want to use this for your "main" actor.
Getting Information
self :: ActorMessage msg => ActorM msg (ActorHandle msg) Source
Gets a handle to the current actor.
actorThread :: ActorMessage msg => ActorHandle msg -> ThreadId Source
Gets the thread ID for the given actor handle.