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.2. Resources and mutual exclusion – Semaphores<br />

Another disadvantage is when a thread temporarily has to leave a critical<br />

section, waiting for some condition to be fulfilled, it can in some cases be<br />

rather complicated to implement. The semaphore concept is as such sufficient<br />

for implementation of any multi-threaded program, but the application may be<br />

unnecessarily complex compared to using monitors or messages as explained<br />

below. Therefore, semaphores are best suited for low-level and small software<br />

modules with special demands on efficiency.<br />

3.2.3 The semaphores package<br />

To support the use of semaphores in Java, a package se.lth.cs.realtime.semaphore<br />

has been developed. Different classes are providing implementations for different<br />

types of semaphores, such as a class MutexSem for the mutual exclusion<br />

case where it can be checked that give is called by the thread currently holding<br />

the semaphore, and a class CountingSem that for signaling purposes is<br />

prescribed to contain a counter. The default implementations are 100% Java<br />

using the synchronized keyword that is further explained in the next section.<br />

That is of course not acceptable for cross-compilation to small embedded systems<br />

since:<br />

• Very small systems often provide only semaphores, so synchronized has<br />

to be mapped to semaphores and not the other way around.<br />

• For low-level software serving interrupts, native semaphores for signaling<br />

from the interrupt routine to threads are indispensable.<br />

Therefore, the default implementation is for the run-anywhere (that is, pure<br />

Java) case. The idea is then to support platforms providing native semaphores<br />

by proper cross-compilation tools (outside the scope of this chapter).<br />

The Semaphore interface<br />

All semaphores provide the methods give, take, and tryTake. That is expressed<br />

by the interface Semaphore, which is useful when a semaphore object is to be<br />

used without the need to know what type of semaphore it is. These methods<br />

are documented as:<br />

public void give() Increments by 1 the counter represented by this semaphore,<br />

and notify at least one, if any, of the waiting threads. Basically this<br />

means executing the following two lines<br />

++count; // Or set to true (or 1) in case of a binary semaphore.<br />

notify(); // If any threads blocked , release the first in queue.<br />

atomically (synchronized, with interrupts disabled, or in hardware, as<br />

accomplished in a particular type of system).<br />

63

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

Saved successfully!

Ooh no, something went wrong!