1.2 Adding a Reactor

An asynchronous programming framework which supports the callback model has many similarities to conventional event-driven frameworks such as GUI toolkits. Central to this is the concept of an event loop which detects events and dispatches callbacks to any registered event handlers. In the context of a GUI toolkit an event would typically be a button click or keystroke; for the asynchronous programming framework the event would signal the completion of a long running task.

A significant consideration is that the use of an event loop implies that all callbacks will be made within the context of the event loop thread. This essentially removes the need for the application developer to deal with thread synchronisation, at the expense of the constraint that callback methods cannot block or be long running.

Within the Reaction asynchronous programming framework, the component which provides the event loop is referred to as the reactor. This term originally derives from the reactor design pattern[7], although the features of the reactor used in the Reaction framework have been expanded considerably from the standard pattern description.

In addition to providing the event loop for dispatching callbacks, the reactor is also responsible for scheduling long running tasks. This means that instead of the application starting up such a task directly it can delegate the whole messy business to the reactor. In fact this can be reduced to the point where the long running task only needs to implement a single conventional method, as illustrated by Listing 1.3.


\begin{listing}
% latex2html id marker 89\begin{small}
\begin{verbatim}publi...
...atim}
\end{small}\caption{A Basic Threadable Interface Definition}
\end{listing}

This is a basic version of the interface definition for long running tasks which is implemented by the Reaction framework. In keeping with the interface naming, such tasks will subsequently be referred to as threadable objects. Execution of the run method will be farmed out to a worker thread by the Reactor and on completion the Reactor will take the returned value and pass it back to the application as the callback parameter. All thread creation and synchronisation is taken care of by the reactor component.