Multitasking - September 25th, 2002

Multitasking is something found in many Forths. It is certainly nothing new. ANS Forth does not really extend into this area, though it is possible for a multitasking Forth to also be ANS Forth complient. Enth's co-operative multitasking structure and words have evolved somewhat over time. The design is a fairly normal Forth multitasker. It borrows words used in several other Forths.

Being a Forth with no host operating system, there was always the question as to whether it should be extended to be a true multiuser system, in addition to multitasking. This seemed to represent a rather large jump in complexity to implement (at least to this author :), so it wasn't done. However, the multitasking does have capacity to do some interesting things.

There are three types of task...

A Master task has a dictionary and can compile. It is autonomous in that it has no parent task. It will run indefinitely.

A Slave task has a dictionary and can compile. It is non-autonomous in that it always remains linked too, and under the control of, a Master task.

A Child task cannot compile. It is non-autonomous and must run precompiled code. Like the slave, it too is linked too, and under the control of, a Master task.

TASK            ( - )
        Usage: CREATE CHARLIE TASK
        Else: HERE TASK CONSTANT CHARLIE
        Or in color: <red>CHARLIE<white>TASK (reference as address:
        <yellow>CHARLIE)

WAKE            ( up - )
        Wake task UP. eg CHARLIE WAKE

SLEEP           ( up - )
        Sleep task UP. eg CHARLIE SLEEP

START           ( up - )
        WAKE task UP and begin it executing current definition following
        START.

STOP            ( - )
        Volantarily go to SLEEP.

ASSIGN          ( xt up - )
        Initialise task UP to start executing word XT when next
        aWAKEned.

PAUSE           ( - )
        Perform a task switch.

MS              ( n - )
        Continue to PAUSE for n milliseconds.

CHILD           ( xt - )
        Create background child task. ASSIGN xt to it. WAKE it. A CHILD
        cannot compile.

SLAVE           ( u - up )
        Create a SLAVE task with dictionary of U Blocks. A SLAVE can
        compile yet remains under control of the creating task.

MASTER          ( u - up )
        Create autonomous MASTER task with dictionary of U Blocks. A
        master can compile and remain active after its creating task dies.

CEDE            ( n up - )
        Hand N stack items to MASTER or SLAVE task UP. Start task UP
        compiling the current source block following CEDE.

HALT            ( up - )
        HALT task UP. If task UP has children HALT them too. If task UP
        has ALLOCTEd memory, free it. HALTing a MASTER task will kill and
        erase it completely, along with any children it may have. It can
        never be reused. HALTing a SLAVE or CHILD task with merely reset
        them. They may be reused until their creator MASTER task is HALTed.
Get Firefox!