Asynchronous Event Handling
RTSJ uses event handling to communicate asynchronously. Asynchronous events (AE) are data-less ‘happenings’ are fired by the program or associate with interrupts in the environment.
One or more handlers (AEH) can be associated with a single event, and a single AEH can be associated with one or more events. The association is dynamic. An AEH has a count of the number of outstanding firings.
Guess what, the events are classes. They’re similar to POSIX signals. The 3 main classes are:
When an event occurs all the handlers associated with the event are scheduled for execution.
ATC in RTJ
Allows one RT thread to interrupt another RT thread. It is integrated into the Java exception handling and interrupt facility. RTJ requires that each method indicates if it is prepared to allow an ATC; a bit like switching a mobile phone on to ‘say’ you are prepared to be interrupted.
To use ATC you must:
- Declare an
AsynchronouslyInterruptedException(wow, what a long name).
- Identify methods which can be interrupted using a
- Signalling an
AsynchronouslyInterruptedExceptionto a thread.
There are some examples of ATCs in the slides.
The key thing about Asynchronous Exceptions is that you should only have to handle one at once; otherwise it’ll be quite difficult to reason what is going to happen.
There’s also a few examples of what happens, with pictures. It looks a bit complicated and convoluted. Maybe this needs a practical to get the hang of it. He did say that it is ‘quite a complex model’. I swear Ada was simpler.
The ‘Interruptible’ interface
If you want to write some code that is interruptible, implement the
Interruptible interface, which has two methods.
run. You can guess what happens.
Again there’s an example of what you have to do to implement this.
As we have discussed before realtime systems might want to change modes. You can do this by implementing the
Interruptible interface for each Mode (e.g. two classes:
Then, if the mode gets interrupted, it resets the internal state so that it can continue again at some point if necessary.
Then you have a
ModeChanger class which extends
AsynchronouslyInterruptedException. It is passed the initial mode and then keeps track of the current mode. This class can then set or toggle the mode. All this code is in the slides again, so take a look there as to exactly how you would implement this.
ATC Ada versus RTSJ
They are similar because:
- You have to indicate which regions of code can receive the ATC request.
- ATC are deferred during task/thread interaction and finalisation.
They are different because:
- RTJ is integrated with Java exception handling. Ada model is integrated into select and entry-handling.
- RTJ model requires each method to indicate in advance that it is prepared to allow the ATC to occur.
- Ada’s default is to allow the ATC if a subprogram has been called from the select-then-abort statement; a deferred response must be explicitly handed. (huh?)