JAVA-BASED REAL-TIME PROGRAMMING
JAVA-BASED REAL-TIME PROGRAMMING
JAVA-BASED REAL-TIME PROGRAMMING
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
3.4. Message-based communication – Mailboxes<br />
copy messages for reasons of memory management (since Java provides<br />
GC). Posting an event object therefore means passing the (one word)<br />
reference to that object, which also saves the time for the copying.<br />
2. The RTEventBuffer is not useful for communication between OS processes:<br />
they normally work in different address spaces and object references<br />
cannot be shared, and hence objects are to be represented by<br />
data that is copied to the buffer. The actions to convert a Java object<br />
to a sequence of bytes are referred to as serialization. That is supported<br />
in Java, partly in terms of the interface java.io.Serializable, but that is<br />
outside the scope of this chapter.<br />
3. Buffers used for concurrent programming, such as the java.util.Vector,<br />
are usually unbounded. That is, they expand in size when full, or a<br />
java.lang.OutOfMemoryError is thrown. Our aim at real-time aware concurrency,<br />
to prepare for even hard real time later, motivates some care<br />
concerning resources, such as memory. Even if we for flexible real-time<br />
systems do not know exactly how many bytes our application needs, we<br />
need some bounds on the used resources. For buffering, this means that<br />
we work with bounded buffers as the default. If, in some cases, there<br />
should be a need for unbounded buffers, it is simple to override post or<br />
fetch methods to expand the buffer size on demand.<br />
Other reasons for our own buffer implementation include: shrinkage without<br />
losing buffer content, support for RTEvent and object ownership, design<br />
suitable for embedded/native implementation on small-memory systems, and<br />
licensing issues for such systems. For the purpose of subclassing, the internals<br />
of the buffer is useful to know; the se.lth.cs.realtime.event.RTEventBuffer<br />
is implemented as follows.<br />
Storage and synchronization Since the buffer is to be used mainly as a<br />
FIFO queue, with a bounded size that is normally kept constant, it is<br />
best represented by an array. That is of course not visible from the outside,<br />
but to subclasses it is. When the index needs to advance beyond<br />
the maximum index of the array it flips over and continues at index<br />
zero. Therefore this type of buffer is also called ring buffer, as depicted<br />
in Figure 3-10. The methods of the buffer could be considered being synchronized,<br />
that is, the buffer is thread safe. To prevent interference with<br />
wait/notify internally from external code synchronizing on the buffer<br />
object, the synchronization of the buffer methods are accomplished by<br />
synchronized(lock){...}<br />
where the lock is declared as<br />
protected Object lock = new Object();<br />
That monitor lock can then be replaced in subclasses or in the construc-<br />
91