IMPLEMENTATION - using C functions for co-routines.

In native mode C, a co-routine is implemented as a group of functions which have been given their own stack. This independent stack is used by the functions which are part of the co-routine, in the same manner as the main NSC stack is used by the functions of a regular NSC program. In fact, the functions of a regular NSC program themselves constitute a valid co-routine: a group of functions with their own stack. This particular co-routine (which might be considered the "main" co-routine) is created during the normal program start-up process.

The stack given to a co-routine is used only by the functions which are part of that co-routine. This allows the stack to be retained until explicitly destroyed, thus preserving the state of all the functions which make up the co-routine. In keeping with the concept that the states of all co-routines exist concurrently, the states of all NSC co-routines as preserved on their independent stacks also exist concurrently.

There are special NSC library subroutines used to create, destroy, and transfer control among co-routines. These all make use of a co-routine's "Co-Routine Header", located at the beginning of the segment that contains the co-routine's stack. The header contains information about the size and use of the co-routine's stack, the manner in which the co-routine was invoked, and some space available for user data.

The co-routine's stack supplies the address at which the co-routine was last suspended. This information is in the form of a return address saved in the linkage section of the stack frame in the co-routine in which execution was suspended. This is the address at which the suspended function and co-routine will resume execution when control next passes to it.

Within a co-routine, which is to say within a group of functions using a common stack, function call and return proceed normally. The same conventions and mechanisms operate as in a regular NSC program using the main NSC stack. Non-local GOTOs (like the C "longjmp" function) will work, but only within the stack of the co-routine; it is not possible to GOTO a function in another co-routine, since the GOTO searches for its destination on the current stack only. Programming inside a co-routine is almost identical to programming in a regular NSC program since, as mentioned previously, every NSC program is actually an implicit co-routine.

See Also:

expl nsc coro

expl nsc lib _coro

expl nsc lib _ccall

expl nsc lib _cret

expl nsc lib _resume

expl nsc lib _cdel

Copyright © 1996, Bull HN and Thinkage Ltd.