edu.vub.at.actors.eventloops
Class EventLoop

java.lang.Object
  extended byedu.vub.at.actors.eventloops.EventLoop
Direct Known Subclasses:
ELActor, ELFarReference, ELVirtualMachine

public abstract class EventLoop
extends java.lang.Object

The EventLoop is the basic concurrency primitive for the AmbientTalk/2 implementation. An Event Loop consists of an event queue, an event processing thread and an event handler. The event loop's thread perpetually takes the next event from the event queue and makes the event loop process it. Hence, the event loop is the event handler in this architecture. Event Loops form the reusable core of both actors, remote references and even the virtual machine itself (i.e. it is the core of both programmer-defined and native actors). This is an abstract class. To be usable, subclasses have to provide a meaningful implemementation strategy for handling events by overriding the handle method.

Author:
tvcutsem, smostinc

Nested Class Summary
 class EventLoop.EventProcessor
          EventProcessor is a thread subclass whose primary goal is to keep a reference to the associated event loop, which is used to transform threads into the more manageable event loops
 
Field Summary
private  boolean askedToStop_
           
protected  EventQueue eventQueue_
          The event loop's event queue is a synchronized queue of Event objects.
private  java.lang.String name_
           
protected  java.lang.Thread processor_
          Each event loop has an event processor, which is a thread responsible for perpetually dequeuing events from the event queue and passing them on to this event loop's handle method.
 
Constructor Summary
EventLoop(java.lang.String name)
          Constructos a new event loop with the default processing behaviour.
 
Method Summary
static EventLoop currentEventLoop()
          Allows access to the currently running event loop.
 void execute()
           
abstract  void handle(Event event)
          Subclasses are responsible for defining a meaningful implementation strategy to handle events from the event queue.
protected  EventLoop owner()
           
protected  void receive(Event event)
          When an event loop receives an asynchronously emitted event, this message is immediately placed into its incoming event queue and will be processed later.
protected  java.lang.Object receiveAndWait(java.lang.String description, Callable callable)
          Schedules an event in this event loop's queue which will execute the provided callable object at a later point in time.
protected  void receivePrioritized(Event event)
          When an event loop receives an asynchronously emitted event, this message is immediately placed into its incoming event queue and will be processed later.
 void stopProcessing()
           
static EventLoop toEventLoop(java.lang.Thread t)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

eventQueue_

protected final EventQueue eventQueue_
The event loop's event queue is a synchronized queue of Event objects. It is the sole communication channel between different event loops. As such, it is the means by which different actors - and event actors and their virtual machines - communicate.


processor_

protected java.lang.Thread processor_
Each event loop has an event processor, which is a thread responsible for perpetually dequeuing events from the event queue and passing them on to this event loop's handle method.


askedToStop_

private boolean askedToStop_

name_

private final java.lang.String name_
Constructor Detail

EventLoop

public EventLoop(java.lang.String name)
Constructos a new event loop with the default processing behaviour.

Parameters:
name - used for debugging purposes
Method Detail

toString

public java.lang.String toString()

toEventLoop

public static EventLoop toEventLoop(java.lang.Thread t)
                             throws java.lang.IllegalStateException
Throws:
java.lang.IllegalStateException

currentEventLoop

public static EventLoop currentEventLoop()
                                  throws java.lang.IllegalStateException
Allows access to the currently running event loop.

Returns:
the currently serving event loop
Throws:
java.lang.IllegalStateException - if the current thread is not the thread of an event loop

stopProcessing

public final void stopProcessing()

receive

protected final void receive(Event event)
When an event loop receives an asynchronously emitted event, this message is immediately placed into its incoming event queue and will be processed later. This method is declared protected such that subclasses can provide a cleaner interface as to what kind of events can be received by this event loop. The convention is that a subclass provides a number of methods prefixed with event_ which call this protected method to schedule a certain event.


receiveAndWait

protected final java.lang.Object receiveAndWait(java.lang.String description,
                                                Callable callable)
                                         throws java.lang.Exception
Schedules an event in this event loop's queue which will execute the provided callable object at a later point in time. Moreover, this method immediately makes the calling thread WAIT for the return value or resulting exception of the callable. Caller must ensure that the thread invoking this method is not this event loop its own thread, which inevitably leads to deadlock. This method is declared protected such that subclasses can provide a cleaner interface as to what kind of tasks may be scheduled in this event loop. The convention is that a subclass provides a number of methods prefixed with sync_event_ which call this protected method to schedule a certain task.

Parameters:
description - a description of the task being scheduled, for debugging purposes
callable - the functor object encapsulating the task to be performed inside the event loop
Throws:
java.lang.Exception

receivePrioritized

protected final void receivePrioritized(Event event)
When an event loop receives an asynchronously emitted event, this message is immediately placed into its incoming event queue and will be processed later. Using this method, events are scheduled first in the queue of upcoming events. This method is declared protected such that subclasses can provide a cleaner interface as to what kind of events can be received by this event loop. The convention is that this method is only used to put back events that could not be processed due to problems outside of the influence of the interpreter (e.g. host unreachability). If a subclass provides direct access to this primitive it should do so in methods prefixed with prioritized_event_ which call this protected method to schedule a certain event.


handle

public abstract void handle(Event event)
Subclasses are responsible for defining a meaningful implementation strategy to handle events from the event queue.

Parameters:
event - the event object which was dequeued and which should be processed

owner

protected final EventLoop owner()

execute

public void execute()