Search code examples
ccontinuations

Is setcontext functions family in C a continuation?


How these two (setcontext, Continuation) concepts relates to each other? Do we create an instance of a continuation when using a setcontext technique?


Solution

  • The ucontext_t object that you initialize via getcontext(), makecontext(), or swapcontext() preserves the stack and CPU state -- but not the heap -- in a way that can later be activated via setcontext() or swapcontext(). This is similar to a continuation.

    The subsystem seems not to have been designed with the idea of implementing continuations as such, however, but more as an improvement on sigsetjmp() and siglongjmp(). In particular, contexts capture a signal mask, which is restored when the context is activated. Each context also maintains a sense of a context to be resumed when the current context terminates. In these senses, contexts may be considered to vary from pure continuations.

    Note that getcontext(2), makecontext(3), and swapcontext(3) are now all deprecated by POSIX, leaving no direct, non-deprecated way for user code to obtain a valid context. A three-arg signal handler still gets one as its third argument, but it's pretty useless because the effect of resuming that context is intentionally unspecified in recent versions of SuS / POSIX.