The Real-Time Specification for Java by David Hardin Listing One import javax.realtime.*; /** Example of using Asynchronous Event/Event Handling facility to provide an interface to hardware events, i.e. interrupts. A hardware interrupt conceptually fires an AsyncEvent, which causes the associated handler to run. */ public class HardwareEventExample extends AsyncEvent { private int interruptNum; /** Construct a new Hardware Event for a given interrupt. * @param num Interrupt number */ public HardwareEventExample(int num) { interruptNum = num; } /** Bind a handler to the interrupt. * @param h Handler for the interrupt */ public void setHandler(AsyncEventHandler h) { super.setHandler(h); Hardware.bind(interruptNum, h); } } class HardwareEventHandler extends AsyncEventHandler { private int interruptCount = 0; /** Interrupt handler method. */ public void handleAsyncEvent() { interruptCount++; // Driver code follows } } Listing Two import javax.realtime.*; /** Example of the use of "Immortal" memory in a periodic processing context. This example eschews heap allocation, thus avoiding garbage collection overhead. */ public class ImmortalMemoryExample { public static void main(String[] Args) { NoHeapRealtimeThread t = null; // Set up periodic processing of 1 msec each 10 msec PeriodicParameters timeParams = new PeriodicParameters(); timeParams.cost = new RelativeTime(1, 0); // 1 msec computation timeParams.period = new RelativeTime(10, 0); // 10 msec period // Set up immortal memory; size is given in RealtimeSystem. MemoryParameters memParams = new MemoryParameters(ImmortalMemory.instance()); // Processing is encapsulated in a Runnable Runner r = new Runner(); // Create a NoHeapRealtimeThread with Periodic scheduling parameters // and ImmortalMemory memory parameters. try { t = new NoHeapRealtimeThread(timeParams, memParams, r); } catch (AdmissionControlException e) {} // Start processing t.start(); } } /** Perform periodic processing in Immortal memory */ class Runner implements Runnable { public void run() { // Processing code here } } Listing Three import javax.realtime.*; /** Example of the use of an AsynchronouslyInterruptedException (actually, the * Timed subclass of AIE) to timeout a long-running computation, and return * a "rough" answer instead. Use of AIE avoids overhead of a polling approach. */ public class AIEExample { /** Timeout class */ private class MyTimed extends Timed { public MyTimed(HiResTime timeout) { super(timeout); } } /** Long-running computation that may be timed out. * @param T Timeout */ int computeRefinedAnswer(MyTimed T) throws MyTimed { int refinedAnswer = 0; T.enable(); // hairy computation goes here T.disable(); return refinedAnswer; } /** Public interface to the potentially timed-out computation. If a * timeout occurs while computing the "refined" answer, a "rough" * answer is returned instead. */ public int computeAnswer() { int roughAnswer = 3; // Set up 100 usec timeout MyTimed T = new MyTimed((new RelativeTime(0, 100000))); try { return computeRefinedAnswer(T); } catch (MyTimed t) { // Computation timed out return roughAnswer; } } } Listing Four import javax.realtime.*; /** Example use of RawMemoryAccess (actually, a subclass of RawMemoryAccess * for accessing the Intel x86 I/O space) to directly address memory. This * (elided) example provides an interface to the Intel 8253 Programmable * Interval Timer, based on code originally developed by Gerald H. Hilderink. */ public class IOAccess extends RawMemoryAccess { /* ... */ } public class I8253Example { /* ... */ private long counter0Offset = 0; private long counter1Offset = 1; private long counter2Offset = 2; private long controlWordOffset = 3; private byte controlWord0 = 0x00; private byte controlWord1 = 0x00; private byte controlWord2 = 0x00; /** Create instance of the I8253 class. * @param baseAddr base address */ IOAccess iox; public I8253Example(long baseAddr) { iox = IOAccess.create(baseAddr, (long)8); } /* ... */ /** Write a 16-bit value to counter 2. * @param value value for counter 2 */ public void setCounter2(short value) { setControlWord(controlWord2); iox.setByte(counter2Offset, (byte)(value & 0xFF)); iox.setByte(counter2Offset, (byte)(value >> 8)); } /** Write a byte value to the control register. * @param value value for control register */ public void setControlWord(byte value) { iox.setByte(controlWordOffset, value); } /* ... */ /** Read a 16-bit value from counter 2. * @return value of counter 2 */ public short getCounter2() { short value; setControlWord(COUNTER2); value = (short)iox.getByte(counter2Offset); value |= ((short)iox.getByte(counter2Offset) << 8); return value; } }