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.3. Objects providing mutual exclusion – Monitors<br />

1. There is the risk that x or y gets corrupted when the program is not<br />

run on a 64-bit machine (where a double is stored in a single machine<br />

word), which is the normal case for embedded software. One remedy is<br />

to use the atomic classes that are part of Java from J2SE 5.0, but that<br />

is not easily applicable in other programming languages (and therefore<br />

not further treated here).<br />

2. Even if we would use single precision (float) for x and y, thereby avoiding<br />

the problem of item 1 without using atomic classes (that decrease performance),<br />

and even if we never get a context switch at the critical point<br />

(e.g., due to the priorities and scheduler/OS used) between the calls of<br />

fx and fy, the result of another thread calling dist could still be wrong or<br />

outdated! The reason is that data can remain in registers of the CPU,<br />

as resulting from an optimizing compiler since we have not provided<br />

any information about the data being shared by multiple threads. The<br />

remedy here is to declare the attribute volatile like:<br />

private float volatile x, y;<br />

Problems arise when data shared by multiple threads is not volatile or is<br />

not protected with proper mutual exclusion (such as synchronized that<br />

results in the runtime system being informed and the contant of the<br />

cash is written to memory). Without volatile, a variable updated by<br />

one thread may remain in a register while another thread reads from<br />

the primary memory according to the address of the variable, and consequently<br />

the old value is obtained instead of the one updated by the<br />

other thread.<br />

Even if there is really only one error in the PathCoord class above (the mix-in<br />

of the monitor in the thread), we have now listed four different faults that<br />

can be the result. Note that these problems all disappear if access object<br />

attributes by methods that are synchronized, and also note that the first four<br />

problems illustrated in the class PathCoord are the same if we use monitors<br />

or semaphores for mutual exclusion, and the problems are the same in Java<br />

as in C/C++. Thus, independently of which of these languages you use,<br />

implement monitors and mutual exclusion as described 2 , and do not mix<br />

active objects and monitors.<br />

Synchronization details<br />

As with any set of basic rules, there are some cases when the experienced<br />

programmer for good reasons, or the beginner for bad reasons, may want to<br />

2 When accomplishing mutual exclusion in Java via a proper monitor with synchronized<br />

methods, or when a program written in C uses semaphore functions from some library of the<br />

system, professional programmers are usually aware of the preemption aspect represented<br />

by the described concurrency fault (first error) of the PathCoord class. As a (positive) side<br />

effect the other possible runtime errors also disappear.<br />

71

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

Saved successfully!

Ooh no, something went wrong!