Setcontext

setcontext is one of a family of C library functions (the others being getcontext, makecontext and swapcontext) used for context control. The  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,  allows the creation of multiple cooperative threads of control, each with its own stack.

Specification
was 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 were removed, with POSIX Threads indicated as a possible replacement.

Definitions
The functions and associated types are defined in the  system header file. This includes the  type, with which all four functions operate:

points to the context which will be resumed when the current context exits, if the context was created with  (a secondary context). is used to store the set of signals blocked in the context, and  is the stack used by the context. stores execution state, including all registers and CPU flags, the instruction pointer, and the stack pointer;  is an opaque type.

The functions are:
 * This function transfers control to the context in . Execution continues from the point at which the context was stored in  .   does not return.
 * Saves current context into . This function returns in two possible cases: after the initial call, or when a thread switches to the context in   via   or  . The   function does not provide a return value to distinguish the cases (its return value is used solely to signal error), so the programmer must use an explicit flag variable, which must not be a register variable and must be declared volatile to avoid constant propagation or other compiler optimizations.
 * The  function sets up an alternate thread of control in , which has previously been initialised using  . The   member should be pointed to an appropriately sized stack; the constant   is commonly used. When   is jumped to using   or  , execution will begin at the entry point to the function pointed to by  , with   arguments as specified. When   terminates, control is returned to.
 * Transfers control to  and saves the current execution state into.
 * The  function sets up an alternate thread of control in , which has previously been initialised using  . The   member should be pointed to an appropriately sized stack; the constant   is commonly used. When   is jumped to using   or  , execution will begin at the entry point to the function pointed to by  , with   arguments as specified. When   terminates, control is returned to.
 * Transfers control to  and saves the current execution state into.
 * Transfers control to  and saves the current execution state into.
 * Transfers control to  and saves the current execution state into.

Example
The example below demonstrates an iterator using.

NOTE: this example is not correct, but may work as intended in some cases. The function  requires additional parameters to be type , 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.

"On architectures where int and pointer types are the same size (e.g., x86-32, where both types are 32 bits), you may be able to get away with passing pointers as arguments to makecontext following argc. However, doing this is not guaranteed to be portable, is undefined according to the standards, and won't work on architectures where pointers are larger than ints. Nevertheless, starting with version 2.8, glibc makes some changes to, to permit this on some 64-bit architectures (e.g., x86-64)."

For get and set context, a smaller context can be handy: This makes an infinite loop because context holds the program counter.