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.

2.5. Interrupts, pre-emption, and reentrance<br />

disadvantage is that programming gets more complicated since it may<br />

be hard to know from the source code if a statement is atomic or not.<br />

Summing up advantages and disadvantages, and considering the importance<br />

of timely operation also when non-concurrent code is used, alternative 3 is the<br />

best choice. That solution is assumed in the sequel. In the Java case, it is not<br />

clear what we mean by machine level. There are three cases:<br />

• The Java program has been compiled into byte code which is then executed<br />

on a Java Virtual Machine (JVM) hardware. Then, the machine<br />

instructions actually used are the byte codes, and the program may be<br />

interrupted between each byte code which implies that a statement in<br />

the source code may be partially evaluated.<br />

• The Java program, again represented by its byte codes, may be run on a<br />

JVM implemented in software. Internally in the JVM, there may be a so<br />

called JIT (Just In Time) compiler which compiles the byte codes into<br />

native machine code. The JVM may then permit pre-emption between<br />

byte codes, or between machine instructions when performing a byte<br />

code, depending on implementation. In both cases, the Java statements<br />

may be partially evaluated.<br />

• If we know what hardware (and OS) we will run our program on, there<br />

is also the possibility to have a conventional compiler that compiles the<br />

Java source directly into machine code. Also in this case, pre-emption on<br />

machine level implies that expressions may be partially evaluated. For<br />

example, assignment of a double variable may be performed partially,<br />

resulting in one half of the bytes from the old value and one half from<br />

the new value for another thread.<br />

Thus, from a programming point of view, it does not matter which of these<br />

cases that describe the actual situation. The fact to consider is that our<br />

sequential execution may be interrupted also in the middle of one Java statement.<br />

We will, however, in some situations know that an operation is atomic<br />

(one byte code or one machine instruction depending on the case) and then<br />

utilize that to simplify the program.<br />

2.5.3 Reentrance<br />

Now when we have multiple points of execution in our program, and we realize<br />

that access of shared data requires special care (which will be covered in the<br />

next chapter), a good question is: What about shared functions? In other<br />

words: what happens if the same function is being called concurrently from<br />

several points of execution? We say that we re-enter the function, and if that<br />

is permitted and working, we say that the code/class/function is reentrant.<br />

29

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

Saved successfully!

Ooh no, something went wrong!