setcontext is one of a family of C library functions (the others being getcontext, makecontext and swapcontext) used for context control. The <code>setcontext</code> family allows the implementation in C of advanced control flow patterns such as iterators, fibers, and coroutines. They may be viewed as an advanced version of setjmp/longjmp; whereas the latter allows only a single non-local jump up the stack, <code>setcontext</code> allows the creation of multiple cooperative threads of control, each with its own stack.
<code>setcontext</code> is specified in POSIX.1-2001 and the Single Unix Specification, version 2, but not all Unix-like operating systems provide them. POSIX.1-2004 obsoleted these functions, and in POSIX.1-2008 they are not included, with POSIX Threads indicated as a possible replacement.
The functions and associated types are defined in the <code>ucontext.h</code> system header file. This includes the <code>ucontext_t</code> type, with which all four functions operate:
<code>uc_link</code> points to the context which will be resumed when the current context exits, if the context was created with <code>makecontext</code> (a secondary context). <code>uc_sigmask</code> is used to store the set of signals blocked in the context, and <code>uc_stack</code> is the stack used by the context. <code>uc_mcontext</code> stores execution state, including all registers and CPU flags, the instruction pointer, and the stack pointer; <code>mcontext_t</code> is an opaque type.
The functions are:
The example below demonstrates an iterator using <code>setcontext</code>.
NOTE: this example is not correct, but may work as intended in some cases. The function <code>makecontext</code> requires additional parameters to be type <code>int</code>, but the example passes pointers. Thus, the example may fail on 64-bit machines (specifically LP64-architectures, where ). This problem can be worked around by breaking up and reconstructing 64-bit values, but that introduces a performance penalty.
For get and set context, a smaller context can be handy:
This makes an infinite loop because context holds the program counter.