06.08.2013 Views

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.4. Message-based communication – Mailboxes<br />

as a shared resource in a monitor, while the input can be directly available<br />

for posting events (from several threads since the RTEventBuffer by itself is a<br />

monitor). Within such a monitor holding an RTEventBufferInput, a potentially<br />

blocking call such as doFetch should only be used with great care since the<br />

blocked thread will lock also the outer monitor.<br />

In general for producer-consumer situations, the input and output interfaces<br />

to the buffer simply provide a convenient way of passing one end of the<br />

buffer to the producer or the consumer. Of course an object (a monitor or an<br />

ordinary object) given an RTEventBufferInput can check if it is an instance of<br />

RTEventBuffer (which it does not need to be since any object can implement<br />

the same interface), and if so, cast to RTEventBuffer and use also the post<br />

methods. Hence, these interfaces are provided for convenience for certain programming<br />

situations involving cooperating objects, but as usually, misuse can<br />

be made cumbersome but not completely prevented. Recall, however, that<br />

multi-threading mainly is about cooperating concurrency.<br />

3.4.3 Threads and event-based communication<br />

A mailbox, here accomplished by an instance of RTEventBuffer 7 , can be declared<br />

and referred to in different ways:<br />

• The mailbox(es) can be made globally available, for example created<br />

in the main program and passed as argument to the constructors of<br />

the different sending/receiving objects( typically threads), or statically<br />

defined attributes of some class known to other classes at compile time.<br />

With such globally defined mailboxes, they are visible in the software<br />

design, as entities of their own.<br />

• We can also view the provided buffering as a property of some object,<br />

and have the mailbox as an attribute internal to the class that provides<br />

the methods that make use of the buffering. Typically the enclosing class<br />

will be of type Thread and act as the receiver of the events (although the<br />

sender can be unaware of that if sending is accomplished by some other<br />

method of the receiving thread. With such object-internal mailboxes,<br />

the buffering is part of the internal design of that class.<br />

Thus, the the main difference here is a design choice: should mailboxes be<br />

exposed-to or part-of the objects that make use of them? Following common<br />

object-oriented design rules, this depends on what we see as entities within<br />

our design. If the buffers form the core of the application it may be preferable<br />

to have them exposed. However, in most cases the buffers are introduced to<br />

7 The upcoming new release of the LJRT classes will include both the RTEventBuffer and<br />

other buffer classes with different properties and different implementations. The Java-doc<br />

pages provide the valid description for programming using the available buffers, while the<br />

(not complete) description here covers the general ideas of events and buffers.<br />

95

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!