CS452 - Real-Time Programming - Fall 2008

Lecture 3 - Old-style RT Programming Methods

Busy Waiting

This is used to synchronize with an external event, with minimal response time.

#define FOREVER for( ; ; )
FOREVER {
   while( !ready( ) ) ;
   do-it( );
}

or in another form

FOREVER {
   if ( ready( ) ) do-it( );
}

Worst case response time

From the time that the ready bit sets until the next byte is in the data register

The NOP technique

Maybe this isn't fast enough for you. Here's an "improvement", for your amusement

L10000000:
  nop
....
L2:
  nop
L1:
  nop
L0:
  ldb  r0, [r3] // get the next byte
  inc  r3
  stb  r0, DATA-ADDRESS
  br   r5       // r5 contains the address to which we branch

In the initialization code we determine, by experiment, exactly how many nops we need in order to land on the stb instruction just after the ready bit sets.

Worst case response time

The lesson

There's a lesson for everybody in this extreme example. (Actually there are more extreme examples, and they are used also.) Wherever there is a nicely structured solution with response time x seconds there is a disgustingly-structured example with reponse time less than x/10.

The problem with busy-waiting

Polling Loops

Polling loops allow you to manage more than one condition/activity pair at the same time.

The basic polling loop

FOREVER {
  if( c1 ) a1;
  if( c2 ) a2;
  ...
  if( cN ) aN;
}

Worst case response time

Testing more than once

Suppose you want a better response time for a1. Then try the loop

FOREVER {
  if( c1 ) a1;
  if( c2 ) a2;
  if( c1 ) a1;
  if( c3 ) a3;
  ...
  if( c1 ) a1;
  if( cN ) aN;
}

Worst case response time for a1

Breaking into pieces

Suppose the response time is still too long, because the execution of one action, say a2, is too long. Then you can break a2 into two parts

FOREVER {
  if( c1 ) a1;
  if( c2 ) { a2.1; half-done = TRUE }
  if( c1 ) a1;
  if( half-done ) a2.2;
  ...
}

This is strarting to get a little complicated and we haven't said anything about inter-action communication

Interrupt Service Routines (ISRs)

We can use interrupts to get rid of polling

Interrupts are exceptions

  1. Mask interrupts
  2. Set link register
  3. Save CSR
  4. Transfer control

ISRs

Like function calls without arguments or return values.

  1. Save state of interrupted task
  2. Turn off source of interrupt
  3. Do the work
  4. Restore state of interrupted task
  5. Return to next instruction of interrupted task

Contrast RT to non-RT

Worst case reponse time

Interrupt vectors


Return to: