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. Multi-Threaded Programming<br />
requires certain features from the operating or run-time system, and cannot be<br />
purely implemented in, for instance, Java without help from native systems<br />
calls. Therefore, the semaphore methods we are about to introduce should<br />
be considered as system calls (part of the operating or runtime system) with<br />
certain properties, not to be confused with ordinary methods that can be<br />
implemented on the Java level.<br />
We may, on the other hand, be able to implement semaphores in Java<br />
if we can use some built-in or native Java feature (synchronized methods<br />
in this case) to accomplish suspension of execution until certain conditions<br />
are fulfilled. Alternatively, some way of implementing atomic methods could<br />
be available (such as native methods for disabling and enabling interrupts).<br />
An atomic method is a method that cannot be interrupted by execution of<br />
other parts of the software. Anyhow, no matter how we implement semaphore<br />
mechanism, natively or built on some Java feature, we think of the methods<br />
as atomic and with properties according to the sequel.<br />
3.2.1 Semaphore methods<br />
Recall that semaphores are the primary mechanism in many small embedded<br />
systems, so we need semaphore classes that model the semaphore concept and<br />
supports cross compilation to native code. Linking with native implementations<br />
of semaphores as provided in most real-time kernels then permits full<br />
efficiency also when special hardware is used. To this end, let us first study<br />
the concept and then look into the implementation.<br />
Core operations<br />
A semaphore is basically a non-negative integer-valued counter with two atomic<br />
methods and a queue for blocked/waiting threads. The methods (with return<br />
type void) of interface Semaphore are:<br />
take(); decrements the counter if it is positive, but if the counter is zero,<br />
execution of the caller is suspended and that thread is put in some kind<br />
of queue waiting for the counter to become positive. Hence, an operation<br />
that increments the counter must check the queue and wake up the first<br />
thread.<br />
give(); increments the counter and checks if there are any threads waiting in<br />
the queue. If so, a thread is made ready for execution, which can resume<br />
when the caller of give has completed its (atomic) call of give.<br />
Apart from some initialization of the counter when the semaphore is created/initialized,<br />
these two methods are the only two methods available according<br />
to the original definition. When a blocked thread is placed in the waiting<br />
56 2012-08-29 16:05