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 />
: Executing<br />
: Blocked<br />
: Potential blocking<br />
Thread<br />
(active obj.)<br />
Semaphore<br />
(passive obj.)<br />
Figure 3.1: Symbols used for the (UML/OMT-based but enhanced) sequence<br />
diagrams.<br />
Looking for appropriate names for the semaphore operations, the best<br />
choice (according to the author) was found in VxWorks, a major RTOS from<br />
WindRiver Systems Inc. That C-based API contained the functions sem_take<br />
and sem_give, both taking a pointer to the semaphore data structure as an<br />
argument. In object oriented terms, the argument is the object (the so called<br />
this pointer named as the object), sem is the name of the class, and the _ by<br />
convention corresponds to method selection in the global namespace in C.<br />
Correspondingly, the tryTake method is available in some systems, and if<br />
not, it can always be implemented using an extra (timer) thread. Typically,<br />
there is one timer thread serving all timeout requests.<br />
3.2.2 Using semaphores<br />
The (atomic) semaphore methods can be used in different ways to solve concurrency<br />
problems, or semaphores can be used to create other abstractions.<br />
Let us therefore review some generic situations when semaphores can be used.<br />
To illustrate the sequencing and blocking that takes place when threads call<br />
the semaphore methods, we use an UML (or OMT) based notation, with some<br />
extra symbols to emphasize the concurrency and blocking issues. Figure 3-1<br />
explains the notation used.<br />
Signaling<br />
The counter contained in the standard type of semaphore, according to the<br />
description above, is mainly useful for signaling. That is, when one activity<br />
has some result or resource ready for use by some other activity, the producing<br />
thread signals to the consuming thread. If the producer signals (calling<br />
give) several times before the consumer waits (calling take), the “non-negative<br />
integer-valued counter” contained in the ordinary type of counting semaphore<br />
results in that take needs to be called as many times as give has been called.<br />
In other words, the counter comprises a memory which implies that signaling<br />
in advance is not forgotten. Figure 3.2 shows how a thread T1 signals to a<br />
thread T2; T1 can run in advance whereas T2 can catch up with earlier calls<br />
58 2012-08-29 16:05