Upload
dnkvicton6248
View
243
Download
0
Embed Size (px)
Citation preview
7/27/2019 Real-Time Java and ATC
1/44
Real-time Java&
Asynchronous Transfer of Control
Jihua Zhong
Marc E Loy
Maung Han
Seminor in Real-time SystemsInstructor: Professor Insup Lee
7/27/2019 Real-Time Java and ATC
2/44
Different Flavours of Java
(JavaFamily)
Java
(J2SE,J2EE)
Personal
Java(J2ME)
Embedded
Java
ROM Size 4 MB
7/27/2019 Real-Time Java and ATC
3/44
Real-time Java
(A brief intro)
7/27/2019 Real-Time Java and ATC
4/44
What is Real-time Java (RTJ)?
Standard Java is not enough to handle real-time constraints.
Java (and JVM) lacks semantic for standardreal-time programming techniques.
Embedded Java Specification was there, butmerely a subset of standard Java API.
There is a gap for a language real-timecapable and equipped with all Javas powerfuladvantages.
7/27/2019 Real-Time Java and ATC
5/44
Real-time Spec. for Java (RTSJ)
IBM, Sun and other partners formedReal-time for Java Expert Group
sponsored by NIST in 1998.It came up with RTSJ to fill this gap for
real-time systems.
RTSJ proposed seven areas ofenhancements to the standard Java.
7/27/2019 Real-Time Java and ATC
6/44
RTSJ 7 Areas of Enhancement
1. Thread scheduling and dispatching.
2. Memory management.
3. Synchronization and Resource sharing.4. Asynchronous Event Handling.
5. Asynchronous Transfer of Control.
6. Asynchronous Thread Termination.7. Physical Memory Access.
7/27/2019 Real-Time Java and ATC
7/44
RTSJ Implementations
RTSJ is still under evaluation.
Sun has no practical implementation.
Some implementations by others:- VisualAge by IBM
- Simple RTJ by RTJ Computing
Theres another spec by J-consortium backedby Microsoft, HP.
Other just sit on the fence.
7/27/2019 Real-Time Java and ATC
8/44
Asynchrony
7/27/2019 Real-Time Java and ATC
9/44
Asynchrony
Asynchronous event handling andtransfer of control in execution.
Not all real-life events are predictablein time and frequency.
Ability of real-time systems to react to
them correctly requires reliableasynchrony techniques.
7/27/2019 Real-Time Java and ATC
10/44
Asynchrony (contd.)
Conventional ways of achieving it inJava is by interrupts and exceptions.
They are deadlock prone.Can cause data structure corruption.
7/27/2019 Real-Time Java and ATC
11/44
Asynchrony (contd.)
Leaving exiting techniques intact forconventional codes,
RTSJ offers two extended approachesfor real-time threads-
1. Asynchronous Events (AEv).
2. Asynchronously InterruptedExceptions (AIE).
7/27/2019 Real-Time Java and ATC
12/44
I. Asynchronous EventsSome internal or
external event thathappens.
System need torespond to thechangingenvironment.
Not known inadvance about timeor frequency.
7/27/2019 Real-Time Java and ATC
13/44
Class Hierarchy
AsyncEvent
Schedulable (I)
AsyncEventHandler
BoundAsyncEventHandler
AsyncEventHandler behavelike Thread. It has run()method.
BoundAsynchronousHandler
can be used for addedtimeliness by binding eachhandler to a dedicated thread.
Object
Runnable (I)
7/27/2019 Real-Time Java and ATC
14/44
Asynchronous Event (contd.)
Event1Event2
Event3
Handler AHandler B
One event can be handled by more than one handler.
One handler can handle more than one event.
Many to many relationship.
7/27/2019 Real-Time Java and ATC
15/44
AsyncEvent (how it works)
A Handler can choose to process itsfire-counts individually or collectively.
AsyncEvent
bindTo()Realworldevent
abstract class AsyncEventHandler:
run() // final
handleAsyncEvent()
getAndDecrementPendingFireCount()
addHandler()
Handler implementsSchedulable and Runnableinterfaces.
7/27/2019 Real-Time Java and ATC
16/44
Sample code for AsyncEvent handling
public static void main(String args){
AsyncEventHandler hdlrA = new AsyncEventHandler(){
public void handleAsyncEvent(){
do{
System.out.print(Handler A executed.);
} while(getAndDecrementPendingFireCount()>0);
}}
7/27/2019 Real-Time Java and ATC
17/44
AsyncEvent Sample Code
AsyncEvent event1 = new AsyncEvent();
event1.addHandler(hdlrA);
System.out.println(AsyncEvent Test.\n);
event1.fire();
System.out.println(Event fired.\n);
} // main()
7/27/2019 Real-Time Java and ATC
18/44
Use of AEv Handling
In real-time systems, there can be hundreds
(or even thousands) of possible events.
But usually, only few happens at one time.
Impractical to create and assign one threadfor each possible event.
AEv Handling offers low cost alternative to
threads.Not like methods, AEv handlers can be
scheduled and executed asynchronously.
7/27/2019 Real-Time Java and ATC
19/44
II. ATC
(Asynchronous Transfer of Control)
Some change inthe systemenvironment needs
immediateattention.
Abandon currentexecution or take
appropriate action.
7/27/2019 Real-Time Java and ATC
20/44
AIE(AsynchronouslyInterruptedException)
Interruptible (I)
Exception
Object
Throwable
InterruptedException
AsynchronouslyInterruptedException
Timed
Parameter forAIE.doInterruptible()
7/27/2019 Real-Time Java and ATC
21/44
AIE (contd.)
In RT Java, ATC is achieved by throwing aspecial exception AIE.
RT threads and methods can choose toreceive (or not to) AIE by including it inthrows clause.
e.g. void run() throwsasynchronouslyInterruptedException {}
Existing Non-RT implementations are notaffected.
7/27/2019 Real-Time Java and ATC
22/44
AIE (contd.)
AIE can be thrown to cut short a threadsexecution and take appropriate action.
It can be thrown explicitly by firing inprogram codes, or implicitly by interruptinga real-time thread.
e.g. aie.fire() or thread.interrupt()
If AIE is fired in a methods it did not declareAIE in throws clause, AIE is put to pendingstate until control reaches AIE-enabledmethod.
7/27/2019 Real-Time Java and ATC
23/44
Some rules on AIE
Only one AIE can become active at anyone time, no nested AIEs.
If newly generated AIE is less in depththan the pending AIE, it is replaced bynew one.
If newly generated AIE is deeper indepth, it is discarded.
7/27/2019 Real-Time Java and ATC
24/44
How AIE works
AIE Handler should invoke theAIE.happened() method to clear it.
Otherwise, it will continue to propagateoutward.
AIE method
Non-AIE method
AIE method (catch)
AIE fired
Deferred untilAIE method
AIE caughtand cleared
7/27/2019 Real-Time Java and ATC
25/44
When AIE is deferred
In AIE methods, it is deferred if controlis in synchronized block.
In non-AIE methods, deferred untilcontrol reaches in AIE method.
If both AIE and other exceptions
occurred when control enters AIEmethod, AIE overrides others.
7/27/2019 Real-Time Java and ATC
26/44
When AIEs overlap
Another AIE can get generated whileone is in execution.
If AIE of outer block is fired, executionstops and control transfers to outerblock.
7/27/2019 Real-Time Java and ATC
27/44
Asynchronous Thread Termination
AEv and AIE in RTJ provides ways tomanage execution loci in real-time
systems asynchronously.Using these two together can achieve
asynchronous termination of threads.
7/27/2019 Real-Time Java and ATC
28/44
Sample Implementation
Single ElevatorControl system.
Separate classes for: Carriage (floor pos,
door open?, moving?)
MotorControl (up,
down) PanelControl (buttons
pressed,next dest)
7/27/2019 Real-Time Java and ATC
29/44
Elevator (contd.)
Events;
A floor select button is pressed.
Elevator request button is pressed. Fire alarm sounded.
Exceptions;
Hazard (fire, blackout, etc.).
7/27/2019 Real-Time Java and ATC
30/44
AsyncEvent classes
class ButtonEvent extends AsyncEvent{
public ButtonEvent(String bindstring){
bindTo(bindstring);
}}
class FireAlarm extends AsyncEvent{
public FireAlarm(){
super();bindTo("FireSignal");
}
}
7/27/2019 Real-Time Java and ATC
31/44
Event Handlers (inner classes)
class ReqBtnHandlerextends AsyncEventHandler{
int flr;
ReqBtnHandler(int f){flr=f;}
publicvoid handleAsyncEvent(){floorReq[flr]=true;}}
class SelBtnHandlerextends AsyncEventHandler{
int flr;
SelBtnHandler(int f){flr=f;}publicvoid handleAsyncEvent(){floorSel[flr]=true;}
}
Code
7/27/2019 Real-Time Java and ATC
32/44
Hazard Handler(in Carriage class)
class HzHandlerextends AsyncEventHandler{
publicvoid handleAsyncEvent(){
aie.fire(); // fire AIE to interrupt control thread
// open the door
if(curFloor==0 && !moving) openDoor();
}
}
Code
7/27/2019 Real-Time Java and ATC
33/44
Linking Events to Handlers
// in PanelControl class
ButtonEvent sb0=new ButtonEvent("selbutton0"); // 1,2,
ButtonEvent rb0=new ButtonEvent("reqbutton0"); // 1,2,
sb0.addHandler(newSelBtnHandler(0)); // 1,2,
rb0.addHandler(newReqBtnHandler(0)); // 1,2,
// in Carriage class
HzHandler hh=new HzHandler();
e.addHandler(hh); // fire alarm is stored in attribute e of Carriage.
// similarly in PanelControl, share the same event.Need to link every single event and handler, cannot differentiatebetween different events of the same class. Better if we canpass instance specific data along with event. But RTSJ does notallow that.
Code
Code
7/27/2019 Real-Time Java and ATC
34/44
AIE exception
class HazardAie extends
synchronouslyInterruptedException{
public HazardAie(){super();}
}
HazardAie myAie= new HazardAie();
Carriage car=new Carriage(fa,myAie);
Somebody has to fire it. So pass to Carriage that
receive the fire event
7/27/2019 Real-Time Java and ATC
35/44
AIE handling
publicclass MotorControl implements Interruptible{
.
// action when AIE is fired.
publicvoidinterruptAction(AsynchronouslyInterruptedException e){
if(carriage.getFloor()>0){
carriage.closeDoor();
godown();}
}
7/27/2019 Real-Time Java and ATC
36/44
MotorControl (contd.)
// action when interruptible code is run
publicvoid run(AsynchronouslyInterruptedException e){
while(true){
try{myrun(e);}catch(AsynchronouslyInterruptedException
e1){}
}// while
}void myrun(AsynchronouslyInterruptedException e)
throws AsynchronouslyInterruptedException {
// normal control sequence
}
7/27/2019 Real-Time Java and ATC
37/44
Main body of program
publicclass ElevatorSim {
publicstaticvoid main(java.lang.String[] args) {
FireAlarm fa = new FireAlarm(); // create fire alarm
eventHazardAie myAie = new HazardAie();
Carriage car = new Carriage(fa,myAie);
PanelControl pc = new PanelControl(5,fa);
MotorControl mc = new MotorControl(car, pc, 4, 0);
myAie.doInterruptible(mc); // run interruptible code
}
}
7/27/2019 Real-Time Java and ATC
38/44
Memory Management
7/27/2019 Real-Time Java and ATC
39/44
Purpose
To allow memory areas of differentbehavior to be accessed by real-time
tasks.Specify memory consumption behavior
of RT tasks.
RT tasks can enter and exit memoryareas and allocate objects as needed.
7/27/2019 Real-Time Java and ATC
40/44
Memory Class Types
MemoryArea Heap Memory (normal Java Heap)
Scoped Memory (limited lifetime.)
Immortal Memory (app lifetime) ImmortalPhysicalMemory (app lifetime, physical)
PhysicalMemoryFactory (Supply physicalmem to other mem objects, e.g. DMA, Byte
swapping)RawMemoryAccess (Drivers, Memory
mapped I/O)
7/27/2019 Real-Time Java and ATC
41/44
Memory Class Hierarchy
Object
MemoryArea
HeapMemory
ImmortalPhysicalMemory
ScopedMemoryLTMemory
ImmortalMemory
ScopedPhysicalMemory
VTMemory
RawMemoryAcess
PhysicalMemoryFactory
RawMemoryFloatAcess
7/27/2019 Real-Time Java and ATC
42/44
Scoped Memory Rules
Reference to an object in ScopeMemory;
Can never be stored in an object in Java Heap.
Can never be stored in an object inImmortalMemory.
Can be stored in an object in the same scope orinner scope memory.
An object in Immortal or Heap may storereferences in ScopeMemory.
7/27/2019 Real-Time Java and ATC
43/44
ScopeMemory activation
An area of ScopeMemory can become activefor a thread by;
Calling enter() method of ScopeMemory object.
Creating instances of objects in ScopeMemory.
ScopeMemory areas can be activated innested manner.
Objects in an inner scope can refer to outerscopes, but not the other way.
7/27/2019 Real-Time Java and ATC
44/44
Conclusion
Java is trying to become real-time capable.
Most systems today are either non real-time
or implement only a subset of RTSJ.We expect to see more complete and robust
implementations soon with increasinglypopular embedded and real-time systems.