CO-ROUTINES - using your main program as a co-routine.

MAIN as a Co-routine:

A regular B program is actually an implicitly created instance of a co-routine, complete with its own stack and initialized FCV. This means that you may CALL or RESUME your main program as if it were a co-routine you had created, by using the FCV which describes your main program. There is, however, a catch.

The source of the difficulty lies in the answer to the question "Who is the caller of my main program?" Philosophically, the answer to this might be "the operating system", and you would expect that a DETACH from your main program would return you to whatever invoked your program as a whole. It may be philosophically disappointing that this isn't the case; empirically though, what actually happens is likely more useful in the long run.

In actual fact, the caller field in the FCV of your main B program is explicitly initialized to zero. Any attempt to use DETACH from your main program without first having artificially set its caller (via CALLBY) will result in an attempt to address FCV control words below memory location zero, resulting in an immediate memory fault and the ungraceful end of your program. Although inconsistent with the logic of co-routines as a whole, this situation is justified on the grounds that any DETACH done from your main program (or from any co-routine simply RESUMEd by your main program) is likely an error which should be caught. If you really want it, program termination may be performed anywhere by calling EXIT, or from within MAIN by a standard B function return statement.

A second inconsistency is evident when you "fall off the end" of the co-routine which is your main program. In all co-routines which you explicitly create, falling off the end is equivalent to doing a DETACH. From your main program, falling off the end (doing a return from MAIN) terminates your program. This happens regardless of the validity of the caller field in your main program's FCV, which perhaps is just as well considering the disaster of doing a DETACH with a zero caller field.

The above difficulties lie only with the implicit co-routine which is your main B program, i.e. the co-routine which uses the main B stack. It is perfectly permissible to pass any of the functions of your main B program (including MAIN) in a call to CREATE, in which case the resulting co-routine is explicit, does not use the main B stack, and does not suffer from any of the above limitations/features.

Copyright © 1996, Thinkage Ltd.