[Oberon] On Oberon Tasks.

Bob Walkden bob at web-options.com
Sun Dec 23 22:00:25 CET 2012

> From: Srinivas Nayak [mailto:sinu.nayak2001 at gmail.com]
> I couldn't understand, how can task switching happen between commands
> only.
> Then how, Oberon runs two tasks simultaneously? Or it is not possible
> to view a movie in video player and read some document simultaneously?

The code for the Oberon loop is on page 48 of the book, and the mail server
in chapter 11 is an example of how to write and install a task. 

If you want to run more than one task at a time then you have to program
them differently from the conventional way you might write code. Your design
is more like an interrupt-handler which you install in the loop.

Conventionally your code might be structured as follows:

read the first input
until exhausted do
  case input = e1: Response1;
  | input = e2: Response1;
  | ...
  | input = eN: ResponseN;
    cause a horrible disaster;
  read the next input

This style have been called [1] 'implicit mode representation' because the
state, or mode, of the program is implied by the current position in the
program text.

In a normal multitasking system, the system itself takes care of swapping
the code in and out, saving & restoring its state and so on, and you don't
have to take much notice of it being a multitasking system, although you
might throw in a 'yield' from time to time, just to be friendly. 

In Oberon you have to be explicit about writing a task, and in [1] it is
called 'explicit mode representation' because you define a variable which
contains the program's current state, or mode. The behaviour of the program
depends on its current state and on the event which it has been asked to
handle. The best way, in my opinion, to design such a task is to use a state
transition diagram, and code a handler based on a nested case statement over
the state variable and the event / input that's passed into it.

var state: T;

proc handler( input: T2 );
    case state of 
	case input of 
	i1: response1; 
	    state := whatever the next state is
      iN: ...
    sN: ...
end handler

The loop in the implicit mode program is essentially replaced by the Oberon
loop, and the body is replaced by code which gets the next input event and
forwards it to the handler. 

You install the 'read next input' procedure as a task in the Oberon loop. It
might look something like this:

	exhausted: boolean;
	input: T2;

procedure task;
  read next input
  if not exhausted then
     handle( input );
     Oberon.Install( task ) (* re-install the task in the oberon loop *)
end task;

You'd also need to write some initialisation and termination procedures, but
the essence of it is there.

So, if you wanted to watch a video and write a document at the same time,
you'd have to write the video player as a task. The keyboard events are
already installed in the Oberon loop, so that should continue to work.

[1] 'Software systems construction with examples in Ada' ---Bo Sanden.

More information about the Oberon mailing list