Next: , Previous: Type Conversions, Up: Operations


6.2.13 Continuations

The source files continue.h and continue.c are designed to function as an independent resource for programs wishing to use continuations, but without all the rest of the SCM machinery. The concept of continuations is explained in call-with-current-continuation.

The C constructs jmp_buf, setjmp, and longjmp implement escape continuations. On VAX and Cray platforms, the setjmp provided does not save all the registers. The source files setjump.mar, setjump.s, and ugsetjump.s provide implementations which do meet this criteria.

SCM uses the names jump_buf, setjump, and longjump in lieu of jmp_buf, setjmp, and longjmp to prevent name and declaration conflicts.

— Data type: CONTINUATION jmpbuf length stkbse other parent

is a typedefed structure holding all the information needed to represent a continuation. The other slot can be used to hold any data the user wishes to put there by defining the macro CONTINUATION_OTHER.

— Macro: SHORT_ALIGN

If SHORT_ALIGN is #defined (in scmfig.h), then the it is assumed that pointers in the stack can be aligned on short int boundaries.

— Data type: STACKITEM

is a pointer to objects of the size specified by SHORT_ALIGN being #defined or not.

— Macro: CHEAP_CONTINUATIONS

If CHEAP_CONTINUATIONS is #defined (in scmfig.h) each CONTINUATION has size sizeof CONTINUATION. Otherwise, all but root CONTINUATIONs have additional storage (immediately following) to contain a copy of part of the stack.

Note On systems with nonlinear stack disciplines (multiple stacks or non-contiguous stack frames) copying the stack will not work properly. These systems need to #define CHEAP_CONTINUATIONS in scmfig.h.

— Macro: STACK_GROWS_UP

Expresses which way the stack grows by its being #defined or not.

— Variable: long thrown_value

Gets set to the value passed to throw_to_continuation.

— Function: long stack_size (STACKITEM *start)

Returns the number of units of size STACKITEM which fit between start and the current top of stack. No check is done in this routine to ensure that start is actually in the current stack segment.

— Function: CONTINUATION * make_root_continuation (STACKITEM *stack_base)

Allocates (malloc) storage for a CONTINUATION of the current extent of stack. This newly allocated CONTINUATION is returned if successful, 0 if not. After make_root_continuation returns, the calling routine still needs to setjump(new_continuation->jmpbuf) in order to complete the capture of this continuation.

— Function: CONTINUATION * make_continuation (CONTINUATION *parent_cont)

Allocates storage for the current CONTINUATION, copying (or encapsulating) the stack state from parent_cont->stkbse to the current top of stack. The newly allocated CONTINUATION is returned if successful, 0q if not. After make_continuation returns, the calling routine still needs to setjump(new_continuation->jmpbuf) in order to complete the capture of this continuation.

— Function: void free_continuation (CONTINUATION *cont)

Frees the storage pointed to by cont. Remember to free storage pointed to by cont->other.

— Function: void throw_to_continuation (CONTINUATION *cont, long value, CONTINUATION *root_cont)

Sets thrown_value to value and returns from the continuation cont.

If CHEAP_CONTINUATIONS is #defined, then throw_to_continuation does longjump(cont->jmpbuf, val).

If CHEAP_CONTINUATIONS is not #defined, the CONTINUATION cont contains a copy of a portion of the C stack (whose bound must be CONT(root_cont)->stkbse). Then: