setcontext is one of a family of Clibraryfunctions (the others being getcontext, makecontext and swapcontext) used for context control. The setcontext family allows the implementation in C of advanced control flowpatterns 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, setcontext allows the creation of multiple cooperativethreads of control, each with its own stack.

. . . Setcontext . . .

setcontext was specified in POSIX.1-2001 and the Single Unix Specification, version 2, but not all Unix-likeoperating 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. Citing IEEE Std 1003.1, 2004 Edition:[1]

With the incorporation of the ISO/IEC 9899:1999 standard into this specification it was found that the ISO C standard (Subclause 6.11.6) specifies that the use of function declarators with empty parentheses is an obsolescent feature. Therefore, using the function prototype:

void makecontext(ucontext_t *ucp, void (*func)(), int argc, ...);

is making use of an obsolescent feature of the ISO C standard. Therefore, a strictly conforming POSIX application cannot use this form. Therefore, use of getcontext(), makecontext(), and swapcontext() is marked obsolescent.

There is no way in the ISO C standard to specify a non-obsolescent function prototype indicating that a function will be called with an arbitrary number (including zero) of arguments of arbitrary types (including integers, pointers to data, pointers to functions, and composite types).

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

typedef struct{	ucontext_t *uc_link;	sigset_t    uc_sigmask;	stack_t     uc_stack;	mcontext_t  uc_mcontext;	...} ucontext_t;

uc_link points to the context which will be resumed when the current context exits, if the context was created with makecontext (a secondary context). uc_sigmask is used to store the set of signals blocked in the context, and uc_stack is the stack used by the context. uc_mcontext stores executionstate, including all registers and CPUflags, the instruction pointer, and the stack pointer; mcontext_t is an opaque type.

The functions are:

  • int setcontext(const ucontext_t *ucp)
    This function transfers control to the context in ucp. Execution continues from the point at which the context was stored in ucp. setcontext does not return.
  • int getcontext(ucontext_t *ucp)
    Saves current context into ucp. This function returns in two possible cases: after the initial call, or when a thread switches to the context in ucp via setcontext or swapcontext. The getcontext 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 optimisations.
  • void makecontext(ucontext_t *ucp, void (*func)(), int argc, ...)
    The makecontext function sets up an alternate thread of control in ucp, which has previously been initialised using getcontext. The ucp.uc_stack member should be pointed to an appropriately sized stack; the constant SIGSTKSZ is commonly used. When ucp is jumped to using setcontext or swapcontext, execution will begin at the entry point to the function pointed to by func, with argc arguments as specified. When func terminates, control is returned to ucp.uc_link.
  • int swapcontext(ucontext_t *oucp, ucontext_t *ucp)
    Transfers control to ucp and saves the current execution state into oucp.

. . . Setcontext . . .

This article is issued from web site Wikipedia. The original article may be a bit shortened or modified. Some links may have been modified. The text is licensed under “Creative Commons – Attribution – Sharealike” [1] and some of the text can also be licensed under the terms of the “GNU Free Documentation License” [2]. Additional terms may apply for the media files. By using this site, you agree to our Legal pages . Web links: [1] [2]

. . . Setcontext . . .