User guide

48 CHAPTER 3. THE LIBRARY
next resumes execution. The parent link points to the coroutine that called this one,
or is zero if the coroutine not active. The outermost corouti ne (or root coro u ti ne) is
marked by the special value -1 in its parent link. As a debugging aid, all coroutines
are chained together in a list held in the global colist. The values fn and sz hold the
main function of the coroutine and its stack size, and c is a private variabl e used by
the coroutine mechanism.
changeco
PC
CHGCOP
aL1P1
P1 L1 a
currco PPC
stack frame
currco
resumption point
resumption point
cptr
Figure 3.2: The effect of changeco(a, cptr)
At any time just one coroutine (the current coroutine) has control, and all the
others are said to be suspended. The current coroutine is held in the global variable
currco, and the Cintcode P register points to a stack frame within its stack. Passing
control from one coroutine to another involves saving the resumption point in the
current coroutine, and setting new values for the program counter (PC), the P pointer
and currco. This is done by changeco(a,cptr) as shown in figure 3.2. The functi on
changeco is defined by hand in syslib used by cintsys and cintpos and its body
consists of the single Cintcode instruction CHGCO. As can be seen its effect is somewhat
subtle. The only uses of changeco are in the definitions of createco, callco, cowait
and resumeco, and these are the only functions that cause coroutine suspension. In
the native code version of BCPL changeco is de fin ed in mlib.s